Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Adding To Position

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

    Adding To Position

    I would like to know how to program a strategy that adds to a position every X pips as long as the entry conditions are still valid and then moves the stoploss up by the same amount each time a new position is added.

    Example:

    if(MA1 crosses MA2)
    if(MA1 > MA2)
    EnterLong(100000);

    Then I would like to add another position at that 1st long entry plus 20 pips and move the trade1 stop up to breakeven. So now I am long 2 lots and have one stop at an initial value and another stop at breakeven. This can continue until the positions are either stopped out or the max number of positions has been reached.

    This is asking a lot so any direction on where to start will be greatly appreciated. I am familiar with basic NinjaScript and have already written what I consider basic strategies.

    Cheers!

    #2
    Hello teak2,

    Thanks for the post and welcome to the NinjaTrader forums.
    For scaling in, you will want to take a look at EntryHandling and EntriesPerDirection properties. These, along with the entry signal names, will determine whether additional entries are submitted.

    For dynamically moving stop loss orders, you can follow this reference sample:
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      OK I will try this again.

      I want to buy if MA2 > MA1 every 20 pips as long as MA2 remains above MA1 and they are not stopped out by a trailing stop. I also want it not to take on a new position if the positions are stopped out.

      How do I set this every 20 pips entry orders in. I know at the first cross I could add a number of buy stops spaced every 20 pips but there is no way to know how many orders to have working.

      What is the best way to tackle this?

      Cheers!

      Comment


        #4
        We unfortunately could not custom code this for you, but you may consider explaining your strategy requirements to a NinjaScript consultant:
        Ryan M.NinjaTrader Customer Service

        Comment


          #5
          I am not looking for anyone to do it for me just a little more direction than "look at EntryHandling and EntriesPerDirection properties." I did not think this was so complex but maybe I am wrong.

          Comment


            #6
            We unfortunately can't provide custom coding services and it sounds like you have the basics of your idea down.

            You place stop orders at various price levels. The number of orders is determined based on those properties and the signal names. If you are unsure how to code this and have limited time or programming experience, then a NinjaScript consultant is the best way to see your ideas realized into code.
            Ryan M.NinjaTrader Customer Service

            Comment


              #7
              First off, thank you for your help I do appreciate it!

              In using multiple entries and exits with IOrder orders would I use the following code for each order?
              Code:
              protected override void OnOrderUpdate(IOrder order)
              {
                   if (entryOrder != null && entryOrder == order)
                   {
                        Print(order.ToString());
                        if (order.OrderState == OrderState.Filled)
                             entryOrder = null;
                   }
              }
              so it would look like:
              Code:
              protected override void OnOrderUpdate(IOrder order)
              {
                   if (entryOrder1 != null && entryOrder1 == order)
                   {
                        Print(order.ToString());
                        if (order.OrderState == OrderState.Filled)
                             entryOrder1 = null;
                   }
              
               if (entryOrder2 != null && entryOrder2 == order)
                    {
                         Print(order.ToString());
                         if (order.OrderState == OrderState.Filled)
                              entryOrder2 = null;
                    }
              }
              and so on for the number of entry orders I have?

              Cheers!

              Comment


                #8
                Yes, will need to follow best practices for all IOrders and you have most of this there.

                Before assigning (submitting the order), check for a null value. When the order is filled or cancelled, reset back to null.
                Ryan M.NinjaTrader Customer Service

                Comment


                  #9
                  Thank you. Where do I find the
                  best practices for all IOrders

                  Comment


                    #10
                    In the reference samples and help guide examples. See below for two of these.
                    The OnOrderUpdate() and OnExecution() methods are reserved for experienced programmers. Instead of using Set() methods to submit stop-loss and profit target orders, you can submit and update them manually through the use of IOrder and IExecution objects in the OnOrderUpdate() and OnExecution() methods. The OnOrderUpdate()

                    When using NinjaTrader's Enter() and Exit() methods, the default behavior is to automatically expire them at the end of a bar unless they are resubmitted to keep them alive. Sometimes you may want more flexibility in this behavior and wish to submit orders as live-until-cancelled. When orders are submitted as live-until


                    This is the IOrders section of our help guide:
                    Ryan M.NinjaTrader Customer Service

                    Comment


                      #11
                      How can I access the values of the variable LongSet and LongPrice in the second condition to resubmit the same orders at the same prices?

                      Code:
                          
                       if (CrossAbove(SMA(20), SMA(50), 1))
                                  {
                                      EnterLong(50000, "LE1");
                                      EnterLongStop(50000, GetCurrentAsk()+100 * TickSize, "LE2");
                                      EnterLongStop(50000, GetCurrentAsk()+200 * TickSize, "LE3");
                                      LongSet = true;
                                      double LongPrice = GetCurrentAsk();
                                  }
                      
                      if (LongSet = true && SMA(20)[0] > SMA(50)[0])
                                  {
                                      EnterLongStop(50000, LongPrice + 100 * TickSize, "LE2");
                                      EnterLongStop(50000, LongPrice + 200 * TickSize, "LE3");
                                  }
                      
                      if (SMA(20)[0] <= SMA(50)[0])
                      { LongSet = false }
                      I decided this approach would be fine to start with then if needed I can get into the more complicated IOrders coding.

                      Cheers

                      Comment


                        #12
                        It looks like you are accessing these values in the snippet you posted. You're using LongSet in the second condition and then LongPrice for the orders you're submitting.
                        Last edited by NinjaTrader_RyanM1; 09-01-2011, 05:07 PM.
                        Ryan M.NinjaTrader Customer Service

                        Comment


                          #13
                          Thank you Ryan. I am still receiving this error:

                          "The name 'LongPrice' does not exist in the current content"

                          Any idea on how to fix this?

                          Cheers

                          Comment


                            #14
                            teak2,

                            Did you declare the private bool LongSet = false; in the variables section of your code first?

                            If you comment out the double LongPrice = GetCurrentAsk(); line, can you compile the error, or do you receive other compile errors?
                            MatthewNinjaTrader Product Management

                            Comment


                              #15
                              1) In the variable declaration section I have this:

                              Code:
                              private bool longSet = false;
                              2) I do not get the error for LongPrice when I declare it in this section:
                              Code:
                              if (CrossAbove(SMA(20), SMA(50), 1) && !LongSet)
                                          {
                                              EnterLong(50000, "LE1");
                                              EnterLongStop(50000, GetCurrentAsk()+100 * TickSize, "LE2");
                                              EnterLongStop(50000, GetCurrentAsk()+200 * TickSize, "LE3");
                                              LongSet = true;
                                              double LongPrice = GetCurrentAsk();
                                          }
                              but when I try to call it on the next section to resubmit the orders assuming LongSet = true.

                              Am I missing something?

                              Cheers!

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              656 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              371 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              109 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              574 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              579 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X