├── config.py └── main.py /config.py: -------------------------------------------------------------------------------- 1 | api_key="" 2 | api_secret="" 3 | 4 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | #we will start with some defining some constant like 2 | import requests 3 | import numpy as np 4 | import talib #talib library for various calculation related to financial and technical indicators 5 | from binance.client import Client #importing client 6 | from config import api_key , api_secret 7 | 8 | 9 | client = Client(api_key, api_secret) #it takes two parameter, first one api_key and second api_secret_key, i will define that in configuration file 10 | 11 | 12 | 13 | SYMBOL = "BTCUSDT" #taking this as a example 14 | TIME_PERIOD= "15m" #taking 15 minute time period 15 | LIMIT = "200" # taking 200 candles as limit 16 | QNTY = 0.006 # we will define quantity over here 17 | 18 | #Now we will write function to get data from binance to work with 19 | # for that we will need to import requests library to fetch data 20 | 21 | 22 | def place_order(order_type): 23 | #order type could be buy or sell 24 | # before that , we have to initialize the binance client 25 | if(order_type == "buy"): 26 | # we will place buy order 27 | order = client.create_order(symbol=SYMBOL, side="buy",quantity=QNTY,type="MARKET") # for type i am going with market order, so whatever price at market. it will place order 28 | else: 29 | order = client.create_order(symbol=SYMBOL, side="sell", quantity=QNTY,type="MARKET") # same thing but changed side 30 | 31 | print("order placed successfully!") 32 | print(order) 33 | return 34 | 35 | #now , you will need to define api_key and api-secret in the config file like this 36 | 37 | 38 | #function to get data from binance 39 | def get_data(): 40 | url = "https://api.binance.com/api/v3/klines?symbol={}&interval={}&limit={}".format(SYMBOL, TIME_PERIOD, LIMIT) 41 | res = requests.get(url) 42 | #now we can either save the response or convert it to numpy array , converting is more reasonable 43 | return_data = [] 44 | for each in res.json(): 45 | return_data.append(float(each[4])) 46 | return np.array(return_data) 47 | 48 | 49 | #now we have function to get data from binance, now we need to calculate ema. for calculating ema, we are going to use 50 | # talib library, to install it do this 51 | 52 | 53 | #define main entry point for the script 54 | def main(): 55 | buy = False #it means we are yet to buy and have not bought 56 | sell = True #we have not sold , but if you want to buy first then set it to True 57 | ema_8 = None #starting with None 58 | ema_21 = None #starting with None 59 | 60 | #we also need to store the last variables that was the value for the ema_8 and ema_21, so we can compare 61 | last_ema_8 = None 62 | last_ema_21 = None 63 | 64 | print("started running..") 65 | #the script will run continously and fetch latest data from binance 66 | while True: 67 | closing_data = get_data() #get latest closing data for the candles 68 | #now feed the data to the Ema function of talib 69 | # i am using ema crossover strategy, in which i am using timeframe 8,21 70 | # 8 for smaller ema line and 21 for larger 71 | ema_8 = talib.EMA(closing_data,8)[-1] #data and timeperiod are the two parameters that the function takes 72 | ema_21 = talib.EMA(closing_data, 21)[-1] #same as the last one 73 | #now, let's see if values are correct or not 74 | #print("ema 8", ema_8[-1]) 75 | #print("ema_21",ema_21[-1]) #ema_21 returns whole array, but we are only interested in last value 76 | #now we will check it against real values, 77 | 78 | #now , the last thing we need to do is get the order going. we will create function to place 79 | #order 80 | 81 | #logic for buy and sell 82 | # also one thing 83 | if(ema_8 > ema_21 and last_ema_8): #we have to check if the value of ema_8 crossed ema_21 or not 84 | if(last_ema_8 < last_ema_21 and not buy): # to check if previously, it was below of ema_21 and we haven't already bought it 85 | print("buy logic goes here") 86 | buy = True 87 | sell = False #switch the values for next order 88 | 89 | if(ema_21 > ema_8 and last_ema_21): #to check if ema_8 got down from top to bottom 90 | if(last_ema_21 < last_ema_8 and not sell): # to check if previously it was above ema_21 91 | print("sell logic goes here") 92 | sell = True 93 | buy = False #switching values for next order 94 | 95 | #at last we are setting the current values as last one 96 | last_ema_8 = ema_8 97 | last_ema_21 = ema_21 98 | #return 99 | 100 | if __name__ == "__main__": 101 | main() 102 | --------------------------------------------------------------------------------