Getting Pool apply_async return too slow

I'm trying to make a bot for IQ Option.

I already did it, but i did it one by one, like, i had to open 10 bots so i could check 10 pairs.

I've been trying all day long doing with ThreadPool, Threadings, map and starmap (i think i didn't use them as good as they can be).

The thing is: i'm checking pairs (EURUSD, EURAUD...) values of the last 100 minutes. When i do it one by one, it takes between 80 and 300ms to return each. I'm trying now to do this in a way that i could do like all the calls at the same time and get their results around the same time to their respective var.

Atm my code is like this:

from iqoptionapi.stable_api import IQ_Option
from functools import partial
from multiprocessing.pool import ThreadPool as Pool
from time import *
from datetime import datetime, timedelta
import os
import sys
import dados #my login data
import config #atm is just payoutMinimo = 0.79

parAtivo = {}

class PAR:
    def __init__(self, par, velas):
        self.par = par
        self.velas = velas
        self.lucro = 0
        self.stoploss = 50000
        self.stopgain = 50000

def verificaAbertasPayoutMinimo(API, payoutMinimo):
    status = API.get_all_open_time()
    profits = API.get_all_profit()
    abertasPayoutMinimo = []

    for x in status['turbo']:        
        if status['turbo'][x]['open'] and profits[x]['turbo'] >= payoutMinimo:
            abertasPayoutMinimo.append(x)
    return abertasPayoutMinimo

def getVelas(API, par, tempoAN, segundos, numeroVelas):
    return API.get_candles(par, tempoAN*segundos, numeroVelas, time()+50)


def logVelas(velas, par):
    global parAtivo
    parAtivo[par] = PAR(par, velas)


def verificaVelas(API, abertasPayoutMinimo, tempoAN, segundos, numeroVelas):    
    pool = Pool()
    global parAtivo
    
    for par in abertasPayoutMinimo:
        print(f"Verificando par {par}")

        pool = Pool()

        if par not in parAtivo:
            callbackFunction = partial(logVelas, par=par)
            
            pool.apply_async(
                getVelas,
                args=(API, par, tempoAN, segundos, numeroVelas),
                callback=callbackFunction
            )
    
    pool.close()
    pool.join()

def main():
    tempoAN = 1
    segundos = 60
    numeroVelas = 20
    tempoUltimaVerificacao = datetime.now() - timedelta(days=99)
    global parAtivo
    
    conectado = False

    while not conectado:
        API = IQ_Option(dados.user, dados.pwd)
        API.connect()

        if API.check_connect():
            os.system("cls")
            print("Conectado com sucesso.")
            sleep(1)
            conectado = True
        else:
            print("Erro ao conectar.")
            sleep(1)
            conectado = False
    
    API.change_balance("PRACTICE")
    
    while True:
        if API.get_balance() < 2000:
            API.reset_practice_balance()

        if datetime.now() > tempoUltimaVerificacao + timedelta(minutes=5):
            abertasPayoutMinimo = verificaAbertasPayoutMinimo(API, config.payoutMinimo)
            tempoUltimaVerificacao = datetime.now()

        verificaVelas(API, abertasPayoutMinimo, tempoAN, segundos, numeroVelas)
        
        for item in parAtivo:
            print(parAtivo[item])

        break #execute only 1 time for testing
    

if __name__ == "__main__":
    main()

@edit1: just complemented with more info, actually this is the whole code right now.

@edit2: when i print it like this:

for item in parAtivo:
    print(parAtivo[item].velas[-1]['close']

I get:

0.26671
0.473878
0.923592
46.5628
1.186974
1.365679
0.86263

It's correct, the problem is it takes too long, like almost 3 seconds, the same as if i was doing without ThreadPool.

1 answer

  • answered 2021-04-11 22:07 Maicon Araújo

    Solved.

    Did it using threadings.Thread, like this:

    for par in abertasPayoutMinimo:        
            t = threading.Thread(
                target=getVelas,
                args=(API, par, tempoAN, segundos)
            )
            t.start()
            t.join()