venerdì 17 aprile 2020

PYTHON - BINANCE API - PRINT THE LATEST PRICE AT EACH TIME FRAME

This Python script print the last price for each time frame. 
TIME FRAME: 4H 30M 15M 5M
COUPLE: all those on spot Binance

from binance.client import Client
from twisted.internet import reactor
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
    vPrz=cl.get_all_tickers()
    for i in range(len(vPrz)):
        if vPrz[i]['symbol']==coppia.upper():
            lP=vPrz[i]['price']
    return lP

def verificaTF(cl,tf):
    ok=False
    t = cl.get_server_time()
    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]

client=accesso()
while True:
    stampa,ora=verificaTF(client,sys.argv[2])#5M 15M 30M 4H
    if stampa:
        prezzo = ultimoPrezzo(client,sys.argv[1])#asset name
        print(prezzo," ",ora)

You must make a text file containing: public key API binance (dash) -private key API binance. You don't have to type send at the end of the line. You have to save it as: chiavi.dat .
This program is part of the next AI Signal Bot that I am programming.

Nessun commento:

Posta un commento