-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy path_iteration.py
35 lines (29 loc) · 875 Bytes
/
_iteration.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from backtesting import Backtest
from backtesting import Strategy
from backtesting.test import GOOG, SMA
from backtesting.lib import crossover
import types
import random
random.seed(0)
class TestStrategy(Strategy):
def init(self):
print("Init", self.equity)
def next(self, action=None):
# uncomment if you want to test run()
# if not action:
# action = random.randint(0, 1)
if action!=None:
if action == 0:
self.buy()
elif action == 1:
self.position.close()
bt = Backtest(GOOG, TestStrategy, cash=10_000, commission=.002)
# stats = bt.run()
bt.initialize()
while True:
action = random.randint(0, 1)
stats = bt.next(action=action)
if not isinstance(stats, types.NoneType):
break
print(stats)
bt.plot(results=stats, open_browser=True)