def GetBNFStoplossTargetValues(contract_ce, contract_pe, lots): token_ce = fetch_token(contract_ce) token_pe = fetch_token(contract_pe) combined_premium = fetch_ltp(token_ce) + fetch_ltp(token_pe) quantity = lots * 25 stoploss = -0.15 * quantity * combined_premium target = 0.15 * quantity * combined_premium step = 0.15 * quantity * combined_premium return stoploss, target, step
def GetATMStrike(symbol): instruments = pd.read_csv('instruments.csv', index_col=0) token = instruments.index[instruments['tradingsymbol'] == symbol].tolist()[0] ltp = fetch_ltp(token) strike_price = int(math.ceil(ltp / 100.0)) * 100 return strike_price
def GetStoplossTargetValues(positions_info): combined_premium = 0 for instrument, instrument_info in positions_info.items(): ltp = fetch_ltp(instrument_info['token']) combined_premium = combined_premium + ltp quantity = abs(instrument_info['quantity']) stoploss = -0.10 * quantity * combined_premium target = 0.10 * quantity * combined_premium step = 0.10 * quantity * combined_premium return stoploss, target, step
def RunSystem(kite, access_token, trades): fixed_stoploss = trades['fixed_stoploss'] stoploss = trades['stoploss'] target = trades['target'] step = trades['step'] transaction_types = ['BUY', 'SELL'] transaction_types.remove(trades['transaction_type']) exit_transaction_type = transaction_types[0] positions_info = FetchPositionsInfo(kite) # this takes care of restartng from middle runs if not positions_info: position_flag = False else: #TODO Change this to true position_flag = True # TODO remove this counter # count = 0 while True: current_time = datetime.datetime.now().time() if current_time >= trades['trade_time'] and position_flag == False: all_orders_status = [] for contract in trades['symbols']: order_status = PlaceMarketOrders(access_token, contract, trades['transaction_type'], trades['product_type'], trades['quantity']) print(order_status) yield order_status + "\n" order_status = json.loads(order_status) all_orders_status.append(order_status) if all(d['status'] == 'success' for d in all_orders_status): yield "################### ALL THE ORDERS ARE SUCCESSFUL ###################" + "\n" positions_info = FetchPositionsInfo(kite) position_flag = True elif any(d['status'] == 'error' for d in all_orders_status): yield "################### ONE OF THE ORDERS FAILED ###################" + "\n" # TODO Remove this line # position_flag = True else: # TODO Remove this line # position_flag = True continue pnl = 0 # TODO remove this line # count += 1 yield f"CURRENT TIME: {current_time}" + "\n" for instrument, instrument_info in positions_info.items(): ltp = fetch_ltp(instrument_info['token']) yield f"{instrument} ltp: {ltp}" + "\n" pnl = pnl + instrument_info['value_change'] + ( instrument_info['quantity'] * ltp * instrument_info['multiplier']) yield f"pnl = {pnl}" + "\n" yield f"fixed stoploss = {fixed_stoploss}" + "\n" yield f"stoploss = {stoploss}" + "\n" yield f"target = {target}" + "\n" yield "======================================" + "\n" if pnl < fixed_stoploss or pnl < stoploss: all_orders_status = [] for instrument, instrument_info in positions_info.items(): order_status = PlaceMarketOrders(access_token, instrument, exit_transaction_type, trades['product_type'], trades['quantity']) all_orders_status.append(order_status) yield str(all_orders_status) return all_orders_status elif pnl >= target: stoploss = stoploss + step target = target + step elif current_time >= trades['exit_time']: all_orders_status = [] for contract in trades['symbols']: order_status = PlaceMarketOrders(access_token, contract, exit_transaction_type, trades['product_type'], trades['quantity']) all_orders_status.append(order_status) yield str(all_orders_status) return all_orders_status time.sleep(1)
def RunSystem(kite, access_token, lots): trade_start_time = datetime.time(18, 45) trade_end_time = datetime.time(14, 45) fixed_stoploss = -3000 stoploss = 0 target = 0 step = 0 positions_info = FetchPositionsInfo(kite) # this takes care of restartng from middle runs if not positions_info: position_flag = False else: #TODO Change this to true position_flag = False positions_info = FetchPositionsInfo(kite) stoploss, target, step = GetStoplossTargetValues(positions_info) while True: current_time = datetime.datetime.now().time() if current_time >= trade_start_time and position_flag == False: atm_strike = GetATMStrike('NIFTY BANK') contract_ce, contract_pe = GetCurrentWeeklyOptions( atm_strike, 'BANKNIFTY') order_status_ce = PlaceMarketOrders(access_token, contract_ce, "SELL", "MIS", 25 * lots) print(order_status_ce) yield order_status_ce + "\n" order_status = json.loads(order_status_ce) if order_status['status'] == 'success' and order_status[ 'status'] == 'success': positions_info = FetchPositionsInfo(kite) stoploss, target, step = GetStoplossTargetValues( positions_info) position_flag = True else: """remove this""" # position_flag = True continue pnl = 0 yield f"CURRENT TIME: {current_time}" + "\n" for instrument, instrument_info in positions_info.items(): ltp = fetch_ltp(instrument_info['token']) print(f"{instrument} ltp: {ltp}") pnl = pnl + instrument_info['value_change'] + ( instrument_info['quantity'] * ltp * instrument_info['multiplier']) yield f"pnl = {pnl}" + "\n" yield f"fixed stoploss = {fixed_stoploss}" + "\n" yield f"stoploss = {stoploss}" + "\n" yield f"target = {target}" + "\n" # time.sleep(2) # os.system('clear') # stoploss = -10000 instruments = list(positions_info.keys()) if pnl < fixed_stoploss or pnl < stoploss: order_status = PlaceMarketOrders(access_token, instruments[0], "BUY", "MIS", 25 * lots) return order_status elif pnl >= target: stoploss = stoploss + step target = target + step elif current_time >= trade_end_time: order_status = PlaceMarketOrders(access_token, instruments[0], "BUY", "MIS", 25 * lots) return order_status time.sleep(1)