Dear quantra teachers,
the following is a code I wrote on Blueshift. When backtesting, the performance line remains straight and constant, like a straight line, not deviating. There is also no alpha and return%. Why is this happening ?
Thank you
from blueshift.api import (
symbol,
schedule_function,
order_target_percent,
date_rules,
time_rules
)
import talib
from datetime import datetime,timedelta
def initialize(context):
context.security=symbol('TCS')
schedule_function(
square_off,
date_rule=date_rules.every_day(),
time_rule=time_rules.market_close(minutes=3)
)
context.entrytime=datetime.min
context.exittime=datetime.min
context.current_position=0
context.sl=0
context.tp=0
def before_trading_start(context, data):
context.entrytime=datetime.now()
context.exittime=datetime.now()
context.current_position=0
context.sl=0
context.tp=0
def handle_data(context,data):
ct=datetime.now()
df=data.history(context.security,['open','close'],30,'1m')
macd, macdsignal, _ = talib.MACD(df['close'], fastperiod=12, slowperiod=26, signalperiod=9)
macd=macd[-1]
macdsignal=macdsignal[-1]
cp=data.current(context.security,'close')
if context.current_position==0 and (ct-context.exittime).total_seconds()/60>=10:
if macd>macdsignal:
order_target_percent(context.security,1)
context.current_position=1
context.entryprice=cp
context.tp= cp1.1
context.sl=cp0.95
context.entrytime=ct
elif macd<macdsignal:
order_target_percent(context.security,-1)
context.current_position=-1
context.entryprice=cp
context.tp= cp0.95
context.sl=cp1.1
context.entrytime=ct
else:
pass
elif context.current_position!=0 and (ct-context.entrytime).total_seconds()/60>=30:
order_target_percent(context.security,0)
context.current_position=0
context.exittime=ct
elif context.current_position!=0 and (ct-context.entrytime).total_seconds()/60<30:
if context.current_position==1:
if cp>=context.tp or cp<=context.sl:
order_target_percent(context.security,0)
context.exittime=ct
context.current_position=0
elif context.current_position==-1:
if cp<=context.tp or cp>=context.sl:
order_target_percent(context.security,0)
context.exittime=ct
context.current_position=0
def square_off(context,data):
if context.current_position!=0:
order_target_percent(context.security,0)
Hi Advait,
The flat line in the backtest results is likely because the strategy is not placing any orders. This issue can arise if the conditions for placing buy or sell orders are not being met. You can check the code once and try to debug the same by adding some print statements and checking the output.