-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from dev-abuke/task-backtesting
Task backtesting
- Loading branch information
Showing
5 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
timestamp,open,high,low,close,volume,close_time,quote_asset_volume,number_of_trades,taker_buy_base_asset_volume,taker_buy_quote_asset_volume,ignore | ||
2021-06-01,37253.82000000,37894.81000000,35666.00000000,36693.09000000,81234.66377000,1622591999999,2973928300.37558711,1760417,40291.83485100,1475008291.23364731,0 | ||
2021-06-02,36694.85000000,38225.00000000,35920.00000000,37568.68000000,67587.37249500,1622678399999,2523462575.68843765,1530915,34328.49628700,1281854474.91208278,0 | ||
2021-06-03,37568.68000000,39476.00000000,37170.00000000,39246.79000000,75889.10601100,1622764799999,2930945325.70920706,1949658,38664.29784900,1493504667.61380185,0 | ||
2021-06-04,39246.78000000,39289.07000000,35555.15000000,36829.00000000,91317.79924500,1622851199999,3379399754.93052259,1926311,43105.97173000,1595098152.96682665,0 | ||
2021-06-05,36829.15000000,37925.00000000,34800.00000000,35513.20000000,70459.62149000,1622937599999,2560547116.83640575,1463754,34397.77436000,1249708171.27239164,0 | ||
2021-06-06,35516.07000000,36480.00000000,35222.00000000,35796.31000000,47650.20663700,1623023999999,1711857075.04716966,993430,23661.26063600,850204250.76255567,0 | ||
2021-06-07,35796.31000000,36900.00000000,33300.00000000,33552.79000000,77574.95257300,1623110399999,2755673601.71391388,1532260,36431.56058600,1295757611.70864594,0 | ||
2021-06-08,33556.96000000,34068.01000000,31000.00000000,33380.81000000,123251.18903700,1623196799999,4030784324.60968405,2074885,60095.99020100,1966354847.56919820,0 | ||
2021-06-09,33380.80000000,37534.79000000,32396.82000000,37388.05000000,136607.59751700,1623283199999,4794741674.43882304,2601479,68790.28926500,2415789675.08593024,0 | ||
2021-06-10,37388.05000000,38491.00000000,35782.00000000,36675.72000000,109527.28494300,1623369599999,4062957162.36429993,2231430,53639.42402200,1990486079.21771466,0 | ||
2021-06-11,36677.83000000,37680.40000000,35936.77000000,37331.98000000,78466.00530000,1623455999999,2903043643.31007191,1727645,39406.71922400,1458619433.27684363,0 | ||
2021-06-12,37331.98000000,37463.63000000,34600.36000000,35546.11000000,87717.54999000,1623542399999,3132387326.53751386,1719650,42571.20531300,1520432734.29990903,0 | ||
2021-06-13,35546.12000000,39380.00000000,34757.00000000,39020.57000000,86921.02555500,1623628799999,3208991521.63768005,1894103,45675.27361600,1688611358.41170236,0 | ||
2021-06-14,39020.56000000,41064.05000000,38730.00000000,40516.29000000,108522.39194900,1623715199999,4320307646.76538952,2185089,53645.90874400,2135285912.62050537,0 | ||
2021-06-15,40516.28000000,41330.00000000,39506.40000000,40144.04000000,80679.62283800,1623801599999,3247746713.62740970,1931890,39428.82972600,1587381565.81541288,0 | ||
2021-06-16,40143.80000000,40527.14000000,38116.01000000,38349.01000000,87771.97693700,1623887999999,3450556041.60058353,2134897,42137.24225100,1657135674.82302152,0 | ||
2021-06-17,38349.00000000,39559.88000000,37365.00000000,38092.97000000,79541.30711900,1623974399999,3065622530.03988603,1620210,37936.81267300,1462729677.11582557,0 | ||
2021-06-18,38092.97000000,38202.84000000,35129.29000000,35819.84000000,95228.04293500,1624060799999,3507868126.30620260,1809253,44824.43656700,1651613641.38681674,0 | ||
2021-06-19,35820.48000000,36457.00000000,34803.52000000,35483.72000000,68712.44946100,1624147199999,2453817534.77716944,1212278,33788.29334500,1207082481.76471773,0 | ||
2021-06-20,35483.72000000,36137.72000000,33336.00000000,35600.16000000,89878.17085000,1624233599999,3124634336.84076064,1691871,44194.06738700,1537385055.49291186,0 | ||
2021-06-21,35600.17000000,35750.00000000,31251.23000000,31608.93000000,168778.87315900,1624319999999,5545926077.78806706,2688993,81297.46975100,2671033140.18897872,0 | ||
2021-06-22,31614.12000000,33298.78000000,28805.00000000,32509.56000000,204208.17976200,1624406399999,6406159197.07678900,3113977,99149.18226600,3115465362.96708140,0 | ||
2021-06-23,32509.56000000,34881.00000000,31683.00000000,33678.07000000,126966.10056300,1624492799999,4282250433.91944518,2026228,63340.03313700,2136458765.58680401,0 | ||
2021-06-24,33675.07000000,35298.00000000,32286.57000000,34663.09000000,86625.80426000,1624579199999,2925373222.10597716,1531230,43994.70829800,1486960778.32677295,0 | ||
2021-06-25,34663.08000000,35500.00000000,31275.00000000,31584.45000000,116061.13035600,1624665599999,3846120542.65729680,2094932,55656.15625200,1846078278.30478276,0 | ||
2021-06-26,31576.09000000,32730.00000000,30151.00000000,32283.65000000,107820.37528700,1624751999999,3376415451.79312412,1814051,52978.15925600,1659918820.24047804,0 | ||
2021-06-27,32283.65000000,34749.00000000,31973.45000000,34700.34000000,96613.24421100,1624838399999,3199980157.75033328,1799875,48772.52918000,1616348030.48262429,0 | ||
2021-06-28,34702.49000000,35297.71000000,33862.72000000,34494.89000000,82222.26781900,1624924799999,2837122779.52440164,1868627,40552.29218000,1399861431.16463476,0 | ||
2021-06-29,34494.89000000,36600.00000000,34225.43000000,35911.73000000,90788.79622000,1625011199999,3233947791.86338857,1673802,45376.58762900,1616093004.85216473,0 | ||
2021-06-30,35911.72000000,36100.00000000,34017.55000000,35045.00000000,77152.19763400,1625097599999,2688939291.09567313,1301036,37035.76967400,1290873346.22416245,0 | ||
2021-07-01,35045.00000000,35057.57000000,32711.00000000,33504.69000000,71708.26611200,1625183999999,2410809595.39045694,1240681,34986.05501600,1176002216.56568103,0 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Date,Open,High,Low,Close,Adj Close,Volume |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import backtrader as bt | ||
import pandas as pd | ||
import os | ||
import sys | ||
|
||
class TestStrategy(bt.Strategy): | ||
def __init__(self): | ||
self.sma = bt.indicators.SimpleMovingAverage(self.data.close, period=15) | ||
|
||
def next(self): | ||
if self.sma > self.data.close: | ||
self.buy() | ||
elif self.sma < self.data.close: | ||
self.sell() | ||
|
||
if __name__ == '__main__': | ||
print(os.getcwd()) | ||
cerebro = bt.Cerebro() | ||
df = pd.read_csv('data/binance_btc_usdt_candlestick.csv', index_col='timestamp', parse_dates=True) | ||
data = bt.feeds.PandasData(dataname=df) | ||
cerebro.adddata(data) | ||
cerebro.addstrategy(TestStrategy) | ||
cerebro.run() | ||
cerebro.plot() |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import requests | ||
import pandas as pd | ||
import os | ||
|
||
def fetch_candlestick_data(symbol, interval, start, end, api_key): | ||
url = f"https://api.binance.com/api/v3/klines?symbol={symbol}&interval={interval}&startTime={start}&endTime={end}" | ||
headers = {'X-MBX-APIKEY': api_key} | ||
response = requests.get(url, headers=headers) | ||
data = response.json() | ||
df = pd.DataFrame(data, columns=[ | ||
'timestamp', 'open', 'high', 'low', 'close', 'volume', 'close_time', | ||
'quote_asset_volume', 'number_of_trades', 'taker_buy_base_asset_volume', | ||
'taker_buy_quote_asset_volume', 'ignore']) | ||
df['timestamp'] = pd.to_datetime(df['timestamp'], unit='ms') | ||
df.set_index('timestamp', inplace=True) | ||
return df | ||
|
||
if __name__ == '__main__': | ||
API_KEY = os.getenv('BINANCE_API_KEY') | ||
symbol = 'BTCUSDT' | ||
interval = '1d' | ||
start = 1622505600000 # Start timestamp in milliseconds | ||
end = 1625097600000 # End timestamp in milliseconds | ||
df = fetch_candlestick_data(symbol, interval, start, end, API_KEY) | ||
df.to_csv(f'btc_usdt_candlestick_{start}_to_{end}.csv') | ||
print(df.head()) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import yfinance as yf | ||
import pandas as pd | ||
|
||
def download_data(ticker, start_date, end_date): | ||
data = yf.download(ticker, start=start_date, end=end_date) | ||
data.to_csv(f"data/{ticker}.csv") | ||
|
||
if __name__ == "__main__": | ||
start_date = "2020-01-01" | ||
end_date = "2021-01-01" | ||
download_data(f"yfinance_btc_usdt_{start_date}_to_{end_date}", start_date=start_date, end_date=end_date) |