Guest User

Untitled

a guest
Jan 23rd, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Sat Apr 7 20:47:10 2018
  4. @author: troymcfont
  5. """
  6.  
  7. # Imports
  8. from telegram_helper import telegramBot
  9. from discord_helper import discordWebhook
  10. from binance_client.client import Client as BinanceClient
  11. from nuats_ta import Candlestick, NuatsTA
  12. import time
  13. import config
  14. from tqdm import *
  15. from concurrent.futures import ThreadPoolExecutor
  16.  
  17.  
  18. # Functions
  19. def request_tickers():
  20. tickers = []
  21. bclient = BinanceClient().get_exchange_info()
  22. for element in bclient['symbols']:
  23. tickers.append(element['symbol'])
  24. return tickers
  25.  
  26.  
  27. def broadcast_signals(tickers_TA_list):
  28. all_notifications_list = list(filter(None.__ne__, tickers_TA_list)) # Filter out tickers with no bull/bear notifications
  29. all_notifications_list.sort()
  30. ntb_str = '\nSignals: \n'
  31. for ticker_notifications in all_notifications_list:
  32. for notification in ticker_notifications:
  33. ntb_str += str(notification) + '\n'
  34.  
  35. print(ntb_str)
  36.  
  37. print('Telegram broadcasting: \n {}'.format(telegram_chat_ids))
  38. print('Discord broadcasting: \n {}'.format(discord_webhooks))
  39.  
  40. telegramBot(token=telegram_token, chat_ids=telegram_chat_ids).broadcast_message(ntb_str)
  41. discordWebhook(urls=discord_webhooks).broadcast_message(ntb_str)
  42.  
  43. def TA_task(a):
  44. ticker, interval = a[0], a[1]
  45. bclient = BinanceClient()
  46.  
  47. if start_time == '':
  48. klines = bclient.get_last_klines(ticker, interval, n_periods)
  49. elif start_time != '' and end_time != '':
  50. klines = bclient.get_historical_klines(ticker, interval, start_time, end_time)
  51. else:
  52. raise ValueError('Error on configuration. Check n_periods and/or start_time and end_time. Look at README.md for more details')
  53.  
  54. candles = []
  55. if len(klines) > 0:
  56. for kline in klines:
  57. candles.append(Candlestick(kline))
  58. nta = NuatsTA(ticker, interval, candles)
  59. # print("{} {}".format(ticker, interval), sep=' ', end='\n', flush=True)
  60. return nta.analyse()
  61.  
  62.  
  63.  
  64. # Get configuration from config.py
  65. tickers = config.kline['tickers']
  66. intervals = config.kline['intervals']
  67. n_periods = config.kline['n_periods']
  68. start_time = config.kline['start_time']
  69. end_time = config.kline['end_time']
  70. sleep = config.live_bot['sleep']
  71. threading = config.live_bot['threading']
  72. live = config.live_bot['live']
  73. telegram_token = config.live_bot['telegram_token']
  74. telegram_chat_ids = config.live_bot['telegram_chat_id']
  75. discord_webhooks = config.live_bot['discord_web_hook']
  76.  
  77.  
  78. def main():
  79. global tickers, intervals, n_periods, start_time, end_time, sleep, threading, live, telegram_token, telegram_chat_ids, discord_webhooks
  80.  
  81. # Get the tickers to analyse
  82. if tickers is None or not tickers:
  83. tickers = request_tickers() # This will request tickers from Binance.
  84. # Generate a list of tuples containing (ticker, interval)
  85. a = []
  86. for ticker in tickers:
  87. for interval in intervals:
  88. a.append((ticker, interval))
  89.  
  90. # Loop over tickers and intervals to get signals (threading or serial)
  91. tickers_TA_list = []
  92. if threading and live:
  93. while True:
  94. with ThreadPoolExecutor(max_workers=16) as executor:
  95. notifications_list = list(tqdm(executor.map(TA_task, a), total=len(a)))
  96. if (len(notifications_list) > 0): broadcast_signals(notifications_list)
  97. print('Sleeping {} sec now ... \n'.format(sleep))
  98. time.sleep(sleep)
  99.  
  100. elif not threading and live:
  101. while True:
  102. with tqdm(total=len(a)) as pbar:
  103. for tup in a:
  104. tickers_TA_list.append(TA_task(tup))
  105. pbar.update(1)
  106. if (len(tickers_TA_list) > 0): broadcast_signals(tickers_TA_list)
  107. print('Sleeping {} sec now ... \n'.format(sleep))
  108. time.sleep(sleep)
  109.  
  110. elif threading and not live:
  111. with ThreadPoolExecutor(max_workers=16) as executor:
  112. tickers_TA_list = list(tqdm(executor.map(TA_task, a), total=len(a)))
  113.  
  114. elif not threading and not live:
  115. with tqdm(total=len(a)) as pbar:
  116. for tup in a:
  117. tickers_TA_list.append(TA_task(tup))
  118. pbar.update(1)
  119.  
  120. if (len(tickers_TA_list) > 0): broadcast_signals(tickers_TA_list)
  121.  
  122.  
  123. if __name__ == '__main__':
  124. main()
Add Comment
Please, Sign In to add comment