Receive data every time through API using websockets

0

I am working with the API of a platform to receive currency quotes minute by minute, I have a way of doing it but it is not very functional and I wanted to check if someone could help me, this is my code:

from iqoptionapi.api import IQOptionAPI
from datetime import datetime

velas = None
contador = 1

while True:

    if velas is None:
        api = IQOptionAPI("iqoption.com", "usuario", "contraseña") # Datos de conexión 
        api.connect()
        api.getcandles(1,1) # (id_activo, tiempo)
        velas = api.candles.candles_data # Lista con vela 1 segundo retrasada y vela actual
        print ("Intento de conexion...\n")
    else:
        if datetime.now().second == 59 and datetime.now().microsecond > 998000: # determinar cuándo sea un minuto
            print ("Peticion de velas:", contador, "-", datetime.now(), "\n")
            api.getcandles(1, 1)
            velas = api.candles.candles_data
            print (velas, "\n")
            print ("Hora primera lista:" datetime.fromtimestamp(velas[0][0]), "Hora segunda lista:", datetime.fromtimestamp(velas[1][0]), "\n")
            contador += 1
        else:
            pass

The problem is that sometimes you get more than one quote because being a loop still falls within 59 seconds or there are even times when you do not receive anything for not falling within the range of the comparison, try to solve it with time.sleep(60) but it introduces me a one-minute deface in the quotes that I do not know why it happens.

Thank you.

    
asked by Samael Olascoaga 25.01.2017 в 08:27
source

1 answer

1

The problem is that in your own way you can not control when the code is executed, it will be executed as many times as possible while falling in that time range that you have defined, which can cause times that it does not run ( the processor may have been somewhat busy and one cycle of the loop may not have run within the time range of those milliseconds) or may be executed multiple times.

To do this there are task programming libraries, cron type or configurable, take a look at this library to see if you can set one without losing your head.

Try this: link

    
answered by 25.01.2017 в 09:36