예제 #1
0
class TestLongEURUSDPosition(unittest.TestCase):
    """
    Unit tests that cover going short EUR/USD with an account
    denominated currency of GBP, using 2,000 units of EUR/USD.
    """
    def setUp(self):
        home_currency = "GBP"
        position_type = "short"
        currency_pair = "EURUSD"
        units = Decimal("2000")
        ticker = TickerMock()
        self.position = Position(home_currency, position_type, currency_pair,
                                 units, ticker)

    def test_calculate_init_pips(self):
        pos_pips = self.position.calculate_pips()
        self.assertEqual(pos_pips, Decimal("-0.00015"))

    def test_calculate_init_profit_base(self):
        profit_base = self.position.calculate_profit_base()
        self.assertEqual(profit_base, Decimal("-0.19954"))

    def test_calculate_init_profit_perc(self):
        profit_perc = self.position.calculate_profit_perc()
        self.assertEqual(profit_perc, Decimal("-0.00998"))

    def test_calculate_updated_values(self):
        """
        Check that after the bid/ask prices move, that the updated
        pips, profit and percentage profit calculations are correct.
        """
        prices = self.position.ticker.prices
        prices["GBPUSD"] = {
            "bid": Decimal("1.50486"),
            "ask": Decimal("1.50586")
        }
        prices["USDGBP"] = {
            "bid": Decimal("0.66451"),
            "ask": Decimal("0.66407")
        }
        prices["EURUSD"] = {
            "bid": Decimal("1.07811"),
            "ask": Decimal("1.07827")
        }
        self.position.update_position_price()

        # Check pips
        pos_pips = self.position.calculate_pips()
        self.assertEqual(pos_pips, Decimal("0.00005"))
        # Check profit base
        profit_base = self.position.calculate_profit_base()
        self.assertEqual(profit_base, Decimal("0.06641"))
        # Check profit percentage
        profit_perc = self.position.calculate_profit_perc()
        self.assertEqual(profit_perc, Decimal("0.00332"))
예제 #2
0
class TestLongEURUSDPosition(unittest.TestCase):

    """
    Unit tests that cover going short EUR/USD with an account
    denominated currency of GBP, using 2,000 units of EUR/USD.
    """

    def setUp(self):
        home_currency = "GBP"
        position_type = "short"
        currency_pair = "EURUSD"
        units = Decimal("2000")
        ticker = TickerMock()
        self.position = Position(
            home_currency, position_type,
            currency_pair, units, ticker
        )

    def test_calculate_init_pips(self):
        pos_pips = self.position.calculate_pips()
        self.assertEqual(pos_pips, Decimal("-0.00015"))

    def test_calculate_init_profit_base(self):
        profit_base = self.position.calculate_profit_base()
        self.assertEqual(profit_base, Decimal("-0.19954"))

    def test_calculate_init_profit_perc(self):
        profit_perc = self.position.calculate_profit_perc()
        self.assertEqual(profit_perc, Decimal("-0.00998"))

    def test_calculate_updated_values(self):
        """
        Check that after the bid/ask prices move, that the updated
        pips, profit and percentage profit calculations are correct.
        """
        prices = self.position.ticker.prices
        prices["GBPUSD"] = {
            "bid": Decimal("1.50486"), "ask": Decimal("1.50586")}
        prices["USDGBP"] = {
            "bid": Decimal("0.66451"), "ask": Decimal("0.66407")}
        prices["EURUSD"] = {
            "bid": Decimal("1.07811"), "ask": Decimal("1.07827")}
        self.position.update_position_price()

        # Check pips
        pos_pips = self.position.calculate_pips()
        self.assertEqual(pos_pips, Decimal("0.00005"))
        # Check profit base
        profit_base = self.position.calculate_profit_base()
        self.assertEqual(profit_base, Decimal("0.06641"))
        # Check profit percentage
        profit_perc = self.position.calculate_profit_perc()
        self.assertEqual(profit_perc, Decimal("0.00332"))
예제 #3
0
class TestShortGBPUSDPosition(unittest.TestCase):
    """
    Unit tests that cover going short GBP/USD with an account
    denominated currency of GBP, using 2,000 units of GBP/USD.
    """
    def setUp(self):
        home_currency = "USD"
        position_type = "short"
        currency_pair = "GBPUSD"
        units = Decimal("2000")
        ticker = TickerMock()
        take_profit = "1.50049"
        stop_loss = "1.50649"
        self.position = Position(home_currency, position_type, currency_pair,
                                 units, ticker, take_profit, stop_loss)

    def test_calculate_init_pips(self):
        pos_pips = self.position.calculate_pips()
        self.assertEqual(pos_pips, Decimal("-2.1000"))

    def test_calculate_init_profit_base(self):
        profit_base = self.position.calculate_profit_base()
        self.assertEqual(profit_base, Decimal("-0.4200"))

    def test_calculate_init_profit_perc(self):
        profit_perc = self.position.calculate_profit_perc()
        self.assertEqual(profit_perc, Decimal("-0.02100"))

    def test_calculate_updated_values(self):
        """
        Check that after the bid/ask prices move, that the updated
        pips, profit and percentage profit calculations are correct.
        """
        prices = self.position.ticker.prices
        prices["GBPUSD"] = {
            "bid": Decimal("1.50486"),
            "ask": Decimal("1.50586")
        }
        self.position.update_position_price()

        # Check pips
        pos_pips = self.position.calculate_pips()
        self.assertEqual(pos_pips, Decimal("-25.80000"))
        # Check profit base
        profit_base = self.position.calculate_profit_base()
        self.assertEqual(profit_base, Decimal("-5.16000"))
        # Check profit percentage
        profit_perc = self.position.calculate_profit_perc()
        self.assertEqual(profit_perc, Decimal("-0.25800"))
예제 #4
0
class TestLongEURUSDPosition(unittest.TestCase):
    """
    Unit tests that cover going long EUR/USD with an account
    denominated currency of GBP, using 2,000 units of EUR/USD.
    """
    def setUp(self):
        home_currency = "USD"
        position_type = "long"
        currency_pair = "EURUSD"
        units = Decimal("2000")
        ticker = TickerMock()
        take_profit = "1.08147"
        stop_loss = "1.07532"
        self.position = Position(home_currency, position_type, currency_pair,
                                 units, ticker, take_profit, stop_loss)

    def test_calculate_init_pips(self):
        pos_pips = self.position.calculate_pips()
        self.assertEqual(pos_pips, Decimal("-1.5000"))

    def test_calculate_init_profit_base(self):
        profit_base = self.position.calculate_profit_base()
        self.assertEqual(profit_base, Decimal("-0.30000"))

    def test_calculate_init_profit_perc(self):
        profit_perc = self.position.calculate_profit_perc()
        self.assertEqual(profit_perc, Decimal("-0.01500"))

    def test_calculate_updated_values(self):
        """
        Check that after the bid/ask prices move, that the updated
        pips, profit and percentage profit calculations are correct.
        """
        prices = self.position.ticker.prices
        prices["EURUSD"] = {
            "bid": Decimal("1.07811"),
            "ask": Decimal("1.07827")
        }
        self.position.update_position_price()

        # Check pips
        pos_pips = self.position.calculate_pips()
        self.assertEqual(pos_pips, Decimal("-3.60000"))
        # Check profit base
        profit_base = self.position.calculate_profit_base()
        self.assertEqual(profit_base, Decimal("-0.72000"))
        # Check profit percentage
        profit_perc = self.position.calculate_profit_perc()
        self.assertEqual(profit_perc, Decimal("-0.03600"))
예제 #5
0
class TestLongGBPUSDPosition(unittest.TestCase):
    def setUp(self):
        getcontext.prec = 2
        side = "LONG"
        market = "GBP/USD"
        units = Decimal(str(2000))
        exposure = Decimal("2000.00")
        avg_price = Decimal("1.51819")
        cur_price = Decimal("1.51770")
        self.position = Position(side, market, units, exposure, avg_price,
                                 cur_price)

    def test_calculate_pips(self):
        pos_pips = self.position.calculate_pips()
        self.assertEqual(pos_pips, Decimal("-0.00049"))

    def test_calculate_profit_base(self):
        profit_base = self.position.calculate_profit_base(
            self.position.exposure)
        self.assertEqual(profit_base, Decimal("-0.64571"))

    def test_calculate_profit_perc(self):
        profit_perc = self.position.calculate_profit_perc(
            self.position.exposure)
        self.assertEqual(profit_perc, Decimal("-0.03229"))
예제 #6
0
class TestLongGBPUSDPosition(unittest.TestCase):
    def setUp(self):
        getcontext.prec = 2
        side = "LONG"
        market = "GBP/USD"
        units = Decimal(str(2000))
        exposure = Decimal("2000.00")
        avg_price = Decimal("1.51819")
        cur_price = Decimal("1.51770")
        self.position = Position(
            side, market, units, exposure,
            avg_price, cur_price
        )

    def test_calculate_pips(self):
        pos_pips = self.position.calculate_pips()
        self.assertEqual(pos_pips, Decimal("-0.00049"))

    def test_calculate_profit_base(self):
        profit_base = self.position.calculate_profit_base(self.position.exposure)
        self.assertEqual(profit_base, Decimal("-0.64571"))

    def test_calculate_profit_perc(self):
        profit_perc = self.position.calculate_profit_perc(self.position.exposure)
        self.assertEqual(profit_perc, Decimal("-0.03229"))
예제 #7
0
class TestLongGBPUSDPosition(unittest.TestCase):
    def setUp(self):
        getcontext.prec = 2
        position_type = "long"
        market = "GBP/USD"
        units = Decimal("2000")
        exposure = Decimal("2000.00")
        bid = Decimal("1.50328")
        ask = Decimal("1.50349")
        self.position = Position(
            position_type, market, 
            units, exposure, bid, ask
        )

    def test_calculate_init_pips(self):
        pos_pips = self.position.calculate_pips()
        self.assertEqual(pos_pips, Decimal("-0.00021"))

    def test_calculate_init_profit_base(self):
        profit_base = self.position.calculate_profit_base(self.position.exposure)
        self.assertEqual(profit_base, Decimal("-0.27939"))

    def test_calculate_init_profit_perc(self):
        profit_perc = self.position.calculate_profit_perc(self.position.exposure)
        self.assertEqual(profit_perc, Decimal("-0.01397"))

    def test_calculate_updated_values(self):
        """
        Check that after the bid/ask prices move, that the updated
        pips, profit and percentage profit calculations are correct.
        """
        bid = Decimal("1.50486")
        ask = Decimal("1.50586")
        self.position.update_position_price(bid, ask, self.position.exposure)
        # Check pips
        pos_pips = self.position.calculate_pips()
        self.assertEqual(pos_pips, Decimal("0.00137"))
        # Check profit base
        profit_base = self.position.calculate_profit_base(self.position.exposure)
        self.assertEqual(profit_base, Decimal("1.82077"))
        # Check profit percentage
        profit_perc = self.position.calculate_profit_perc(self.position.exposure)
        self.assertEqual(profit_perc, Decimal("0.09104"))