Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Profit target order using account.CreateOrder

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

    Profit target order using account.CreateOrder

    How do I add profit target order using account.CreateOrder?
    I have an indicator with a custom button on chart, that buys/sells based on conditions. See code snippet below.
    In the strategy, SetProfitTarget/SetStopLoss can achieves this. What is the equivalent way in Indicator class, to submit OCO bracket orders?

    Code:
    account.CreateOrder(Instrument, OrderAction.Buy, OrderType.Market, OrderEntry.Automated, TimeInForce.Day, 1, 0, 0, string.Empty, "Long", new DateTime(), null);
    account.Submit(new Order[] { mar****rder });

    #2
    Hello yubo27,

    You will want to use OrderType.Limit if you want a limit order profit target.

    You may find the example linked below very helpful.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by yubo27 View Post
      How do I add profit target order using account.CreateOrder?
      I have an indicator with a custom button on chart, that buys/sells based on conditions. See code snippet below.
      In the strategy, SetProfitTarget/SetStopLoss can achieves this. What is the equivalent way in Indicator class, to submit OCO bracket orders?

      Code:
      account.CreateOrder(Instrument, OrderAction.Buy, OrderType.Market, OrderEntry.Automated, TimeInForce.Day, 1, 0, 0, string.Empty, "Long", new DateTime(), null);
      account.Submit(new Order[] { mar****rder });
      When submitting orders from an indicator, it's probably best
      to just use ATMs.

      Why? Because when your entry order fills, the ATM handles
      the profit target/stop loss OCO orders automatically -- and
      because it's an ATM, the user gets to manage the trade
      themselves using the Chart Trader buttons, such as Rev
      and Close.

      I do the same thing as you, but I get the entry order details
      from the Chart Trader, meaning I use the ATM strategy the
      user has selected, and the quantity, and the account.

      Something like this,

      Code:
      ChartControl.Dispatcher.InvokeAsync((Action)(() =>
      {
          Account a = ChartControl.OwnerChart.ChartTrader.Account;
          int qty = ChartControl.OwnerChart.ChartTrader.Quantity;
          AtmStrategy atm = ChartControl.OwnerChart.ChartTrader.AtmStrategy;
          Instrument instr = ChartControl.OwnerChart.ChartTrader.Instrument;
          Order o = a.CreateOrder(instr, OrderAction.Buy, OrderType.Market, OrderEntry.Automated,
                                  TimeInForce.Day, qty, 0, 0, "", "Entry", Core.Globals.MaxDate, null);
          if (atm == null)
              // AtmStrategy was set to 'None' in ChartTrader
              a.Submit(new[] { o });
          else
              NinjaTrader.NinjaScript.AtmStrategy.StartAtmStrategy(atm, o);
      }));
      ​
      An example indicator using this approach can be found here.

      Last edited by bltdavid; 12-01-2023, 11:26 AM. Reason: Added link

      Comment


        #4
        The entry order can be anything you want, it doesn't
        have to be a Market order, it can be any type.

        In my latest version of that code, I've wrapped it inside
        a function which allows the entry order to be of any type.

        For example,

        Code:
        private void TradeAssistant_PlaceOrder(int orderDirection, OrderType orderType, double limitPrice, double stopPrice)
        {
            ChartControl.Dispatcher.InvokeAsync((Action)(() =>
            {
                Account a = ChartControl.OwnerChart.ChartTrader.Account;
                int qty = ChartControl.OwnerChart.ChartTrader.Quantity;
                AtmStrategy atm = ChartControl.OwnerChart.ChartTrader.AtmStrategy;
                Instrument instr = ChartControl.OwnerChart.ChartTrader.Instrument;
                Order o = a.CreateOrder(instr, orderDirection == LONG ? OrderAction.Buy : OrderAction.Sell, orderType,
                                        OrderEntry.Automated, TimeInForce.Day, qty, limitPrice, stopPrice, "", "Entry", Core.Globals.MaxDate, null);
                if (atm == null)
                    // AtmStrategy was set to 'None' in ChartTrader
                    a.Submit(new[] { o });
                else
                    NinjaTrader.NinjaScript.AtmStrategy.StartAtmStrategy(atm, o);
            }));
        }
        ​
        Call it like something this,

        Code:
        public const int LONG = 1;
        public const int FLAT = 0;
        public const int SHORT = -1;
        
        // various examples
        ​TradeAssistant_PlaceOrder(LONG, OrderType.Market, 0, 0);
        ​TradeAssistant_PlaceOrder(LONG, OrderType.Limit, GetCurrentBid(), 0);
        ​TradeAssistant_PlaceOrder(SHORT, OrderType.Limit, GetCurrentAsk(), 0);
        ​TradeAssistant_PlaceOrder(LONG, OrderType.Limit, Close[0], 0);​
        Last edited by bltdavid; 12-02-2023, 04:28 AM. Reason: typo

        Comment


          #5
          Thanks bltdavid.

          For exit order placed by AtmStrategy, is there way to further modify stop-limit loss order's limit price through script? The default one has limit price equals stop price.

          I need to modify stop-limit order's limit price to cross market (ie. above Ask for buy, below Bid for sell), to insure market order type of execution. This is also due to broker IB's restriction on stop market order outside RTH.

          In the Strategy, order can be modified using ChangeOrder(). How can this be done in Indicator?

          Comment


            #6
            Hi bltdavid,

            I tried your code, it doesn't work.

            In indicator class, using AtmStrategy.StartAtmStrategy(atm, order) does not start Atm strategy. The order is created but hangs with "initialized" state.

            Adding line account.Submit(new[] { order }) does submit the order. But does not start Atm strategy.

            Comment


              #7
              Originally posted by yubo27 View Post
              I tried your code, it doesn't work.
              Hmm, works for me.
              Do you get an error message?

              Originally posted by yubo27 View Post
              ​In indicator class, using AtmStrategy.StartAtmStrategy(atm, order) does not start Atm strategy. The order is created but hangs with "initialized" state.

              I've never seen that.

              I use the fully qualified name,

              Code:
              NinjaTrader.NinjaScript.AtmStrategy.StartAtmStrategy
              You are correct, this method 'does not start Atm strategy'. It is used
              to submit entry orders with an associated Atm strategy -- you should
              not have submit the entry order itself, since that is done for you when
              you use this method.

              What does 'order' look like?

              Can you provide a minimal working indicator
              with the exact code that is causing this problem?

              Who is your broker?
              Was this a live order or a Sim101 order?

              I'm using that code with Rithmic based accounts, such
              as Apex and Leeloo, and it seems to work fine.

              Comment


                #8
                Below is the code. The broker is IB.
                Without last line account.Submit(new[] { ord }), order will hang with initialized state.
                With last line, order will be submitted. But no Atm strategy is started. "atm" object is confirmed not null, and properly set.

                Code:
                public class AtmBuyButton : Indicator
                {
                ​.....
                
                  private void OnButtonClick(object sender, RoutedEventArgs rea)
                   {
                       atm_buy();
                   }
                
                   private void atm_buy(){
                     ord = account.CreateOrder(Instrument, "Buy", OrderType.Limit, OrderEntry.Automated, TimeInForce.Gtc, size,
                         GetCurrentAsk();,0,
                         string.Empty, "Entry atm", new DateTime(), null);
                
                
                     AtmStrategy atm = ChartControl.OwnerChart.ChartTrader.AtmStrategy;
                     if (atm != null)
                        NinjaTrader.NinjaScript.AtmStrategy.StartAtmStrategy(atm, ord);
                
                     account.Submit(new[] { ord });
                    }
                }
                ​
                Last edited by yubo27; 12-03-2023, 04:16 PM.

                Comment


                  #9
                  Never mind. Changing "Entry atm" to "Entry" works.
                  This is such a hard-coded hidden condition, that should not exist.

                  Comment


                    #10
                    Originally posted by yubo27 View Post
                    Never mind. Changing "Entry atm" to "Entry" works.
                    This is such a hard-coded hidden condition, that should not exist.
                    I would agree.

                    But, in NT's defense, this requirement is clearly documented.

                    Comment


                      #11
                      Hi there,
                      when using account.createorderto buy at the best bid price ,how do I retrieve/set the double limitPrice as indicated in the syntax?

                      CreateOrder(Instrument instrument, OrderAction action, OrderType orderType, OrderEntry orderEntry, TimeInForce timeInForce, int quantity, double limitPrice, double stopPrice, string oco, string name, DateTime gtd, CustomOrder customOrder)

                      thanks

                      Comment


                        #12
                        Hello judysamnt7,

                        This would be the price you are are wanting to place the order at.

                        An indicator is provided an Open, High, Low, and Close series which provides the bar pricing information which can be used to calculate a limit price.
                        The current ask and bid is provided with GetCurrentAsk() and GetCurrentBid() which can be used to calculate a limit price.
                        You can call other indicators and use those to calculate a limit price.

                        As an example if I wanted to submit a buy limit order 10 ticks below the ask.
                        double myLimitPrice = GetCurrentAsk() - 10 * TickSize;
                        myAccount.CreateOrder(Instrument, OrderAction.Buy, OrderType.Limit, OrderEntry.Automated, TimeInForce.Gtc, 1, myLimitPrice, 0, string.Emtpy, "myEntryLimit", Core.Globals.MaxDate, null);
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          thanks for your response, Chelsea.
                          another question: what does OrderEntry.Automated do as opposed to OrderEntry.Manual ?

                          Comment


                            #14
                            Hello judysamnt7,

                            This marks the order for the exchange. Many exchanges require you to disclose if the order is being submitted by an automated script or if the order is being submitted manually.
                            Chelsea B.NinjaTrader Customer Service

                            Comment


                              #15
                              Do the ATM send the order to the exchange as OrderEntry.Automated or OrderEntry.Manual ?

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by richt3, Today, 10:28 PM
                              0 responses
                              5 views
                              0 likes
                              Last Post richt3
                              by richt3
                               
                              Started by Atychiphobia66, Today, 10:22 PM
                              0 responses
                              7 views
                              0 likes
                              Last Post Atychiphobia66  
                              Started by Scalper1969, Today, 09:24 PM
                              0 responses
                              11 views
                              0 likes
                              Last Post Scalper1969  
                              Started by RISKYBUSINEZZ, 12-12-2023, 05:11 PM
                              3 responses
                              127 views
                              0 likes
                              Last Post judysamnt7  
                              Started by judysamnt7, Today, 08:18 PM
                              0 responses
                              5 views
                              0 likes
                              Last Post judysamnt7  
                              Working...
                              X