Hello,
I'm trying to create an ema crossover strategy in Blueshift. I need to access the ema ndarray for the previous bar in my signal function but am unable to find the proper syntax for doing so. My baseline script is the Bollinger band technical study from GitHub.
It appears that I have the incorrect syntax for accessing the previous ndarray entry. px[-1] appears to return the close float64 instead of the entire previous ndarray.
Also, I haven't been able to find the return signal value to simply do nothing. It seems there is currently only Buy, Sell and Go Flat, all of which are active signal values. Is there a signal return value to do nothing with current positions?
Any guidance would be much appreciated.
The function below produces the following error when running the backtest:
line 59, in handle_data
Argument 'real' has incorrect type (expected numpy.ndarray, got numpy.float64)
def signal_function(px, px_last, params):
last_px = px[-1]
ind2 = ema(px, params['SMA_period_short'])
ind2_last = ema(px_last, params['SMA_period_short'])
ind3 = ema(px, params['SMA_period_long'])
ind3_last = ema(px_last, params['SMA_period_long'])
if (ind2_last - ind3_last < 0) and (ind2 - ind3 > 0):
return 1
elif (ind2_last - ind3_last > 0) and (ind2 - ind3 < 0):
return 0
else:
return
line 59 references the run_strategy(context, data) line from the handle_data function:
def handle_data(context, data):
"""
A function to define things to do at every bar
"""
context.bar_count = context.bar_count + 1
if context.bar_count < context.params['trade_freq']:
return
# time to trade, call the strategy function
context.bar_count = 0
run_strategy(context, data)
Thanks,
Jeremy
The indicator functions defined in the blueshift library always return the last value of the signal instead of the whole ndarray. If you need the whole array, you can define your own indicator function or simply use the talib functions directly - as done in the code in the link above.
For returning a 'do nothing' case, you need to define a return value that acts as sentinel (special value). Say, you can return 999 when you want to do nothing. And in the 'generate_target_position' function where you interpret these signal values, you can check for 999 and simply do nothing (no change in target position).
In fact, there is a built in sentinel value NO_SIGNAL
for such cases, which you can use
from blueshift_library.utils.utils import NO_SIGNAL
Thank you for taking the time to provide some guidance Prodipta. I'll look into these and let you know if I have any further questions.
Jeremy