...
orderId = buy instrument # Spend all amount of cash for BTC
if orderId
# a buy was executed
context.buy_price = instrument.price
...
I am trying to do something similar and build on a simple script.
However no matter what I do I can not keep the instrument.price stored.
#Simple buy and sell over price with EMA
init: (context)->
context.buy_price = 0
handle: (context, data)->
instrument = data.instruments[0]
short = instrument.ema(9)
long = instrument.ema(19)
plot
short: short
long: long
if short > long
buy instrument
#works up to here context.buy_price = instrument.price
#The sell fails by not selling the instrument over the (purchased) price else if context.buy_price > (instrument.price*1.01)
orderId = sell instrument
I assume it has to do with the iff statements, because I can get the sell to work but then the buy just buys as soon as the script starts.
Thanks for any ideas and help.