Thread(target=load_candles, args=(sym,)).start() while len(candles) < len(symbols): print('%s/%s loaded' %(len(candles), len(symbols)), end='\r', flush=True) time.sleep(0.1) # calculate EMAs for each symbol print('Calculating EMAs...') for sym in candles: for period in EMA_PERIODS: iEMA = EMA([period]) lst_candles = candles[sym][:] for c in lst_candles: iEMA.add(c['close']) if sym not in ema_values: ema_values[sym] = {} ema_values[sym][period] = iEMA.v() # save filtered EMA results in txt files print('Saving filtered EMA results to txt files...') for sym in ema_values: ema_50 = ema_values[sym][50] ema_200 = ema_values[sym][200] price = prices[sym] entry = '' if price < ema_50: # save symbols trading below EMA (50) f = open('results/below_50.txt', 'a') entry = '%s: $%s\n' %(sym, round(price,3)) f.write(entry) elif price > ema_50 and price < ema_200: # save symbols trading above EMA(200)
def run_extract_candles(): # create results folder if it doesn't exist if not os.path.exists('results/'): os.makedirs('results/') # start with blank files open('results/below_55.txt', 'w').close() open('results/above_55_below_200.txt', 'w').close() open('results/above_200.txt', 'w').close() # load symbols information print('Getting list of BTC trade pairs...') resp = requests.get(BASE_URL + '/api/v1/ticker/allBookTickers') #print(resp.content) tickers_list = json.loads(resp.content) for ticker in tickers_list: if str(ticker['symbol'])[-3:] == 'BTC': symbols.append(ticker['symbol']) # get 4h candles for symbols print('Loading candle data for symbols...') for sym in symbols: #print('BINANCE:' + sym) Thread(target=load_candles, args=(sym,)).start() while len(candles) < len(symbols): print('%s/%s loaded' %(len(candles), len(symbols)), end='\r', flush=True) time.sleep(0.1) # calculate EMAs for each symbol print('Calculating EMAs...') for sym in candles: for period in EMA_PERIODS: iEMA = EMA(period) lst_candles = candles[sym][:] for c in lst_candles: iEMA.add(c['close']) if sym not in ema_values: ema_values[sym] = {} ema_values[sym][period] = iEMA.v() # save filtered EMA results in txt files print('Saving filtered EMA results to txt files...') for sym in ema_values: ema_55 = ema_values[sym][55] ema_200 = ema_values[sym][200] price = prices[sym] entry = '' if price < ema_200: # save symbols trading below EMA (50) f = open('results/below_55.txt', 'a') entry = '%s: $%s\n' %(sym, round(price,3)) f.write(entry) elif price > ema_55 and price < ema_200: # save symbols trading above EMA(200) f = open('results/above_55_below_200.txt', 'a') entry = '%s: $%s\n' %(sym, round(price,3)) f.write(entry) elif price > ema_200: # save symbols trading above EMA(50) but below EMA(200) f = open('results/above_200.txt', 'a') entry = '%s: $%s\n' %(sym, round(price,3)) f.write(entry) f.close() del f # cleanup print('All done! Results saved in results folder.')
Thread(target=import_candles, args=(sym,)).start() # wait until all candles are loaded while go_on == False: time.sleep(1) print('\nAll candles loaded.') # calculate EMA values print('Calculating EMA values and scanning for crosses...', end='', flush=True) for sym in symbols: for period in ema_periods: iEMA = EMA([period]) # define EMA object for candle in candles[sym]: iEMA.add(candle['close']) # add all close prices lst_ema = [] lst_ema.append(iEMA.v()) # add current EMA value for i in range(historic_window): # add historic EMA values lst_ema.append(iEMA.prev(i+1)) if sym not in ema_values: # add symbol key to dictionary ema_values[sym] = {} ema_values[sym][period] = lst_ema # save EMA values # identify EMA crosses ema_results = { 'cross-downs': [], 'cross-ups': [] } for sym in symbols: # get primary and secondary EMA lists, and reverse for oldest first ema_first = ema_values[sym][ema_periods[0]][:]