Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by fx.practic, 10-15-2013, 12:53 AM
                    5 responses
                    5,403 views
                    0 likes
                    Last Post Bidder
                    by Bidder
                     
                    Started by Shai Samuel, 07-02-2022, 02:46 PM
                    4 responses
                    94 views
                    0 likes
                    Last Post Bidder
                    by Bidder
                     
                    Started by DJ888, Yesterday, 10:57 PM
                    0 responses
                    6 views
                    0 likes
                    Last Post DJ888
                    by DJ888
                     
                    Started by MacDad, 02-25-2024, 11:48 PM
                    7 responses
                    158 views
                    0 likes
                    Last Post loganjarosz123  
                    Started by Belfortbucks, Yesterday, 09:29 PM
                    0 responses
                    8 views
                    0 likes
                    Last Post Belfortbucks  
                    Working...
                    X