Jump to content
Sign in to follow this  
tmlatham

Buy with Stop/Limit, but Stop/Limit needs to be OCO

Recommended Posts

Hello,

     This is my first post on this forum :-)  I am working with the API, through a demo account, and have created a buy limit order (order/newstoplimitorder) with a corresponding sell stop/limit.  Here is my JSON:

{
  "MarketId": 401484347,
  "AutoRollover": false,
  "Direction": "Buy",
  "Quantity": 10000,
  "PositionMethodId": 1,
  "BidPrice": 1.18175,
  "OfferPrice": 1.18187,
  "TriggerPrice": 1.18237,
  "TradingAccountId": XXXXXXXXX,
  "IfDone": [
    {
      "Stop": {
        "TrailingDistance": 10,
        "Direction": "SELL",
        "Quantity": 10000
      }
    },
    {
      "Limit": {
        "TriggerPrice": 1.18449,
        "Direction": "SELL",
        "Quantity": 10000
      }
    }
  ]
}

 

The "problem" is that the Stop/Limit orders need to be OCO orders because after the Buy order goes through, both of these orders are submitted. Once one has completed the other one stays out there.  That's why they need to be OCO.  I know this can be done because I am able to create that scenario through the tradingview web site.  My questions is how can this be accomplished?  I have tried several combinations of IFDone and OcoOrder to no avail.  A working JSON example would be perfect!  An explanation would work also.  Thanx in advance!

 

Share this post


Link to post

Hello,

If I understood your request correctly, once your Buy entry order is filled and becomes an open position, you want to have a stop loss and a take profit Limit order attached to the open position. Should either the stop loss or the take profit Limit be hit you then want the other one to be closed? 
If correct then the Stop Loss and take profit Limit should use the If/Done fields as in your example. However, I see a few fields have not been used. The following JSON should achieve what you are looking to do. 

Some field values have to be edited of course. :D

{
    "MarketId": 401484347,
    "OrderId": 0,
    "Direction": "buy",
    "Applicability": "gtc",
    "ExpiryDateTimeUTC": null,
    "Quantity": 1000,
    "BidPrice": 1.18297,
    "OfferPrice": 1.18315,
    "AuditId": "ELU3590891",
    "TradingAccountId": 401637645,
    "TriggerPrice": 1.19,
    "PositionMethodId": 1,
    "IfDone": [
        {
            "Stop": {
                "OrderId": null,
                "Direction": "sell",
                "Applicability": "gtc",
                "ExpiryDateTimeUTC": null,
                "Guaranteed": false,
                "TrailingDistance": 10,
                "Quantity": 1000,
                "TriggerPrice": null,
                "IfDone": [],
                "OcoOrder": null,
                "TriggerLevelCalculationTypeId": null,
                "TriggerLevelCalculationValue": null,
                "Reference": "Origin602"
            },
            "Limit": {
                "OrderId": null,
                "Direction": "sell",
                "Applicability": "gtc",
                "ExpiryDateTimeUTC": null,
                "Quantity": 1000,
                "TriggerPrice": 1.195,
                "IfDone": [],
                "OcoOrder": null,
                "TriggerLevelCalculationTypeId": null,
                "TriggerLevelCalculationValue": null,
                "Reference": "Origin602"
            }
        }
    ],
    "OcoOrder": null,
    "TriggerLevelCalculationTypeId": null,
    "TriggerLevelCalculationValue": null,
    "Reference": "Origin602"
}

 

Kind Regards, PM

 

Share this post


Link to post

You recapped my scenario perfectly!

Unfortunately, that did not change anything.  After either the sell or limit in the IfDone[] becomes active, the other one is not. canceled.  Somehow those two need to be an OCO...

When I create this manually using tradingview they are OCO so I know it can be done.  I am attaching a screenshot of an example.5f48c3f15b9c5_ScreenShot2020-08-26at9_34_36AM.thumb.png.416d5086acf2d034bdb3d17fec14fcf7.png

Share this post


Link to post

Hello,

I just wanted to clarify what was meant by "becomes active" in the line: "After either the sell or limit in the IfDone[] becomes active, the other one is not. canceled."

By active, do you mean

1) when the stop loss or take profit limit has been hit and your position is closed, the other order stays open? As in, you have no open position and a single stop loss / take profit order is left active on your account?

2) Or do you mean that when your entry order is filled and becomes an open position, that the stop loss and take profit both become active?

 

Taking some real numbers in an example to make things clearer as to how it should work. Say EUR/USD is currently trading at 1.1850 and you put in an entry Buy order to open, should the market hit 1.1900 with a 30 point stop loss at 1.1870 and a 60 point profit limit at 1.1960. 

Later, the market hits 1.1900 and your Buy order to open is filled so you now have an open buy position. Both the stop loss order at 1.1870 and the profit limit at 1.1960 become active. They will continue to be active for as long as your position is open. 

The open positoin can then be closed in 1 of 3 ways.

  1. You manually close it
  2. It hits your profit limit 
  3. It hits your stop loss 

 

Say the market rises to 1.1960 and hits your profit limit, this will automatically close your position and then it will automatically cancel your stop loss order at 1.1850. Until your position is closed, both the stop loss and limit will be active. 

Kind Regards, PM

 

Share this post


Link to post

Hello,

    First, thanx for you time on this.  I feel it has to be something obvious that I am missing :-(

 

But it is this scenario:

 

1) when the stop loss or take profit limit has been hit and your position is closed, the other order stays open? As in, you have no open position and a single stop loss / take profit order is left active on your account?

Say the market rises to 1.1960 and hits your profit limit, this will automatically close your position and then it will automatically cancel your stop loss order at 1.1850.

The problem is that my stop loss stays out there after the take profit has occurred.  Then it eventually triggers as a sell with no guard rails.  

Are we sure the stop loss / take profit should not be an OCO within the IfDone?  I have not been able to make it happen but...

 

 

Share this post


Link to post

Hi,

Yes, the stop loss and profit limit in the If/Done fields should work as described.

Are you using a test/demo account or is a real money Live account? If, and only if it is a demo/test account, please send me a private message with the account credentials. I want to send some test calls through the account and observe what happens. 

Kind Regards, PM

Share this post


Link to post

Yes, I am using demo/test account.  I will shut things down so that transactions are not going through then I will send you a private message with that information.  Thanx!

 

Update: 

When I tried to send the message I received: Physicsman cannot receive messages.

Share this post


Link to post

Dooh!!!  I see the problem!  My Stop & Limit orders are not in the same object!  Your example had them like that but I just didn't catch it!  Geez!!  Thanx for your help!!!

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
Sign in to follow this  
×