iqoptionapi icon indicating copy to clipboard operation
iqoptionapi copied to clipboard

How to set expiration time?

Open milteven12 opened this issue 6 years ago • 23 comments

Hello, what files control the expiration time of a buy order? How can I choose the expiration to buy? (1min.. 3min.. 5min.. 15min...)

[UPDATE 1] You can set the expiration time on timesync.py but it works just to 5min or less, we still need a way to buy bigger expirations (> 5min).

[UPDATE 2] is possible to buy bigger expiration times changing the stable_api.py to "binary" instead of "turbo".

milteven12 avatar May 02 '18 16:05 milteven12

I will try to add this tomorrow

Lu-Yi-Hsun avatar May 02 '18 17:05 Lu-Yi-Hsun

Thank you @Lu-Yi-Hsun ! If possible, every time that it fails to buy a order, the api reconnects and try the same order again, if possible, could you add a option that we can switch it on/off?

milteven12 avatar May 02 '18 17:05 milteven12

@milteven12 thank this it work

[UPDATE 2] is possible to buy bigger expiration times changing the stable_api.py to "binary" instead of "turbo".

i add expiration_mode:1~9 you can try image

force_buy:switch it on/off

I_want_money.buy(Money,ACTIVES,ACTION,expirations_mode,force_buy)
                #Money:How many you want to buy type(number)
                #ACTIVES:sample input "EURUSD" OR "EURGBP"....
                #ACTION:"call"/"put" type(str)
                #expirations_mode:1~9
                #force_buy= True: try buy untill sucess 
                            False:if fail break
                #return:True/False if sucess or not

Lu-Yi-Hsun avatar May 04 '18 09:05 Lu-Yi-Hsun

Thank you @Lu-Yi-Hsun Don't you think it is better/"more flexible" to set the expiration time by minutes? For example: I_want_money.buy(15.65,'EURUSD','call',17,False) So it would buy EURUSD for the 19:00 exp. (17 min).

Some actives has more than 9 expirations, see below: image

milteven12 avatar May 04 '18 11:05 milteven12

@milteven12 i will check more expiration mode

if you set the expiration time by minutes it will error

turbo:expiration only can set 1~5 minutes ,if you set 17 minutes you will get error binary: x:00 ,x:15,x:30,x:45......(i will try if it can set more)

Lu-Yi-Hsun avatar May 04 '18 15:05 Lu-Yi-Hsun

@Lu-Yi-Hsun I'm using the old version (without the expiration mode), so when I want to do a order I need to pass the minutes of the expiration, in your example, doing a buy order with 17 minutes would have placed a order with expiration to 19:00.

milteven12 avatar May 04 '18 15:05 milteven12

If you want set by minutes,You neet to get right "remaining time" In my sample: if you set 17 minutes it will get error. you need to set "17:30" to get right ""exp""(expiration)

exp(expiration)="timesync"+"remaining time"

Lu-Yi-Hsun avatar May 04 '18 16:05 Lu-Yi-Hsun

Thanks, but you don't need the remaining seconds, you can set the expiration time just using the minutes. I'm doing this way, but that's ok, thank you!

milteven12 avatar May 04 '18 17:05 milteven12

Thanks for all the work Lu-Yi-Hsun, some insights helped me to get some work done faster :+1: In return, here's a slightly different take on the Buyv2. It takes in 'duration' input as minutes, instead of modes. Takes any number, which, if is weird, like 27, gets rounded towards hour quarters, as IQO offered options are increments of 15mins. However, proper times should be 1,2,3,4,5,15,30,45,60,75... Hope you will use it well :)

class Buyv2(Base):
    name = "buyV2"
    def __call__(self, price, active, direction, duration):
        exp, option = self.get_expiration_time(duration)
        data = {"price": price,
                "act": active,
                "exp": exp,
                "type": option,
                "direction": direction.lower(),
                "time": self.api.timesync.server_timestamp
               }
        self.send_websocket_request(self.name, data)

Wraped neatly into this function bellow:

def get_expiration_time(self, duration):
        exp=int(self.api.timesync.server_timestamp)
        if duration>=1 and duration<=5:
            option="turbo"
            #Round to next full minute
            if datetime.datetime.now().second > 30:
                exp = exp - (exp % 60) + 60*(duration+1)
            else:
                exp = exp - (exp % 60)+60*(duration)
        elif duration > 5:
            option = "binary"
            period = int(round(duration / 15))
            tmp_exp = exp - (exp % 60)#nuima sekundes
            tmp_exp = tmp_exp - (tmp_exp%3600)#nuimam minutes
            j=0
            while exp > tmp_exp + (j)*15*60:#find quarter
                j = j+1
            if exp - tmp_exp > 5 * 60:
                quarter = tmp_exp + (j)*15*60
                exp = quarter + period*15*60
            else:
                quarter = tmp_exp + (j+1)*15*60
                exp = quarter + period*15*60
        return exp, option

Darth-Carrotpie avatar May 10 '18 13:05 Darth-Carrotpie

@Darth-Carrotpie !!thank your code i am add this!!, and put your name on the code haha

Lu-Yi-Hsun avatar May 11 '18 04:05 Lu-Yi-Hsun

Encountered an unhandled exception recently, where option variable does not have a value. You can update it, by adding a default value "binary" in the first line inside the method.

def get_expiration_time(self, duration):
        exp=int(self.api.timesync.server_timestamp)
        option='turbo'
...

In normal conditions this shouldn't happen, but if you give a 0 minutes, by some mistake, option type check will get lost... Even a better way is to throw a proper exception if this method receives 0 minute duration input.

Darth-Carrotpie avatar May 23 '18 13:05 Darth-Carrotpie

@Darth-Carrotpie Thank ! I will add for check 0 minute problem

Lu-Yi-Hsun avatar May 24 '18 02:05 Lu-Yi-Hsun

Could someone help me? Always I try to negociate like: I_want_money.buy(2, "EURUSD", "put", 5) I use 5 or any other value but it buys with 1 min, but I need 5

How to fiz it? thanks

daniloeder avatar Nov 21 '19 20:11 daniloeder

What your version?

Lu-Yi-Hsun avatar Nov 22 '19 00:11 Lu-Yi-Hsun

My version is 5.0

daniloeder avatar Nov 22 '19 00:11 daniloeder

Try uninstall all iqoptionapi and reinstall iqoptionapi , if that can not work , i test that now

Lu-Yi-Hsun avatar Nov 22 '19 00:11 Lu-Yi-Hsun

I already tried, in other virtual envolriment and in other python version(2.7), same error.

daniloeder avatar Nov 22 '19 01:11 daniloeder

Ok I will check what happen

Lu-Yi-Hsun avatar Nov 22 '19 03:11 Lu-Yi-Hsun

Ok, thank you bro

daniloeder avatar Nov 22 '19 13:11 daniloeder

same problem here, using last version from 1 to 3 gives me 1 minute, 4 gives me 2 min and 5 gives me 4 min

SebastianMoyano avatar Jul 09 '20 03:07 SebastianMoyano

Ok I think I know what caused it, its because uses datetime library

def get_expiration_time(timestamp, duration):
    now_date = datetime.fromtimestamp(timestamp)

Which it looks like its affected by the computers time, my computer was 1 minute late, so everything was delayed. I ended up writing this code, which is almost identical to the one posted by @Darth-Carrotpie

l = I_want_money.get_server_timestamp()
if l%60 > 30:
    z = l - l%60 +60
else:
    z = l - l%60
expiredTime = z + (60*expirations_mode)
check,id=I_want_money.buy_by_raw_expirations(Money,ACTIVES,ACTION,"turbo",expiredTime)

This worked even if time was changed. Changing the time to the correct one also worked

SebastianMoyano avatar Jul 09 '20 04:07 SebastianMoyano

is it possible get all minimum expiration time for all pair? Some times site use 1 minute and others option using 15 minutes as minimum purchase time. I want understand and get all this value for all open market. Tks for all

Claudio-Matheus avatar May 01 '23 18:05 Claudio-Matheus

Hello, how do I make a purchase using the BUY function with a fractional time, for example: Iq.buy(2.5, "EURUSD-OTC", "call", 2.57). I know when analyzing the API codes that it is only possible to enter an expiration time with an integer, 1, 5, 10, etc. However, I need it to be possible to inform a personalized/fractional expiration, as I know that through the API this time is added together with the current time on the IQ Option server.

Code node that I am implementing, for example: I am analyzing a candle with a timeframe of 5 minutes (14:30 candles), I receive a signal at 14:32:30 and open a purchase request, which needs to expire exactly at the final destination current candle, that is, it would need to expire at 2:35 pm: (Iq.buy(2.5, "EURUSD-OTC", "call", 2.30)), informing the expiration with 2.30. If you can help me please.

Fabio-MC avatar Mar 30 '24 17:03 Fabio-MC