Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Script for OCO orders with autobreakeven

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

    Script for OCO orders with autobreakeven

    Hello,

    For an open operation, I need to create a stop order and a limit OCO order (one cancels the other) using a script that I will execute using a button.
    And for the SL order, I need to indicate autobreakeven.

    Can you give me an example?

    Thanks​

    #2
    Hi,

    I'm trying something like this:

    Code:
                        //miramos si hay alguna posición abierta
                        outputLog(account.Positions.Count.ToString());
                        if (account.Positions.Count > 0)
                        {
                            //hay una posición abierta
                            if(account.Orders.Any(o => o.Instrument.FullName == instrument.FullName && (o.OrderState == OrderState.Working || o.OrderState == OrderState.Accepted)))
                                outputLog("Ya hay órdenes abiertas para este instrumento");
                            else
                            {
                                //abrir las órdenes SL / TP OCO
                                //SL
                                Order newOrderSL = account.CreateOrder(instrument, account.Positions[0].MarketPosition == MarketPosition.Long ? OrderAction.Sell : OrderAction.Buy,
                                                    OrderType.StopMarket, OrderEntry.Automated, TimeInForce.Gtc, account.Positions[0].Quantity, 0, 20000, "TESTOCO", "ORDERNAMESL",
                                                    Core.Globals.MaxDate, null);
                                //TP
                                Order newOrderTP = account.CreateOrder(instrument, account.Positions[0].MarketPosition == MarketPosition.Long ? OrderAction.Sell : OrderAction.Buy,
                                                    OrderType.Limit, OrderEntry.Automated, TimeInForce.Gtc, account.Positions[0].Quantity, 20500, 0, "TESTOCO", "ORDERNAMETP",
                                                    Core.Globals.MaxDate, null);
                            }
                        }
    ​
    This works OK but I don't know how configure the autobreakeven for SL order, like ATM Strategy works.

    Thanks
    Last edited by Powerbucker; 10-07-2024, 06:34 AM.

    Comment


      #3
      Hello Powerbucker,

      I have a few examples you may find helpful.

      Placing orders with a button click.


      Using the addon approach to place a pair of OCO orders with custom stop price management.
      ProfitChaseStopTrailAddonExample - https://support.ninjatrader.com/s/ar...ase-Stop-Trail
      Chelsea B.NinjaTrader Customer Service

      Comment


        #4
        Ok, thanks !

        I understand in this code you are managing the stoploss to breakeven manually.

        My question is the stoploss order does not have a property to program the autobreakeven as it would be done from the UI?

        Click image for larger version

Name:	image.png
Views:	186
Size:	5.1 KB
ID:	1320661

        This way I don't have to program it through a script.

        Thanks​

        Comment


          #5
          Hello Powerbucker,

          "I understand in this code you are managing the stoploss to breakeven manually."

          This is incorrect.
          In the ProfitChaseStopTrailAddonExample the stop price is managed with logic on lines 159 to 170.

          "My question is the stoploss order does not have a property to program the autobreakeven as it would be done from the UI?"

          Unfortunately no, when directly sending orders through an account the breakeven will have to executed by the custom logic in the script.

          If you want to use an Atm Strategy template, this would be possible instead.

          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            Hi,

            when I say "manually" is by script

            My main problem is I need create the order by UI (limit or market) and when the position is open, then configure the SL/TP and breakeven. If I open and order without ATM, and once position is open I select an ATM Strategy, the system doesn't create the orders.


            So, is possible start an ATM Strategy (manually or by script) once the position is open? Then I know how is the SL, TP and autobreakeven.

            Thanks

            Comment


              #7
              Hello Powerbucker,

              Manually would indicate that you are using the mouse to manually change the price of an order either on a chart or on the Orders tab of the Control Center.
              Automated would mean the order is being adjusted by code in a script.

              "So, is possible start an ATM Strategy (manually or by script) once the position is open? Then I know how is the SL, TP and autobreakeven."

              Unfortunately, there is not a way to automate starting an Atm Strategy for an open position. This would have to be associated to a specific entry order that is submitted by the script.

              But you can use logic in the script to place a stop order and limit for an open position and adjust the prices of the order with logic in the script.
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                ok, thanks !!

                Comment


                  #9
                  Hi,

                  I have a question about this way of creating orders:

                  If I do it with an ATM,

                  Code:
                                          Order entryOrder = account.CreateOrder(orderFake.Instrument, orderFake.OrderAction, orderFake.OrderType,
                                              TimeInForce.Gtc, orderFake.Quantity, 0, 0, "", "Entry", null);
                      
                                          // Submits our entry order with the ATM strategy named "myAtmStrategyName"
                                          AtmStrategy.StartAtmStrategy("ATM Isra 100", entryOrder);
                  the orders come out this way and it allows me to configure the AutoBreakEven.

                  image Order1.png

                  If I do it independently,

                  Code:
                  Order entryOrder = account.CreateOrder(orderFake.Instrument, orderFake.OrderAction, orderFake.OrderType,
                  TimeInForce.Gtc, orderFake.Quantity, 0, 0, "", "Entry", null);
                  
                  string oco = Guid.NewGuid().ToString("N");
                  
                  double SL = getIsraPos(button).getCurrentSL();
                  Order slOrder = account.CreateOrder(orderFake.Instrument, orderFake.OrderAction == OrderAction.Buy ? OrderAction.Sell:OrderAction.BuyToCover,
                  OrderType.StopMarket,TimeInForce.Gtc,orderFake.Qua ntity, 0, SL, oco, "Stop1", null);
                  
                  double TP = getIsraPos(button).getCurrentEntry();
                  Order tpOrder = account.CreateOrder(orderFake.Instrument, orderFake.OrderAction == OrderAction.Buy ? OrderAction.Sell:OrderAction.BuyToCover,
                  OrderType.Limit,TimeInForce.Gtc,orderFake.Quantity , TP, 0, oco, "TakeProfit1", null);
                  
                  account.Submit(new [] {entryOrder, slOrder, tpOrder});
                  ​
                  it creates the orders of another type (different color) and does not let me configure the autoBreakeven, it comes out deactivated.

                  ​image Order2.png

                  I have checked and the type of orders is the same, can you help me?​

                  Thanks
                  Attached Files

                  Comment


                    #10
                    Hello Powerbucker,

                    I'm not sure what the issue is.

                    The AtmStrategy.StartAtmStrategy() method starts an ATM strategy from an Atm template which would may a stop strategy configured.

                    If AtmStrategy.StartAtmStrategy() is not used, then there is no Atm, and any order modifications would have to be done with logic in the script. The ProfitChaseStopTrailAddonExample linked in post # 3 demonstrates how to modify the stop price using logic.

                    What specifically do you need help with?

                    Do you need help understanding the example and how to modify the price of an order with logic?
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      I would like to know how to create an SL order that can then configure the autobreakeven, as if I created it by ATM

                      Because I can't create a predefined SL order by script by ATM

                      Thanks​

                      Comment


                        #12
                        Hello Powerbucker,

                        You can find a sample in the following link that shows how to do a break even in code from a strategy. It would be the same general concept if you are using an addon, you would have to locate the position in the accounts position collection and get the average price and then make a condition that uses that to move the order.

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                        0 responses
                        557 views
                        0 likes
                        Last Post Geovanny Suaza  
                        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                        0 responses
                        324 views
                        1 like
                        Last Post Geovanny Suaza  
                        Started by Mindset, 02-09-2026, 11:44 AM
                        0 responses
                        101 views
                        0 likes
                        Last Post Mindset
                        by Mindset
                         
                        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                        0 responses
                        545 views
                        1 like
                        Last Post Geovanny Suaza  
                        Started by RFrosty, 01-28-2026, 06:49 PM
                        0 responses
                        547 views
                        1 like
                        Last Post RFrosty
                        by RFrosty
                         
                        Working...
                        X