Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Enter Long Position after Price Drops

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

    Enter Long Position after Price Drops

    Hello Everyone,

    I am working on a strategy to enter a long position once the price drops by -20 ticks from the the highest bar. I have done some working on it and accomplished to enter the long position but there is an error it only enters the long position but when that bar closes not when the condition in the strategy meets.

    I'm encountering an issue with the entry into the long position. It seems to be entering at -30 ticks, regardless of the condition I set.
    For instance, I set the condition to -20 ticks as expected, but it enters at -30 ticks. I also tried various tick numbers like -24, -26, -28, and -30, but the entry still occurs after -30 ticks. When I set numbers greater than -30 (e.g., -36, -38, -40), it doesn’t enter the long position at all. My point is that the system should allow entry when any desired number is set in the condition, whether it’s -20, -25, -35, etc.

    Below are the steps i followed to build the strategy:
    In the Strategy Builder add a double custom series to track the current saved high and use the offset to do math.
    In the first condition set, set the current high series to the value of the previous bar (to carry it forward). Don't add any conditions to this set.
    In the second condition set, add a condition comparing Misc > Current bar, select Equals as the center comparison operator, on the right select Misc > Numeric value > 1.
    In the actions, select Misc > Set CurrentHighSeries, click 'set' in the CurrentHighSeries field, select Price > High.
    In the third condition set, add a condition comparing the Price > High with bars ago 1, Greater, Price > High with bars ago 2.
    In the same (third) condition set, add another condition comparing Price > High with bars ago 0, Greater, Price > High with bars ago 1​
    In the actions, select Misc > Set CurrentHighSeries, click 'set' in the CurrentHighSeries field, select Price > High.
    In the forth condition set, add a condition comparing the Price > Close, select Less as the center comparison operator, on the right select Misc > Custom Series > CurrentHighSeries, and set the Offset Type to Ticks and use -20 as the Value.
    Add an action, select Order management > Enter long position


    and here is my code:
    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class Buyafterdrop : Strategy
    {

    private Series<double> CurrentHighSeries;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Strategy here.";
    Name = "Buyafterdrop";
    Calculate = Calculate.OnBarClose;
    EntriesPerDirection = 1;
    EntryHandling = EntryHandling.AllEntries;
    IsExitOnSessionCloseStrategy = true;
    ExitOnSessionCloseSeconds = 30;
    IsFillLimitOnTouch = false;
    MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
    OrderFillResolution = OrderFillResolution.Standard;
    Slippage = 0;
    StartBehavior = StartBehavior.WaitUntilFlat;
    TimeInForce = TimeInForce.Gtc;
    TraceOrders = false;
    RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
    StopTargetHandling = StopTargetHandling.PerEntryExecution;
    BarsRequiredToTrade = 20;
    // Disable this property for performance gains in Strategy Analyzer optimizations
    // See the Help Guide for additional information
    IsInstantiatedOnEachOptimizationIteration = true;
    TargetPrice = 1;
    }
    else if (State == State.Configure)
    {
    }
    else if (State == State.DataLoaded)
    {
    CurrentHighSeries = new Series<double>(this);
    SetProfitTarget(CalculationMode.Ticks, 28);
    SetStopLoss(CalculationMode.Ticks, 40);
    }
    }

    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;

    if (CurrentBars[0] < 2)
    return;

    CurrentHighSeries[1] = 0;
    // Set 2
    if (CurrentBars[0] == 1)
    {
    CurrentHighSeries[0] = High[0];
    }

    // Set 3
    if ((High[1] > High[2])
    && (High[0] > High[1]))
    {
    CurrentHighSeries[0] = High[0];
    }

    // Set 4
    if (Close[0] < (CurrentHighSeries[0] + (-20 * TickSize)) )
    {
    EnterLong(2, "");
    }

    }
    }
    }​
    Can please anyone guide me why its is entering the long position when the bar closes, it should enter the position once the condition is met.

    Thanks!

    #2
    You can try to modified the entry condition to use the highestHigh variable instead of CurrentHighSeries[0]. This ensures that we're always comparing against the true highest high, even within a bar​

    Comment


      #3
      The main issue here is that your strategy is set to calculate on bar close (Calculate = Calculate.OnBarClose. This means that all conditions are only checked when each bar completes, rather than intrabar​

      Comment


        #4
        Hello maaxk,

        You have mentioned there was an error.

        What is the full error message appearing on the Log tab of the Control Center? (Ctrl + c to copy, Ctrl + v to paste)

        Use Calculate set to OnPriceChange to allow OnBarUpdate() to update for each price change (instead of OnBarClose which updates when the bar closes).
        Chelsea B.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        558 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