-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Multiple entry problems in using backtesting.py #1133
Unanswered
coding-work-man
asked this question in
Q&A
-
I am just trying to write a very simple multiple entry strategy, the logic is as bellow:
- strategy only long, no short sell
- close cross up sma 5, then buy 1000 shares
- close cross up sma 60 ,buy another 1000 shares
- close cross down sma 5 , close 1000 shares
- close cross down sma 60, close 1000 shares
The sample code is like bellow
`class SmaCross(Strategy):
n1 = 5
n2 = 60
def init(self):
close = self.data.Close
self.sma1 = self.I(SMA, close, self.n1)
self.sma2 = self.I(SMA, close, self.n2)
self.entry_times = 0
def next(self):
if crossover(self.data.Close, self.sma1):
shares_to_buy = int(100000 // self.data.Close[-1])
shares_to_buy = (shares_to_buy // 100) * 100
if self.entry_times < 2:
self.buy(size=shares_to_buy)
self.entry_times += 1
if crossover(self.data.Close, self.sma2):
shares_to_buy = int(100000 // self.data.Close[-1])
shares_to_buy = (shares_to_buy // 100) * 100
if self.entry_times < 2:
self.buy(size=shares_to_buy)
self.entry_times += 1
if crossover(self.sma2, self.data.Close):
self.position.close()
self.entry_times = 0
elif crossover(self.sma1, self.data.Close):
if self.entry_times == 2:
self.position.close(portion=0.5)
self.entry_times -= 1
else:
self.position.close()
self.entry_times = 0`
however if I look at the trade log, on 4-20, there are multiple entries, what's wrong with my code? There should be at most 2 entries . I have checked the stock data, nothing wrong and it's correct.
| Size | EntryBar | ExitBar | EntryPrice | ExitPrice | PnL | ReturnPct | EntryTime | ExitTime | Duration -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- 4500.0 | 69 | 73 | 22.199286 | 22.635166 | 1961.461247 | 0.019635 | 2021年03月29日 15:00:00 | 2021年04月02日 15:00:00 | 4 days 2050.0 | 86 | 90 | 24.191797 | 24.293687 | 208.875693 | 0.004212 | 2021年04月22日 15:00:00 | 2021年04月28日 15:00:00 | 6 days 2250.0 | 84 | 90 | 21.990646 | 24.293687 | 5181.843478 | 0.104728 | 2021年04月20日 15:00:00 | 2021年04月28日 15:00:00 | 8 days 2050.0 | 91 | 97 | 24.348277 | 24.293687 | -111.908156 | -0.002242 | 2021年04月29日 15:00:00 | 2021年05月12日 15:00:00 | 13 days 1025.0 | 86 | 97 | 24.191797 | 24.293687 | 104.437847 | 0.004212 | 2021年04月22日 15:00:00 | 2021年05月12日 15:00:00 | 20 days 1125.0 | 84 | 97 | 21.990646 | 24.293687 | 2590.921739 | 0.104728 | 2021年04月20日 15:00:00 | 2021年05月12日 15:00:00 | 22 days 2000.0 | 102 | 105 | 24.413534 | 24.062656 | -701.756402 | -0.014372 | 2021年05月19日 15:00:00 | 2021年05月24日 15:00:00 | 5 days 1025.0 | 91 | 105 | 24.348277 | 24.062656 | -292.761422 | -0.011731 | 2021年04月29日 15:00:00 | 2021年05月24日 15:00:00 | 25 days 512.0 | 86 | 105 | 24.191797 | 24.062656 | -66.120178 | -0.005338 | 2021年04月22日 15:00:00 | 2021年05月24日 15:00:00 | 32 days 562.0 | 84 | 105 | 21.990646 | 24.062656 | 1164.469614 | 0.094222 | 2021年04月20日 15:00:00 | 2021年05月24日 15:00:00 | 34 daysBeta Was this translation helpful? Give feedback.
All reactions
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment