In the bollinger band example part of the template. Context.target_position is defined as follows -
if context.signals[security] > context.params['buy_signal_threshold']:
context.target_position[security] = weight
elif context.signals[security] < context.params['sell_signal_threshold']:
context.target_position[security] = -weight
else:
context.target_position[security] = 0
if positive weight is to buy the stock, negative weight to sell than what does 0 weight lead to ? This is getting used in the following to execure the orders
order_target_percent(security, context.target_position[security])
Here the securities weight will be set as part the target_position value. So if the current weight of the stock is 0% and the signal is to buy then it will take weight % worth of position. in case of 0 weight, i am assuming it will make the weight of that stock 0% in the portfolio. Then what does -weight do in this scenario ?
A zero weight along with order_target_percent function means the target weight for that security is set at 0. If we have an existing long (short) position, this means selling (buying) the security to reach a zero position (i.e. square off). For more about order_target_percent, see here.
Thanks a lot for the response. So in the bollinger_band backtest it is allowing to sell of a stock without ever buying it ? Thats not possible in real market in india, no ?
The template uses nifty and bank nifty futures, so that is not a problem. If you are replacing this with stocks, shorting is ok only for intraday trades (you need to enforce intraday behaviour by square it off towards the end of the trading day, e.g. using a schedule_function call). Note: for NSE all equity trades are modelled as delivery (i.e. non-margin) products. So intraday shorting while allowed in Indian equities, the associated cash flows and margins will not be modelled correctly on Blueshift. If your strategy has enough capital and available margin at all time, the impact from this may not be significant.
Got it. Thanks a lot @Prodipta