def _order_from_dict(self, o): order_dict = {} asset = self._asset_finder.lookup_symbol(o['tradingsymbol']) order_dict['oid'] = o['order_id'] order_dict['broker_order_id'] = o['order_id'] order_dict['exchange_order_id'] = o['exchange_order_id'] order_dict['parent_order_id'] = o['parent_order_id'] order_dict['asset'] = asset order_dict['user'] = '******' order_dict['placed_by'] = o['placed_by'] order_dict['product_type'] = product_type_map.get(o['product']) order_dict['order_flag'] = OrderFlag.NORMAL order_dict['order_type'] = order_type_map.get(o['order_type']) order_dict['order_validity'] = order_validity_map.get(o['validity']) order_dict['quantity'] = o['quantity'] order_dict['filled'] = o['filled_quantity'] order_dict['pending'] = o['pending_quantity'] order_dict['disclosed'] = o['disclosed_quantity'] order_dict['price'] = o['price'] order_dict['average_price'] = o['average_price'] order_dict['trigger_price'] = o['trigger_price'] order_dict['stoploss_price'] = o['stoploss_price'] order_dict['side'] = order_side_map.get(o['transaction_type']) order_dict['status'] = order_status_map.get(o['status']) order_dict['status_message'] = o['status_message'] order_dict['exchange_timestamp'] = pd.Timestamp( o['exchange_timestamp']) order_dict['timestamp'] = pd.Timestamp(o['order_timestamp']) order_dict['tag'] = o['tag'] order = Order.from_dict(order_dict) return order.oid, order
def order_stuff(qty, side, asset): global orders global positions o = Order(qty, side, asset) orders[o.oid] = o execute_order(o) return o.oid
def read_orders(orders, asset_finder, key_transform=str): """ read from a order_id keyed dictionary of orders """ out = {} order_ids = set() for key in orders: order = orders[key] order['asset'] = asset_finder.lookup_symbol(order['asset']) out[key_transform(key)] = Order.from_dict(order) order_ids.add(key) return out, order_ids
def _order_from_dict(self, o): # pd.Timestamp(datetime.strptime('12142018032919','%m%d%Y%H%M%S')) order_dict = {} asset = self._asset_finder.lookup_symbol(o['currency']) order_dict['oid'] = o['orderId'] order_dict['broker_order_id'] = o['tradeId'] order_dict['exchange_order_id'] = o['orderId'] order_dict['parent_order_id'] = o['ocoBulkId'] order_dict['asset'] = asset order_dict['user'] = '******' order_dict['placed_by'] = o['accountId'] order_dict['product_type'] = ProductType.DELIVERY order_dict['order_flag'] = OrderFlag.NORMAL # FXCM is the liquidity provide, so filled is either all or nothing # TODO: check this assumption order_dict['quantity'] = o['amountK'] * 1000 order_dict['filled'] = 0 order_dict['pending'] = order_dict['quantity'] order_dict['disclosed'] = 0 ''' Entry Orders are by definition limit orders. The stoploss and limit rates mimick a bracket order with defined stoploss and take profit target respectively. We do not have native support for these in Blueshift yet.''' order_dict['order_type'] = OrderType.LIMIT order_dict['price'] = o['buy'] if o['isBuy'] is True else o['sell'] # TODO: hack, the stops and the limits are NOT handled properly here. order_dict['trigger_price'] = o['limitRate'] order_dict['stoploss_price'] = o['stopRate'] order_dict['side'] = OrderSide.BUY if o['isBuy'] is True else\ OrderSide.SELL # TODO: assumes order is zero fill. Validate. order_dict['average_price'] = 0 order_dict['order_validity'] = order_validity_map.get( o['timeInForce'], OrderValidity.DAY) order_dict['status'] = order_status_map[o['status']] order_dict['status_message'] = '' timestamp = datetime.strptime(o['time'], '%m%d%Y%H%M%S%f') order_dict['exchange_timestamp'] = pd.Timestamp(timestamp, tz=self.tz) # TODO: figure out the timestamp order_dict['timestamp'] = timestamp order_dict['tag'] = o['accountId'] order = Order.from_dict(order_dict) return order.oid, order
def read_transactions_from_dict(txns_dict, asset_finder, key_transform=lambda x:x): """ read from a timestamped ordered dict of transactions """ txns = OrderedDict() order_ids = set() for key in txns_dict: values = txns_dict[key] transactions = [] for value in values: value['asset'] = asset_finder.lookup_symbol(value['asset']) transactions.append(Order.from_dict(value)) order_ids.add(value['oid']) txns[key_transform(key)] = transactions return txns, order_ids
def order(self, asset, quantity, limit_price=0, stop_price=0, style=None): ''' Place new order. This is the interface to underlying broker for ALL order related API functions. ''' if not self.is_TRADING_BAR(): msg = f"can't place order for {asset.symbol}," msg = msg + " market not open yet." self.log_warning(msg) return mult = asset.mult quantity = int(round(quantity / mult) * mult) if quantity == 0: return side = OrderSide.BUY if quantity > 0 else OrderSide.SELL order_type = 0 if style: order_type = style else: if limit_price > 0: order_type = 1 if stop_price > 0: order_type = order_type | 2 o = Order(abs(quantity), side, asset, order_type=order_type, price=limit_price, stoploss_price=stop_price) order_id = self.context.broker.place_order(o) return order_id