#掘金3的常见指标实现 BY赏玫瑰LUOW。(如有出错请指出)
coding=utf-8
from future import print_function, absolute_import, unicode_literals
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import talib
#ta.set_compatibility(1)
from gm.api import *
def init(context):
context.goods交易的品种
context.goods = [‘SZSE.000063’]
订阅context.goods里面的品种, bar频率为1min
subscribe(symbols=context.goods, frequency=‘1d’, count=500)
止损的比例区间
def on_bar(context, bars):
bar = bars[0]
mysymbol = bar[‘symbol’]
recent_data = context.data(symbol=mysymbol, frequency=‘1d’, count=500, fields=‘symbol,open,close,high,low,bob’)
today=context.now #获取当天日期时间
close=recent_data.close.values #获取收盘列表
high=recent_data.high.values #最高价列表
low=recent_data.low.values #最低价列表
print(’\n’)
下面显示当天时间,交易代码,最新收盘价取值来自close[-1。如果前一天收盘价为close[-2],前两天收盘价为close[-3]
print (today,mysymbol,‘收盘’,round(close[-1],3))
均线的获取
MA5 = talib.MA(close, timeperiod=5) # 获取5天均线列表
MA10 = talib.MA(close, timeperiod=10) # 获取10天均线列表
MA20 = talib.MA(close, timeperiod=20) # 获取20天均线列表
MA60 = talib.MA(close, timeperiod=60) # 获取60天均线列表
print (‘5天均线 :’, round(MA5[-1],3))
print (‘10天均线 :’, round(MA10[-1],3))
print (‘20天均线 :’, round(MA20[-1],3))
print (‘60天均线 :’, round(MA60[-1],3))
macd 指标 2026重试OK
macddiff, macddea, macd = talib.MACDEXT(recent_data.close.values, fastperiod=12, fastmatype=1, slowperiod=26, slowmatype=1, signalperiod=9, signalmatype=1)
macd = macd * 2
print ('macd is ',round(macd[-1],3),‘diff is’,round(macddiff[-1],3),‘dea is’,round(macddea[-1],3))
rsi 威廉指标 2026已经OK
rsi6=talib.RSI(close, timeperiod=6)
rsi12=talib.RSI(close, timeperiod=12)
rsi24=talib.RSI(close, timeperiod=24)
print (‘rsi6’,round(rsi6[-1],2),‘rsi12’,round(rsi12[-1],2),‘rsi24’,round(rsi24[-1],2))
WILLR威廉指标 2026已经OK 。需要用abs函数返回绝对值即取正数
will10 = abs(talib.WILLR(high, low, close, timeperiod=10))
will6 = abs(talib.WILLR(high, low, close, timeperiod=6))
will20 = abs(talib.WILLR(high, low, close, timeperiod=20))
will80 = abs(talib.WILLR(high, low, close, timeperiod=80))
print('wr10 ',round(will10[-1],2),'wr6 ',round(will6[-1],2),'wr20 ',round(will20[-1],2),'wr80 ',round(will80[-1]))
KDJ指标。引用下面的cal_kdj_vector(df_data)函数得出,与通达信同花顺数值一样 2026测试 OK。
mykdj= cal_kdj_vector(recent_data)
K=mykdj[‘K’].values
D=mykdj[‘D’].values
J=mykdj[‘J’].values
print (‘K is’,round(K[-1],2),‘D is’,round(D[-1],2),‘J is’,round(J[-1],2))
#LWR 是KDJ的补数。2026测试OK
lwr1=100-K
lwr2=100-D
lwr3=100-J
print (‘LWR1’,round(lwr1[-1],2),‘LWR2’,round(lwr2[-1],2),'LWR3 ',round(lwr3[-1],2))
def cal_kdj_vector(df_data):
low_list = df_data[‘low’].rolling(9, min_periods=1).min()
high_list = df_data[‘high’].rolling(9, min_periods=1).max()
rsv = (df_data[‘close’] - low_list) / (high_list - low_list) * 100
df_data[‘K’] = rsv.ewm(com=2, adjust=False).mean()
df_data[‘D’] = df_data[‘K’].ewm(com=2, adjust=False).mean()
df_data[‘J’] = 3 * df_data[‘K’] - 2 * df_data[‘D’]
return df_data
if name == ‘main’:
run(strategy_id=‘strategy_id’,
filename=‘main.py’,
mode=MODE_BACKTEST,
token=‘token_id’,
backtest_start_time=‘-07-23 09:15:00’,
backtest_end_time=‘-07-24 15:01:00’,
backtest_adjust=ADJUST_PREV,
backtest_initial_cash=500000,
backtest_commission_ratio=0.0001,
backtest_slippage_ratio=0.0001)
如果觉得《Python 编写 掘金3量化交易软件的常见指标实现。均线 macd kdj RSI LWR。》对你有帮助,请点赞、收藏,并留下你的观点哦!