经典的轨道突破策略
根据均线进行价格趋势追踪是一种常用的方法,通过比较不同均线速度的改变程度来判断买入与卖出点,根据其统计特性从而形成一套完整的交易策略
轨道线是趋势线概念的延伸,当股价沿道趋势上涨到某一价位水准时会遇到阻力,回档至某一水准时又获得支撑,轨道线就在接高点的延长线及接低点的延长线之间上下来回,当轨迹线确立后,股价就可以非常容易地找出高低价位所在,投资人可依此判断来操作股票
Dual Thrust 策略
Dual Thrust 简称DT,是Michael Chalek 在80年代开发的,是海外TOP10交易系统中的一个,属于开盘区间突破类交易系统,以今日开盘价 加 / 减一定比例的昨日振幅确定上下轨,日内突破上轨时平空做多,突破下轨时平多做空
# coding=utf-8
from __future__ import print_function, absolute_import
from gm.api import *
import numpy as np
import talib
# 策略中必须有init方法
def init(context):
context.symbol = "SHSE.600000" # sys.argv[1]
context.frequency = "1d"
context.fields = "symbol,open,high,low,close,volume,eob"
context.count = 25
context.k1 = 0.7
schedule(schedule_func=algo,date_rule="1d",time_rule="09:30:00")
context.flag = 100
context.flag_check = 2 # int(sys.argv[5])
def algo(context):
today = context.now
print(today)
last_day = get_previous_trading_date("SZSE",today)
print(last_day)
data = history_n(symbol=context.symbol, frequency=context.frequency, count=context.count,
end_time=last_day, fields=context.fields,
fill_missing="last", adjust=ADJUST_PREV, df=True)
print(data)
today_data = history_n(symbol=context.symbol, frequency=context.frequency, count=context.count,
end_time=today, fields=context.fields,
fill_missing="last", adjust=ADJUST_PREV, df=True)
open = np.asarray((data["open"].values))
high = np.asarray((data["high"].values))
low = np.asarray((data["low"].values))
close = np.asarray((data["close"].values))
today_open = np.asarray((today_data["open"].values))[-1]
hh = np.max(high)
hc = np.max(close)
lc = np.min(close)
ll = np.min(low)
range = np.max([hh-hc,lc-ll])
## 实时回测
# data_now = current(symbols=context.symbol)[0]
# print(current(symbols=context.symbol))
# data_now_open = data_now["open"]
# data_now_price = data_now["price"]
## 历史回测
data_now = current(symbols=context.symbol)[0]
data_now_open = today_open
data_now_price = data_now["price"]
range_up_price = data_now_open + context.k1 * range
if (data_now_price > range_up_price):
order_target_percent(symbol=context.symbol, percent=0.75, position_side=PositionSide_Long, order_type=OrderType_Market, price=0)
context.flag = 0
if (data_now_price < range_up_price):
order_target_percent(symbol=context.symbol, percent=0, position_side=PositionSide_Long,order_type=OrderType_Market, price=0)
if __name__ == '__main__':
run(strategy_id='xxxxxxxxxxxxxxxxxxxxxx',
filename='main.py',
mode=MODE_BACKTEST,
token='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
backtest_start_time='2017-01-01 09:00:00',
backtest_end_time='2017-12-31 15:00:00',
backtest_initial_cash=20000,
backtest_adjust=ADJUST_PREV
)
Dynamic Breakout II 策略
# coding=utf-8
from __future__ import print_function, absolute_import
from gm.api import *
import numpy as np
import talib
# 策略中必须有init方法
def init(context):
context.symbol = "SZSE.300296"
context.frequency = "1d"
context.fields = "open,high,low,close,volume"
context.ceilingAmt = 60
context.floorAmt = 20
context.bolBandTrig = 1
schedule(schedule_func=algo, date_rule="1d", time_rule="09:30:00")
lookBackDays = 30
def algo(context):
global lookBackDays
today = context.now
print(today)
last_day = get_previous_trading_date("SZSE",today)
data = history_n(symbol=context.symbol, frequency = context.frequency, count=int(lookBackDays),
end_time=last_day, fields=context.fields,
fill_missing="last", adjust=ADJUST_PREV, df=True)
close = np.asarray((data["close"].values))
high = np.asarray((data["high"].values))
low = np.asarray((data["low"].values))
todayVolatility = np.std(close)
yesterDayVolatility = np.std(close[:-1])
deltaVolatility = (todayVolatility - yesterDayVolatility) / todayVolatility
lookBackDays = np.round(lookBackDays * (1 + deltaVolatility))
lookBackDays = np.min([lookBackDays,context.ceilingAmt])
lookBackDays = np.max([lookBackDays,context.floorAmt])
MidLine = np.average(close)
Band = np.std(close)
upBand = MidLine + context.bolBandTrig * Band
lowBand = MidLine - context.bolBandTrig * Band
buyPoint = np.max(high)
sellPoint = np.min(low)
current_data = current(symbols= context.symbol)
print(current_data)
current_price = current_data[0]["price"]
if ((close[-1] > upBand) and (current_price > buyPoint)):
order_percent(symbol=context.symbol, percent=1, side=OrderSide_Buy,position_effect = PositionEffect_Open , order_type=OrderType_Market, price=0)
if ((close[-1] < lowBand) and (current_price < sellPoint)):
order_percent(symbol=context.symbol, percent=0, side=OrderSide_Sell,position_effect = PositionEffect_Open , order_type=OrderType_Market, price=0)
if __name__ == '__main__':
run(strategy_id='xxxxxxxxxxxxxxxxxxxxxxxx',
filename='main.py',
mode=MODE_BACKTEST,
token='xxxxxxxxxxxxxxxxxxxxxxxx',
backtest_start_time='2017-01-01 09:00:00',
backtest_end_time='2017-12-31 15:00:00',
backtest_initial_cash=20000,
backtest_adjust=ADJUST_PREV)