Jump to content

Recommended Posts

Hello,

I'm having a hard time achieving what I'd like to do. 

First off, although it doesn't matter most likely: I'm writing in Python.  Using the REST APIs for creating orders, capturing status from the orders feed with LS.  US customer.  I'm performing the operations below via a python script.  I'm placing all of these orders against the GBP/USD FOREX pair.

 

I put buy trade A on for quantity X.  I put buy trade B on for quantity X.  I put a stop order on, referencing trade B's order ID, for quantity X.  When I look at my response on putting that stop order on, however, is that the stop order's quantity is actually 2X. 

I look at the web interface for my account for confirmation.  I see both of my trades as separate positions, each with their own order ID.  I see both of them have a stop order attached to them.  The web interface confirms what I see coming out of my script.

 

For some actual data ... when I ran my script just now, I got back order ID 716173586 for the first trade, order ID 716173589 for the second trade.  The script then submitted the following parameters to the update trade API (I x'd out my trading ID for the post here; it's a valid ID when I actually submit it to the API):

{'OrderId': 716173589, 'MarketId': 401484392, 'Currency': None, 'AutoRollover': False, 'Direction': 'buy', 'Quantity': 1000, 'QuoteId': None, 'PositionMethodId': 1, 'AuditId': None, 'TradingAccountId': xxx, 'IfDone': [{'Stop': {'Direction': 'sell', 'Applicability': 'gtc', 'Guaranteed': False, 'Quantity': 1000, 'TriggerPrice': 1.27393}}], 'Close': [], 'Reference': 'GCAPI', 'AllocationProfileId': None, 'OrderReference': None, 'Source': None}

It got back the following response:

{'Status': 1, 'StatusReason': 1, 'OrderId': 716173589, 'Orders': [{'OrderId': 716173589, 'StatusReason': 1, 'Status': 3, 'OrderTypeId': 1, 'Price': 1.27593, 'Quantity': 1000.0, 'TriggerPrice': 0.0, 'CommissionCharge': 0.0, 'IfDone': [], 'GuaranteedPremium': 0.0, 'OCO': None, 'AssociatedOrders': {'Stop': None, 'Limit': None}, 'Associated': False}, {'OrderId': 716173590, 'StatusReason': 1, 'Status': 2, 'OrderTypeId': 2, 'Price': 0.0, 'Quantity': 2000.0, 'TriggerPrice': 1.27393, 'CommissionCharge': 0.0, 'IfDone': [], 'GuaranteedPremium': 0.0, 'OCO': None, 'AssociatedOrders': {'Stop': None, 'Limit': None}, 'Associated': False}], 'Quote': None, 'Actions': [], 'ErrorMessage': None}

From what I can tell, the response is telling me I successfully altered order ID 716173589 (which is my second trade).  In the orders section, I can see information on my second trade and information on my new stop order.  The trade information looks right, although has no IfDone curiously.  The stop order has an order ID and target which looks correct but it's quantity is not what I asked for.  Instead, it is enough to cover my total position in the market on the GBPUSD pair.

 

I also tried running the script with putting on three trades, then the stop against the third trade.  The stop ends up having 3X quantity when I do this.

 

Is it possible for me to attach a stop to just a single trade by its order ID?  From what I can tell, I can only attach the stop to all open positions against a given FOREX pair.  Please help!

 

Thanks in advance.  

 

 

 

Share this post


Link to post

Hello,

For US clients our system forces stops to always be for the full total position size against each Forex pair, even if as in your case, multiple trades were placed to build up the position.

What you've been seeing/experiencing is not from any errors in your coding or the way you are sending calls through the API, but are a result of how the system works for US clients. I'm sorry it is not better news.

Kind Regards, PM  

Share this post


Link to post

PM,

Thanks for the response.  The US client rules make it quite difficult to code multiple algorithms to trade against the APIs.  I'll take some solace in the fact that I'm seeing what's expected, I suppose :)

The platform I've written runs many systems at the same time.  This US client mechanic is problematic.  If system X enters the market, then system Y enters the market, then system Y hits an exit condition, I'll see system X's orders get the fill notifications.  I've been playing with some other angles of attack.  

The platform keeps records of all orders it places.  My best thought at this point is to, any time I get an order update from LS, query the REST APIs for all active orders (including stops/limits).  Compare what I know I've placed and was most recently active vs what I get back from the APIs.  There's still room for error there, unfortunately.  But it seems to be my best shot, from what I can tell.  Question 1: Should there be any concerns with that that you know about?  

 

I have another question as well.  I was testing out doing this in some fashion last night and came upon another mechanic that surprised me.  Here's what I did:

Placed a market buy for 1000 on the GBPUSD

Placed a market buy for 1000 on the GBPUSD

Placed a market buy for 1000 on the GBPUSD

Placed a sell limit for 1500 on the GBPUSD

I wanted to see if I'd get the first order's position fully closed  + the second order's position half closed.  I did confirm that through the web interface - when my sell limit triggered, the first buy closed completely and the second buy had half quantity remaining.  What surprised me was that I didn't receive any notification on the first order being filled.  When my sell limit triggered, my platform only received a notification of the second market buy having half of its position closed.  There was nothing about the first order sent back to me at any point.  

Question 2: Is that expected?  Even if, as a US client, I'm forced to move against my position by forex pair, it seems odd that I wouldn't get a notification when one of my orders gets closed.  If this is expected, can you suggest how I should determine what I got filled at on the closed position I got no callback on?  Is it safe to assume the fill price is exactly the same as the order I did get a callback on?  Do I have to query a closed order API or something along those lines?

 

 

Last question.  If I do the same setup as above - put three market buy orders on for 1000 on the GBPUSD, it seems I can't then put on a market sell order against the GBPUSD for any position which does not fully close one or more of those original market buy orders.  In other words, I can't put a market sell order on the GBPUSD for 1500 or 2250 or 750.  I can only put it on for 1000, 2000, or 3000.  Question 3: Can you confirm this is expected?  This one seems particularly nasty in that if I have multiple systems running on my platform and one decides to short a given market while others are currently long - something which would not at all be unexpected with algorithmic trading - I could face rejections which interrupt trading.  

 

Sorry for all the questions.  I've never faced so many oddities when integrating with a broker before.  I understand it's my account limitations, being a US citizen, and not your platform.  Just trying to figure them all out.  I'm really being thrown for a loop!  :) 

 

Thanks!

Share this post


Link to post

Hello,

Quick post to keep you updated. For question 2, I passed that on to the development team to look into and will reply when I have their answer.

I'm also looking at your questions 1 & 3 and will reply again when I have a response to those as well!

Kind Regards, PM

Share this post


Link to post

Hi,

Since my reply to your Q1 is related to my answer to Q3, I shall answer Q3 first then Q1. :D

3) Yes this is expected behaviour. Several years ago, the US regulator passed a rule/law that forbade US clients from opening simultaneous long and short positions in the same Forex pair, which in the spot Forex world is known as "Hedging". Additionally, this lead to the First In First Out (FIFO) rule, where if you have multiple buy trades in GBP/USD for example, placing sell trades always closes the first trade you opened, then the 2nd etc.

1) As you mentioned in your Q3, your systems may trade in opposite directions in the same market. This could lead you to being flat a market whereas your systems "think" you have 2 positions open. EG: system 1 buys 10,000 in GBP/USD and then a little later system 2 sells 10,000 in GBP/USD. Your account is now actually flat with zero open position in GBP/USD.   

Hence, trading multiple systems on the same market is not advisable. Better would be 1 trading system per forex pair so that they never interfere with each other.

From what you wrote (quoted below), it sounds as though you are thinking of mapping the close trade to system Y instead of booking it to X, even though the fill notification is for X? I think this could get very confusing if you have multiple open positions (in the same direction of course) for both System X and Y. 

Quote

 If system X enters the market, then system Y enters the market, then system Y hits an exit condition, I'll see system X's orders get the fill notifications.  I've been playing with some other angles of attack.  

The platform keeps records of all orders it places.  My best thought at this point is to, any time I get an order update from LS, query the REST APIs for all active orders (including stops/limits).  Compare what I know I've placed and was most recently active vs what I get back from the APIs.  There's still room for error there, unfortunately.  But it seems to be my best shot, from what I can tell.  Question 1: Should there be any concerns with that that you know about?

 

I'm sorry it couldn't be better news, but our hands are tied by the regulators in this case. Will update you on Q2 as soon as I hear back from the dev team.

Kind Regards, PM

Share this post


Link to post

Hello PM,

 

First off, thanks so much for the prompt and clear replies.  I really appreciate it!

 

Yeah, as you pointed out, my systems could trade opposite directions, hedging/flatting my account position, while they each believe they are still in the market.  Obviously I'm not making a lot of money when that's going on, but the systems do manage themselves and will close out what they think is their position at some point, always resulting in a true zero position in the platform.  

So I coded up a new workflow which I think solves most of my problems.  When my platform submits an new order/trade via the REST APIs, it correlates the returned broker order ID to my platform's internal ID.  I track those orders, including those which were last known as active.  Any time I get an order update via the LS interface, I query the REST APIs for all active orders.  I compare the new list of active orders vs my most recent list of active orders.  Determine what's gone, whether or not it was because they were filled or cancelled, and forward the updates to my platform.  I then also go through the active orders and make sure they are in the same state they were last time I tracked them.  Any changes are forwarded to the platform.

This seems to get past a lot of the issues.  It's not pretty/ideal, but not dealing with a pretty/ideal situation (special thanks to my government!).  What it doesn't solve is the opposite order sizing issue.  AKA question #3 above, which you confirmed is the case.  

I'm left wondering if I should open a second trading account if I really want/need to go both directions as a US customer.  I could set the platform to use account X for all systems attempting to long the market and account Y for all systems attempting to short the market.  When running lots of systems together in the past, I have been put in the market both directions, sometimes with varying contract sizings, on the same forex pair.  Trying to get ahead of that, even if the answer is there is simply no way to do it.  

That leaves me with question #4 ... can an individual have two accounts?  :) 

 

Thanks!

Share this post


Link to post

Hi,

I have answers to your Q2 from and also Q4.

Q4) Yes, you can have 2 Live accounts. I suggest contacting Client Services and ask them how you would go about opening a 2nd account when you already have one. They can then inform you of the process.

Q2) The order stream contains the 3 responses. Since you are using the API and listening on the LS order stream you will receive a reponse for the Sell Limit order, position 1 being closed, and position 2 with the trade size modification.

The reason why the web platform skips showing a notification of the position 1 close is because the order steam response for position 1 gives the opening price and not the closing price. We decided not to show this notification as it could confuse the client when they see the opening price rather than the closing price.  

Kind Regards, PM

Share this post


Link to post

EDIT: Forgive me - it looks like this issue has already been addressed here: 

I need to be more diligent in looking through past threads before posting! Lol. Anyway, I didn't even think to check the JSON formatting, as I'm using probably the most popular of serializer libraries for the language I'm writing in (Newtonsoft.Json for C#). When in doubt, double check your syntax! 

I can't test this right now, as markets are closed, but if this doesn't solve it come Monday (which would be very surprising), I'm sure I'll be back. :)

Quote

 

Hi PM,

I'm having some trouble with the /order/updatetradeorder endpoint as well - I successfully call it with something like the following POST data to add a stop loss order to an open position:

{\"OrderId\":726370869,\"MarketId\":401484414,\"AutoRollover\":false,\"Direction\":\"buy\",\"Quantity\":1000.0,\"PositionMethodId\":1,\"BidPrice\":108.589,\"OfferPrice\":108.604,\"AuditId\":\"EL55394\",\"IfDone\":{\"Stop\":{\"Guaranteed\":false,\"Direction\":\"sell\",\"TriggerPrice\":108.545,\"Applicability\":\"GTC\",\"Quantity\":1000.0}},\"Close\":[],\"AllocationProfileId\":0,\"TradingAccountId\":*********}

The call returns 200, and tells me the order was accepted as OK. However, the return data doesn't have any IfDone, or attached orders to the parent order. In checking my Forex.com backend for the demo account, I also don't see any new orders in my list of active orders. 

Thinking it might just be a weird glitch, I waited until the price hit the trigger price of the attempted stop loss order to see if it triggered anyway. Unfortunately, nothing happened.

I've tried this with a variety of parameter variations on both stop and limit orders (including setting the ParentOrderId parameter), but haven't had one be successfully added yet, even though the returned order code says that it was accepted in every case.

Am I missing something in my POST request?

Thanks,

Garrett 

 

 

Share this post


Link to post

Hi Garrett,

It does appear to be missing square brackets is the culprit. Glad you managed to spot this and hope it has fixed the problem already.

If it is still causing an error please inform us and we can investigate further.

Kind Regards, PM

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×