mardi 13 septembre 2016

Python - IF statement not calculating properly

So I'm pretty new to coding and would appreciate any help I can get!

I have some code that looks at the average price of a stock for 8 days and 5 days. If one is higher than the over it should produce a number 1.5 or 0.5.

When I test the code, it doesn't change between 0.5 and 1.5, it only prints 0.5...even though I know it should change! (I have tested the price history and that is correct).

Here is the code:

shortsma = data.history(context.spy, 'price', 6, '1d')[:-1].mean()

longsma = data.history(context.spy, 'price', 9, '1d')[:-1].mean()

if longsma.all() > shortsma.all():
    context.long_leverage = 1.5
    context.short_leverage = -0.5

else:
    context.long_leverage = 0.5
    context.short_leverage = -1.5

print context.long_leverage, context.short_leverage

Also can anyone tell me why I had to put .all() or .any() after longsma and shortsma to get it to work? Slightly confused. Thanks all.

Aucun commentaire:

Enregistrer un commentaire