sabato 11 aprile 2020

PYTHON BINANCE API - HOW TO MAKE: A LIST OF SMA and MACD

This little python script uses the Binance API.
The customer position is opened to access the data.
Data on the chosen pair are retrieved in the chosen time frame.
The moving average vector is constructed.
The macd vector is constructed.
The macd vector is drawn.
As always if you like the blog you can help me by going on by donating BTC, Link, BAT or by donating through gofundme.
from binance.client import Client
from twisted.internet import reactor
from binance.enums import *
import matplotlib.pyplot as plt
import dateparser

def recuperoDati(coppia,tf):
    f=open('chiavi.dat')
    #esempio di chiavi.dat file: chiavepubblicabinance-chiaveprivatabinance
    #tra le due chiavi deve esserci '-' senza spazi, alla fine della riga non
    #deve essere battuto invio
    rawkeys=f.read()
    f.close()
    k=rawkeys.split('-')
    client=Client(api_key=k[0], api_secret=k[1])
    if tf=='DAY':
        rawData=client.get_klines(symbol=coppia, interval=client.KLINE_INTERVAL_1DAY)
    elif tf=='4H':
        rawData=client.get_klines(symbol=coppia, interval=client.KLINE_INTERVAL_4HOUR)
    elif tf=='30M':
        rawData=client.get_klines(symbol=coppia, interval=client.KLINE_INTERVAL_30MINUTE)
    elif tf=='15M':
        rawData=client.get_klines(symbol=coppia, interval=client.KLINE_INTERVAL_15MINUTE)
    elif tf=='5M':
        rawData=client.get_klines(symbol=coppia, interval=client.KLINE_INTERVAL_5MINUTE)
    return rawData

def vettoreMA(dati,periodo):
    itad=[]
    vMediaINV=[]
    mediaFinale=[]
    i=len(dati)
    c=0
    prov=0
    conta=0
    while i >0 :
        itad.append(dati[c])
        c+=1
        i-=1
    long1=len(itad)//int(periodo)
    for i in range(len(itad)-int(periodo)):
        for c in range(periodo):
            prov=float(itad[i+c][4])+prov
        vMediaINV.append(prov/periodo)
        prov=0
    i=len(vMediaINV)
    c=0
    while i >0:
        mediaFinale.append(vMediaINV[c])
        c+=1
        i-=1
    return mediaFinale

def vettoreMACD(mediasmall,mediabig):
    macd=[]
    for i in range(len(mediabig)):
        macd.append(mediabig[i]-mediasmall[i])
    return macd

def asseX(l,dati):
    x=[]
    for i in range(l):
        data=dateparser.parse(str(dati[i][0]))
        data=str(data)
        oD=data.split(" ")
        x.append(oD[0])
    return x

rawData=recuperoDati('BATUSDT','DAY')
ms=vettoreMA(rawData,12)
mb=vettoreMA(rawData,26)
macd=vettoreMACD(ms,mb)
x=asseX(len(macd),rawData)
fig=plt.figure(figsize=(20,17))
plt.plot(x, macd, color = 'black')
plt.show()

Nessun commento:

Posta un commento