请 [注册] 或 [登录]  | 返回主站

量化交易吧 /  量化平台 帖子:3365793 新帖:0

【策略研发】期货双均线策略

爱汇小王子发表于:5 月 9 日 23:16回复(1)

写了一个简单的期货双均线策略,如有错误地方,请指正(^__^)

import datetime
import matplotlib.pyplot as plt
# 获取主力合约代码
future_code = get_dominant_future('RB')
# 当前日期和上一个日期
dt = datetime.datetime.now().date()
last_day = dt - datetime.timedelta(days=1)
start_day = dt - datetime.timedelta(days=365*2)
ma5_list = []
ma60_list = []
day_list = []
count = 0
for i in range(365*2):
    count += 1
    today = start_day + datetime.timedelta(days=i)
    # 获取期货对应的历史价格
    price = get_price(security=future_code, 
              end_date=today, 
              frequency='daily', 
              fields=['close'], 
              skip_paused=False, 
              fq='pre', 
              count=100)
    # 求出上一个交易日的m5均线值与ma60值
    last_ma5 = price.iloc[-5:]['close'].mean()
    last_ma60 = price.iloc[-60:]['close'].mean()
    ma5_list.append(last_ma5)
    ma60_list.append(last_ma60)
    day_list.append(count)
    # 求出三个交易日之前的ma5与ma60值
#     old_ma5 = price.iloc[-5-2:-2]['close'].mean()
#     old_ma60 = price.iloc[-60-2:-2]['close'].mean()
plt.plot(day_list,ma5_list,'r')
plt.plot(day_list,ma60_list,'g')
plt.legend(['ma5', 'ma60'], loc=0)
plt.show()
 

全部回复

0/140

量化课程

    移动端课程