# This is an auto-generated code <5e6a9a9446b068a534fe5a9542db6e58>
# uses generator version 1.1.0
# please use dataset nse
from blueshift.api import set_max_order_size, order, date_rules
from blueshift.api import set_max_order_count, set_slippage, schedule_function
from blueshift.api import set_commission, time_rules, symbol
from blueshift.finance import slippage, commission
from blueshift.library.library import handle_stop_loss, alpha_function, finish_prune_tracking
from blueshift.library.library import enter_short, squareoff, get_portfolio_assets
from blueshift.library.library import init_prune_tracking, handle_take_profit, get_history
from blueshift.library.library import enter_long
def initialize(context):
set_max_order_count(20)
set_max_order_size(max_quantity=1)
set_slippage(slippage.FixedSlippage(0.05))
set_commission(commission.PerShare(0.0))
context.universe = [symbol('nifty bank')]
context.short_signal = {}
context.long_signal = {}
context.my_10_ema = {}
context.my_13_ema = {}
schedule_function(scheduled_func_5924, date_rules.every_day(),
time_rules.market_open(hours=0, minutes=15))
def stop_loss_func(context, data):
assets = get_portfolio_assets(context)
if not assets:
return
handle_stop_loss(context, data, None, 'PERCENT', 3)
def handle_data(context, data):
take_profit_func(context, data)
stop_loss_func(context, data)
def take_profit_func(context, data):
assets = get_portfolio_assets(context)
if not assets:
return
handle_take_profit(context, data, None, 'PERCENT', 6)
def rule_func_5942(context, data):
for asset in context.universe:
if context.short_signal[asset]:
enter_short(context, asset, order, -1, None, 'SCHEDULE')
squareoff(context, symbol('nifty bank'))
def rule_func_5936(context, data):
for asset in context.universe:
if context.long_signal[asset]:
squareoff(context, symbol('nifty bank'))
def rule_func_5928(context, data):
for asset in context.universe:
if context.long_signal[asset]:
enter_long(context, asset, order, 1, None, 'SCHEDULE')
def scheduled_func_5924(context, data):
init_prune_tracking(context, 'SCHEDULE')
context.history_1d = get_history(data, context.universe, ['close'], 20,
'1d')
for asset in context.universe:
context.my_10_ema[asset] = alpha_function(context.history_1d, asset,
func='exponential_moving_average', kwargs={'lookback': 10})
context.my_13_ema[asset] = alpha_function(context.history_1d, asset,
func='exponential_moving_average', kwargs={'lookback': 13})
context.short_signal[asset] = context.my_10_ema[asset
] < context.my_13_ema[asset]
context.long_signal[asset] = context.my_10_ema[asset
] > context.my_13_ema[asset]
rule_func_5928(context, data)
rule_func_5936(context, data)
rule_func_5942(context, data)
finish_prune_tracking(context, 'SCHEDULE')