十五、区块量化 双均线策略

发布时间 2023-06-18 15:08:16作者: 金记缘

新增cross_dualma_order.py

# -*- coding: utf-8 -*-
import cross_order as order
import time

SHORT_WIN = 50 # 短周期窗口
LONG_WIN = 200 # 长周期窗口


def main():
print("任务开始时间:", time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))

for symbol in order.symbol_pool:
# 设置杠杆倍数
order.set_leverage(symbol=symbol, leverage='25')
# 获取标的的最新价
current_price = order.get_candlesticks(symbol=symbol, interval='15m', limit=str(LONG_WIN + 1))
close = current_price['close']
# 计算双均线
short_avgs = close.rolling(window=SHORT_WIN).mean()
long_avgs = close.rolling(window=LONG_WIN).mean()
# 双均线策略
# 短期均线上穿长期均线,做多
if short_avgs.iloc[-2] < long_avgs.iloc[-2] and short_avgs.iloc[-1] >= long_avgs.iloc[-1]:
order.up_cross_order(symbol, '短期均线上穿长期均线,做多')
# 短期均线下穿长期均线,做空
elif short_avgs.iloc[-2] > long_avgs.iloc[-2] and short_avgs.iloc[-1] <= long_avgs.iloc[-1]:
order.down_cross_order(symbol, '短期均线下穿长期均线,做空')
time.sleep(5)

print("任务结束时间:", time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))


if __name__ == '__main__':
print("程序运行时间:", time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
main()