How do I assign dynamic key values in a dictionary in Python?

418 views Asked by At

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'..]

0

There are 0 answers