Description
As I mentioned in #47 , I've been beginning by conducting some 'purity tests' on that pull request.
Note that I have disabled FIFO by commenting out the lines I referred to earlier on in the comments on that PR.
Expected Behavior
It may be my expectations are flawed, but that is part of this test 😜
Generally speaking my expectations are that with a set of 20 orders already placed (in the init()
function) and one oscillation of the market, all orders should result in positions being taken and take profit ... ending with a neat little bow on top.
I'm also expecting that the numbers that result from print(output)
somewhat reveal the overly simple and pure scenario that preceded it - with a lot of figures that may have even numbers and numbers with relatively clear relationships to one another.
The code:
from backtesting import Backtest, Strategy
from backtesting.lib import crossover
from backtesting.test import SMA, PureSimpleData
from backtesting.lib import crossover
from pprint import pformat
class PureSimple(Strategy):
def init(self):
tradeCount=0
for price in range(1,12,1):
if price < 11:
self.buy(size=1,limit=float(price),tp=price+1.0)
print(f" BUY@{price}, tp@{price+1}")
tradeCount+=1
if price > 1:
self.sell(size=1,limit=float(price),tp=price-1.0)
print(f"SELL@{price}, tp@{price-1}")
tradeCount+=1
print(f"{tradeCount} trades placed.")
def next(self):
pass
bt = Backtest(PureSimpleData, PureSimple, cash=10000, commission=.002)
output = bt.run()
bt.plot()
print(output)
That should place the following trade orders before the first OHLC is processed:
- BUY@1, tp@2
- BUY@2, tp@3
- SELL@2, tp@1
- BUY@3, tp@4
- SELL@3, tp@2
- BUY@4, tp@5
- SELL@4, tp@3
- BUY@5, tp@6
- SELL@5, tp@4
- BUY@6, tp@7
- SELL@6, tp@5
- BUY@7, tp@8
- SELL@7, tp@6
- BUY@8, tp@9
- SELL@8, tp@7
- BUY@9, tp@10
- SELL@9, tp@8
- BUY@10, tp@11
- SELL@10, tp@9
- SELL@11, tp@10
The data being used for market movements is:
,Open,High,Low,Close
1/01/2020 12:01,1.25,1.99,0.99,1.75
1/01/2020 12:02,2.25,2.99,1.99,2.75
1/01/2020 12:03,3.25,3.99,2.99,3.75
1/01/2020 12:04,4.25,4.99,3.99,4.75
1/01/2020 12:05,5.25,5.99,4.99,5.75
1/01/2020 12:06,6.25,6.99,5.99,6.75
1/01/2020 12:07,7.25,7.99,6.99,7.75
1/01/2020 12:08,8.25,8.99,7.99,8.75
1/01/2020 12:09,9.25,9.99,8.99,9.75
1/01/2020 12:10,10.25,10.99,9.99,10.75
1/01/2020 12:11,9.25,9.99,8.99,9.75
1/01/2020 12:12,8.25,8.99,7.99,8.75
1/01/2020 12:13,7.25,7.99,6.99,7.75
1/01/2020 12:14,6.25,6.99,5.99,6.75
1/01/2020 12:15,5.25,5.99,4.99,5.75
1/01/2020 12:16,4.25,4.99,3.99,4.75
1/01/2020 12:17,3.25,3.99,2.99,3.75
1/01/2020 12:18,2.25,2.99,1.99,2.75
1/01/2020 12:19,1.25,1.99,0.99,1.75
Actual Behavior
- It is not clear to me why a position is not taken from the very first candle - I'm assuming that the dotted lines are deals closed?
- It is not entirely clear to me why only 16 trades are shown. I expected at least 18 and as you can see from my code and the trade request list it generates, I was aiming for 20 orders placed and 20 deals closed. Again, I'd not be surprised if I've missed something here.
- The numbers printed at the end aren't as clear cut as I'd have expected ... but again, perhaps I've expected too much simplicity, perhaps I've indeed picked up on something or perhaps the results could do with some more fundamentals in there too (potentially more absolute values rather than %ages).
Zip file of the chart produced:
PureSimple.zip
This is the output that is printed:
Start 2020-01-01 12:01:00
End 2020-01-01 12:19:00
Duration 0 days 00:18:00
Exposure Time [%] 89.4737
Equity Final [$] 10046.1
Equity Peak [$] 10046.1
Return [%] 0.418744
Buy & Hold Return [%] 0
Max. Drawdown [%] -0.160377
Avg. Drawdown [%] -0.160377
Max. Drawdown Duration 0 days 00:06:00
Avg. Drawdown Duration 0 days 00:06:00
# Trades 16
Win Rate [%] 100
Best Trade [%] 354.646
Worst Trade [%] 12.0192
Avg. Trade [%] 111.359
Max. Trade Duration 0 days 00:15:00
Avg. Trade Duration 0 days 00:07:00
Expectancy [%] NaN
SQN 4.88235
Sharpe Ratio 0.944904
Sortino Ratio NaN
Calmar Ratio 694.362
_strategy PureSimple
_equity_curve ...
_trades Size EntryB...
dtype: object
Steps to Reproduce
- Run the code
- With the data
- Have the same possibly flawed expectations! 🌝
Additional info
- Backtesting version: Order/Trade/Position API #47 with lines 824 to 842 commented out
- More tests to come soon hopefully!