0
def good_buying_condition_stoch_ema_ema(the_exchange, symbol, timeframe):
    #print ("finding the good to go long/buy signal")
    r = dict();
    hist_data = get_historical_data(the_exchange, symbol, timeframe)
    stock_data = create_stock(hist_data) #stock stats data
    stock_data.KDJ_WINDOW=14
    stock_data.KDJ_PARAM=(3.0 / 14.0, 3.0 / 14.0)
    #stock_data._calc_kd(stock_data['close']);

    stock_data.MACD_EMA_SHORT = 50
    stock_data.MACD_EMA_LONG = 200
    macd = stock_data._get_macd(stock_data)
    gap = stock_data['kdjk'][499] - stock_data['kdjd'][499]
    abs_gap = abs(gap);
    r['datetime'] = stock_data['timestamp'][499]
    r['symbol'] = symbol
    r['timeframe'] = timeframe
    r['close'] = stock_data['close'][499]
    r['kdjk3'] = stock_data['kdjk_3'][499]
    r['kdjd3'] = stock_data['kdjd_3'][499]
    r['kdjk'] = stock_data['kdjk'][499]
    r['kdjd'] = stock_data['kdjd'][499]
    r['macd'] = stock_data['macd'][499]
    stock_data.KDJ_WINDOW=7
    stock_data._calc_kd(stock_data['close']);
    r['kdjk7'] = stock_data['kdjk_7'][499]
    r['kdjd7'] = stock_data['kdjd_7'][499]
    stock_data.KDJ_WINDOW=14
    stock_data._calc_kd(stock_data['close']);
    r['kdjk14'] = stock_data['kdjk_14'][499]
    r['kdjd14'] = stock_data['kdjd_14'][499]
    if (gap < 1 and gap > -1): r['side'] = 'HODL'
    elif (gap > 1): r['side'] = 'buy'
    elif (gap < -1): r['side'] = 'sell'
    else: r['side'] = 'unknown'

    if (r['side'] == "buy"):
        if (stock_data['kdjk'][499] < 20): r['confidence'] = "strong"
        elif (stock_data['kdjk'][499] < 75): r['confidence'] = "ok"
        elif (stock_data['kdjk'][499] < 80): r['confidence'] = "questionable"
        elif (stock_data['kdjk'][499] < 90): r['confidence'] = "risky"
        elif (stock_data['kdjk'][499] < 100): r['confidence'] = "extreme risk"
    elif (r['side'] == "sell"):
        if (stock_data['kdjk'][499] < 20): r['confidence'] = "extreme risk"
        elif (stock_data['kdjk'][499] < 40): r['confidence'] = "risky"
        elif (stock_data['kdjk'][499] < 60): r['confidence'] = "questionable"
        elif (stock_data['kdjk'][499] < 80): r['confidence'] = "ok"
        elif (stock_data['kdjk'][499] < 100): r['confidence'] = "strong"
    else: r['confidence'] = 'unknown'

    r['momentum'] = gap

    if (stock_data['macd'][499] > 0 and stock_data['macd'][499] < 1):
        r['trend'] = "bullish"
    elif (stock_data['macd'][499] > 1 ):
        r['trend'] = "uncertain"
    elif (stock_data['macd'][499] < 0 ):
        r['trend'] = "bearish"


    #### FINTA tech indicators
    #ohlc = resample(hist_data, "1m")
    #ema50 = TA.EMAIndicator(close=hist_data.close, n=50)
    #hist_data['ema50_tpj'] = ema50
    #print ("TA.STOCH K = ", TA.STOCH(hist_data['close'], period=14)[499])
    #print ("TA.STOCH D = ", TA.STOCHD(hist_data['close'], period=3, stoch_period=14)[499])
    #print ("TA.EMA 9 = ", TA.EMA(hist_data)[499])
    #print ("TA.EMA 50 = ", TA.EMA(hist_data, 50)[499])
    #print ("TA.EMA 200 = ", TA.EMA(hist_data, 200)[499])

    r['ta_ema9'] = TA.EMA(hist_data)[499]
    r['ta_ema50'] = TA.EMA(hist_data, 50)[499]
    r['ta_ema200'] = TA.EMA(hist_data, 200)[499]
    r['ta_atr14'] = TA.ATR(hist_data, 14)[499]
    r['atr14'] = stock_data['atr'][499]
    r['min14'] = hist_data.close.rolling(14).min()[499];
    r['max14'] = hist_data.close.rolling(14).max()[499];

    print ("returning", r)
    return r


def get_historical_data(exchange, coin_pair, timeframe):
    """Get Historical data (ohlcv) from a coin_pair
    """
    # optional: exchange.fetch_ohlcv(coin_pair, '1h', since)
    data = exchange.fetch_ohlcv(coin_pair, timeframe)
    # update timestamp to human readable timestamp
    data = [[exchange.iso8601(candle[0])] + candle[1:] for candle in data]
    header = ['Timestamp', 'Open', 'High', 'Low', 'Close', 'Volume']
    df = pd.DataFrame(data, columns=header)
    return df

def create_stock(historical_data):
    """Create StockData from historical data using stockstats
    """
    stock  = Sdf.retype(historical_data)
    return stock

在 10 秒的循环中,以上产生以下输出:

returning {'datetime': '2021-05-26T15:55:00.000Z', 'symbol': 'XRP/USDT', 'timeframe': '1m', 'close': 0.9838, 'kdjk3': 23.00115833047544, 'kdjd3': 33.246639262609804, 'kdjk': 31.557468092520086, 'kdjd': 40.89122272232644, 'macd': -0.00016899252758151295, 'kdjk7': 29.02460416563654, 'kdjd7': 40.37714935820455, 'kdjk14': 31.502465082807905, 'kdjd14': 41.35215340927536, 'side': 'sell', 'confidence': 'risky', 'momentum': -9.333754629806354, 'trend': 'bearish', 'ta_ema9': 0.9863129810243016, 'ta_ema50': 0.9876962538671861, 'ta_ema200': 0.9980103854792317, 'ta_atr14': 0.003885714285714286, 'atr14': 0.0038138082006646855, 'min14': 0.9827, 'max14': 0.9927}
returning {'datetime': '2021-05-26T15:56:00.000Z', 'symbol': 'XRP/USDT', 'timeframe': '1m', 'close': 0.9831, 'kdjk3': 14.450607500341013, 'kdjd3': 26.42224572620021, 'kdjk': 23.26053428390232, 'kdjd': 34.77728953914806, 'macd': -0.000447425790014111, 'kdjk7': 18.859864087371616, 'kdjd7': 32.94481190275474, 'kdjk14': 23.223865610760864, 'kdjd14': 35.07235377273352, 'side': 'sell', 'confidence': 'risky', 'momentum': -11.516755255245737, 'trend': 'bearish', 'ta_ema9': 0.9856063848194415, 'ta_ema50': 0.9875009374853446, 'ta_ema200': 0.9978533449719774, 'ta_atr14': 0.0036571428571428666, 'atr14': 0.0035699647577600697, 'min14': 0.9827, 'max14': 0.9927}
baseCurrBal: 556.21882609       percent: .4     ticker['last']: 0.984
returning {'datetime': '2021-05-26T15:56:00.000Z', 'symbol': 'XRP/USDT', 'timeframe': '1m', 'close': 0.9838, 'kdjk3': 20.006163055896863, 'kdjd3': 28.27409757805216, 'kdjk': 25.127200950569065, 'kdjd': 35.399511761370306, 'macd': -0.00039158533417382735, 'kdjk7': 20.88884959461808, 'kdjd7': 33.62114040517023, 'kdjk14': 25.09053227742761, 'kdjd14': 35.69457599495577, 'side': 'sell', 'confidence': 'risky', 'momentum': -10.27231081080124, 'trend': 'bearish', 'ta_ema9': 0.9857463848194415, 'ta_ema50': 0.9875283884657932, 'ta_ema200': 0.9978603573934625, 'ta_atr14': 0.003707142857142861, 'atr14': 0.003619964757760064, 'min14': 0.9827, 'max14': 0.9927}
returning {'datetime': '2021-05-26T15:56:00.000Z', 'symbol': 'XRP/USDT', 'timeframe': '1m', 'close': 0.9841, 'kdjk3': 22.387115436848994, 'kdjd3': 29.06774837170287, 'kdjk': 25.927200950568974, 'kdjd': 35.66617842803694, 'macd': -0.0003676537102420552, 'kdjk7': 21.758414812009285, 'kdjd7': 33.91099547763397, 'kdjk14': 25.890532277427518, 'kdjd14': 35.96124266162241, 'side': 'sell', 'confidence': 'risky', 'momentum': -9.73897747746797, 'trend': 'bearish', 'ta_ema9': 0.9858063848194416, 'ta_ema50': 0.9875401531716997, 'ta_ema200': 0.997863362716956, 'ta_atr14': 0.003707142857142861, 'atr14': 0.003619964757760064, 'min14': 0.9827, 'max14': 0.9927}
baseCurrBal: 556.21882609       percent: .4     ticker['last']: 0.9839
returning {'datetime': '2021-05-26T15:56:00.000Z', 'symbol': 'XRP/USDT', 'timeframe': '1m', 'close': 0.9843, 'kdjk3': 23.974417024150412, 'kdjd3': 29.596848900803344, 'kdjk': 26.46053428390225, 'kdjd': 35.8439562058147, 'macd': -0.0003516992942875774, 'kdjk7': 22.338124956936753, 'kdjd7': 34.10423219260979, 'kdjk14': 26.423865610760792, 'kdjd14': 36.13902043940016, 'side': 'sell', 'confidence': 'risky', 'momentum': -9.38342192191245, 'trend': 'bearish', 'ta_ema9': 0.9858463848194414, 'ta_ema50': 0.9875479963089707, 'ta_ema200': 0.9978653662659518, 'ta_atr14': 0.003721428571428574, 'atr14': 0.003634250472045777, 'min14': 0.9827, 'max14': 0.9927}
returning {'datetime': '2021-05-26T15:56:00.000Z', 'symbol': 'XRP/USDT', 'timeframe': '1m', 'close': 0.9848, 'kdjk3': 27.94267099240484, 'kdjd3': 30.91960022355482, 'kdjk': 27.79386761723572, 'kdjd': 36.28840065025919, 'macd': -0.0003118132544015495, 'kdjk7': 23.787400319255745, 'kdjd7': 34.587323980049455, 'kdjk14': 27.757198944094263, 'kdjd14': 36.583464883844655, 'side': 'sell', 'confidence': 'risky', 'momentum': -8.494533033023473, 'trend': 'bearish', 'ta_ema9': 0.9859463848194416, 'ta_ema50': 0.9875676041521483, 'ta_ema200': 0.997870375138441, 'ta_atr14': 0.0038000000000000095, 'atr14': 0.0037128219006172126, 'min14': 0.9827, 'max14': 0.9927}
baseCurrBal: 556.21882609       percent: .4     ticker['last']: 0.9842

当我比较所有 kdj* 值时,我看不出与交易视图的标准 stoch rsi 与 3/3/14/14/close 参数在同一时间段内的值有很强的相似性。我已经多次调整了 KDJ_WINDOW 和 KDJ_PARAM 值,几乎所有对我来说都有意义的 3、7 和 14 组合;我尝试过调用 _calc_kd 而没有,我尝试在 _calc_kd 调用之间调整 KDJ_WINDODW 和 KDJ_PARAM;我试图只引用 ['kdjk'] 和 ['kdjd']; 我已经尝试使用https://www.programmersought.com/article/69583827158/中提到的 ['kdjk_N'] 列,但我无法密切复制交易视图值......

我错过了什么/做错了什么?

(注 1:对交换的任何引用都是针对ccxt交换对象的)。(注 2:我正在使用stockstats

4

1 回答 1

0

i ended up using the following manual calculation: i ended up using the following manual calculation:

stock_data['rsi1'] = stock_data['rsi_14']
    stock_data['rsi_L14'] = stock_data['rsi1'].rolling(window=14).min()
    stock_data['rsi_H14'] = stock_data['rsi1'].rolling(window=14).max()
    stock_data['stoch'] = 100*((stock_data['rsi1'] - stock_data['rsi_L14']) / (stock_data['rsi_H14'] - stock_data['rsi_L14']) )
    stock_data['my_k2'] = stock_data['stoch'].rolling(window=3).mean()
    stock_data['my_d2'] = stock_data['my_k2'].rolling(window=3).mean()
    r['kdjk'] = stock_data['my_k2'][499]
    r['kdjd'] = stock_data['my_d2'][499]

i found this pine script and used explanations at the tradingview explanation and explanation from this page to come up with the true algorithm.

i subsequently cross referenced against a live 1m chart of xrp/usdt on trading view and the values aligned almost exactly.

于 2021-05-27T03:31:50.703 回答