context.pair = 'btc_usd'
context.buy_treshold = 0.24
context.sell_treshold = 0.19
context.buy_price = 0
context.previous_price = 0
handle: (context, data)->
instrument = data[context.pair]
short = instrument.ema(10) # calculate EMA value using ta-lib function
long = instrument.ema(21)
diff = 100 * (short - long) / ((short + long) / 2)
if diff > context.buy_treshold
buy instrument # Spend all amount of cash for BTC
if buy instrument #<< how do I check if a buy was executed this tick?
context.buy_price = instrument.price
debug "Buy Threshold"
debug "#{context.previous_price} #{context.buy_price} #{instrument.price}"
else if diff < -context.sell_treshold
sell instrument # Sell BTC position
if sell instrument
debug "Sell Threshold"
debug "#{context.previous_price} #{context.buy_price} #{instrument.price}"
else if instrument.price < (0.985*context.previous_price)
sell instrument
if sell instrument
debug "Trailing Stop Loss"
debug "#{context.previous_price} #{context.buy_price} #{instrument.price}"
else if instrument.price < (0.985*context.buy_price)
sell instrument
if sell instrument
debug "Stop Loss"
debug "#{context.previous_price} #{context.buy_price} #{instrument.price}"
context.previous_price = instrument.price
http://cryptotrader.org/backtests/f2wBfWCXHvha7N9ip