What is an AI BOT trading? It's a software that trades for you.
We must build the logic of the trading system.
If
you want to help me you can choose to send me crypto currencies to my
addresses indicated on the right of the blog or make a donation on
gofundme.
This is a very trivial example of AI for trading. It is based only on book order volumes and two moving averages over a 5 minute time frame.
I am developing algorithms for the recognition of graphic patterns, the management of the MACD (an example of MACD is already present in this blog).
The function of the Pivot Point is already automated: my Pivot Point algorithm decides which is the right candle from which to extrapolate all the resistances and supports as well as the p.p.(.exe program for Windows here).
from binance.client import Client
from binance.enums import *
import string
import time
import dateparser
import sys
def accesso():
f=open('chiavi.dat','r') #text file : pubblicAPIkey-privateAPIkey
rawkeys=f.read()
f.close()
k=rawkeys.split('-')
cl=Client(api_key=k[0], api_secret=k[1])
return cl
def ultimoPrezzo(cl,coppia):
lP=0.0
while True:
try:
vPrz=cl.get_all_tickers()
break
except:
print("Ticker connection - error")
for i in range(len(vPrz)):
if vPrz[i]['symbol']==coppia.upper():
lP=vPrz[i]['price']
return lP
def verificaTF(cl,tf):
ok=False
while True:
try:
t = cl.get_server_time()
break
except:
print("Server Time Error")
ms=str(t['serverTime'])
data=dateparser.parse(ms)
data=str(data)
oD=data.split(" ")
orario=oD[1].split(":")
if tf=='4H':
i=0
d=4
elif tf=='30M':
i=1
d=30
elif tf=='15M':
i=1
d=15
elif tf=='5M':
i=1
d=5
resto=float(orario[i])%d
if resto==0 and (int(orario[2])==0):
ok=True
time.sleep(10)
return ok,oD[1]
def book(cl,cp):
bidsPrice=[]
asksPrice=[]
while True:
try:
libro = cl.get_order_book(symbol=cp.upper())
break
except:
print("book connection - error")
for i in range(len(libro)):
sumBidsVol=float(libro['bids'][i][1])
sumAsksVol=float(libro['asks'][i][1])
for i in range(30):
bidsPrice.append(float(libro['bids'][i][0]))
asksPrice.append(float(libro['asks'][i][0]))
return bidsPrice, asksPrice,sumBidsVol,sumAsksVol
def MA(dati,periodo):
prezzi=[]
for i in range(len(dati)):
prezzi.append(float(dati[i]))
ris=0
for i in range(periodo):
ris=ris+prezzi[i]
ris=ris/periodo
return ris
def recuperoPrezzi(cl,coppia):
vPrezzi=[]
while True:
try:
vTot=cl.get_klines(symbol=coppia.upper(), interval=Client.KLINE_INTERVAL_5MINUTE)
break
except:
print("kLines errore")
for i in range(len(vTot)):
vPrezzi.append(vTot[i][4])
return vPrezzi
def main(coppia,diff):
cl=accesso()
vP=recuperoPrezzi(cl,coppia)
buy=False
sell=False
inOp=False
while True:
stampa,ora=verificaTF(cl,'5M')#5M 15M 30M 4H
prezzo = ultimoPrezzo(cl,coppia)
if stampa:
print("Prezzo ",prezzo," - ",ora)
ma3=MA(vP,3)
ma14=MA(vP,14)
print("MA3 ",ma3,"\nMA14 ",ma14)
vP.append(prezzo)
bidsP,asksP,sumBV,sumAV=book(cl,coppia)#asset name
f=open("dati.dat","a")
stringa=str(prezzo)+","+str(ma3)+","+str(ma14)+","+str(ora)+"\n"
f.write(stringa)
f.close()
if ((float(sumBV)-float(sumAV)) > 0) and (ma3>ma14) and not(inOp):
print("BUY ",prezzo," ",ora)
prezzoB=prezzo
buy=True
inOp=True
sell=False
elif ((float(sumBV)-float(sumAV)) < 0) and (ma3<ma14) and not(inOp):
prezzoS=prezzo
print("SELL ",prezzo," ",ora)
buy=False
inOp=True
sell=True
if buy:
prezzo=ultimoPrezzo(cl,coppia)
if float(prezzo)>(float(prezzoB)+float(diff)) :
print("Preso Buy ",prezzoB," ",prezzo," ",ora)
f=open("operazioni.dat","a")
stringa="Prezzo Buy "+str(prezzoB)+" uscita: "+str(float(prezzoB)+float(diff))+"\n"
f.write(stringa)
f.close()
buy=False
inOp=False
if sell:
prezzo=ultimoPrezzo(cl,coppia)
if float(prezzo)<(float(prezzoS)-float(diff)):
print("Preso sell ",prezzoS," ",prezzo," ",ora)
f=open("operazioni.dat","a")
stringa="Prezzo Sell "+str(prezzoS)+" uscita: "+str(float(prezzoS)-float(diff))+"\n"
f.write(stringa)
f.close()
sell=False
inOp=False
#name asset takeprofit
main(sys.argv[1],sys.argv[2])
Sorry, alittle mistake in def MA: replace this with the one in the main listing.
def MA(dati,periodo):
ris=0
i=len(dati)-1
for c in range(periodo):
prezzo=(float(dati[i]))
ris=ris+prezzo
i-=1
ris=ris/periodo
return ris