Skip to content

Instantly share code, notes, and snippets.

@SajidLhessani
Created January 21, 2024 05:50
Show Gist options
  • Save SajidLhessani/12f13ae17dd8898738f7822738aba788 to your computer and use it in GitHub Desktop.
Save SajidLhessani/12f13ae17dd8898738f7822738aba788 to your computer and use it in GitHub Desktop.
#Neccessary librairies
from binance import Client
import pandas as pd
import numpy as np
import talib as ta
import time
#Insert your secret key
key = 'AsEGPnZK7hpF7DFUb4Bdsmd5nTf1q6qQ1ULhqlaafdPWl0T1l6bbTm6QyuTrOJeP'
secret = '640p8wsE9cOUmJ7qH2RkleRzOn8HCdlXE2r4uZctLgP25Cp4lT9j3cCrAsrslKUx'
#Check the connection with your binance account
client = Client(api_key=key, api_secret = secret)
#Get live data function
def getdata(symbol, interval, past):
frame = pd.DataFrame(client.get_historical_klines(symbol,interval,past + ' min ago UTC'))
frame = frame.iloc[:,:5]
frame.columns = ['Time','Open','High','Low','Close']
frame = frame.set_index('Time')
frame.index = pd.to_datetime(frame.index, unit = 'ms')
frame = frame.astype(float)
return frame
#Function to calculate our "calculated fields"
def algocalculation(df):
df['rsi'] = ta.RSI(np.array(df['Close']),timeperiod=14)
df.dropna(inplace=True)
#Strategy final loop
def strategy(pair,interval,past, qty,open_position = False):
df = getdata(pair,interval, past)
algocalculation(df)
print(f'Current Close Price is ' + str(df.Close.iloc[-1]))
print(f'Current RSI value is ' + str(df.rsi.iloc[-1]))
if df.rsi.iloc[-1] <60:
order = client.create_order(symbol = pair, side='BUY',type='MARKET', quantity = qty)
print(order)
open_position = True
while open_position:
df = getdata(pair,interval, past)
algocalculation(df)
if df.rsi.iloc[-1] > 70:
order = client.create_order(symbol = pair, side='SELL',type='MARKET', quantity = qty)
print(order)
open_position = False
print(order)
break
#Execute trading bot
while True:
strategy('MATICUSDT','1m','30', 5)
time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment