Advertisement
Guest User

Untitled

a guest
Apr 4th, 2020
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.25 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': 'LA TUA CHIAVE',
  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.     print(currencies)
  36.     i = 1 #incremento in percentuale
  37.     r = 4 #valore sopra il quale fare partire l'operazione di acquisto
  38.     n = 0 #numero di valute cui prezzo ha subito un incremento maggiore di i nell'ultima ora
  39.     z = -1#percentuale sotto la quale vendi la valuta
  40.     bestCurrency = None #la valuta con la rivalutazione di prezzo maggiore
  41.  
  42.     #logic
  43.     if impactBot.canBuy():
  44.         print(f'Non ci sono posizioni aperte - Controllo se trovo valute che hanno guadagnato più di {i}% nella ultima ora')
  45.         #il codice qui dentro viene eseguito solo se non ci sono operazioni aperte
  46.         for currency in currencies:
  47.             if not bestCurrency or currency['quote']['USD']['percent_change_1h'] > bestCurrency['quote']['USD']['percent_change_1h']:
  48.                 bestCurrency = currency
  49.             if currency['quote']['USD']['percent_change_1h'] > i:
  50.                 n = n +1
  51.         if n > 4:
  52.             print('Ho trovato più di quattro valute - Creo un nuovo ordine')
  53.             newOrder = {
  54.                 'datetime': now,
  55.                 'symbol': currency['symbol'],
  56.                 'enterPrice': currency['quote']['USD']['price'], #prezzo a cui abbiamo acquistato
  57.                 'exitPrice': None,
  58.                 'status': 'open'
  59.             }
  60.             impactBot.orders.append(newOrder)
  61.     else:
  62.         print('Controllo gli ordini ancora aperti - Se si verifica la condizione di svalutazione allora vendo')
  63.         for currency in currencies:
  64.             if currency['quote']['USD']['percent_change_1h'] < -1:
  65.                 for order in impactBot.orders:
  66.                     if order['status'] == 'open' and order['symbol'] == currency['symbol']:
  67.                         #vendi
  68.                         order['status'] = 'close'
  69.                         order['exitPrice'] = currency['quote']['USD']['price']
  70.  
  71.  
  72.     #overview
  73.     initialAmount = 10000
  74.     profit = 0
  75.     for order in impactBot.orders:
  76.         if order['status'] == 'close':
  77.             profit += initialAmount * order['exitPrice'] / order['enterPrice']
  78.     finalAmount = initialAmount + profit
  79.     print(f'Ho realizzato {len(impactBot.orders)} compravendite - Sono partito con {initialAmount}€ e adesso ne ho {finalAmount} ')
  80.  
  81.     #routine
  82.     minutes = 10
  83.     seconds = minutes * 60
  84.     time.sleep(seconds)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement