Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. import requests
  2. from datetime import datetime
  3. import time
  4.  
  5. class Bot:
  6. def __init__(self):
  7. self.url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest'
  8. self.params = {
  9. 'start': '1',
  10. 'limit': '100',
  11. 'convert': 'USD'
  12. }
  13. self.headers = {
  14. 'Accepts': 'application/json',
  15. 'X-CMC_PRO_API_KEY': '85de4284-57df-41b0-aaf9-4c4e56738e4e',
  16. }
  17. self.orders = []
  18.  
  19. def fetchCurrenciesData(self):
  20. r = requests.get(url=self.url, headers=self.headers, params=self.params).json()
  21. return r['data']
  22.  
  23. def canBuy(self):
  24. #controlla se esistono operazioni di acquisto ancora non chiuse
  25. for order in self.orders:
  26. if order['status'] == 'open':
  27. return False
  28. return True
  29.  
  30. impactBot = Bot()
  31.  
  32. while(1):
  33. now = datetime.now()
  34. currencies = impactBot.fetchCurrenciesData()
  35. i = 1 #incremento in percentuale
  36. r = 4 #valore sopra il quale fare partire l'operazione di acquisto
  37. n = 0 #numero di valute cui prezzo ha subito un incremento maggiore di i nell'ultima ora
  38. z = -1#percentuale sotto la quale vendi la valuta
  39. bestCurrency = None #la valuta con la rivalutazione di prezzo maggiore
  40.  
  41. #logic
  42. if impactBot.canBuy():
  43. print(f'Non ci sono posizioni aperte - Controllo se trovo valute che hanno guadagnato più di {i}% nella ultima ora')
  44. #il codice qui dentro viene eseguito solo se non ci sono operazioni aperte
  45. for currency in currencies:
  46. if not bestCurrency or currency['quote']['USD']['price'] > bestCurrency.get('price', 0):
  47. bestCurrency = currency
  48. if currency['quote']['USD']['price'] > i:
  49. n = n +1
  50. if n > 4:
  51. print('Ho trovato più di quattro valute - Creo un nuovo ordine')
  52. newOrder = {
  53. 'datetime': now,
  54. 'symbol': currency['symbol'],
  55. 'enterPrice': currency['quote']['USD']['price'], #prezzo a cui abbiamo acquistato
  56. 'exitPrice': None,
  57. 'status': 'open'
  58. }
  59. impactBot.orders.append(newOrder)
  60. else:
  61. print('Controllo gli ordini ancora aperti - Se si verifica la condizione di svalutazione allora vendo')
  62. for currency in currencies:
  63. if currency['quote']['USD']['percent_change_1h'] < -1:
  64. for order in impactBot.orders:
  65. if order['status'] == 'open' and order['symbol'] == currency['symbol']:
  66. #vendi
  67. order['status'] = 'close'
  68. order['exitPrice'] = currency['quote']['USD']['price']
  69.  
  70.  
  71. #overview
  72. initialAmount = 10000
  73. profit = 0
  74. for order in impactBot.orders:
  75. if order['status'] == 'close':
  76. profit += initialAmount * order['exitPrice'] / order['enterPrice']
  77. finalAmount = initialAmount + profit
  78. print(f'Ho realizzato {len(impactBot.orders)} compravendite - Sono partito con {initialAmount}€ e adesso ne ho {finalAmount} ')
  79.  
  80. #routine
  81. minutes = 10
  82. seconds = minutes * 60
  83. time.sleep(seconds)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement