Lu-Yi-Hsun / iqoptionapi

IQ Option API
372 stars 288 forks source link

How to set expiration time? #6

Open milteven12 opened 6 years ago

milteven12 commented 6 years ago

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".

Lu-Yi-Hsun commented 6 years ago

I will try to add this tomorrow

milteven12 commented 6 years ago

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?

Lu-Yi-Hsun commented 6 years ago

@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
milteven12 commented 6 years ago

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

Lu-Yi-Hsun commented 6 years ago

@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)

milteven12 commented 6 years ago

@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.

Lu-Yi-Hsun commented 6 years ago

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"

milteven12 commented 6 years ago

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!

Darth-Carrotpie commented 6 years ago

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
Lu-Yi-Hsun commented 6 years ago

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

Darth-Carrotpie commented 6 years ago

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.

Lu-Yi-Hsun commented 6 years ago

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

daniloeder commented 4 years ago

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

Lu-Yi-Hsun commented 4 years ago

What your version?

daniloeder commented 4 years ago

My version is 5.0

Lu-Yi-Hsun commented 4 years ago

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

daniloeder commented 4 years ago

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

Lu-Yi-Hsun commented 4 years ago

Ok I will check what happen

daniloeder commented 4 years ago

Ok, thank you bro

SebastianMoyano commented 4 years ago

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 commented 4 years ago

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

Claudio-Matheus commented 1 year ago

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

Fabio-MC commented 5 months ago

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.