Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Help with OCO order in a strategy

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Help with OCO order in a strategy

    Plz help me correct this code. It compiles but unable to enable the strategy. My goals is to enter long based on some criteria and as soon i am long, want to place OCO order to book profit or loss.

    Code:
            protected override void OnBarUpdate()
            {
                if (BarsInProgress != 0)
                    return;
    
                if (CurrentBars[0] < 2)
                    return;
    
                 // Set 1
                if ((Close[0] > Close[1])
                     && (Position.MarketPosition == MarketPosition.Flat)
                     && (CrossAbove(Close, EMA1, 1))
                     && (ATR1[0] > ATRLength))
                {
                    Targetprice = (ATR1[0] / 2) ; // store a target price to exit
                    Stopprice = (Low[2] - 0.25) ; // store a stop price to exit
                    SubmitOrderUnmanaged(0, OrderAction.Buy, OrderType.Market, 1, 0,  0, "oco1", "emascalp"); // entr long if the criteria is met
                    
                }
                
                if ((Position.MarketPosition == MarketPosition.Long)) // if long position established
                {
                    SubmitOrderUnmanaged(0, OrderAction.Sell, OrderType.Limit, 1, Targetprice,  0, "oco1", "emascalp"); //submit oco profit booking order
                    SubmitOrderUnmanaged(0, OrderAction.Sell, OrderType.StopMarket,  1, Stopprice,  0, "oco1", "emascalp");// submit oco loss booking order
                }
                
            }
            ​

    #2
    Hello percy3687,

    Thank you for your post.

    If your strategy is getting automatically disabled, what is the full error being shown in the Log tab of the Control Center?

    Please note that OCO strings should not be reused. Use unique strings for each OCO group, and reset after orders in that group are filled/canceled.

    Below is also a link to a sample script which demonstrates submitting orders using the Unmanaged Approach:

    Hi, Is there a collection of sample/reference strategies that illustrates how to use the unmanaged order approach in advanced scenarios which also illustrates all the things one would need to be concerned about vs using the managed approach? Is there something like that you can point me to? Thanks, FaaastEddy

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by CarlTrading, 03-31-2026, 09:41 PM
    1 response
    43 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by CarlTrading, 04-01-2026, 02:41 AM
    0 responses
    20 views
    0 likes
    Last Post CarlTrading  
    Started by CaptainJack, 03-31-2026, 11:44 PM
    0 responses
    30 views
    1 like
    Last Post CaptainJack  
    Started by CarlTrading, 03-30-2026, 11:51 AM
    0 responses
    48 views
    0 likes
    Last Post CarlTrading  
    Started by CarlTrading, 03-30-2026, 11:48 AM
    0 responses
    38 views
    0 likes
    Last Post CarlTrading  
    Working...
    X