五、区块量化 boll轨道策略
1、新增cross_boll_order.py
# -*- coding: utf-8 -*-
import talib
import cross_order as order
import time
BOLL_N = 20 # BBands参数N
BOLL_M = 2 # BBands参数M
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')
# 获取标的的最新价
data = order.get_candlesticks(symbol=symbol, interval='15m', limit=str(BOLL_N + 1))
close = data['close'].values
# 计算BBands
uppers, middles, lowers = talib.BBANDS(close, timeperiod=BOLL_N, nbdevdn=BOLL_N, nbdevup=BOLL_M)
# BBands策略
# K线上穿BOLL下轨,以市价做多
if (close[-2] < lowers[-2]) and (close[-1] >= lowers[-1]):
order.up_cross_order(symbol=symbol,ordtype='market', message='K线上穿BOLL下轨,以市价做多')
# K线下穿BOLL上轨,以市价做空
elif (close[-2] > uppers[-2]) and (close[-1] <= uppers[-1]):
order.down_cross_order(symbol=symbol,ordtype='market', message='K线下穿BOLL上轨,以市价做空')
# K线下穿BOLL中轨,以市价平多单
elif (close[-2] > middles[-2]) and (close[-1] <= middles[-1]):
order.close_positions(symbol=symbol, side='sell', positionside='long', message='K线下穿BOLL中轨,以市价平多单')
# K线上穿BOLL中轨,以市价平空单
elif (close[-2] < middles[-2]) and (close[-1] >= middles[-1]):
order.close_short_positions(symbol=symbol, side='buy', positionside='short', message='K线上穿BOLL中轨,以市价平空单')
time.sleep(5)
print("任务结束时间:", time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
if __name__ == '__main__':
main()
import talib
import cross_order as order
import time
BOLL_N = 20 # BBands参数N
BOLL_M = 2 # BBands参数M
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')
# 获取标的的最新价
data = order.get_candlesticks(symbol=symbol, interval='15m', limit=str(BOLL_N + 1))
close = data['close'].values
# 计算BBands
uppers, middles, lowers = talib.BBANDS(close, timeperiod=BOLL_N, nbdevdn=BOLL_N, nbdevup=BOLL_M)
# BBands策略
# K线上穿BOLL下轨,以市价做多
if (close[-2] < lowers[-2]) and (close[-1] >= lowers[-1]):
order.up_cross_order(symbol=symbol,ordtype='market', message='K线上穿BOLL下轨,以市价做多')
# K线下穿BOLL上轨,以市价做空
elif (close[-2] > uppers[-2]) and (close[-1] <= uppers[-1]):
order.down_cross_order(symbol=symbol,ordtype='market', message='K线下穿BOLL上轨,以市价做空')
# K线下穿BOLL中轨,以市价平多单
elif (close[-2] > middles[-2]) and (close[-1] <= middles[-1]):
order.close_positions(symbol=symbol, side='sell', positionside='long', message='K线下穿BOLL中轨,以市价平多单')
# K线上穿BOLL中轨,以市价平空单
elif (close[-2] < middles[-2]) and (close[-1] >= middles[-1]):
order.close_short_positions(symbol=symbol, side='buy', positionside='short', message='K线上穿BOLL中轨,以市价平空单')
time.sleep(5)
print("任务结束时间:", time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
if __name__ == '__main__':
main()
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)