声明:
1.本文策略源码均来自掘金量化示例策略库,仅供参考!
2.由于雪球编辑器不支持python语言,源码可能存在格式错误,请自行修正。
KDJ中J值python策略源码:
# -*- coding: utf-8 -*-
#本策略基于掘金量化交易平台 网址:网页链接
# 简便起见,可以直接用 from gm.api import *
from gm.api import run
from gm.api import ADJUST_PREV
from gm.api import MODE_BACKTEST
from gm.api import subscribe
from gm.api import history_n
from gm.api import order_percent
from gm.api import order_volume
from gm.api import (OrderSide_Buy, OrderSide_Sell)
from gm.api import (PositionEffect_Open, PositionEffect_Close)
from gm.api import OrderType_Market
from datetime import datetime
from datetime import timedelta
import talib
import numpy as np
from collections import deque
# 常用参量设置
DATE_STR = "%Y-%m-%d"
TIME_STR = "%Y-%m-%d %H:%M:%S"
HIST_WINDOW = 30
SHORT_PERIOD = 5
LONG_PERIOD = 20
def init(context):
# 全局变量设置
context.dict_stock_price = dict()
# 以 50 EFT作为交易标的
context.stock_pool = ['SZSE.00001']
# 订阅日线行情
subscribe(symbols=context.stock_pool, frequency='1d', wait_group=True)
# 日期设定,避免出现未来函数,将起始日往前取一日
start_date = datetime.strptime(context.backtest_start_time, TIME_STR)
context.start_date = datetime.strftime(start_date - timedelta(days=1),
TIME_STR)
# 获取起始日之前行情,便于计算指标
deque_high = deque(maxlen=HIST_WINDOW)
deque_low = deque(maxlen=HIST_WINDOW)
deque_close = deque(maxlen=HIST_WINDOW)
for stock in context.stock_pool:
history_info = history_n(symbol=stock,
frequency='1d',
count=HIST_WINDOW,
adjust=ADJUST_PREV,
adjust_end_time=context.backtest_end_time,
end_time=context.start_date,
fields='high, low, close')
for bar in history_info:
deque_high.append(bar['high'])
deque_low.append(bar['low'])
deque_close.append(bar['close'])
context.dict_stock_price.setdefault(stock,
[deque_high, deque_low, deque_close])
print('finish initialization')
def on_bar(context, bars):
for bar in bars:
if bar.symbol not in context.dict_stock_price.keys():
print('Warning: cannot obtain price of stock {} at date {}'.format(
bar.symbol, context.now))
# 数据填充
context.dict_stock_price[bar.symbol][0].append(bar.high)
context.dict_stock_price[bar.symbol][1].append(bar.low)
context.dict_stock_price[bar.symbol][2].append(bar.close)
# 计算指标,这里以双均线为例
highs = np.array(context.dict_stock_price[bar.symbol][0])
lows = np.array(context.dict_stock_price[bar.symbol][1])
closes = np.array(context.dict_stock_price[bar.symbol][2])
k_value, d_value = talib.STOCH(highs,
lows,
closes,
fastk_period=9,
slowk_period=3,
slowk_matype=0,
slowd_period=3,
slowd_matype=0)
j_value = 3*k_value - 2*d_value
# 金叉,满仓买入
if j_value[-1] >= 80 or j_value[-1] <= 20:
order_percent(symbol=bar.symbol,
percent=1.0,
side=OrderSide_Buy,
order_type=OrderType_Market,
position_effect=PositionEffect_Open,
price=0)
print(context.now)
# 死叉,全部卖出
pos = context.account().position(symbol=bar.symbol, side=OrderSide_Buy)
if (j_value[-1] < 80 and j_value[-1] > 20):
if pos is None:
continue
order_volume(symbol=bar.symbol,
volume=pos.volume,
side=OrderSide_Sell,
order_type=OrderType_Market,
position_effect=PositionEffect_Close,
price=0)
if __name__ == "__main__":
run(strategy_id='569b4ffc-6d44-11e8-bd88-80ce62334e41',
filename='demo_03.py',
mode=MODE_BACKTEST,
backtest_adjust=ADJUST_PREV,
token='64c33fc82f334e11e1138eefea8ffc241db4a2a0',
backtest_start_time='2017-01-17 09:00:00',
backtest_end_time='2018-06-21 15:00:00')
关联阅读:你常用的技术指标到底能不能赚钱?