├── config.py └── bot.py /config.py: -------------------------------------------------------------------------------- 1 | API_KEY = 'YOUR_KEY' 2 | API_SECRET = 'YOUR_SECRET' 3 | -------------------------------------------------------------------------------- /bot.py: -------------------------------------------------------------------------------- 1 | import config 2 | from binance.client import Client 3 | from binance.enums import * 4 | import time 5 | import datetime 6 | import numpy as np 7 | 8 | client = Client(config.API_KEY, config.API_SECRET, tld='com') 9 | symbolTicker = 'BTCBUSD' 10 | quantityOrders = 0.0013 11 | 12 | def tendencia(): 13 | x = [] 14 | y = [] 15 | sum = 0 16 | ma50_i = 0 17 | 18 | resp = False 19 | 20 | klines = client.get_historical_klines(symbolTicker, Client.KLINE_INTERVAL_15MINUTE, "18 hour ago UTC") 21 | 22 | if (len(klines) != 72): 23 | return False 24 | for i in range (56,72): 25 | for j in range (i-50,i): 26 | sum = sum + float(klines[i][4]) 27 | ma50_i = round(sum / 50,2) 28 | sum = 0 29 | x.append(i) 30 | y.append(float(ma50_i)) 31 | 32 | modelo = np.polyfit(x,y,1) 33 | if (modelo[0]>0): 34 | resp = True 35 | 36 | return resp 37 | 38 | def _ma50_(): 39 | 40 | ma50_local = 0 41 | sum = 0 42 | 43 | klines = client.get_historical_klines(symbolTicker, Client.KLINE_INTERVAL_15MINUTE, "15 hour ago UTC") 44 | 45 | if (len(klines)==60): 46 | for i in range (10,60): 47 | sum = sum + float(klines[i][4]) 48 | ma50_local = sum / 50 49 | 50 | return ma50_local 51 | 52 | while 1: 53 | 54 | orders = client.get_open_orders(symbol=symbolTicker) 55 | 56 | if (len(orders) != 0): 57 | print("THERE IS OPEN ORDERS") 58 | time.sleep(10) 59 | continue 60 | 61 | # get price 62 | list_of_tickers = client.get_all_tickers() 63 | for tick_2 in list_of_tickers: 64 | if tick_2['symbol'] == symbolTicker: 65 | symbolPrice = float(tick_2['price']) 66 | # get price 67 | 68 | ma50 = _ma50_() 69 | if (ma50 == 0): continue 70 | 71 | print("***** " + symbolTicker + " *******") 72 | print("Actual MA50 " + str(round(ma50,2))) 73 | print("Actual Price " + str(round(symbolPrice,2))) 74 | print("Price to Buy " + str(round(ma50*0.995,2))) 75 | 76 | if (not tendencia()): 77 | print ("TENDENCIA BAJISTA") 78 | time.sleep(10) 79 | continue 80 | else: 81 | print ("ALCISTA") 82 | 83 | if (symbolPrice < ma50*0.995): 84 | print ("BUY") 85 | 86 | order = client.order_market_buy( 87 | symbol = symbolTicker, 88 | quantity = quantityOrders 89 | ) 90 | 91 | time.sleep(5) 92 | 93 | orderOCO = client.order_oco_sell( 94 | symbol = symbolTicker, 95 | quantity = quantityOrders, 96 | price = round(symbolPrice*1.02,2), 97 | stopPrice = round(symbolPrice*0.995,2), 98 | stopLimitPrice = round(symbolPrice*0.994,2), 99 | stopLimitTimeInForce = 'GTC' 100 | ) 101 | time.sleep(20) 102 | --------------------------------------------------------------------------------