From 2c82a16cdc62f796736d20826380ac3bded64ea2 Mon Sep 17 00:00:00 2001 From: stevenhgs <48204700+stevenhgs@users.noreply.github.com> Date: Sun, 29 Aug 2021 11:40:23 +0200 Subject: [PATCH] Improve execution time from 0.34 to 0.22s --- main.py | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/main.py b/main.py index aa65b9d..eaae890 100644 --- a/main.py +++ b/main.py @@ -2,6 +2,7 @@ from store_order import * from load_config import * +from collections import defaultdict from datetime import datetime, time import time @@ -20,12 +21,33 @@ def get_all_coins(): return client.get_all_tickers() -def get_new_coins(all_coins): +def generate_coin_seen_dict(all_coins): """ - Returns new coins and the new coin list + This method should be used once before starting the loop. + The value for every coin detected before the loop is set to True in the coin_seen_dict. + All the new coins detected during the loop will have a value of False. """ + coin_seen_dict = defaultdict(bool) + for old_coin in all_coins: + coin_seen_dict[old_coin['symbol']] = True + return coin_seen_dict + + +def get_new_coins(coin_seen_dict): + """ + This method checks if there are new coins listed and returns them in a list. + The value of the new coins in coin_seen_dict will be set to True to make them not get detected again. + """ + result = [] all_coins_recheck = get_all_coins() - return [new_coins for new_coins in all_coins_recheck if new_coins['symbol'] not in [coin['symbol'] for coin in all_coins]], all_coins_recheck + + for new_coin in all_coins_recheck: + if not coin_seen_dict[new_coin['symbol']]: + result += [new_coin] + # this line ensures the new coin isn't detected again + coin_seen_dict[new_coin['symbol']] = True + + return result def get_price(coin, pairing): @@ -52,6 +74,7 @@ def main(): test_mode = config['TRADE_OPTIONS']['TEST'] all_coins = get_all_coins() + coin_seen_dict = generate_coin_seen_dict(all_coins) while True: try: @@ -136,13 +159,12 @@ def main(): else: order = {} - # store new coins and rechecked coins list here - new_coins, all_coins_recheck = get_new_coins(all_coins) + # check if new coins are listed + new_coins = get_new_coins(coin_seen_dict) # the buy block and logic pass if len(new_coins) > 0: - all_coins = all_coins_recheck print(f'New coins detected: {new_coins}') for coin in new_coins: