def not_covered(self, tick_size=0.05, **kwargs): """ Get the list of orders/positions not covered. returns a named tuple containing the symbol, the side not covered, quantity not covered and the average price of the side covered tick_size tick_size to be adjusted in the final price kwargs list of keyword arguments to filter orders and positions to be passed to the consolidated function Note ----- 1) This is a consolidated list including both positions and orders 2) Orders are compared by quantity and not by price 3) Bracket, OCO and other order types not covered """ all_orders = self.consolidated(**kwargs) tup = namedtuple('uncovered', ['symbol', 'side', 'quantity', 'price']) uncovered = [] for k,v in all_orders.items(): if v['BUY'] > v['SELL']: avg_price = tick(v['BUY_value']/v['BUY'], tick_size) tp = tup(k, 'SELL', v['BUY']-v['SELL'], avg_price) uncovered.append(tp) elif v['SELL'] > v['BUY']: avg_price = tick(v['SELL_value']/v['SELL'], tick_size) tp = tup(k, 'BUY', v['SELL']-v['BUY'], avg_price) uncovered.append(tp) return uncovered
def order(self, symbol: str, side: str, **kwargs): order_id = stop_id = None v = self.data[symbol] price = tick(v.ltp) stop = tick( self.stop_loss(symbol=symbol, side=side, stop=3, method='percent')) quantity = self.get_quantity(price=price, stop=stop) v.can_trade = False if side == 'SELL': v.positions = 0 - quantity else: v.positions = quantity side_map = {'BUY': 'SELL', 'SELL': 'BUY'} # Defaults for live order defaults = deepcopy(self.ORDER_DEFAULT_KWARGS) defaults.update( dict(symbol=symbol, order_type='LIMIT', price=price, quantity=quantity, side=side)) defaults.update(kwargs) if self.env == 'live': order_id = self.broker.order_place(**defaults) side2 = side_map.get(side) stop_args = dict(order_type='SL-M', trigger_price=stop, side=side2) defaults.update(stop_args) stop_id = self.broker.order_place(**defaults) else: order_id = random.randint(100000, 999999) stop_id = random.randint(100000, 999999) v.order_id = order_id v.stop_id = stop_id return (order_id, stop_id)
def get_sl(price, side, percent): """ Get stop loss for the order Note ----- The implementation is in the reverse since we already knew the side to place the order and the price is that of the opposite side """ if side == 'BUY': return tick(price*(1+percent*0.01), tick_size) elif side == 'SELL': return tick(price*(1-percent*0.01), tick_size) else: return price