ig-python / trading-ig

A lightweight Python wrapper for the IG Markets API
https://trading-ig.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
313 stars 197 forks source link

Exception: error.security.account-token-invalid #328

Open NewEconomicsUK opened 6 months ago

NewEconomicsUK commented 6 months ago

Greetings World!

I was testing my simple code: buy = ig_service.create_open_position(epic='IX.D.DAX.DAILY.IP', direction='BUY') print(buy)

and the error appeared: Exception: error.security.account-token-invalid

My requests using ig_service.fetch_open_positions() and ig_service.fetch_accounts() worked fine so I don't think the token is the problem?

Or am I wrong gentlemen?

Any guidance is much appreciated.

Kind regards

galburn commented 6 months ago

It's working perfectly for me. Is you account Spreadbet or CFD? I'm only working with Spreadbet.

I send a lot more parameters, so perhaps you need to add some more such as size, stop_level...

Here's a snippet from my code...

      resp = ig_service.create_open_position(
                # currency_code=currency_code, # 2024-02-12
                currency_code=CURRENCY_CODE,
                direction = direction,
                epic = epic,
                expiry = expiry,
                force_open = force_open,
                guaranteed_stop = guaranteed_stop,
                level = level,                             
                limit_level = limit_level,
                limit_distance = limit_distance,
                order_type = order_type,
                quote_id = quote_id,
                size = size,
                stop_distance = stop_distance,
                stop_level = stop_level,
                trailing_stop = trailing_stop,                         # Needed?
                trailing_stop_increment = trailing_stop_increment,     # Needed?
                session=None,
                )
NewEconomicsUK commented 6 months ago

Thank you for your reply @galburn

I've try the following code and it works

resp = ig_service.create_open_position( currency_code='GBP', direction='BUY', epic='IX.D.DAX.DAILY.IP', order_type='MARKET', expiry='DFB', #DFB in original force_open='false', guaranteed_stop='false', size=0.5, level=None, limit_distance=None, limit_level=None, quote_id=None, stop_level=None, stop_distance=None, trailing_stop=None, trailing_stop_increment=None) resp print(resp)

But it looks like if I change it to 'SELL', the error comes back again?

galburn commented 6 months ago

I'll repeat my question. Is you account Spreadbet or CFD?

This looks to be a code or account issue, not a bug in the library. Please provide a complete minimal example that demonstrates the issue.

NewEconomicsUK commented 6 months ago

The account I'm testing on is Spreadbet. It works fine if I space out the requests for at least 1 minute. Is that normal?

This is one of the codes

# resp = ig_service.create_open_position(
#     currency_code='GBP',
#     direction='SELL',
#     epic='IX.D.DAX.DAILY.IP',
#     order_type='MARKET',
#     expiry='DFB', #- does not work
#     force_open='false',
#     guaranteed_stop='false',
#     size=0.5, level=None,
#     limit_distance=None,
#     limit_level=None,
#     quote_id=None,
#     stop_level=None,
#     stop_distance=None,
#     trailing_stop=None,
#     trailing_stop_increment=None)
# resp
# print(resp)
galburn commented 6 months ago

Well that code runs perfectly without error for me! Even if I remove all the hashes.

If you need further help, please take a look here... here

It may well be that your library imports or versions are causing a problem. Or there is some other code mistake. This is why we insist upon a complete minimal example.

NewEconomicsUK commented 6 months ago

It works fine on the first request but the second one showed the error until I waited a minute, and it worked fine again.

Does that mean there's a limit to how many requests we can put within a 1 minute period?

Many thanks

galburn commented 6 months ago

There are API limits, but you should not be hitting them with one request per minute. I have no trouble sending one immediately after another.