def main(argv): startTime = False endTime = False forwardTest = True movingAverageLength = 20 try: opts, args = getopt.getopt( argv, "hp:c:n:s:e", ["period=", "currency=", "exchange=", "live"]) except getopt.GetoptError: print('trading-bot.py -p <period length> -c <currency pair>') sys.exit(2) for opt, arg in opts: if opt == '-h': print('trading-bot.py -p <period length> -c <currency pair>') sys.exit() elif opt in ("-s"): startTime = float(arg) elif opt in ("-e"): endTime = float(arg) elif opt in ("-p", "--period"): period = int(arg) elif opt in ("-c", "--currency"): pair = str(arg) shared.exchange['pair'] = pair shared.exchange['market'] = pair.split("_")[0] shared.exchange['coin'] = pair.split("_")[1] elif opt in ("--exchange"): if str(arg) not in ['', 'poloniex', 'kraken']: print("Only poloniex and kraken are supported for now") sys.exit() exchange = str(arg) shared.exchange['name'] = exchange elif opt == "--live": print( "You're going live... All losts are your reponsability only!") forwardTest = False if shared.exchange['name'] == "kraken": shared.exchange['pair'] = str(shared.exchange['coin']) + str( shared.exchange['market']) # startTime specified: we are in backtest mode if (startTime): chart = BotChart(period, startTime, endTime) strategy = BotStrategy() strategy.showPortfolio() for candlestick in chart.getPoints(): strategy.tick(candlestick) chart.drawChart(strategy.candlesticks, strategy.movingAverages, strategy.trades) strategy.showPortfolio() else: chart = BotChart(period, False, False, False) strategy = BotStrategy(False, forwardTest) strategy.showPortfolio() candlestick = BotCandlestick(float(period)) x = 0 while True: try: currentPrice = chart.getCurrentPrice() except Exception as e: print(e) print("Error fetching current price") return try: candlestick.tick(currentPrice) except Exception as e: print(e) print("Error fetching tick") return strategy.tick(candlestick) drawingCandles = copy.copy(strategy.candlesticks) if not candlestick.isClosed(): drawingCandles.append(copy.copy(candlestick)) drawingCandles[-1].close = candlestick.currentPrice chart.drawChart(drawingCandles, strategy.movingAverages, strategy.trades) if candlestick.isClosed(): candlestick = BotCandlestick(float(period)) x += 1 time.sleep(int(10))
def main(argv): startTime = False endTime = False live = False movingAverageLength = 20 try: opts, args = getopt.getopt(argv,"ht:c:n:s:e",["timeframe=","currency=","exchange=","live"]) except getopt.GetoptError: print('trading-bot.py -t <timeframe> -c <currency pair>') sys.exit(2) for opt, arg in opts: if opt == '-h': print('trading-bot.py -t <timeframe> -c <currency pair>') sys.exit() elif opt in ("-s"): startTime = str(arg) elif opt in ("-e"): endTime = str(arg) elif opt in ("-t", "--timeframe"): timeframe = str(arg) shared.strategy['timeframe'] = timeframe elif opt in ("-c", "--currency"): pair = str(arg) shared.exchange['pair'] = pair shared.exchange['market'] = pair.split("/")[1] shared.exchange['coin'] = pair.split("/")[0] elif opt in ("--exchange"): exchange = str(arg) shared.exchange['name'] = exchange elif opt == "--live": print("You're going live... All loss are your reponsability only!") live = True # startTime specified: we are in backtest mode if (startTime): chart = BotChart(timeframe, startTime, endTime) strategy = BotStrategy() strategy.showPortfolio() for candlestick in chart.getPoints(): strategy.tick(candlestick) chart.drawChart(strategy.candlesticks, strategy.trades, strategy.movingAverages) strategy.showPortfolio() else: chart = BotChart(timeframe, False, False, False) strategy = BotStrategy(False, live) strategy.showPortfolio() candlestick = BotCandlestick() x = 0 while True: try: currentPrice = chart.getCurrentPrice() candlestick.tick(currentPrice) strategy.tick(candlestick) except ccxt.NetworkError as e: print(type(e).__name__, e.args, , 'Exchange error (ignoring)') except ccxt.ExchangeError as e: print(type(e).__name__, e.args, , 'Exchange error (ignoring)') except ccxt.DDoSProtection as e: print(type(e).__name__, e.args, 'DDoS Protection (ignoring)') except ccxt.RequestTimeout as e: print(type(e).__name__, e.args, 'Request Timeout (ignoring)') except ccxt.ExchangeNotAvailable as e: print(type(e).__name__, e.args, 'Exchange Not Available due to downtime or maintenance (ignoring)') except ccxt.AuthenticationError as e: print(type(e).__name__, e.args, 'Authentication Error (missing API keys, ignoring)') drawingCandles = copy.copy(strategy.candlesticks) if not candlestick.isClosed(): drawingCandles.append(copy.copy(candlestick)) drawingCandles[-1].close = candlestick.currentPrice chart.drawChart(drawingCandles, strategy.trades, strategy.movingAverages) if candlestick.isClosed(): candlestick = BotCandlestick() x+=1 time.sleep(int(10))
def main(argv): live = False try: opts, args = getopt.getopt(argv, "", ["live"]) except getopt.GetoptError: print('trading-bot.py') sys.exit(2) for opt, arg in opts: if opt == "--live": print("You're going live... Losses are your responsibility only!") live = True # START_DATE specified: we are in backtest mode if shared.strategy['start_date']: chart = BotChart() strategy = BotStrategy() strategy.showPortfolio() for candlestick in chart.getPoints(): strategy.tick(candlestick) chart.drawChart(strategy.candlesticks, strategy.trades, strategy.movingAverages) strategy.showPortfolio() else: chart = BotChart(False) strategy = BotStrategy(False, live) strategy.showPortfolio() candlestick = BotCandlestick() x = 0 while True: try: currentPrice = chart.getCurrentPrice() candlestick.tick(currentPrice) strategy.tick(candlestick) except ccxt.NetworkError as e: print(type(e).__name__, e.args, 'Exchange error (ignoring)') except ccxt.ExchangeError as e: print(type(e).__name__, e.args, 'Exchange error (ignoring)') except ccxt.DDoSProtection as e: print(type(e).__name__, e.args, 'DDoS Protection (ignoring)') except ccxt.RequestTimeout as e: print(type(e).__name__, e.args, 'Request Timeout (ignoring)') except ccxt.ExchangeNotAvailable as e: print( type(e).__name__, e.args, 'Exchange Not Available due to downtime or maintenance (ignoring)' ) except ccxt.AuthenticationError as e: print( type(e).__name__, e.args, 'Authentication Error (missing API keys, ignoring)') drawingCandles = copy.copy(strategy.candlesticks) if not candlestick.isClosed(): drawingCandles.append(copy.copy(candlestick)) drawingCandles[-1].close = candlestick.currentPrice chart.drawChart(drawingCandles, strategy.trades, strategy.movingAverages) if candlestick.isClosed(): candlestick = BotCandlestick() x += 1 time.sleep(shared.exchange['interval'])