Ejemplo n.º 1
0
    def setUp(self):
        TestUnitSetUpStage.setUp(self)

        self.sell_order = create_filled_order(trade_summary=self.trade_summary,
                                              underlying=self.underlying,
                                              spread='BACKRATIO',
                                              contract='CALL',
                                              side='SELL',
                                              quantity=-1,
                                              strike=43,
                                              price=1.42,
                                              net_price=-0.58)

        self.buy_order = create_filled_order(trade_summary=self.trade_summary,
                                             underlying=self.underlying,
                                             spread='BACKRATIO',
                                             contract='CALL',
                                             side='BUY',
                                             quantity=2,
                                             strike=46,
                                             price=.42,
                                             net_price=0.0)

        self.contract_right = 100

        filled_orders = FilledOrder.objects.filter(underlying=self.underlying)

        self.long_call_backratio = StageLongCallBackratio(
            filled_orders=filled_orders, contract_right=self.contract_right)
Ejemplo n.º 2
0
    def setUp(self):
        TestUnitSetUpStage.setUp(self)

        self.sell_order = create_filled_order(
            trade_summary=self.trade_summary,
            underlying=self.underlying,
            spread='BACKRATIO',
            contract='CALL',
            side='SELL',
            quantity=-1,
            strike=43,
            price=1.42,
            net_price=-0.58
        )

        self.buy_order = create_filled_order(
            trade_summary=self.trade_summary,
            underlying=self.underlying,
            spread='BACKRATIO',
            contract='CALL',
            side='BUY',
            quantity=2,
            strike=46,
            price=.42,
            net_price=0.0
        )

        self.contract_right = 100

        filled_orders = FilledOrder.objects.filter(underlying=self.underlying)

        self.long_call_backratio = StageLongCallBackratio(
            filled_orders=filled_orders, contract_right=self.contract_right
        )
Ejemplo n.º 3
0
class TestStageLongCallBackratio1(TestUnitSetUpStage):
    def setUp(self):
        TestUnitSetUpStage.setUp(self)

        self.sell_order = create_filled_order(trade_summary=self.trade_summary,
                                              underlying=self.underlying,
                                              spread='BACKRATIO',
                                              contract='CALL',
                                              side='SELL',
                                              quantity=-1,
                                              strike=43,
                                              price=1.42,
                                              net_price=-0.58)

        self.buy_order = create_filled_order(trade_summary=self.trade_summary,
                                             underlying=self.underlying,
                                             spread='BACKRATIO',
                                             contract='CALL',
                                             side='BUY',
                                             quantity=2,
                                             strike=46,
                                             price=.42,
                                             net_price=0.0)

        self.contract_right = 100

        filled_orders = FilledOrder.objects.filter(underlying=self.underlying)

        self.long_call_backratio = StageLongCallBackratio(
            filled_orders=filled_orders, contract_right=self.contract_right)

    def test_create_even_stage1(self):
        """
        Create even stage using filled orders data
        """
        even_stage = self.long_call_backratio.create_even_stage1()

        self.method_test_create_stage(
            stage=even_stage,
            name='EVEN',
            expression='{price_a} == {current_price}',
            detail={
                'price_a': 48.42,
                'amount_a': 0.0,
                'price_b': 0.0,
                'amount_b': 0.0,
                'left_status': '',
                'left_expression': '',
                'right_status': '',
                'right_expression': '',
            })

        self.check_in_stage(stage_cls=even_stage, price=48.42, expect=True)
        self.check_in_stage(stage_cls=even_stage, price=49, expect=False)

        self.check_get_status(stage_cls=even_stage,
                              new_price=48.42,
                              old_price=48.42,
                              expect='UNKNOWN')

    def test_create_even_stage2(self):
        """
        Create even stage using filled orders data
        """
        even_stage = self.long_call_backratio.create_even_stage2()

        self.method_test_create_stage(
            stage=even_stage,
            name='EVEN',
            expression='{price_a} == {current_price}',
            detail={
                'price_a': 43.58,
                'amount_a': 0.0,
                'price_b': 0.0,
                'amount_b': 0.0,
                'left_status': '',
                'left_expression': '',
                'right_status': '',
                'right_expression': '',
            })

        self.check_in_stage(stage_cls=even_stage, price=43.58, expect=True)
        self.check_in_stage(stage_cls=even_stage, price=44, expect=False)

        self.check_get_status(stage_cls=even_stage,
                              new_price=43.58,
                              old_price=43.58,
                              expect='UNKNOWN')

    def test_create_max_loss_stage(self):
        """
        Create loss stage using filled orders data
        """
        max_loss_stage = self.long_call_backratio.create_max_loss_stage1()

        self.method_test_create_stage(
            stage=max_loss_stage,
            name='MAX_LOSS',
            expression='{price_a} == {current_price}',
            detail={
                'price_a': 46.00,
                'amount_a': -242.0,
                'price_b': 0.0,
                'amount_b': 0.0,
                'left_status': '',
                'left_expression': '',
                'right_status': '',
                'right_expression': '',
            })

        self.check_in_stage(stage_cls=max_loss_stage, price=46.0, expect=True)
        self.check_in_stage(stage_cls=max_loss_stage, price=47.7, expect=False)

        self.check_get_status(max_loss_stage,
                              new_price=46.6,
                              old_price=46.6,
                              expect='UNKNOWN')

    def test_create_loss_stage1(self):
        """
        Create loss stage using filled orders data
        """
        loss_stage = self.long_call_backratio.create_loss_stage1()

        self.method_test_create_stage(
            stage=loss_stage,
            name='LOSS',
            expression='{price_a} < {current_price} < {price_b}',
            detail={
                'price_a':
                46.0,
                'amount_a':
                -242.0,
                'price_b':
                48.42,
                'amount_b':
                0.0,
                'left_status':
                'RECOVERING',
                'left_expression':
                '{price_a} < {old_price} < {new_price} < {price_b}',
                'right_status':
                'LOSING',
                'right_expression':
                '{price_a} < {new_price} < {old_price} < {price_b}',
            })

        self.check_in_stage(stage_cls=loss_stage, price=47.7, expect=True)
        self.check_in_stage(stage_cls=loss_stage, price=49.5, expect=False)

        self.check_get_status(loss_stage,
                              new_price=47.9,
                              old_price=47.7,
                              expect='RECOVERING')
        self.check_get_status(loss_stage,
                              new_price=46.5,
                              old_price=47.7,
                              expect='LOSING')
        self.check_get_status(loss_stage,
                              new_price=46.5,
                              old_price=46.5,
                              expect='UNKNOWN')

    def test_create_profit_stage1(self):
        """
        Create profit stage using filled orders data
        """
        profit_stage = self.long_call_backratio.create_profit_stage1()

        self.method_test_create_stage(
            stage=profit_stage,
            name='PROFIT',
            expression='{price_a} < {current_price}',
            detail={
                'price_a': 48.42,
                'amount_a': 0.0,
                'price_b': 0.0,
                'amount_b': 0.0,
                'left_status': 'DECREASING',
                'left_expression': '{price_a} < {new_price} < {old_price}',
                'right_status': 'PROFITING',
                'right_expression': '{price_a} < {old_price} < {new_price}',
            })

        self.check_in_stage(stage_cls=profit_stage, price=48.5, expect=True)
        self.check_in_stage(stage_cls=profit_stage, price=47.5, expect=False)

        self.check_get_status(profit_stage,
                              new_price=49.5,
                              old_price=50,
                              expect='DECREASING')
        self.check_get_status(profit_stage,
                              new_price=49,
                              old_price=48.5,
                              expect='PROFITING')
        self.check_get_status(profit_stage,
                              new_price=49,
                              old_price=49,
                              expect='UNKNOWN')

    def test_create_loss_stage2(self):
        """
        Create loss stage using filled orders data
        """
        loss_stage = self.long_call_backratio.create_loss_stage2()

        self.method_test_create_stage(
            stage=loss_stage,
            name='LOSS',
            expression='{price_a} < {current_price} < {price_b}',
            detail={
                'price_a':
                43.58,
                'amount_a':
                0.0,
                'price_b':
                46.0,
                'amount_b':
                -242.0,
                'left_status':
                'RECOVERING',
                'left_expression':
                '{price_a} < {new_price} < {old_price} < {price_b}',
                'right_status':
                'LOSING',
                'right_expression':
                '{price_a} < {old_price} < {new_price} < {price_b}',
            })

        self.check_in_stage(stage_cls=loss_stage, price=44.3, expect=True)
        self.check_in_stage(stage_cls=loss_stage, price=41, expect=False)

        self.check_get_status(loss_stage,
                              new_price=44,
                              old_price=45,
                              expect='RECOVERING')
        self.check_get_status(loss_stage,
                              new_price=45,
                              old_price=44,
                              expect='LOSING')
        self.check_get_status(loss_stage,
                              new_price=45.5,
                              old_price=45.5,
                              expect='UNKNOWN')

    def test_create_profit_stage2(self):
        """
        Create profit stage using filled orders data
        """
        profit_stage = self.long_call_backratio.create_profit_stage2()

        self.method_test_create_stage(
            stage=profit_stage,
            name='PROFIT',
            expression='{price_a} < {current_price} < {price_b}',
            detail={
                'price_a':
                43.0,
                'amount_a':
                58.0,
                'price_b':
                43.58,
                'amount_b':
                0.0,
                'left_status':
                'DECREASING',
                'left_expression':
                '{price_a} < {old_price} < {new_price} < {price_b}',
                'right_status':
                'PROFITING',
                'right_expression':
                '{price_a} < {new_price} < {old_price} < {price_b}',
            })

        self.check_in_stage(stage_cls=profit_stage, price=43.3, expect=True)
        self.check_in_stage(stage_cls=profit_stage, price=41, expect=False)

        self.check_get_status(profit_stage,
                              new_price=43.5,
                              old_price=43.3,
                              expect='DECREASING')
        self.check_get_status(profit_stage,
                              new_price=43.1,
                              old_price=43.3,
                              expect='PROFITING')
        self.check_get_status(profit_stage,
                              new_price=43.3,
                              old_price=43.3,
                              expect='UNKNOWN')

    def test_create_max_profit_stage(self):
        """
        Create profit stage using filled orders data
        """
        max_profit_stage = self.long_call_backratio.create_max_profit_stage()

        self.method_test_create_stage(
            stage=max_profit_stage,
            name='MAX_PROFIT',
            expression='{current_price} <= {price_a}',
            detail={
                'price_a': 43.0,
                'amount_a': 58.0,
                'price_b': 0.0,
                'amount_b': 0.0,
                'left_status': 'VANISHING',
                'left_expression': '{old_price} < {new_price} <= {price_a}',
                'right_status': 'GUARANTEEING',
                'right_expression': '{new_price} < {old_price} <= {price_a}',
            })

        self.check_in_stage(stage_cls=max_profit_stage, price=42, expect=True)
        self.check_in_stage(stage_cls=max_profit_stage,
                            price=43.5,
                            expect=False)

        self.check_get_status(max_profit_stage,
                              new_price=41.5,
                              old_price=41,
                              expect='VANISHING')
        self.check_get_status(max_profit_stage,
                              new_price=40,
                              old_price=41,
                              expect='GUARANTEEING')
        self.check_get_status(max_profit_stage,
                              new_price=41,
                              old_price=41,
                              expect='UNKNOWN')

    def test_create_stages(self):
        """
        Test create stages using filled orders
        """
        print 'run create_stages...'
        stages = self.long_call_backratio.create_stages()
        self.assertEqual(type(stages), list)

        for stage in stages:
            print stage
            self.assertEqual(type(stage), PositionStage)
Ejemplo n.º 4
0
class TestStageLongCallBackratio2(TestUnitSetUpStage):
    def setUp(self):
        TestUnitSetUpStage.setUp(self)

        self.sell_order = create_filled_order(trade_summary=self.trade_summary,
                                              underlying=self.underlying,
                                              spread='BACKRATIO',
                                              contract='CALL',
                                              side='SELL',
                                              quantity=-1,
                                              strike=165,
                                              price=5.25,
                                              net_price=1.0)

        self.buy_order = create_filled_order(trade_summary=self.trade_summary,
                                             underlying=self.underlying,
                                             spread='BACKRATIO',
                                             contract='CALL',
                                             side='BUY',
                                             quantity=2,
                                             strike=170,
                                             price=3.25,
                                             net_price=0.0)

        self.contract_right = 100

        filled_orders = FilledOrder.objects.filter(underlying=self.underlying)

        self.long_call_backratio = StageLongCallBackratio(
            filled_orders=filled_orders, contract_right=self.contract_right)

    def test_create_even_stage1(self):
        """
        Create even stage using filled orders data
        """
        even_stage = self.long_call_backratio.create_even_stage1()

        self.method_test_create_stage(
            stage=even_stage,
            name='EVEN',
            expression='{price_a} == {current_price}',
            detail={
                'price_a': 176.0,
                'amount_a': 0.0,
                'price_b': 0.0,
                'amount_b': 0.0,
                'left_status': '',
                'left_expression': '',
                'right_status': '',
                'right_expression': '',
            })

        self.check_in_stage(stage_cls=even_stage, price=176.0, expect=True)
        self.check_in_stage(stage_cls=even_stage, price=178.0, expect=False)

        self.check_get_status(stage_cls=even_stage,
                              new_price=176,
                              old_price=176,
                              expect='UNKNOWN')

    def test_create_max_loss_stage1(self):
        """
        Create loss stage using filled orders data
        """
        max_loss_stage = self.long_call_backratio.create_max_loss_stage1()

        self.method_test_create_stage(
            stage=max_loss_stage,
            name='MAX_LOSS',
            expression='{price_a} == {current_price}',
            detail={
                'price_a': 170.0,
                'amount_a': -600.0,
                'price_b': 0.0,
                'amount_b': 0.0,
                'left_status': '',
                'left_expression': '',
                'right_status': '',
                'right_expression': '',
            })

        self.check_in_stage(stage_cls=max_loss_stage, price=170, expect=True)
        self.check_in_stage(stage_cls=max_loss_stage, price=172, expect=False)

        self.check_get_status(max_loss_stage,
                              new_price=171,
                              old_price=171,
                              expect='UNKNOWN')

    def test_create_loss_stage1(self):
        """
        Create loss stage using filled orders data
        """
        loss_stage = self.long_call_backratio.create_loss_stage1()

        self.method_test_create_stage(
            stage=loss_stage,
            name='LOSS',
            expression='{price_a} < {current_price} < {price_b}',
            detail={
                'price_a':
                170.0,
                'amount_a':
                -600.0,
                'price_b':
                176.0,
                'amount_b':
                0.0,
                'left_status':
                'RECOVERING',
                'left_expression':
                '{price_a} < {old_price} < {new_price} < {price_b}',
                'right_status':
                'LOSING',
                'right_expression':
                '{price_a} < {new_price} < {old_price} < {price_b}',
            })

        self.check_in_stage(stage_cls=loss_stage, price=171, expect=True)
        self.check_in_stage(stage_cls=loss_stage, price=169, expect=False)

        self.check_get_status(loss_stage,
                              new_price=174,
                              old_price=173,
                              expect='RECOVERING')
        self.check_get_status(loss_stage,
                              new_price=173,
                              old_price=174,
                              expect='LOSING')
        self.check_get_status(loss_stage,
                              new_price=172,
                              old_price=172,
                              expect='UNKNOWN')

    def test_create_loss_stage2(self):
        """
        Create loss stage using filled orders data
        """
        loss_stage = self.long_call_backratio.create_loss_stage2()

        self.method_test_create_stage(
            stage=loss_stage,
            name='LOSS',
            expression='{price_a} < {current_price} < {price_b}',
            detail={
                'price_a':
                165.00,
                'amount_a':
                -100.0,
                'price_b':
                170.0,
                'amount_b':
                -600.0,
                'left_status':
                'RECOVERING',
                'left_expression':
                '{price_a} < {new_price} < {old_price} < {price_b}',
                'right_status':
                'LOSING',
                'right_expression':
                '{price_a} < {old_price} < {new_price} < {price_b}',
            })

        self.check_in_stage(stage_cls=loss_stage, price=168, expect=True)
        self.check_in_stage(stage_cls=loss_stage, price=172, expect=False)

        self.check_get_status(loss_stage,
                              new_price=167,
                              old_price=169,
                              expect='RECOVERING')
        self.check_get_status(loss_stage,
                              new_price=169,
                              old_price=167,
                              expect='LOSING')
        self.check_get_status(loss_stage,
                              new_price=168,
                              old_price=168,
                              expect='UNKNOWN')

    def test_create_max_loss_stage2(self):
        """
        Create loss stage using filled orders data
        """
        max_loss_stage = self.long_call_backratio.create_max_loss_stage2()

        self.method_test_create_stage(
            stage=max_loss_stage,
            name='MAX_LOSS',
            expression='{current_price} <= {price_a}',
            detail={
                'price_a': 165.0,
                'amount_a': -100.0,
                'price_b': 0.0,
                'amount_b': 0.0,
                'left_status': 'EASING',
                'left_expression': '{old_price} < {new_price} <= {price_a}',
                'right_status': 'WORST',
                'right_expression': '{new_price} < {old_price} <= {price_a}',
            })

        self.check_in_stage(stage_cls=max_loss_stage, price=164, expect=True)
        self.check_in_stage(stage_cls=max_loss_stage, price=167, expect=False)

        self.check_get_status(max_loss_stage,
                              new_price=163,
                              old_price=163,
                              expect='UNKNOWN')

    def test_create_profit_stage1(self):
        """
        Create profit stage using filled orders data
        """
        profit_stage = self.long_call_backratio.create_profit_stage1()

        self.method_test_create_stage(
            stage=profit_stage,
            name='PROFIT',
            expression='{price_a} < {current_price}',
            detail={
                'price_a': 176.0,
                'amount_a': 0.0,
                'price_b': 0.0,
                'amount_b': 0.0,
                'left_status': 'DECREASING',
                'left_expression': '{price_a} < {new_price} < {old_price}',
                'right_status': 'PROFITING',
                'right_expression': '{price_a} < {old_price} < {new_price}',
            })

        self.check_in_stage(stage_cls=profit_stage, price=177, expect=True)
        self.check_in_stage(stage_cls=profit_stage, price=175, expect=False)

        self.check_get_status(profit_stage,
                              new_price=177,
                              old_price=178.5,
                              expect='DECREASING')
        self.check_get_status(profit_stage,
                              new_price=179,
                              old_price=177,
                              expect='PROFITING')
        self.check_get_status(profit_stage,
                              new_price=177,
                              old_price=177,
                              expect='UNKNOWN')

    def test_create_stages(self):
        """
        Test create stages using filled orders
        """
        print 'run create_stages...'
        stages = self.long_call_backratio.create_stages()
        self.assertEqual(type(stages), list)

        for stage in stages:
            print stage
            self.assertEqual(type(stage), PositionStage)
Ejemplo n.º 5
0
class TestStageLongCallBackratio1(TestUnitSetUpStage):
    def setUp(self):
        TestUnitSetUpStage.setUp(self)

        self.sell_order = create_filled_order(
            trade_summary=self.trade_summary,
            underlying=self.underlying,
            spread='BACKRATIO',
            contract='CALL',
            side='SELL',
            quantity=-1,
            strike=43,
            price=1.42,
            net_price=-0.58
        )

        self.buy_order = create_filled_order(
            trade_summary=self.trade_summary,
            underlying=self.underlying,
            spread='BACKRATIO',
            contract='CALL',
            side='BUY',
            quantity=2,
            strike=46,
            price=.42,
            net_price=0.0
        )

        self.contract_right = 100

        filled_orders = FilledOrder.objects.filter(underlying=self.underlying)

        self.long_call_backratio = StageLongCallBackratio(
            filled_orders=filled_orders, contract_right=self.contract_right
        )

    def test_create_even_stage1(self):
        """
        Create even stage using filled orders data
        """
        even_stage = self.long_call_backratio.create_even_stage1()

        self.method_test_create_stage(
            stage=even_stage,
            name='EVEN',
            expression='{price_a} == {current_price}',
            detail={
                'price_a': 48.42,
                'amount_a': 0.0,
                'price_b': 0.0,
                'amount_b': 0.0,
                'left_status': '',
                'left_expression': '',
                'right_status': '',
                'right_expression': '',
            }
        )

        self.check_in_stage(stage_cls=even_stage, price=48.42, expect=True)
        self.check_in_stage(stage_cls=even_stage, price=49, expect=False)

        self.check_get_status(
            stage_cls=even_stage, new_price=48.42, old_price=48.42, expect='UNKNOWN'
        )

    def test_create_even_stage2(self):
        """
        Create even stage using filled orders data
        """
        even_stage = self.long_call_backratio.create_even_stage2()

        self.method_test_create_stage(
            stage=even_stage,
            name='EVEN',
            expression='{price_a} == {current_price}',
            detail={
                'price_a': 43.58,
                'amount_a': 0.0,
                'price_b': 0.0,
                'amount_b': 0.0,
                'left_status': '',
                'left_expression': '',
                'right_status': '',
                'right_expression': '',
            }
        )

        self.check_in_stage(stage_cls=even_stage, price=43.58, expect=True)
        self.check_in_stage(stage_cls=even_stage, price=44, expect=False)

        self.check_get_status(
            stage_cls=even_stage, new_price=43.58, old_price=43.58, expect='UNKNOWN'
        )

    def test_create_max_loss_stage(self):
        """
        Create loss stage using filled orders data
        """
        max_loss_stage = self.long_call_backratio.create_max_loss_stage1()

        self.method_test_create_stage(
            stage=max_loss_stage,
            name='MAX_LOSS',
            expression='{price_a} == {current_price}',
            detail={
                'price_a': 46.00,
                'amount_a': -242.0,
                'price_b': 0.0,
                'amount_b': 0.0,
                'left_status': '',
                'left_expression': '',
                'right_status': '',
                'right_expression': '',
            }
        )

        self.check_in_stage(stage_cls=max_loss_stage, price=46.0, expect=True)
        self.check_in_stage(stage_cls=max_loss_stage, price=47.7, expect=False)

        self.check_get_status(max_loss_stage, new_price=46.6, old_price=46.6, expect='UNKNOWN')

    def test_create_loss_stage1(self):
        """
        Create loss stage using filled orders data
        """
        loss_stage = self.long_call_backratio.create_loss_stage1()

        self.method_test_create_stage(
            stage=loss_stage,
            name='LOSS',
            expression='{price_a} < {current_price} < {price_b}',
            detail={
                'price_a': 46.0,
                'amount_a': -242.0,
                'price_b': 48.42,
                'amount_b': 0.0,
                'left_status': 'RECOVERING',
                'left_expression': '{price_a} < {old_price} < {new_price} < {price_b}',
                'right_status': 'LOSING',
                'right_expression': '{price_a} < {new_price} < {old_price} < {price_b}',
            }
        )

        self.check_in_stage(stage_cls=loss_stage, price=47.7, expect=True)
        self.check_in_stage(stage_cls=loss_stage, price=49.5, expect=False)

        self.check_get_status(loss_stage, new_price=47.9, old_price=47.7, expect='RECOVERING')
        self.check_get_status(loss_stage, new_price=46.5, old_price=47.7, expect='LOSING')
        self.check_get_status(loss_stage, new_price=46.5, old_price=46.5, expect='UNKNOWN')

    def test_create_profit_stage1(self):
        """
        Create profit stage using filled orders data
        """
        profit_stage = self.long_call_backratio.create_profit_stage1()

        self.method_test_create_stage(
            stage=profit_stage,
            name='PROFIT',
            expression='{price_a} < {current_price}',
            detail={
                'price_a': 48.42,
                'amount_a': 0.0,
                'price_b': 0.0,
                'amount_b': 0.0,
                'left_status': 'DECREASING',
                'left_expression': '{price_a} < {new_price} < {old_price}',
                'right_status': 'PROFITING',
                'right_expression': '{price_a} < {old_price} < {new_price}',
            }
        )

        self.check_in_stage(stage_cls=profit_stage, price=48.5, expect=True)
        self.check_in_stage(stage_cls=profit_stage, price=47.5, expect=False)

        self.check_get_status(profit_stage, new_price=49.5, old_price=50, expect='DECREASING')
        self.check_get_status(profit_stage, new_price=49, old_price=48.5, expect='PROFITING')
        self.check_get_status(profit_stage, new_price=49, old_price=49, expect='UNKNOWN')

    def test_create_loss_stage2(self):
        """
        Create loss stage using filled orders data
        """
        loss_stage = self.long_call_backratio.create_loss_stage2()

        self.method_test_create_stage(
            stage=loss_stage,
            name='LOSS',
            expression='{price_a} < {current_price} < {price_b}',
            detail={
                'price_a': 43.58,
                'amount_a': 0.0,
                'price_b': 46.0,
                'amount_b': -242.0,
                'left_status': 'RECOVERING',
                'left_expression': '{price_a} < {new_price} < {old_price} < {price_b}',
                'right_status': 'LOSING',
                'right_expression': '{price_a} < {old_price} < {new_price} < {price_b}',
            }
        )

        self.check_in_stage(stage_cls=loss_stage, price=44.3, expect=True)
        self.check_in_stage(stage_cls=loss_stage, price=41, expect=False)

        self.check_get_status(loss_stage, new_price=44, old_price=45, expect='RECOVERING')
        self.check_get_status(loss_stage, new_price=45, old_price=44, expect='LOSING')
        self.check_get_status(loss_stage, new_price=45.5, old_price=45.5, expect='UNKNOWN')

    def test_create_profit_stage2(self):
        """
        Create profit stage using filled orders data
        """
        profit_stage = self.long_call_backratio.create_profit_stage2()

        self.method_test_create_stage(
            stage=profit_stage,
            name='PROFIT',
            expression='{price_a} < {current_price} < {price_b}',
            detail={
                'price_a': 43.0,
                'amount_a': 58.0,
                'price_b': 43.58,
                'amount_b': 0.0,
                'left_status': 'DECREASING',
                'left_expression': '{price_a} < {old_price} < {new_price} < {price_b}',
                'right_status': 'PROFITING',
                'right_expression': '{price_a} < {new_price} < {old_price} < {price_b}',
            }
        )

        self.check_in_stage(stage_cls=profit_stage, price=43.3, expect=True)
        self.check_in_stage(stage_cls=profit_stage, price=41, expect=False)

        self.check_get_status(profit_stage, new_price=43.5, old_price=43.3, expect='DECREASING')
        self.check_get_status(profit_stage, new_price=43.1, old_price=43.3, expect='PROFITING')
        self.check_get_status(profit_stage, new_price=43.3, old_price=43.3, expect='UNKNOWN')

    def test_create_max_profit_stage(self):
        """
        Create profit stage using filled orders data
        """
        max_profit_stage = self.long_call_backratio.create_max_profit_stage()

        self.method_test_create_stage(
            stage=max_profit_stage,
            name='MAX_PROFIT',
            expression='{current_price} <= {price_a}',
            detail={
                'price_a': 43.0,
                'amount_a': 58.0,
                'price_b': 0.0,
                'amount_b': 0.0,
                'left_status': 'VANISHING',
                'left_expression': '{old_price} < {new_price} <= {price_a}',
                'right_status': 'GUARANTEEING',
                'right_expression': '{new_price} < {old_price} <= {price_a}',
            }
        )

        self.check_in_stage(stage_cls=max_profit_stage, price=42, expect=True)
        self.check_in_stage(stage_cls=max_profit_stage, price=43.5, expect=False)

        self.check_get_status(max_profit_stage, new_price=41.5, old_price=41, expect='VANISHING')
        self.check_get_status(max_profit_stage, new_price=40, old_price=41, expect='GUARANTEEING')
        self.check_get_status(max_profit_stage, new_price=41, old_price=41, expect='UNKNOWN')

    def test_create_stages(self):
        """
        Test create stages using filled orders
        """
        print 'run create_stages...'
        stages = self.long_call_backratio.create_stages()
        self.assertEqual(type(stages), list)

        for stage in stages:
            print stage
            self.assertEqual(type(stage), PositionStage)
Ejemplo n.º 6
0
class TestStageLongCallBackratio2(TestUnitSetUpStage):
    def setUp(self):
        TestUnitSetUpStage.setUp(self)

        self.sell_order = create_filled_order(
            trade_summary=self.trade_summary,
            underlying=self.underlying,
            spread='BACKRATIO',
            contract='CALL',
            side='SELL',
            quantity=-1,
            strike=165,
            price=5.25,
            net_price=1.0
        )

        self.buy_order = create_filled_order(
            trade_summary=self.trade_summary,
            underlying=self.underlying,
            spread='BACKRATIO',
            contract='CALL',
            side='BUY',
            quantity=2,
            strike=170,
            price=3.25,
            net_price=0.0
        )

        self.contract_right = 100

        filled_orders = FilledOrder.objects.filter(underlying=self.underlying)

        self.long_call_backratio = StageLongCallBackratio(
            filled_orders=filled_orders, contract_right=self.contract_right
        )

    def test_create_even_stage1(self):
        """
        Create even stage using filled orders data
        """
        even_stage = self.long_call_backratio.create_even_stage1()

        self.method_test_create_stage(
            stage=even_stage,
            name='EVEN',
            expression='{price_a} == {current_price}',
            detail={
                'price_a': 176.0,
                'amount_a': 0.0,
                'price_b': 0.0,
                'amount_b': 0.0,
                'left_status': '',
                'left_expression': '',
                'right_status': '',
                'right_expression': '',
            }
        )

        self.check_in_stage(stage_cls=even_stage, price=176.0, expect=True)
        self.check_in_stage(stage_cls=even_stage, price=178.0, expect=False)

        self.check_get_status(
            stage_cls=even_stage, new_price=176, old_price=176, expect='UNKNOWN'
        )

    def test_create_max_loss_stage1(self):
        """
        Create loss stage using filled orders data
        """
        max_loss_stage = self.long_call_backratio.create_max_loss_stage1()

        self.method_test_create_stage(
            stage=max_loss_stage,
            name='MAX_LOSS',
            expression='{price_a} == {current_price}',
            detail={
                'price_a': 170.0,
                'amount_a': -600.0,
                'price_b': 0.0,
                'amount_b': 0.0,
                'left_status': '',
                'left_expression': '',
                'right_status': '',
                'right_expression': '',
            }
        )

        self.check_in_stage(stage_cls=max_loss_stage, price=170, expect=True)
        self.check_in_stage(stage_cls=max_loss_stage, price=172, expect=False)

        self.check_get_status(max_loss_stage, new_price=171, old_price=171, expect='UNKNOWN')

    def test_create_loss_stage1(self):
        """
        Create loss stage using filled orders data
        """
        loss_stage = self.long_call_backratio.create_loss_stage1()

        self.method_test_create_stage(
            stage=loss_stage,
            name='LOSS',
            expression='{price_a} < {current_price} < {price_b}',
            detail={
                'price_a': 170.0,
                'amount_a': -600.0,
                'price_b': 176.0,
                'amount_b': 0.0,
                'left_status': 'RECOVERING',
                'left_expression': '{price_a} < {old_price} < {new_price} < {price_b}',
                'right_status': 'LOSING',
                'right_expression': '{price_a} < {new_price} < {old_price} < {price_b}',
            }
        )

        self.check_in_stage(stage_cls=loss_stage, price=171, expect=True)
        self.check_in_stage(stage_cls=loss_stage, price=169, expect=False)

        self.check_get_status(loss_stage, new_price=174, old_price=173, expect='RECOVERING')
        self.check_get_status(loss_stage, new_price=173, old_price=174, expect='LOSING')
        self.check_get_status(loss_stage, new_price=172, old_price=172, expect='UNKNOWN')

    def test_create_loss_stage2(self):
        """
        Create loss stage using filled orders data
        """
        loss_stage = self.long_call_backratio.create_loss_stage2()

        self.method_test_create_stage(
            stage=loss_stage,
            name='LOSS',
            expression='{price_a} < {current_price} < {price_b}',
            detail={
                'price_a': 165.00,
                'amount_a': -100.0,
                'price_b': 170.0,
                'amount_b': -600.0,
                'left_status': 'RECOVERING',
                'left_expression': '{price_a} < {new_price} < {old_price} < {price_b}',
                'right_status': 'LOSING',
                'right_expression': '{price_a} < {old_price} < {new_price} < {price_b}',
            }
        )

        self.check_in_stage(stage_cls=loss_stage, price=168, expect=True)
        self.check_in_stage(stage_cls=loss_stage, price=172, expect=False)

        self.check_get_status(loss_stage, new_price=167, old_price=169, expect='RECOVERING')
        self.check_get_status(loss_stage, new_price=169, old_price=167, expect='LOSING')
        self.check_get_status(loss_stage, new_price=168, old_price=168, expect='UNKNOWN')

    def test_create_max_loss_stage2(self):
        """
        Create loss stage using filled orders data
        """
        max_loss_stage = self.long_call_backratio.create_max_loss_stage2()

        self.method_test_create_stage(
            stage=max_loss_stage,
            name='MAX_LOSS',
            expression='{current_price} <= {price_a}',
            detail={
                'price_a': 165.0,
                'amount_a': -100.0,
                'price_b': 0.0,
                'amount_b': 0.0,
                'left_status': 'EASING',
                'left_expression': '{old_price} < {new_price} <= {price_a}',
                'right_status': 'WORST',
                'right_expression': '{new_price} < {old_price} <= {price_a}',
            }
        )

        self.check_in_stage(stage_cls=max_loss_stage, price=164, expect=True)
        self.check_in_stage(stage_cls=max_loss_stage, price=167, expect=False)

        self.check_get_status(max_loss_stage, new_price=163, old_price=163, expect='UNKNOWN')

    def test_create_profit_stage1(self):
        """
        Create profit stage using filled orders data
        """
        profit_stage = self.long_call_backratio.create_profit_stage1()

        self.method_test_create_stage(
            stage=profit_stage,
            name='PROFIT',
            expression='{price_a} < {current_price}',
            detail={
                'price_a': 176.0,
                'amount_a': 0.0,
                'price_b': 0.0,
                'amount_b': 0.0,
                'left_status': 'DECREASING',
                'left_expression': '{price_a} < {new_price} < {old_price}',
                'right_status': 'PROFITING',
                'right_expression': '{price_a} < {old_price} < {new_price}',
            }
        )

        self.check_in_stage(stage_cls=profit_stage, price=177, expect=True)
        self.check_in_stage(stage_cls=profit_stage, price=175, expect=False)

        self.check_get_status(profit_stage, new_price=177, old_price=178.5, expect='DECREASING')
        self.check_get_status(profit_stage, new_price=179, old_price=177, expect='PROFITING')
        self.check_get_status(profit_stage, new_price=177, old_price=177, expect='UNKNOWN')

    def test_create_stages(self):
        """
        Test create stages using filled orders
        """
        print 'run create_stages...'
        stages = self.long_call_backratio.create_stages()
        self.assertEqual(type(stages), list)

        for stage in stages:
            print stage
            self.assertEqual(type(stage), PositionStage)