介绍参考:
国内4种常用日内CTA策略介绍及实现:https://blog.csdn.net/u011331731/article/details/88326872
要点:
日High的最高价HH, N日Close的最低价LC
N日Close的最高价HC,N日Low的最低价LL
Range = Max(HH-LC,HC-LL)
BuyLine = Open + K1*Range
SellLine = Open - K2*Range
当价格向上突破上轨时,如果当时持有空仓,则先平仓,再开多仓;如果没有仓位,则直接开多仓;
当价格向下突破下轨时,如果当时持有多仓,则先平仓,再开空仓;如果没有仓位,则直接开空仓;
当K1时,多头相对容易被触发,当K1>K2时,空头相对容易被触发。因此,投资者在使用该策略时,一方面可以参考历史数据测试的最优参数,另一方面,则可以根据自己对后势的判断,或从其他大周期的技术指标入手,阶段性地动态调整K1和K2的值。
代码:
import talib
from rqalpha.api import *
import numpy
import pandas as pd
from pandas import Series
from rqalpha import run_func
from rqalpha.api import industry, update_universe, order_target_value
from rqalpha.utils import scheduler
import numpy as np
import pandas as pd
import logging
import warnings
from math import ceil
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from rqalpha.api import *
import scipy.optimize as sco
import seaborn as sns
import talib
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import TimeSeriesSplit
from sklearn.preprocessing import StandardScaler
# 在这个方法中编写任何的初始化逻辑。context对象将会在你的算法策略的任何方法之间做传递。
def init(context):
context.future_id = "IH88"
context.max_length = 10
subscribe(context.future_id)
context.fired = False
context.k1 = 0.5
context.k2 = 0.5
context.qty = 5
# before_day_trading此函数会在每天日盘交易开始前被调用,当天只会被调用一次
def before_day_trading(context):
pass
# 你选择的期货数据更新将会触发此段逻辑,例如日线或分钟线更新
def handle_bar(context, bar_dict):
if context.now.strftime('%H:%M') < '14:59':
hist_close = history_bars(context.future_id, context.max_length, '1d', 'close')
hist_high = history_bars(context.future_id, context.max_length, '1d', 'high')
hist_low = history_bars(context.future_id, context.max_length, '1d', 'low')
price_open = history_bars(context.future_id, 1, '1d', 'open')[0]
range = max(max(hist_high) - min(hist_close), max(hist_close) - min(hist_low))
up = price_open + context.k1 * range
down = price_open - context.k2 * range
price_now = bar_dict[context.future_id].close
if price_now > up:
sell_qty = context.portfolio.positions[context.future_id].sell_quantity
if sell_qty > 0:
buy_close(context.future_id, sell_qty)
buy_open(context.future_id, context.qty)
if price_now < down:
buy_qty = context.portfolio.positions[context.future_id].buy_quantity
if buy_qty > 0:
sell_close(context.future_id, buy_qty)
sell_open(context.future_id, context.qty)
else:
buy_qty = context.portfolio.positions[context.future_id].buy_quantity
sell_qty = context.portfolio.positions[context.future_id].sell_quantity
if buy_qty > 0:
sell_close(context.future_id, buy_qty)
if sell_qty > 0:
buy_close(context.future_id, sell_qty)
# before_night_trading此函数会在每天夜盘交易开始前被调用,当天只会被调用一次
def before_night_trading(context):
pass
这是个裸策略,未做任何优化,结果也不佳,也在意料之中的.
经典策略还是得依靠调优等方式改造后才能使用,要是拿来就用那历史上就出来无数的大富翁了.
参考:
https://www.ricequant.com/community/topic/2473
https://www.ricequant.com/community/topic/1726
https://www.ricequant.com/community/topic/4920
https://www.ricequant.com/community/topic/2411
https://www.ricequant.com/community/topic/4971
https://www.ricequant.com/community/topic/4830
https://www.ricequant.com/community/topic/36847
https://uqer.io/v3/community/share/586f319989e3ba0047efdc09