有人对数字货币很感兴趣获取1分钟k线,执行简单的双均线策略回测

资讯 9个月前 manoon
0

有人对数字货币很感兴趣。以下 backtrader 代码从 Binance Exchange 在线 api 获取 1 分钟柱,并执行简单的双平均线策略回测。我测试过,可以直接运行。 一起来体验数字货币的回测吧。

注意API每次有1000k行的限制,所以程序会分批获取。读取的数据放入dataframe,然后发送给backtrader。

另外要注意数据类型的设置,尤其是日期时间索引字段的设置币安数字货币,数据获取后币安数字货币,只有设置正确才能被backtrader接受。

。代码来自这里

# testBinance.py
# 从Binance币安在线api下载1分钟k线,进行回测
import requests 
import backtrader as bt
import backtrader.analyzers as btanalyzers
import json 
import pandas as pd
import datetime as dt
import matplotlib.pyplot as plt
def get_binance_bars(symbol, interval, startTime, endTime):
 
    url = "https://api.binance.com/api/v3/klines"
 
    startTime = str(int(startTime.timestamp() * 1000))
    endTime = str(int(endTime.timestamp() * 1000))
    limit = '1000'
 
    req_params = {"symbol" : symbol, 'interval' : interval, 'startTime' : startTime, 'endTime' : endTime, 'limit' : limit}
 
    df = pd.DataFrame(json.loads(requests.get(url, params = req_params).text))
 
    if (len(df.index) == 0):
        return None
     
    df = df.iloc[:, 0:6]
    df.columns = ['datetime', 'open', 'high', 'low', 'close', 'volume']
 
    df.open      = df.open.astype("float")
    df.high      = df.high.astype("float")
    df.low       = df.low.astype("float")
    df.close     = df.close.astype("float")
    df.volume    = df.volume.astype("float")
    
    df['adj_close'] = df['close']
     
    df.index = [dt.datetime.fromtimestamp(x / 1000.0) for x in df.datetime]
 
    return df
df_list = []
# 数据起点时间
last_datetime = dt.datetime(2020, 11, 23)
while True:
    new_df = get_binance_bars('ETHUSDT', '1m', last_datetime, dt.datetime.now()) # 获取1分钟k线数据
    
    if new_df is None:
        break
    df_list.append(new_df)
    last_datetime = max(new_df.index) + dt.timedelta(0, 1)
 
df = pd.concat(df_list)
df.shape
class MaCrossStrategy(bt.Strategy):
 
    def __init__(self):
        ma_fast = bt.ind.SMA(period = 10)
        ma_slow = bt.ind.SMA(period = 50)
         
        self.crossover = bt.ind.CrossOver(ma_fast, ma_slow)
 
    def next(self):
        if not self.position:
            if self.crossover > 0: 
                self.buy()
        elif self.crossover < 0: 
            self.close()
cerebro = bt.Cerebro()
print('k线数量', len(df)) 
data = bt.feeds.PandasData(dataname = df)
cerebro.adddata(data)
 
cerebro.addstrategy(MaCrossStrategy)
cerebro.broker.setcash(1000000.0)
 
cerebro.addsizer(bt.sizers.PercentSizer, percents = 50)
cerebro.addanalyzer(btanalyzers.SharpeRatio, timeframe=bt.TimeFrame.Minutes, _name = "sharpe")
cerebro.addanalyzer(btanalyzers.Transactions, _name = "trans")
back = cerebro.run()
print('最终市值', cerebro.broker.getvalue()) # Ending balance
print(back[0].analyzers.sharpe.get_analysis()) # Sharpe
print(len(back[0].analyzers.trans.get_analysis())) # Number of Trades

13 小时前编辑

暂无评论

暂无评论...