Ejemplo n.º 1
0
 def test_execute_fill_event_add_to_position(self):
     buyevent = FillEvent("EUR_USD", 200, 'LONG', 4)
     self.pf.execute_fill_event(buyevent)
     buyevent = FillEvent("EUR_USD", 200, 'LONG', 4)
     self.pf.execute_fill_event(buyevent)
     self.assertEqual(self.pf.positions["EUR_USD"].side, 'LONG')
     self.assertEqual(self.pf.positions["EUR_USD"].market, 'EUR_USD')
     self.assertEqual(self.pf.positions["EUR_USD"].units, 400)
     self.assertEqual(self.pf.positions["EUR_USD"].exposure, 400)
Ejemplo n.º 2
0
 def test_execute_signal_invert_position(self):
     buyevent = FillEvent("EUR_USD", 200, 'LONG', 4)
     self.pf.execute_fill_event(buyevent)
     sellevent = FillEvent("EUR_USD", 400, 'SHORT', 2)
     self.pf.execute_fill_event(sellevent)
     self.assertEqual(self.pf.positions["EUR_USD"].side, 'SHORT')
     self.assertEqual(self.pf.positions["EUR_USD"].market, 'EUR_USD')
     self.assertEqual(self.pf.positions["EUR_USD"].units, 200)
     self.assertEqual(self.pf.positions["EUR_USD"].exposure, 200)
     self.assertEqual(self.pf.balance, 9800)
Ejemplo n.º 3
0
 def execute_order(self, order_event):
     headers = {
         "Content-Type": "application/x-www-form-urlencoded",
         "Authorization": "Bearer " + self.access_token
     }
     params = urllib.urlencode({
         "instrument": order_event.instrument,
         "units": order_event.units,
         "type": order_event.order_type,
         "side": order_event.side
     })
     self.conn.request("POST",
                       "/v1/accounts/%s/orders" % str(self.account_id),
                       params, headers)
     response = self.conn.getresponse().read()
     if response:
         try:
             msg = json.loads(response)
         except Exception as e:
             self.logger.critical(
                 "Caught exception when converting message to json %s\n" +
                 str(e))
             return
         if msg.has_key("price") and (msg.has_key("tradeOpened")
                                      or msg.has_key("tradeClosed")):
             self.logger.debug(msg)
             instrument = msg["instrument"]
             price = msg["price"]
             if msg["tradeOpened"].has_key("units"):
                 units = msg["tradeOpened"]["units"]
                 side = msg["tradeOpened"]["side"]
                 if side == "buy":
                     fevent = FillEvent(instrument, units, "LONG", price)
                 elif side == "sell":
                     fevent = FillEvent(instrument, units, "SHORT", price)
                 else:
                     raise ValueError("side should be 'buy' or 'sell' "\
                             "but is %s", side)
                 self.event_queue.put(fevent)
             else:
                 for close in msg["tradesClosed"]:
                     units = close["units"]
                     side = close["side"]
                     if side == "buy":
                         fevent = FillEvent(instrument, units, "LONG",
                                            price)
                     elif side == "sell":
                         fevent = FillEvent(instrument, units, "SHORT",
                                            price)
                     else:
                         raise ValueError("side should be 'buy' or 'sell' "\
                             "but is %s", side)
                     self.event_queue.put(fevent)
Ejemplo n.º 4
0
 def execute_order(self, order_event):
     instrument = order_event.instrument
     units = order_event.units
     order_type = order_event.order_type
     side = order_event.side
     self.logger.debug("Would have executed: %s ", order_event)
     if side == "buy":
         price = self.ticker.cur_prices[instrument].ask
         fevent = FillEvent(instrument, units, "LONG", price)
     elif side == "sell":
         price = self.ticker.cur_prices[instrument].bid
         fevent = FillEvent(instrument, units, "SHORT", price)
     else:
         raise ValueError("side should be 'buy' or 'sell' "\
                 "but is %s", side)
     self.event_queue.put(fevent)
Ejemplo n.º 5
0
 def test_execute_fill_event_new_position_short(self):
     sellevent = FillEvent("EUR_USD", 100, 'SHORT', 3)
     self.leverage_pf.execute_fill_event(sellevent)
     self.assertEqual(self.leverage_pf.positions["EUR_USD"].side, 'SHORT')
     self.assertEqual(self.leverage_pf.positions["EUR_USD"].market,
                      'EUR_USD')
     self.assertEqual(self.leverage_pf.positions["EUR_USD"].units, 100)
     self.assertEqual(self.leverage_pf.positions["EUR_USD"].exposure, 2000)
     self.assertEqual(self.leverage_pf.positions["EUR_USD"].avg_price, 3)
Ejemplo n.º 6
0
 def test_execute_fill_event_new_position_long(self):
     buyevent = FillEvent("EUR_USD", 200, 'LONG', 4)
     self.pf.execute_fill_event(buyevent)
     self.assertEqual(self.pf.positions["EUR_USD"].side, 'LONG')
     self.assertEqual(self.pf.positions["EUR_USD"].market, 'EUR_USD')
     self.assertEqual(self.pf.positions["EUR_USD"].units, 200)
     self.assertEqual(self.pf.positions["EUR_USD"].exposure, 200)
     self.assertEqual(self.pf.positions["EUR_USD"].avg_price, 4)
     self.assertEqual(self.pf.positions["EUR_USD"].cur_price, 3)
Ejemplo n.º 7
0
 def test_execute_signal_remove_position(self):
     buyevent = FillEvent("EUR_USD", 200, 'LONG', 4)
     self.pf.execute_fill_event(buyevent)
     sellevent = FillEvent("EUR_USD", 200, 'SHORT', 4)
     self.pf.execute_fill_event(sellevent)
     self.assertNotIn('EUR_USD', self.pf.positions.keys())