-
-
Save dalinaum/43258cb15378f457858892913883d94a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import FinanceDataReader as fdr | |
import os.path | |
import pandas as pd | |
data_exists = os.path.exists('data.csv') | |
if data_exists: | |
print("데이터를 이미 받아왔습니다.") | |
data = pd.read_csv('data.csv', parse_dates=['Date'], index_col=['Date']) | |
else: | |
print("데이터를 새로 가져옵시다.") | |
data = fdr.DataReader('KS11', '2015') | |
data.to_csv('data.csv') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bt.plot() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from backtesting import Strategy | |
from backtesting.test import SMA | |
from backtesting.lib import crossover | |
class SmaCross(Strategy): | |
n1 = 5 | |
n2 = 20 | |
def init(self): | |
Close = self.data.Close | |
self.ma5 = self.I(SMA, Close, self.n1) | |
self.ma20 = self.I(SMA, Close, self.n2) | |
def next(self): | |
if crossover(self.ma5, self.ma20): | |
self.buy() | |
elif crossover(self.ma20, self.ma5): | |
self.position.close() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data = data.dropna() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from backtesting import Backtest | |
bt = Backtest(data, SmaCross, cash=10000, commission=.002, exclusive_orders=True, trade_on_close=True) | |
bt.run() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bt.plot() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
stats = bt.optimize(n1=range(5, 120, 5), | |
n2=range(10, 250, 5), | |
maximize='Equity Final [$]', | |
constraint=lambda param: param.n1 < param.n2) | |
stats |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
stats._strategy |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
stats['_equity_curve'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
stats['_trades'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment