0

Environment: Zipline 1.3.0 miniconda3 windows OS

I am trying to iterate S in data. S.symbol has like 15 values.

When iterating in data for 1 symbol, say ‘spy’ as in below code; i want to create 2keys

(S.symbol + “c”) —-> spyc to hold current value

(S.symbol + “s”) —→ spys to hold a float value.

def before_trading_start(context,data):
print("*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#")
print("Get DateTime: ",get_datetime())
#context is a dictionary
for S in data:
    if(S.symbol == 'SPY'):
        arr = list(range(1,91))
        hist = data.history(S,"price",90,"1d")
        price_list = np.log(hist.tolist())
        context.spyc = data.current(S,"price")
        context.spys = Slope(arr, price_list)
        print (context.spyc)
        print (S.symbol, context.spys)


    else:
        continue
############### My failed Version of dynamic naming
def before_trading_start(context,data):
print("*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*")
print("Get DateTime: ",get_datetime())
for S in data:
    arr = list(range(1,91))
    hist = data.history(S,"price",90,"1d")
    price_list = np.log(hist.tolist())

    context[S.symbol + "c"] = data.current(S,"price")
    context[S.symbol+"s"] = Slope(arr, price_list)
    print (context[S.symbol + "c"])
    print (S.symbol, context[S.symbol + "s"])

S.symbols = ["Spy","AAPl",'xom','L','T','CSCO','MSFT'..]

Akash
  • 36
  • 4
  • What does your attempt do wrong? Raise an exception? Then paste the traceback here. Give the wrong values? Then tell us the expected and actual values. Without some idea of what you're asking, it's hard to do anything but guess at things that look weird. – abarnert Aug 02 '18 at 18:01
  • But as a wild guess: There's nothing obviously wrong with the way you're dynamically generating keys (you can test that out yourself by doing `d = {}; d["ab" + "c"] = 2; print(d)` and see what you get), so there's probably something else wrong. Is `context` actually a dict? If not, you can't add keys to it, any more than you can add keys to the number `1`. If so, that's your problem, not the thing you're asking about. – abarnert Aug 02 '18 at 18:03
  • Update: I kind of researched my bit and found that , though context is a dict; I cannot assign variables dynamically to it. Only to the context dict as it is kind of root scope to all the underlying functions. Weird But i just replaced the context with my own variable and everything runs smoothly. – Akash Aug 03 '18 at 21:30

0 Answers0