n1nj4z33 / iqoptionapi

IQ Option API 4.x (Python 2.7) The project is obsolete and is not supported because of problems with access to IQ Options in Russia
120 stars 545 forks source link

candles: api.getcandles is not working #13

Closed Olascoaga closed 7 years ago

Olascoaga commented 7 years ago

I have this code with the iqoption api:

from iqoptionapi.api import IQOptionAPI
api = IQOptionAPI("iqoption.com", "My user", "My pass")
api.connect()
balance = api.profile.balance
print balance
api.setactives([1, 2])
candles = api.getcandles(1,1)
print candles

all is working except the last two lines, candles always return None, not a list how i fix them?

Thanks.

n1nj4z33 commented 7 years ago

api.getcandles(1,1) -- send ws request data = api.candles.candles_data -- get ws response print api.candles.candles_data

Olascoaga commented 7 years ago

Thanks

that returns a list with three elements like this:

[[1485215293, 1076380, 1076395, 1076395, 1076380], [1485215294, 1076395, 1076400, 1076400, 1076395], [[1485215293, 1076380, 1076395, 1076395, 1076380]]

it is correct? which is the current candle? is the [-1] element?

n1nj4z33 commented 7 years ago

yes current candle data is the last, [-1] element of list

n1nj4z33 commented 7 years ago

or you can get it like this api.candles.current_candle.candle_time api.candles.current_candle.candle_open api.candles.current_candle.candle_close api.candles.current_candle.candle_high api.candles.current_candle.candle_low

and two others candels like this api.candles.first_candle.candle_time api.candles.second_candle.candle_time etc

Olascoaga commented 7 years ago

Thanks a lot!

One thing more, some times api.buy() fails don't open a position, why?

Regards

burny91 commented 7 years ago

Hi Olascoaga, I was following your questions to get the api running. But I am also not able to retrieve even my account balance. Do you have any experience if the api is only running on Python 2.x and 32-bit? Because my dev setup is based on Python 3.x and 64-bit. All necessary packages are installed: pylint, requests and websocket-client. Would be great if you can post your setup which I can use for troubleshooting.

Code which I am executing:

from iqoptionapi.api import IQOptionAPI api = IQOptionAPI("iqoption.com", "accountname", "pw") api.connect() balance = api.profile.balance api.setactives([1, 2]) api.getcandles(1,1) print (api.candles.candles_data)

Output: None [Finished in 7.1s]

Regards B

n1nj4z33 commented 7 years ago

@Olascoaga for debug you can add a logger to your code like this import logging

formatter = logging.Formatter( "%(asctime)s:%(levelname)s: %(message)s", datefmt="%Y-%m-%d %H:%M:%S")

console_handler = logging.StreamHandler() console_handler.setLevel(logging.DEBUG) console_handler.setFormatter(formatter)

logger = logging.getLogger("iqoptionapi") logger.addHandler(console_handler)

and get all websocket messages

n1nj4z33 commented 7 years ago

@burny91 try to get data in a loop like this while True: print (api.candles.candles_data)

Olascoaga commented 7 years ago

@n1nj4z33 Thanks i try with that, because all the afternoon api.buy() is not working

@burny91 usually the first connection is refused by the server, then you could try whit this:

from iqoptionapi.api import IQOptionAPI
import time

balance = None
while True:

    if balance is None:
        api = IQOptionAPI("iqoption.com", "user", "pass")
        api.connect()
        balance = api.profile.balance
    else:
        print balance

    time.sleep(1)
Olascoaga commented 7 years ago

@n1nj4z33

I almost forgot, api.getcandles(1,1) and then api.candles.candle_data returns the value of the cotization second by second, correct? How do I get the value per minute for example the value from 12:58:00 to 12:58:59

---- EDIT----

I followed your recommendation

from iqoptionapi.api import IQOptionAPI
import time
import logging

formatter = logging.Formatter("%(asctime)s:%(levelname)s: %(message)s", datefmt="%Y-%m-%d %H:%M:%S")
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.DEBUG)
console_handler.setFormatter(formatter)
logger = logging.getLogger("iqoptionapi")
logger.addHandler(console_handler)

api = IQOptionAPI("iqoption.com", "user", "pass")
api.connect()

balance = None
while True:
    if balance == None:
        api = IQOptionAPI("iqoption.com", "user", "pass")
        api.connect()
        balance = api.profile.balance
    else:
        api.buy(10, 1, "turbo", "call")
        balance = api.profile.balance
        time.sleep(1)

I got this:

2017-01-26 00:18:11:DEBUG: {"msg": {"direction": "call", "price": 10, "act": 1, "exp": 1485411550.0, "time": 1485411490, "type": "turbo"}, "name": "buyV2"} 2017-01-26 00:18:11:DEBUG: {"name":"timeSync","msg":1485411491313} ["189.249.101.203","python-requests/2.12.5"],"host":"iqoption.com","modified":1485411486}} 2017-01-26 00:18:11:DEBUG: {"name":"front","msg":"1-prod-fe-21.mobbtech.com"} 2017-01-26 00:18:11:DEBUG: {"name":"timeSync","msg":1485411491394} 2017-01-26 00:18:11:DEBUG: {"name":"buyComplete","msg":{"isSuccessful":false,"message":["Time for purchasing options is over, please try again later."],"result":{"request_id":null},"

with each active and each time in an interval of a minute

n1nj4z33 commented 7 years ago

try to set candle duration to 60 api.getcandles(1,60)

Sx3 commented 7 years ago

Hi I was trying

from iqoptionapi.api import IQOptionAPI api = IQOptionAPI("iqoption.com", "user", "password") api.connect() balance = api.profile.balance print balance

but it prints "None" in the console.

rnjane commented 6 years ago

@Sx3 make sure you are using python 2. I had the same problem while using python 3, but it was resolved when i switched to python 2.7

l4p4 commented 6 years ago

Hi @n1nj4z33 , has the problem in getting the candles fixed? I tried using with While, Sleep, but nothing works.

Im using python 2.7.10 and OS X like env

The API is deprecated ?

import time
import logging

formatter = logging.Formatter("%(asctime)s:%(levelname)s: %(message)s", datefmt="%Y-%m-%d %H:%M:%S")
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.DEBUG)
console_handler.setFormatter(formatter)
logger = logging.getLogger("iqoptionapi")
logger.addHandler(console_handler)

api = None

def connect():
    global api
    if api is None:
        api = IQOptionAPI("iqoption.com", "mymail@s.com", "mypassword")
        api.connect()
        api.setactives([1, 2])
        time.sleep(0.25)
        print 'Connected at IQ Option'
    else:
        if api.profile.balance is None:
            connect()
        else:
            print 'API Connected!'

def balance():
    connect()
    global api
    print 'Your current blance is: {:.2f}'.format(api.profile.balance)

def candles():
    connect()
    global api
    api.getcandles(1, 60) #send ws request
    while True:
        print api.candles.candles_data
        time.sleep(1)

candles()

OUTPUT: ... Connected at IQ Option 2018-06-07 22:17:00:DEBUG: {"msg": {"duration": 60, "till": 1528420620, "chunk_size": 25, "from": 1528420500, "active_id": 1}, "name": "candles"} None 2018-06-07 22:17:01:DEBUG: {"name":"timeSync","msg":1528420621181} None None 2018-06-07 22:17:02:DEBUG: {"name":"timeSync","msg":1528420622190}

Lu-Yi-Hsun commented 6 years ago

try my fork version

l4p4 commented 6 years ago

I'll test @Lu-Yi-Hsun, thanks! Good job!