-
Notifications
You must be signed in to change notification settings - Fork 2
/
ul_8c_backtestgdy.py
41 lines (30 loc) · 996 Bytes
/
ul_8c_backtestgdy.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
36
37
38
39
40
41
import pandas as pd
from backtesting import Strategy, Backtest
from backtesting.lib import crossover, cross
from backtesting.test import SMA
actualpath = 'data/FCOJ/gdy00_backtest.csv'
class SmaCross19(Strategy):
n1 = 20
n2 = 40
def init(self):
self.sma1 = self.I(SMA, self.data.Close, self.n1)
self.sma2 = self.I(SMA, self.data.Close, self.n2)
def next(self):
# if sma1 crosses above sma2, buy the asset
if crossover(self.sma1, self.sma2):
self.buy()
# else if sma1 crosses below sma2, sell it
elif crossover(self.sma2, self.sma1):
self.sell()
# local variables
cash = 1000000
commission = 0.0102
data = pd.read_csv(actualpath, index_col=0)
data.index = pd.to_datetime(data.index)
bt = Backtest(data, SmaCross19, cash=cash, commission=commission, trade_on_close=True)
results = bt.run()
print(results)
df= results._trade_data
print(df.head())
df.to_csv('out/gdy_trading_results.csv')
# bt.plot()