воскресенье, 1 июля 2018 г.

Binance

https://api.binance.com/api/v1/trades?symbol=BTCUSDT
https://bablofil.ru/binance-api/

#sql_pol.1.4.py
#import numpy
#import talib
import requests
import json
import time
import calendar
#import mysql.connector
import sys
from datetime import datetime
#from matplotlib.finance import candlestick2_ohlc
#i#mport matplotlib.animation as animation
#import matplotlib.pyplot as plt
#import matplotlib.ticker as ticker
#from datetime import datetime
#import pandas as pd



def get_binance():   
    global st_time
    global end_time
    pair = 'USDT_BTC'
    time.sleep(0.2)
    #chart_data={}
    #manual http://docs.python-requests.org/en/master/user/advanced/#request-and-response-objects
    while True:
        try:
            response  = requests.get('https://api.binance.com/api/v1/trades?symbol=BTCUSDT')
            if response.status_code == 200:
                break
            else:
                #log('response.status_code =', response.status_code)
                pass
        except Exception as e:
            #log(e)
            pass
    chart_data = json.loads(response.text)
    return chart_data


def get_signalb():
    global st_time
    global end_time
    DEBUG = False
    #DEBUG = True
    if DEBUG:
###############################################################
#Для тестирования время UTC последниее несколько минут (500 сделок)
#https://www.coinigy.com/main/markets/BINA/BTC/USDT
# на https://www.binance.com/ru/trade/BTC_USDT время Московское!
        #time_start = (2018, 6, 29, 22, 24, 0, 0, 0, 0)
        #time_start = (2018, 7, 2, 11, 8, 0, 0, 0, 0)
        time_start = (2018, 7, 2, 16, 24, 0, 0, 0, 0)
#timestampstart = time.mktime(time_start)-3*60*60 #Время UTC
#timestampstart = time.mktime(time_start) #Локальное время
        timestampstart = calendar.timegm(time_start) # время UTC
        st_time=int(timestampstart)
        print(st_time)
        print('Начало теста', datetime.utcfromtimestamp(st_time))
        #time_end = (2018, 6, 29, 22, 25, 0, 0, 0, 0) #Время UTC
        #time_end = (2018, 7, 2, 11, 9, 0, 0, 0, 0) #Время UTC
        time_end = (2018, 7, 2, 16, 25, 0, 0, 0, 0) #Время UTC
#timestampend = time.mktime(time_end)-3*60*60
#timestampend = time.mktime(time_end) #Локальное время
        timestampend = calendar.timegm(time_end) # время UTC
        end_time = int(timestampend)
        print(end_time)
        print('Конец теста', datetime.utcfromtimestamp(end_time))

###############################################################
    else:
################################################################
# Для бота
        x = int(datetime.now().timestamp())
        end_time = x
        x = x - 60
        st_time = x
        print('Время начала ', datetime.utcfromtimestamp(st_time), st_time)
        print('Время конца ', datetime.utcfromtimestamp(end_time), end_time)
    chart_data = get_binance()
    dict = chart_data
    data = []
    for index, elems in enumerate(dict):
        dict[index]['time'] = int(dict[index]['time']/1000)
        #data.append(elems)
        if dict[index]['time'] >= st_time and dict[index]['time'] < end_time:
            data.append(elems)
    dict =  data
    #print(dict)
    vbuy = 0
    vsell = 0
    high = 0
    low =  float(dict[0]['price'])
    open = float(dict[0]['price'])
    close = float(dict[-1]['price'])
    for index, elems in enumerate(dict):
        if dict[index]['isBuyerMaker']:
            vbuy = vbuy + float(dict[index]['qty'])
            #vsell = vsell + float(dict[index]['qty'])
        else:
            #vbuy = vbuy + float(dict[index]['qty'])
            vsell = vsell + float(dict[index]['qty'])
        if float(dict[index]['price']) > high:
            high = float(dict[index]['price'])
        if float(dict[index]['price']) < low:
            low = float(dict[index]['price'])
    buy = False
    sell = False
    if vbuy > 2 * vsell and vbuy > 10:
        buy =True
    if vsell > 2 * vbuy and vsell > 10:
        sell =True
    #print(dict)
    print('Начало базы', datetime.utcfromtimestamp(int(dict[0]['time'])),int(dict[0]['time']))
    print('Конец базы', datetime.utcfromtimestamp(int(dict[-1]['time'])),int(dict[-1]['time']))
    return (
           {
           'buy':buy, 'sell':sell,'open':open,'close':close,'high':high,'low':low,
           'vbuy':vbuy, 'vsell':vsell
           }
           )
signal = get_signalb()
print(signal)

Комментариев нет:

Отправить комментарий