def __update_standard_deviation(self, all_candles): if len(all_candles) < 20: return last_20_candles = Candle.select_last_candles(all_candles, self.pair, 20) last_20_closing_prices = Candle.select_closing_prices(last_20_candles) from numpy import std self.standard_deviation.append(std(last_20_closing_prices))
def __update_SMA_EMA(self, all_candles, period, previous_EMA): if len(all_candles) > period: last_candles = Candle.select_last_candles(all_candles, self.pair, period) SMA = self.SMA.SMA(Candle.select_closing_prices(last_candles)) EMA = self.EMA.EMA(Candle.select_closing_prices(last_candles), previous_EMA) return SMA, EMA
def __update_SMA_EMA_first_time(self, all_candles, period): if len(all_candles) < period: raise AverageComputationTooEarly if len(all_candles) == period: last_candles = Candle.select_last_candles(all_candles, self.pair, period) SMA = self.SMA.SMA(Candle.select_closing_prices(last_candles)) return SMA, SMA
def feed(self, last_14_candles): last_5_candles = last_14_candles[-5:] last_5_low = Candle.select_low(last_5_candles) last_5_high = Candle.select_high(last_5_candles) last_closing_price = last_5_candles[-1].close self.stochastic_K.append(100 * ((last_closing_price - min(last_5_low)) / (max(last_5_high) - min(last_5_low)))) if len(self.stochastic_K) >= 3: self.stochastic_D.append(SMA.SMA(self.stochastic_K[-3:])) self.__update_stochastics_indicators()
def __debug_plot_charts(self, all_candles): all_USDT_ETH_candles = Candle.select_last_candles( all_candles, "USDT_ETH", -1) closing_prices = Candle.select_closing_prices(all_USDT_ETH_candles) dates = Candle.select_dates(all_USDT_ETH_candles) self.__drawer.draw(closing_prices, dates, self.__USDT_ETH_indicators.EMA.EMA_12, self.__USDT_ETH_indicators.EMA.EMA_26, self.__USDT_ETH_indicators.MACD.MACD, self.__USDT_ETH_indicators.MACD.MACD_signal, self.__USDT_ETH_indicators.stochastic.stochastic_D, self.__USDT_ETH_indicators.RSI.RSI, self.__USDT_ETH_indicators.ADX.ADX)
def parse_three_candles(value): three_candles = [Candle("USDT_ETH"), Candle("USDT_BTC"), Candle("BTC_ETH")] candles_input = value.split(';') for candle_input in candles_input: candle_part = candle_input.split(',') for candle in three_candles: if candle.pair == candle_part[0]: candle.date = float(candle_part[1]) candle.high = float(candle_part[2]) candle.low = float(candle_part[3]) candle.open = float(candle_part[4]) candle.close = float(candle_part[5]) candle.volume = float(candle_part[6]) return three_candles
def feed(self, all_candles, last_14_candles): if len(all_candles) < 14: return last_14_closing_prices = Candle.select_closing_prices(last_14_candles) if len(all_candles) == 14: gains = 0 losses = 0 for x in range(1, 14): delta = last_14_closing_prices[x] - last_14_closing_prices[x - 1] if delta < 0: losses += abs(delta) else: gains += delta self.RSI_previous_average_gain = gains / 14 self.RSI_previous_average_loss = losses / 14 else: delta = last_14_closing_prices[-1] - last_14_closing_prices[-2] current_gain = 0 current_loss = 0 if delta < 0: current_loss = abs(delta) else: current_gain = delta self.RSI_previous_average_gain = ( self.RSI_previous_average_gain * 13 + current_gain) / 14 self.RSI_previous_average_loss = ( self.RSI_previous_average_loss * 13 + current_loss) / 14 RS = self.RSI_previous_average_gain / self.RSI_previous_average_loss self.RSI.append(100 - 100 / (1 + RS)) self.__update_RSI_indicator()
def feed(self, all_candles): last_14_candles = Candle.select_last_candles(all_candles, self.pair, 14) self.__feed_SMA_arrays_and_EMA_arrays(all_candles, len(all_candles), last_14_candles[-1].close) self.MACD.feed(len(all_candles), self.EMA) self.__update_standard_deviation(all_candles) if len(all_candles) >= 14: self.ADX.feed(last_14_candles[-2:]) self.RSI.feed(all_candles, last_14_candles) self.stochastic.feed(last_14_candles) self.__update_oversold_overbought_indicators() if len(self.standard_deviation) >= 1 and len(self.SMA.SMA_20) >= 1: self.BB.feed(self.SMA.SMA_20[-1], self.standard_deviation[-1], last_14_candles[-1].close) if len(all_candles) >= 100: self.__define_trend(last_14_candles[-1].close)
def __do_action_on_USDT_ETH(self, all_candles, current_stockpile, bot_settings, option): #if (self.__buy_authorization(self.__USDT_ETH_indicators) and current_stockpile.USDT > 3): if (self.__buy_authorization(self.__USDT_ETH_indicators) or self.__buy_permission_USDT_ETH is True ) and current_stockpile.USDT > 3: if self.__buy_permission_USDT_ETH is False: self.__buy_permission_USDT_ETH = True return 0 else: self.__buy_permission_USDT_ETH = False price_one_eth = Candle.select_last_candles(all_candles, "USDT_ETH", 1)[0].close amount_i_want_to_buy = current_stockpile.USDT / price_one_eth if option: return 0 if DEBUG_TEXT_MODE: msg = "buy USDT_ETH " + str(amount_i_want_to_buy) self.__debug_print_which_indicator_triggered( all_candles, current_stockpile, self.__USDT_ETH_indicators, msg) return "buy USDT_ETH " + str(amount_i_want_to_buy) #elif self.__sell_authorization(self.__USDT_ETH_indicators): elif self.__sell_authorization( self.__USDT_ETH_indicators ) or self.__sell_permission_USDT_ETH is True: if self.__sell_permission_USDT_ETH is False: self.__sell_permission_USDT_ETH = True return 0 else: self.__sell_permission_USDT_ETH = False amount_i_want_to_sell = current_stockpile.ETH if amount_i_want_to_sell < 0.000001 or option: return 0 if DEBUG_TEXT_MODE: msg = "sell USDT_ETH " + str(amount_i_want_to_sell) self.__debug_print_which_indicator_triggered( all_candles, current_stockpile, self.__USDT_ETH_indicators, msg) return "sell USDT_ETH " + str(amount_i_want_to_sell) return 0
def test_ADX_fixed_values(self): adx = ADX() candle_1 = Candle("USDT_ETH", 30.20, 29.41, 29.87) candle_2 = Candle("USDT_ETH", 30.28, 29.32, 30.24) adx.feed([candle_1, candle_2]) self.assertTrue(truncate(adx.TR[-1], 2) == 0.96) self.assertTrue(truncate(adx.DM_positive[-1], 2) == 0.00) self.assertTrue(truncate(adx.DM_negative[-1], 2) == 0.09) candle_3 = Candle("USDT_ETH", 30.45, 29.96, 30.10) adx.feed([candle_2, candle_3]) self.assertTrue(truncate(adx.TR[-1], 2) == 0.48) self.assertTrue(round(adx.DM_positive[-1], 2) == 0.17) self.assertTrue(round(adx.DM_negative[-1], 2) == 0.00) candle_4 = Candle("USDT_ETH", 29.35, 28.74, 28.90) adx.feed([candle_3, candle_4]) self.assertTrue(truncate(adx.TR[-1], 2) == 1.36) self.assertTrue(round(adx.DM_positive[-1], 2) == 0.00) self.assertTrue(round(adx.DM_negative[-1], 2) == 1.22) candle_5 = Candle("USDT_ETH", 29.35, 28.56, 28.92) adx.feed([candle_4, candle_5]) self.assertTrue(truncate(adx.TR[-1], 2) == 0.79) self.assertTrue(round(adx.DM_positive[-1], 2) == 0.00) self.assertTrue(round(adx.DM_negative[-1], 2) == 0.18) candle_6 = Candle("USDT_ETH", 29.29, 28.41, 29.48) adx.feed([candle_5, candle_6]) candle_7 = Candle("USDT_ETH", 28.83, 28.08, 28.56) adx.feed([candle_6, candle_7]) candle_8 = Candle("USDT_ETH", 28.73, 27.43, 27.56) adx.feed([candle_7, candle_8]) candle_9 = Candle("USDT_ETH", 28.67, 27.66, 28.47) adx.feed([candle_8, candle_9]) candle_10 = Candle("USDT_ETH", 28.85, 27.83, 28.28) adx.feed([candle_9, candle_10]) candle_11 = Candle("USDT_ETH", 28.64, 27.40, 27.49) adx.feed([candle_10, candle_11]) candle_12 = Candle("USDT_ETH", 27.68, 27.09, 27.23) adx.feed([candle_11, candle_12]) candle_13 = Candle("USDT_ETH", 27.21, 26.18, 26.35) adx.feed([candle_12, candle_13]) candle_14 = Candle("USDT_ETH", 26.87, 26.13, 26.33) adx.feed([candle_13, candle_14]) candle_15 = Candle("USDT_ETH", 27.41, 26.63, 27.03) adx.feed([candle_14, candle_15]) #print("TR = ", adx.TR[-1], "DI_pos = ", adx.DI_positive[-1], "DX = ", adx.DX[-1], file=sys.stderr) candle_16 = Candle("USDT_ETH", 26.94, 26.13, 26.22) adx.feed([candle_15, candle_16]) #print("TR = ", adx.TR[-1], "DI_pos = ", adx.DI_positive[-1], "DX = ", adx.DX[-1], file=sys.stderr) candle_17 = Candle("USDT_ETH", 26.52, 25.43, 26.01) adx.feed([candle_16, candle_17]) #print("TR = ", adx.TR[-1], "DI_pos = ", adx.DI_positive[-1], "DX = ", adx.DX[-1], file=sys.stderr) candle_18 = Candle("USDT_ETH", 26.52, 25.35, 25.46) adx.feed([candle_17, candle_18]) #print("TR = ", adx.TR[-1], "DI_pos = ", adx.DI_positive[-1], "DX = ", adx.DX[-1], file=sys.stderr) candle_19 = Candle("USDT_ETH", 27.09, 25.88, 27.03) adx.feed([candle_18, candle_19]) #print("TR = ", adx.TR[-1], "DI_pos = ", adx.DI_positive[-1], "DX = ", adx.DX[-1], file=sys.stderr) candle_20 = Candle("USDT_ETH", 27.69, 26.96, 27.45) adx.feed([candle_19, candle_20]) #print("TR = ", adx.TR[-1], "DI_pos = ", adx.DI_positive[-1], "DX = ", adx.DX[-1], file=sys.stderr) candle_21 = Candle("USDT_ETH", 27.69, 26.96, 28.36) adx.feed([candle_20, candle_21]) #print("TR = ", adx.TR[-1], "DI_pos = ", adx.DI_positive[-1], "DX = ", adx.DX[-1], file=sys.stderr) candle_22 = Candle("USDT_ETH", 28.53, 28.01, 28.43) adx.feed([candle_21, candle_22]) #print("TR = ", adx.TR[-1], "DI_pos = ", adx.DI_positive[-1], "DX = ", adx.DX[-1], file=sys.stderr) candle_23 = Candle("USDT_ETH", 28.67, 27.88, 27.95) adx.feed([candle_22, candle_23]) #print("TR = ", adx.TR[-1], "DI_pos = ", adx.DI_positive[-1], "DX = ", adx.DX[-1], file=sys.stderr) candle_24 = Candle("USDT_ETH", 29.01, 27.99, 29.01) adx.feed([candle_23, candle_24]) #print("TR = ", adx.TR[-1], "DI_pos = ", adx.DI_positive[-1], "DX = ", adx.DX[-1], file=sys.stderr) candle_25 = Candle("USDT_ETH", 29.87, 28.76, 29.38) adx.feed([candle_24, candle_25]) #print("TR = ", adx.TR[-1], "DI_pos = ", adx.DI_positive[-1], "DX = ", adx.DX[-1], file=sys.stderr) candle_26 = Candle("USDT_ETH", 29.80, 29.14, 29.36) adx.feed([candle_25, candle_26]) candle_27 = Candle("USDT_ETH", 29.75, 28.71, 28.91) adx.feed([candle_26, candle_27]) candle_28 = Candle("USDT_ETH", 30.65, 28.93, 30.61) adx.feed([candle_27, candle_28])
def __debug_print_which_indicator_triggered(self, all_candles, stockpile, indicators, msg): if indicators.BB.buy_indicator: print("BB_buy indicator triggered", file=sys.stderr) if indicators.BB.sell_indicator: print("BB_ sell _indicator_ triggered", file=sys.stderr) if indicators.BB.BB_indicator: print("BB indicator triggered", file=sys.stderr) if indicators.stochastic.buy_indicator: print("stochastic buy indicator triggered", file=sys.stderr) if indicators.stochastic.sell_indicator: print("stochastic sell indicator triggered", file=sys.stderr) if indicators.MACD.buy_indicator: print("MACD buy indicator triggered", file=sys.stderr) if indicators.MACD.sell_indicator: print("MACD sell indicator triggered", file=sys.stderr) if indicators.trend == Trend.UPWARD: print("TREND UPWARD ", file=sys.stderr) if indicators.trend == Trend.DOWNWARD: print("TREND DOWNWARD ", file=sys.stderr) if indicators.RSI.buy_indicator: print("RSI buy indicator triggered", file=sys.stderr) if indicators.RSI.sell_indicator: print("RSI sell indicator triggered", file=sys.stderr) if indicators.ADX.buy_indicator: print("ADX buy indicator triggered", file=sys.stderr) if indicators.ADX.sell_indicator: print("ADX sell indicator triggered", file=sys.stderr) if indicators.overbought: print("Overbought triggered", file=sys.stderr) if indicators.oversold: print("Oversold triggered", file=sys.stderr) if indicators.possible_overbought: print("possible Overbought triggered", file=sys.stderr) if indicators.possible_oversold: print("possible Oversold triggered", file=sys.stderr) print("MSG = ", msg, file=sys.stderr) print("sto_D = ", indicators.stochastic.stochastic_D[-20:], file=sys.stderr) print("RSI = ", indicators.RSI.RSI[-20:], file=sys.stderr) print("ADX strength : ", indicators.ADX.ADX[-1], file=sys.stderr) print("DI+ strength : ", indicators.ADX.DI_positive[-1], file=sys.stderr) print("DI- strength : ", indicators.ADX.DI_negative[-1], file=sys.stderr) print("current USDT =", stockpile.USDT, " current ETH = ", stockpile.ETH, "current BTC = ", stockpile.BTC, file=sys.stderr) last_close = Candle.select_last_candles(all_candles, "BTC_ETH", 1)[-1].close print("diff SMA_160 - close : ", indicators.SMA.SMA_160_diff, file=sys.stderr) print("diff EMA_80 - close : ", last_close - indicators.EMA.EMA_80[-1], file=sys.stderr) print("diff SMA_50 - close : ", last_close - indicators.SMA.SMA_50[-1], file=sys.stderr) print("", file=sys.stderr)