Modified the Position handling to use long/short instead of buy/sell for side/position_type. Also modified the unit tests for both Portfolio and Position to reflect these changes. Added a basic historical backtesting capability via backtest.py and using CSV tick data for currency pairs.
This commit is contained in:
+60
-24
@@ -7,55 +7,91 @@ from position import Position
|
||||
class TestLongGBPUSDPosition(unittest.TestCase):
|
||||
def setUp(self):
|
||||
getcontext.prec = 2
|
||||
side = "LONG"
|
||||
position_type = "long"
|
||||
market = "GBP/USD"
|
||||
units = Decimal(str(2000))
|
||||
units = Decimal("2000")
|
||||
exposure = Decimal("2000.00")
|
||||
avg_price = Decimal("1.51819")
|
||||
cur_price = Decimal("1.51770")
|
||||
bid = Decimal("1.50328")
|
||||
ask = Decimal("1.50349")
|
||||
self.position = Position(
|
||||
side, market, units, exposure,
|
||||
avg_price, cur_price
|
||||
position_type, market,
|
||||
units, exposure, bid, ask
|
||||
)
|
||||
|
||||
def test_calculate_pips(self):
|
||||
def test_calculate_init_pips(self):
|
||||
pos_pips = self.position.calculate_pips()
|
||||
self.assertEqual(pos_pips, Decimal("-0.00049"))
|
||||
self.assertEqual(pos_pips, Decimal("-0.00021"))
|
||||
|
||||
def test_calculate_profit_base(self):
|
||||
def test_calculate_init_profit_base(self):
|
||||
profit_base = self.position.calculate_profit_base(self.position.exposure)
|
||||
self.assertEqual(profit_base, Decimal("-0.64571"))
|
||||
self.assertEqual(profit_base, Decimal("-0.27939"))
|
||||
|
||||
def test_calculate_profit_perc(self):
|
||||
def test_calculate_init_profit_perc(self):
|
||||
profit_perc = self.position.calculate_profit_perc(self.position.exposure)
|
||||
self.assertEqual(profit_perc, Decimal("-0.03229"))
|
||||
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"))
|
||||
|
||||
|
||||
class TestShortGBPUSDPosition(unittest.TestCase):
|
||||
def setUp(self):
|
||||
getcontext.prec = 2
|
||||
side = "SHORT"
|
||||
position_type = "short"
|
||||
market = "GBP/USD"
|
||||
units = 2000
|
||||
units = Decimal("2000")
|
||||
exposure = Decimal("2000.00")
|
||||
avg_price = Decimal("1.51819")
|
||||
cur_price = Decimal("1.51770")
|
||||
bid = Decimal("1.50328")
|
||||
ask = Decimal("1.50349")
|
||||
self.position = Position(
|
||||
side, market, units, exposure,
|
||||
avg_price, cur_price
|
||||
position_type, market,
|
||||
units, exposure, bid, ask
|
||||
)
|
||||
|
||||
def test_calculate_pips(self):
|
||||
def test_calculate_init_pips(self):
|
||||
pos_pips = self.position.calculate_pips()
|
||||
self.assertEqual(pos_pips, Decimal("0.00049"))
|
||||
self.assertEqual(pos_pips, Decimal("-0.00021"))
|
||||
|
||||
def test_calculate_profit_base(self):
|
||||
def test_calculate_init_profit_base(self):
|
||||
profit_base = self.position.calculate_profit_base(self.position.exposure)
|
||||
self.assertEqual(profit_base, Decimal("0.64571"))
|
||||
self.assertEqual(profit_base, Decimal("-0.27935"))
|
||||
|
||||
def test_calculate_profit_perc(self):
|
||||
def test_calculate_init_profit_perc(self):
|
||||
profit_perc = self.position.calculate_profit_perc(self.position.exposure)
|
||||
self.assertEqual(profit_perc, Decimal("0.03229"))
|
||||
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.00258"))
|
||||
# Check profit base
|
||||
profit_base = self.position.calculate_profit_base(self.position.exposure)
|
||||
self.assertEqual(profit_base, Decimal("-3.42661"))
|
||||
# Check profit percentage
|
||||
profit_perc = self.position.calculate_profit_perc(self.position.exposure)
|
||||
self.assertEqual(profit_perc, Decimal("-0.17133"))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user