Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Scaling into a position

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

    Scaling into a position

    I am trying to scale into a position based on an indicator. Basically I want it to buy 10% if a condition is met. If the price goes lower than my previous entry price before meeting my exit conditions, I want to buy 20%. Repeat for 30% and 40%. My strategy is close to doing this, but for whatever reason, it will not enter long for two consecutive days. It wants to skip a day between entries. I think I have something wrong with my bar updating. Whatever it is, its probably something simple. Here is sample code...I am very bad at programming...

    protectedoverridevoid Initialize()
    {
    CalculateOnBarClose = true;
    EntriesPerDirection = 1;
    EntryHandling = EntryHandling.UniqueEntries;
    }
    ///<summary>
    /// Called on each bar update event (incoming tick)
    ///</summary>
    protectedoverridevoid OnBarUpdate()
    {
    // Condition set 1 - RSI under 25 for two days
    if (RSI(2, 1).Avg[0] < 25
    && RSI(2, 1).Avg[1] < 25)
    {
    EnterLong(10, "Entry1");
    Variable0 = Close[0];
    Variable1 = 0;
    Variable2 = 0;
    Variable3 = 0;
    }
    // Condition set 2
    if (Close[0] < Variable0)
    {
    EnterLong(20, "Entry2");
    Variable1 = Close[0];
    Variable0 = 0;
    Variable2 = 0;
    Variable3 = 0;
    }
    // Condition set 3
    if (Close[0] < Variable1)
    {
    EnterLong(30, "Entry3");
    Variable2 = Close[0];
    Variable0 = 0;
    Variable1 = 0;
    Variable3 = 0;
    }
    // Condition set 4
    if (Close[0] < Variable2)
    {
    EnterLong(40, "Entry4");
    Variable0 = 0;
    Variable1 = 0;
    Variable2 = 0;
    Variable3 = Close[0];
    }
    // Condition set 5
    if (RSI(2, 1).Avg[0] > 70)
    {
    ExitLong("", "");
    Variable0 = 0;
    Variable1 = 0;
    Variable2 = 0;
    Variable3 = 0;
    }
    }

    #2
    Hello ForrToz,

    Welcome to the NinjaTrader forums!

    What's most likely happening here is that the first condition evaluates to true still, and Variable 0 is updated with the most recent close price.

    The condition you're testing for in Entry2 becomes Close[0] < Close[0], and this effect cascades through your whole strategy.

    If you want to check Close[0] compared to your entry price, and maintain the sequence you're trying to code, your second entry condition can be:

    if (Variable0 < Position.AvgPrice)
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Thanks for the reply. I think I got it now. Missing the all important "return;" It seems to work in that respect now. Now the only problem I'm having is some minor issues with the purchase prices it uses in enterlong(), but hopefully that is just data issues that can be easily sorted out. Thanks again.

      Comment


        #4
        Oh, and I see what you mean with using average price...I might try that to see if it helps my problem. Its not exactly what I'm after though. Is there Position.LowestPrice or similar function I could use instead?

        Comment


          #5
          There isn't a built in function for Position.LowestPrice. You could build one, working with the individual execution price for orders.
          .
          This is advanced NinjaScript and requires structuring the orders differently. If you wanted to get started on this, this sample can help working with IOrder objects and the IExecution interface:

          Using OnOrderUpdate() and OnExecution() methods to submit protective orders
          Ryan M.NinjaTrader Customer Service

          Comment


            #6
            Thanks. I will check that out.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            633 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            364 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            105 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            567 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            568 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X