Ejemplo n.º 1
0
    def fillStopOrder(self, broker_, order, bar):
        # First check if the stop price was hit so the market order becomes active.
        if not order.getStopHit():
            stopPriceTrigger = get_stop_price_trigger(
                order.getAction(), order.getStopPrice(),
                broker_.getStrategy().getUseAdjustedValues(), bar)
            order.setStopHit(stopPriceTrigger is not None)

            if order.getStopHit():
                broker_.submitOrder(order)
                return True
        return None
Ejemplo n.º 2
0
 def testStopOrderTriggerSell(self):
     barsBuilder = broker_backtesting_test.BarsBuilder(INSTRUMENT, bar.Frequency.MINUTE)
     # Bar is above
     self.assertEqual(
         fillstrategy.get_stop_price_trigger(
             broker.Order.Action.SELL, 10, False, barsBuilder.nextBar(15, 15, 15, 15)
         ),
         None
     )
     self.assertEqual(
         fillstrategy.get_stop_price_trigger(
             broker.Order.Action.SELL, 10, False, barsBuilder.nextBar(15, 16, 11, 15)
         ),
         None
     )
     # Low touches
     self.assertEqual(
         fillstrategy.get_stop_price_trigger(
             broker.Order.Action.SELL, 10, False, barsBuilder.nextBar(15, 16, 10, 11)
         ),
         10
     )
     # Low penetrates
     self.assertEqual(
         fillstrategy.get_stop_price_trigger(
             broker.Order.Action.SELL, 10, False, barsBuilder.nextBar(15, 16, 9, 11)
         ),
         10
     )
     # Open touches
     self.assertEqual(
         fillstrategy.get_stop_price_trigger(
             broker.Order.Action.SELL, 10, False, barsBuilder.nextBar(10, 10, 10, 10)
         ),
         10
     )
     # Open is below
     self.assertEqual(
         fillstrategy.get_stop_price_trigger(
             broker.Order.Action.SELL, 10, False, barsBuilder.nextBar(9, 12, 4, 9)
         ),
         9
     )
     # Bar gaps below
     self.assertEqual(
         fillstrategy.get_stop_price_trigger(
             broker.Order.Action.SELL, 10, False, barsBuilder.nextBar(8, 9, 6, 9)
         ),
         8
     )
Ejemplo n.º 3
0
 def testStopOrderTriggerBuy(self):
     barsBuilder = broker_backtesting_test.BarsBuilder(INSTRUMENT, bar.Frequency.MINUTE)
     # Bar is below
     self.assertEqual(
         fillstrategy.get_stop_price_trigger(
             broker.Order.Action.BUY, 10, False, barsBuilder.nextBar(5, 5, 5, 5)
         ),
         None
     )
     self.assertEqual(
         fillstrategy.get_stop_price_trigger(
             broker.Order.Action.BUY, 10, False, barsBuilder.nextBar(5, 6, 4, 5)
         ),
         None
     )
     # High touches
     self.assertEqual(
         fillstrategy.get_stop_price_trigger(
             broker.Order.Action.BUY, 10, False, barsBuilder.nextBar(5, 10, 4, 9)
         ),
         10
     )
     # High penetrates
     self.assertEqual(
         fillstrategy.get_stop_price_trigger(
             broker.Order.Action.BUY, 10, False, barsBuilder.nextBar(5, 11, 4, 9)
         ),
         10
     )
     # Open touches
     self.assertEqual(
         fillstrategy.get_stop_price_trigger(
             broker.Order.Action.BUY, 10, False, barsBuilder.nextBar(10, 10, 10, 10)
         ),
         10
     )
     # Open is above
     self.assertEqual(
         fillstrategy.get_stop_price_trigger(
             broker.Order.Action.BUY, 10, False, barsBuilder.nextBar(11, 12, 4, 9)
         ),
         11
     )
     # Bar gaps above
     self.assertEqual(
         fillstrategy.get_stop_price_trigger(
             broker.Order.Action.BUY, 10, False, barsBuilder.nextBar(12, 13, 11, 12)
         ),
         12
     )
Ejemplo n.º 4
0
 def testStopOrderTriggerSell(self):
     barsBuilder = broker_backtesting_test.BarsBuilder(BaseTestCase.TestInstrument, bar.Frequency.MINUTE)
     # Bar is above
     self.assertEqual(fillstrategy.get_stop_price_trigger(broker.Order.Action.SELL, 10, False, barsBuilder.nextBar(15, 15, 15, 15)), None)
     self.assertEqual(fillstrategy.get_stop_price_trigger(broker.Order.Action.SELL, 10, False, barsBuilder.nextBar(15, 16, 11, 15)), None)
     # Low touches
     self.assertEqual(fillstrategy.get_stop_price_trigger(broker.Order.Action.SELL, 10, False, barsBuilder.nextBar(15, 16, 10, 11)), 10)
     # Low penetrates
     self.assertEqual(fillstrategy.get_stop_price_trigger(broker.Order.Action.SELL, 10, False, barsBuilder.nextBar(15, 16, 9, 11)), 10)
     # Open touches
     self.assertEqual(fillstrategy.get_stop_price_trigger(broker.Order.Action.SELL, 10, False, barsBuilder.nextBar(10, 10, 10, 10)), 10)
     # Open is below
     self.assertEqual(fillstrategy.get_stop_price_trigger(broker.Order.Action.SELL, 10, False, barsBuilder.nextBar(9, 12, 4, 9)), 9)
     # Bar gaps below
     self.assertEqual(fillstrategy.get_stop_price_trigger(broker.Order.Action.SELL, 10, False, barsBuilder.nextBar(8, 9, 6, 9)), 8)
Ejemplo n.º 5
0
 def testStopOrderTriggerBuy(self):
     barsBuilder = broker_backtesting_test.BarsBuilder(BaseTestCase.TestInstrument, bar.Frequency.MINUTE)
     # Bar is below
     self.assertEqual(fillstrategy.get_stop_price_trigger(broker.Order.Action.BUY, 10, False, barsBuilder.nextBar(5, 5, 5, 5)), None)
     self.assertEqual(fillstrategy.get_stop_price_trigger(broker.Order.Action.BUY, 10, False, barsBuilder.nextBar(5, 6, 4, 5)), None)
     # High touches
     self.assertEqual(fillstrategy.get_stop_price_trigger(broker.Order.Action.BUY, 10, False, barsBuilder.nextBar(5, 10, 4, 9)), 10)
     # High penetrates
     self.assertEqual(fillstrategy.get_stop_price_trigger(broker.Order.Action.BUY, 10, False, barsBuilder.nextBar(5, 11, 4, 9)), 10)
     # Open touches
     self.assertEqual(fillstrategy.get_stop_price_trigger(broker.Order.Action.BUY, 10, False, barsBuilder.nextBar(10, 10, 10, 10)), 10)
     # Open is above
     self.assertEqual(fillstrategy.get_stop_price_trigger(broker.Order.Action.BUY, 10, False, barsBuilder.nextBar(11, 12, 4, 9)), 11)
     # Bar gaps above
     self.assertEqual(fillstrategy.get_stop_price_trigger(broker.Order.Action.BUY, 10, False, barsBuilder.nextBar(12, 13, 11, 12)), 12)