Skip to content

Commit

Permalink
additional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
robswc committed Mar 21, 2023
1 parent 65581e0 commit 5665b04
Showing 1 changed file with 67 additions and 1 deletion.
68 changes: 67 additions & 1 deletion app/tests/test_backtest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from components.backtest.backtest import Backtest
from components.ohlc import CSVAdapter
from components.orders.position import Position
from components.orders.order import Order
from components.positions.position import Position
from storage.strategies.examples.sma_cross_over import SMACrossOver

OHLC = CSVAdapter().get_data('tests/data/AAPL.csv', 'AAPL')
Expand Down Expand Up @@ -84,3 +85,68 @@ def test_stop_loss(self):
assert p.size == 0
assert p.pnl == -5

def test_overview_long_orders(self):
strategy = SMACrossOver(data=OHLC)
strategy.orders.orders = []
strategy.data.reset_index()
strategy.data.advance_index(100)

# create positions
for i in range(3):
strategy.data.advance_index(5)
strategy.positions.open(order_type='market', side='buy', quantity=1)
strategy.data.advance_index(5)
strategy.positions.close()

# create backtest
backtest = Backtest(strategy=strategy, data=OHLC)
backtest.test()

# check overview
assert backtest.result.pnl == -5.170000000000016

def test_overview_short_orders(self):
# now do the same with short orders
strategy = SMACrossOver(data=OHLC)
strategy.orders.orders = []
strategy.data.reset_index()
strategy.data.advance_index(100)

# create positions
for i in range(3):
strategy.data.advance_index(5)
strategy.positions.open(order_type='market', side='sell', quantity=1)
strategy.data.advance_index(5)
strategy.positions.close()

# create backtest
backtest = Backtest(strategy=strategy, data=OHLC)
backtest.test()

# check overview
assert backtest.result.pnl == 5.170000000000016

def test_complex_positions(self):
b = Backtest(strategy=SMACrossOver(), data=OHLC)

p1 = Position(
orders=[
Order(side='buy', symbol='AAPL', qty=1, order_type='market', filled_avg_price=100, timestamp=1),
Order(side='sell', symbol='AAPL', qty=1, order_type='limit', filled_avg_price=150, timestamp=2),
]
)

p2 = Position(
orders=[
Order(side='sell', symbol='AAPL', qty=1, order_type='market', filled_avg_price=100, timestamp=1),
Order(side='buy', symbol='AAPL', qty=1, order_type='stop', filled_avg_price=90, timestamp=2),
]
)

b.strategy.positions.add(p1)
b.strategy.positions.add(p2)
b.test()

assert b.result.pnl == 60


0 comments on commit 5665b04

Please sign in to comment.