十四、区块量化 dual thrust策略
新增cross_dual_thrust_order.py文件
# -*- coding: utf-8 -*-
import cross_order as order
import time
SHORT_WIN = 10 # 短周期窗口
SHORT_K1 = 0.5
SHORT_K2 = 0.5
def dual_thrust(symbol='EOS-USDT-SWAP', n=SHORT_WIN, K1=SHORT_K1, K2=SHORT_K2):
"""获取 BuyLine 和 SellLine
1、首先计算:
(1)N日High的最高价HH, N日Close的最低价LC;
(2)N日Close的最高价HC,N日Low的最低价LL;
(3)Range = Max(HH-LC,HC-LL)
(4)BuyLine = Open + K1*Range
(5)SellLine = Open + K2*Range
2.构造系统
(1)当价格向上突破上轨时,如果当时持有空仓,则先平仓,再开多仓;如果没有持仓,则直接开多仓;
(2)当价格向下突破下轨时,如果当时持有多仓,则先平仓,再开空仓;如果没有持仓,则直接开空仓;
"""
hist = order.get_candlesticks(symbol=symbol, interval='1D', limit=str(n + 1))
day_open = order.get_candlesticks(symbol=symbol, interval='1m', limit='1')
HH = max(hist['high'])
LC = min(hist['close'])
HC = max(hist['close'])
LL = min(hist['low'])
Open = day_open['open'].iloc[0]
# 获取 Range
Range = max((HH - LC), (HC - LL))
# 计算BuyLine 和 SellLine
BuyLine = Open + K1 * Range
SellLine = Open - K2 * Range
# 返回结果
return BuyLine, SellLine
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(SHORT_WIN + 1))
close = current_price['close']
# 获取 BuyLine, SellLine
BuyLine, SellLine = dual_thrust(symbol=symbol, n=SHORT_WIN, K1=SHORT_K1, K2=SHORT_K2)
# 双均线策略
# 短期均线上穿长期均线,做多
if (close.values[-2] < BuyLine) and (close.values[-1] >= BuyLine):
order.up_cross_order(symbol=symbol,ordtype='market', message='短期均线上穿长期均线,做多')
# 短期均线下穿长期均线,做空
elif (close.values[-2] > SellLine) and (close.values[-1] <= SellLine):
order.down_cross_order(symbol=symbol,ordtype='market', message='短期均线下穿长期均线,做空')
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()
import cross_order as order
import time
SHORT_WIN = 10 # 短周期窗口
SHORT_K1 = 0.5
SHORT_K2 = 0.5
def dual_thrust(symbol='EOS-USDT-SWAP', n=SHORT_WIN, K1=SHORT_K1, K2=SHORT_K2):
"""获取 BuyLine 和 SellLine
1、首先计算:
(1)N日High的最高价HH, N日Close的最低价LC;
(2)N日Close的最高价HC,N日Low的最低价LL;
(3)Range = Max(HH-LC,HC-LL)
(4)BuyLine = Open + K1*Range
(5)SellLine = Open + K2*Range
2.构造系统
(1)当价格向上突破上轨时,如果当时持有空仓,则先平仓,再开多仓;如果没有持仓,则直接开多仓;
(2)当价格向下突破下轨时,如果当时持有多仓,则先平仓,再开空仓;如果没有持仓,则直接开空仓;
"""
hist = order.get_candlesticks(symbol=symbol, interval='1D', limit=str(n + 1))
day_open = order.get_candlesticks(symbol=symbol, interval='1m', limit='1')
HH = max(hist['high'])
LC = min(hist['close'])
HC = max(hist['close'])
LL = min(hist['low'])
Open = day_open['open'].iloc[0]
# 获取 Range
Range = max((HH - LC), (HC - LL))
# 计算BuyLine 和 SellLine
BuyLine = Open + K1 * Range
SellLine = Open - K2 * Range
# 返回结果
return BuyLine, SellLine
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(SHORT_WIN + 1))
close = current_price['close']
# 获取 BuyLine, SellLine
BuyLine, SellLine = dual_thrust(symbol=symbol, n=SHORT_WIN, K1=SHORT_K1, K2=SHORT_K2)
# 双均线策略
# 短期均线上穿长期均线,做多
if (close.values[-2] < BuyLine) and (close.values[-1] >= BuyLine):
order.up_cross_order(symbol=symbol,ordtype='market', message='短期均线上穿长期均线,做多')
# 短期均线下穿长期均线,做空
elif (close.values[-2] > SellLine) and (close.values[-1] <= SellLine):
order.down_cross_order(symbol=symbol,ordtype='market', message='短期均线下穿长期均线,做空')
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()
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)