def execute_signal(self, signal_event): side = signal_event.side market = signal_event.instrument units = int(self.trade_units) # Check side for correct bid/ask prices if side == "buy": add_price = Decimal(str(self.ticker.cur_ask)) remove_price = Decimal(str(self.ticker.cur_bid)) else: add_price = Decimal(str(self.ticker.cur_bid)) remove_price = Decimal(str(self.ticker.cur_ask)) exposure = Decimal(str(units)) # If there is no position, create one if market not in self.positions: self.add_new_position(side, market, units, exposure, add_price, remove_price) order = OrderEvent(market, units, "market", side) self.events.put(order) # If a position exists add or remove units else: ps = self.positions[market] # Check if the sides equal if side == ps.side: # Add to the position add_position_units(market, units, exposure, add_price, remove_price) else: # Check if the units close out the position if units == ps.units: # Close the position self.close_position(market, remove_price) order = OrderEvent(market, units, "market", side) self.events.put(order) elif units < ps.units: # Remove from the position self.remove_position_units(market, units, remove_price) else: # units > ps.units # Close the position and add a new one with # additional units of opposite side new_units = units - ps.units self.close_position(market, remove_price) if side == "buy": new_side = "sell" else: new_side = "buy" new_exposure = Decimal(str(units)) self.add_new_position(new_side, market, new_units, new_exposure, add_price, remove_price) print "Balance: %0.2f" % self.balance
def execute_signal(self, signal_event): side = signal_event.side market = signal_event.instrument units = int(self.trade_units) exposure = Decimal(str(units)) bid = Decimal(str(self.ticker.cur_bid)) ask = Decimal(str(self.ticker.cur_ask)) # If there is no position, create one if market not in self.positions: if side == "buy": position_type = "long" else: position_type = "short" self.add_new_position(position_type, market, units, exposure, bid, ask) # If a position exists add or remove units else: ps = self.positions[market] if side == "buy" and ps.position_type == "long": add_position_units(market, units, exposure, bid, ask) elif side == "sell" and ps.position_type == "long": if units == ps.units: self.close_position(market, bid, ask) # TODO: Allow units to be added/removed elif units < ps.units: return elif units > ps.units: return elif side == "buy" and ps.position_type == "short": if units == ps.units: self.close_position(market, bid, ask) # TODO: Allow units to be added/removed elif units < ps.units: return elif units > ps.units: return elif side == "sell" and ps.position_type == "short": add_position_units(market, units, exposure, bid, ask) order = OrderEvent(market, units, "market", side) self.events.put(order) print "Balance: %0.2f" % self.balance
def execute_signal(self, signal_event): side = signal_event.side currency_pair = signal_event.instrument units = int(self.trade_units) time = signal_event.time # If there is no position, create one if currency_pair not in self.positions: if side == "buy": position_type = "long" else: position_type = "short" self.add_new_position( position_type, currency_pair, units, self.ticker ) # If a position exists add or remove units else: ps = self.positions[currency_pair] if side == "buy" and ps.position_type == "long": add_position_units(currency_pair, units) elif side == "sell" and ps.position_type == "long": if units == ps.units: self.close_position(currency_pair) # TODO: Allow units to be added/removed elif units < ps.units: return elif units > ps.units: return elif side == "buy" and ps.position_type == "short": if units == ps.units: self.close_position(currency_pair) # TODO: Allow units to be added/removed elif units < ps.units: return elif units > ps.units: return elif side == "sell" and ps.position_type == "short": add_position_units(currency_pair, units) order = OrderEvent(currency_pair, units, "market", side) self.events.put(order)
def execute_signal(self, signal_event): # Check that the prices ticker contains all necessary # currency pairs prior to executing an order execute = True tp = self.ticker.prices for pair in tp: if tp[pair]["ask"] is None or tp[pair]["bid"] is None: execute = False # All necessary pricing data is available, # we can execute if execute: side = signal_event.side currency_pair = signal_event.instrument units = int(self.trade_units) time = signal_event.time # If there is no position, create one if currency_pair not in self.positions: if side == "buy": position_type = "long" else: position_type = "short" self.add_new_position(position_type, currency_pair, units, self.ticker) # If a position exists add or remove units else: ps = self.positions[currency_pair] if side == "buy" and ps.position_type == "long": add_position_units(currency_pair, units) elif side == "sell" and ps.position_type == "long": if units == ps.units: self.close_position(currency_pair) # TODO: Allow units to be added/removed elif units < ps.units: return elif units > ps.units: return elif side == "buy" and ps.position_type == "short": if units == ps.units: self.close_position(currency_pair) # TODO: Allow units to be added/removed elif units < ps.units: return elif units > ps.units: return elif side == "sell" and ps.position_type == "short": add_position_units(currency_pair, units) order = OrderEvent(currency_pair, units, "market", side) self.events.put(order) self.logger.info("Portfolio Balance: %s" % self.balance) else: self.logger.info( "Unable to execute order as price data was insufficient.")
def execute_signal(self, signal_event): # Check that the prices ticker contains all necessary # currency pairs prior to executing an order execute = True tp = self.ticker.prices for pair in tp: if tp[pair]["ask"] is None or tp[pair]["bid"] is None: execute = False print("unable to execute as ask or bid was None") # All necessary pricing data is available, # we can execute if execute: side = signal_event.side currency_pair = signal_event.instrument units = int(self.trade_units) time = signal_event.time # If there is no position, create one if currency_pair not in self.positions: if side == "buy": print("Not in positions; long handler") position_type = "long" else: print("Not in positions; short handler") position_type = "short" self.add_new_position( position_type, currency_pair, units, self.ticker ) # print("adding units: " + str(units) + " at price: " + str(self.ticker.prices)) # INSERT balance updating code here. (add_new_position *and* add_new_position_units) # TEST by showing how much "equity" exits in positions # If a position exists add or remove units else: ps = self.positions[currency_pair] if side == "buy" and ps.position_type == "long": print("Have positions; buy/long") add_position_units(currency_pair, units) elif side == "sell" and ps.position_type == "long": print("Have positions; sell/long") if units == ps.units: self.close_position(currency_pair) # TODO: Allow units to be added/removed elif units < ps.units: return elif units > ps.units: return elif side == "buy" and ps.position_type == "short": print("have positions; buy/short") if units == ps.units: self.close_position(currency_pair) # TODO: Allow units to be added/removed elif units < ps.units: return elif units > ps.units: return elif side == "sell" and ps.position_type == "short": print("have positions; sell/short") add_position_units(currency_pair, units) order = OrderEvent(currency_pair, units, "market", side) print("putting order in") self.events.put(order) print("Portfolio Balance: %s" % self.balance) self.logger.info("Portfolio Balance: %s" % self.balance) else: self.logger.info("Unable to execute order as price data was insufficient.")