Is the backtesting shorting trades? #593
Unanswered
brianferrell787
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Expected Behavior
Hello I was wondering why the backtesting code shorts a trade before I even buy it, When I look at stats._trades it should look like this:
Size EntryBar ... ExitTime Duration
0 28 21 ... 2021-06-04 12:00:00 1 days 22:00:00
1 -28 28 ... 2021-06-16 16:00:00 12 days 04:00:00
3 46 95 ... 2021-07-02 12:00:00 3 days 18:00:00
4 -46 108 ... 2021-07-08 12:00:00 6 days 00:00:00
Where every order has a following selling order.
Actual Behavior
This is what it actually does:
Size EntryBar ... ExitTime Duration
0 28 21 ... 2021-06-04 12:00:00 1 days 22:00:00
1 -28 28 ... 2021-06-16 16:00:00 12 days 04:00:00
2 -37 62 ... 2021-06-28 18:00:00 12 days 02:00:00
3 46 95 ... 2021-07-02 12:00:00 3 days 18:00:00
4 -46 108 ... 2021-07-08 12:00:00 6 days 00:00:00
5 -46 120 ... 2021-07-19 12:00:00 11 days 00:00:00
6 -67 148 ... 2021-07-22 14:00:00 3 days 02:00:00
Where it sells before it even buys. I have tried every combination of using self.sell(), self.buy(), and self.position.close and I can get it to where it looks like this:
stats._trades
Size EntryBar ... ExitTime Duration
0 28 21 ... 2021-06-04 12:00:00 1 days 22:00:00
1 36 95 ... 2021-07-02 12:00:00 3 days 18:00:00
2 40 161 ... 2021-07-30 12:00:00 7 days 22:00:00
3 28 245 ... 2021-08-24 18:00:00 4 days 04:00:00
4 27 265 ... 2021-09-07 14:00:00 11 days 00:00:00
5 27 314 ... 2021-09-20 12:00:00 4 days 20:00:00
6 26 361 ... 2021-10-08 12:00:00 6 days 22:00:00
But it does not show that it sells obviously. Code to get this looks like this:
class strat_2hr_macd(Strategy):
'''this strategy does the ema crossover on 2hr chart with MACD'''
n1 = 9
n2 = 20
Additional info
main code:
class strat_2hr_macd(Strategy):
'''this strategy does the ema crossover on 2hr chart with MACD'''
n1 = 9
n2 = 20
from backtesting import Backtest
bt = Backtest(prices, strat_2hr_macd, cash=800, commission=0.0, trade_on_close=True,exclusive_orders=True)
stats = bt.run()
print(stats)
Beta Was this translation helpful? Give feedback.
All reactions