掘金社区

掘金3仿真,请坛友代写一个简单的策略,酬谢150¥Pinned highlighted

KONGSHIWUHUA 发表在策略研究 2020-04-21 14:10:56

策略研究
711
2
0
  1. 一小时周期
  2. MACD一波连续的绿柱(MACD≤0)翻红(MACD≥0)
  3. 这一波绿柱至少有10根(≥10)
  4. 这一波绿柱对应的所有K线中,至少有一根K线与MA120或者MA250相交(相交的意思是L≤MA120 OR MA250≤H)
  5. 满足1234 以涨停价格买入
    6.此策略针对自选的50个股票

如果有坛友愿意代劳,请与我联系allin874

评论: 2
  • 这个价格恐怕够悬

    2020-06-12 16:02:59
  • coding=utf-8

    from future import print_function, absolute_import
    from gm.api import *
    import talib

    import numpy as np
    import pandas as pd

    def calculateEMA(period, closeArray, emaArray=[]):
    """计算指数移动平均"""
    length = len(closeArray)
    nanCounter = np.count_nonzero(np.isnan(closeArray))
    if not emaArray:
    emaArray.extend(np.tile([np.nan],(nanCounter + period - 1)))
    firstema = np.mean(closeArray[nanCounter:nanCounter + period - 1])
    emaArray.append(firstema)
    for i in range(nanCounter+period,length):
    ema=(2*closeArray[i]+(period-1)*emaArray[-1])/(period+1)
    emaArray.append(ema)
    return np.array(emaArray)

    def calculateMACD(closeArray, shortPeriod = 12, longPeriod = 26, signalPeriod =9):
    ema12 = calculateEMA(shortPeriod ,closeArray,[])
    ema26 = calculateEMA(longPeriod ,closeArray,[])
    diff = ema12-ema26

    dea= calculateEMA(signalPeriod ,diff,[])
    macd = 2*(diff-dea)
    return macd,diff,dea 
    

    策略中必须有init方法

    def init(context):
    symbols = ['SHSE.600000']
    startday = '2020-01-01'
    # 获取沪深300成份股
    stock300 = get_history_constituents(index='SHSE.000300', start_date=startday,
    end_date=startday)[0]['constituents'].keys()
    symbols = list(stock300)[0:50]
    context.macd_bar_count = 300 # 最少是长周期+信号周期
    context.continue_bars = 10
    subscribe(symbols=symbols, frequency='3600s', count=context.macd_bar_count, wait_group=True)

    def on_bar(context, bars):
    now = context.now

    target_symbols = []
    for bar in bars:
        symbol = bar['symbol']
        recent_data = context.data(symbol=symbol, frequency='3600s', count=context.macd_bar_count, fields='close,low,high')
        closes = recent_data['close'].values
        lows = recent_data['low'].values
        highs = recent_data['high'].values
        tadiff, tadea, tamacd = talib.MACD(closes)
        ma120 = talib.MA(closes, timeperiod=120, matype=talib.MA_Type.SMA)
        ma250 = talib.MA(closes, timeperiod=250, matype=talib.MA_Type.SMA)
        #print('TAlib diff=%.3f, dea=%.3f, macd=%.3f, ma120=%.3f, ma250=%.3f' % (tadiff[-1], tadea[-1], tamacd[-1]*2, ma120[-1], ma250[-1]))
        if tamacd[-1] >= 0:
            firstindex = len(tamacd) * -1
            has_cross = False
            count = 0
            for i in range(-2, firstindex-1, -1):
                if tamacd[i] <= 0:
                    count = count+1
                else:
                    break
                if (lows[i] <= ma120[i] and highs[i] >= ma120[i]) or (lows[i] <= ma250[i] and highs[i] >= ma250[i]):
                    has_cross = True
            if count >= context.continue_bars and has_cross:
                target_symbols.append(symbol)
    for symbol in target_symbols:
        print(now, 'Buy', symbol, 'at price %.2f' % closes[-1])
    

    if name == 'main':
    run(strategy_id='0db9dccb-b163-11ea-9af0-488ad2110d88',
    filename='main.py',
    mode=MODE_BACKTEST,
    token='fb64648c6acc4b98cec09de68b07b9c6047e3318',
    backtest_start_time='2019-6-1 9:00:00',
    backtest_end_time='2020-6-19 15:00:00')

    2020-06-30 21:30:34

Looks like your connection to 掘金量化社区 - 量化交易者的策略交流学习社区 was lost, please wait while we try to reconnect.