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

Для polo

#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_polo():   
    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:
            #log('requests.get')
            #response  = requests.get('https://poloniex.com/public?command=returnTradeHistory&currencyPair=%s&start=%s&end=%s' % (pair,st_time,end_time), timeout = None)
            response  = requests.get('https://poloniex.com/public?command=returnTradeHistory&currencyPair=%s&start=%s&end=%s' % (pair,st_time,end_time), timeout = 5)
            #log('response.status_code =', response.status_code)
            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_signal():
    global st_time
    global end_time
    #DEBUG = False
    DEBUG = True
    if DEBUG:
###############################################################
#Для тестирования
        #time_start = (2018, 6, 29, 22, 24, 0, 0, 0, 0)
        time_start = (2018, 6, 30, 22, 53, 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)
        #time_end = (2018, 6, 29, 22, 25, 0, 0, 0, 0) #Время UTC
        time_end = (2018, 6, 30, 22, 54, 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)
###############################################################
    else:
################################################################
# Для бота
        dt = datetime.utcnow()
        print(dt)
        dtt = dt.timetuple()
        #print(dtt)
        x = int(time.mktime(dtt)) + 3600 * 3
        print(x)
        print(datetime.fromtimestamp(x))
        end_time = x
        x = x - 60
        print(x)
        print(datetime.fromtimestamp(x))
        st_time = x
    chart_data = get_polo()
    dict = chart_data
    vbuy = 0
    vsell = 0
    high = 0
    low =  float(dict[-1]['rate'])
    open = float(dict[-1]['rate'])
    close = float(dict[0]['rate'])
    for index, elems in enumerate(dict):
        if dict[index]['type'] == 'buy':
            vbuy = vbuy + float(dict[index]['amount'])
        if dict[index]['type'] == 'sell':
            vsell = vsell + float(dict[index]['amount'])
        if float(dict[index]['rate']) > high:
            high = float(dict[index]['rate'])
        if float(dict[index]['rate']) < low:
           low = float(dict[index]['rate'])
    buy = False
    sell = False
    if vbuy > 2 * vsell and vbuy > 10:
        buy =True
    if vsell > 2 * vbuy and vsell > 10:
        sell =True
    return (
           {
           'buy':buy, 'sell':sell,'open':open,'close':close,'high':high,'low':low,
           'vbuy':vbuy, 'vsell':vsell
           }
           )

signal = get_signal()
print(signal)

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

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