Lu-Yi-Hsun / iqoptionapi

IQ Option API
371 stars 286 forks source link

How to solve copy trading on binary options #247

Open lohrann opened 4 years ago

lohrann commented 4 years ago

Hi,

I want to copy the trades of the other traders on IQ Option.

I just using the get_live_deal to obtain the parameters, and I saw that the expiration is getting me issues.

On the JSON, it's just using expiration variable, that uses a random value (ex. 1594476780000).

That's the error when i use the buy option instead of buyraw (buyraw also sends me the same error), invalid request 4000: image

lohrann commented 4 years ago

Using buyraw:

image

provezano commented 3 years ago

expiration doesn't contain a random value. In your example, 1594476780000 is an Epoch Timestamp that is in milliseconds. To make it human readable you can convert it to ISO-8601 format.

from datetime import datetime
expiration = 1594476780000
print("Expiration:", datetime.fromtimestamp(expiration / 1e3).isoformat())
lohrann commented 3 years ago

Hi,

So, I need to convert first, and after that send it from buyraw or buy?

isaacdalmarco commented 3 years ago

Hello, I have the same issue, I tried the functions buy and buy_by_raw_expirations, but didn´t work both.

SebastianMoyano commented 3 years ago

If you use buy_by_raw_expirations and the expired time obtain with get_live_deal, you need to divide by a thousand, and then use it, but if you use the buy function, you need to calculate the time between the expiration and the current time and calculate to which minute it correspond 1,2,3 ... Have in mind that there is an issue that hasn't been fixed yet #6 where I have posted my own fix

isaacdalmarco commented 3 years ago

Look to my values: Binary - buy_by_raw_expirations: value: 2 - Par: EURUSD - Direction: put - Type: turbo - Expiration timestamp: 1594682580 - Expiration datetime: 2020-07-13 23:23:00

Binary - buy: value: 2 - Par: EURUSD - Direction: put - Timeframe: 1

image

Is there anything wrong?

provezano commented 3 years ago

Hi,

So, I need to convert first, and after that send it from buyraw or buy?

I didn't see it in deep, but it is probably the time the deal will expire. So, you have to construct a get_remaining_time function to return the value you should pass to buy function.

For example:

If the expiration time is 2020-07-13 23:23:00 and the time now is 2020-07-13 23:20:00 you should open a trade with 3 minutes as expiration time in your buy function.

isaacdalmarco commented 3 years ago

@lohrann did you manage to fix the problem?

lohrann commented 3 years ago

@isaacdalmarco no. I'm really confused.

Anyone to help me to find a real solution in code?

isaacdalmarco commented 3 years ago

@lohrann I just got it!

There is a fix in buyv2 api:

From: image

To: image

I uninstalled this repo and installed that one with the fix: https://github.com/iqoptionapi/iqoptionapi

SebastianMoyano commented 3 years ago

@isaacdalmarco I just tested it also, and it has the same bug as this repo, not your error though but if you change your date, for example I delayed my date one minute, and the buy and sell times were wrong. So be careful. I mention here my solution https://github.com/Lu-Yi-Hsun/iqoptionapi/issues/6#issuecomment-655899438 which it seems to be working ok at least for me, though I think is a little slower. But is ok, I prefer that than having to check my computers time

SebastianMoyano commented 3 years ago

I just modify the class which the buy method is using (buyv3), changing what I commented before and i ended up with this

class Buyv3(Base):

    name = "sendMessage"

    def __call__(self, price, active, direction, duration, request_id):

        tserver = int(self.api.timesync.server_timestamp)
        if tserver%60 > 30:
            exp = tserver - tserver%60 +60*(1+duration)
        else:
            exp = tserver - tserver%60 + (60*duration)

        if duration <= 5:
            option = 3  # "turbo"
        else:
            option = 1  # "binary"
        data = {
            "body": {"price": price,
                     "active_id": active,
                     "expired": int(exp),
                     "direction": direction.lower(),
                     "option_type_id": option,
                     "user_balance_id": int(global_value.balance_id)
                     },
            "name": "binary-options.open-option",
            "version": "1.0"
        }
        self.send_websocket_request(self.name, data, str(request_id))

I tested it in turbo and its working ok