Skip to content

Commit

Permalink
Improve execution time from 0.34 to 0.22s
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenhgs authored Aug 29, 2021
1 parent 427780f commit 2c82a16
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from store_order import *
from load_config import *

from collections import defaultdict
from datetime import datetime, time
import time

Expand All @@ -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):
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 2c82a16

Please sign in to comment.