Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Help with simple strategy - break of previous daily close

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

    Help with simple strategy - break of previous daily close

    Hi all,

    I have a simple strategy that compiles successfully however it does not show entry and exits on my chart.

    The strategy goes long after the break of the previous close to the up side and exits at 4:55 pm.

    Any idea what could be causing this?

    Code is below:

    #region Using declarations
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Gui;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Gui.SuperDom;
    using NinjaTrader.Gui.Tools;
    using NinjaTrader.Data;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Core.FloatingPoint;
    using NinjaTrader.NinjaScript.Indicators;
    using NinjaTrader.NinjaScript.DrawingTools;
    #endregion

    //This namespace holds Strategies in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class BreakofClose : Strategy
    {
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Strategy here.";
    Name = "BreakofClose";
    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;
    }
    else if (State == State.Configure)
    {
    }
    }

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

    if (CurrentBars[0] < 1)
    return;

    // Set 1
    if (CrossAbove(Close, Close, 1))
    {
    EnterLong(Convert.ToInt32(DefaultQuantity), "");
    }

    // Set 2
    if (Times[0][0].TimeOfDay == new TimeSpan(16, 55, 0))
    {
    ExitLong(Convert.ToInt32(DefaultQuantity), "", "");
    }

    }
    }
    }

    #2
    Hello omermirza, thanks for writing in.

    Your entry condition is never getting reached: CrossAbove(Close, Close, 1)

    The CrossAbove method needs two distinct series because it checks for the following: Series1[0] > Series2[0] && Series1[1] < Series2[1]

    Consider checking the Close against a different series, like the High or Low series. E.g. to check if the Close went above the High of last bar:

    Code:
    if(CurrentBar < 1) return;
    
    if (Close[0] > High[1] && Position.MarketPosition == MarketPosition.Flat)
    {
    Print("Condition True");
    EnterLong(Convert.ToInt32(DefaultQuantity), "");
    }
    When you get unexpected results like this, always use the Print method to print data from your script to see if the entry conditions are becoming true or not.

    Please let me know if any further questions come up.

    Comment


      #3
      Originally posted by NinjaTrader_ChrisL View Post
      Hello omermirza, thanks for writing in.

      Your entry condition is never getting reached: CrossAbove(Close, Close, 1)

      The CrossAbove method needs two distinct series because it checks for the following: Series1[0] > Series2[0] && Series1[1] < Series2[1]

      Consider checking the Close against a different series, like the High or Low series. E.g. to check if the Close went above the High of last bar:

      Code:
      if(CurrentBar < 1) return;
      
      if (Close[0] > High[1] && Position.MarketPosition == MarketPosition.Flat)
      {
      Print("Condition True");
      EnterLong(Convert.ToInt32(DefaultQuantity), "");
      }
      When you get unexpected results like this, always use the Print method to print data from your script to see if the entry conditions are becoming true or not.

      Please let me know if any further questions come up.
      HI Chris,

      There's no way to check if the current price crossed above the close of the previous bar?

      -Omer

      Comment


        #4
        Hello Omer, thanks for your reply.

        That would look like this:

        if(Close[0] > Close[1])
        { //... }

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Mindset, 04-21-2026, 06:46 AM
        0 responses
        88 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by M4ndoo, 04-20-2026, 05:21 PM
        0 responses
        134 views
        0 likes
        Last Post M4ndoo
        by M4ndoo
         
        Started by M4ndoo, 04-19-2026, 05:54 PM
        0 responses
        68 views
        0 likes
        Last Post M4ndoo
        by M4ndoo
         
        Started by cmoran13, 04-16-2026, 01:02 PM
        0 responses
        119 views
        0 likes
        Last Post cmoran13  
        Started by PaulMohn, 04-10-2026, 11:11 AM
        0 responses
        67 views
        0 likes
        Last Post PaulMohn  
        Working...
        X