Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Problems defining highest and lowest price

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

    Problems defining highest and lowest price

    Hi, I have created a strategy where I would like to first define the highest price and the lowest price. First define an exact time in this case 9:25am and define Nbars 185 (that number is the number of bars I plan to take as a range from 9:25am backwards). But I have some problems because there are days when the strategy does not do well and chooses prices that are neither the highest price nor the lowest price, see image (blue dot highest price, purple dot lowest price):

    Click image for larger version

Name:	dots1.png
Views:	97
Size:	255.9 KB
ID:	1312176
    Here is my code:

    Code:
    #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 HighLowBarandPrice : Strategy
        {
            private int HighBar;
            private int LowBar;
            private double HighPrice;
            private double LowPrice;
    
    
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"Enter the description for your new custom Strategy here.";
                    Name                                        = "HighLowBarandPrice";
                    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;
                    PivotHour                        = DateTime.Parse("09:25", System.Globalization.CultureInfo.InvariantCulture);
                    NBars                    = 185;
                    HighBar                    = 0;
                    LowBar                    = 0;
                    HighPrice                    = 0;
                    LowPrice                    = 0;
                }
                else if (State == State.Configure)
                {
                }
            }
    
            protected override void OnBarUpdate()
            {
                if (BarsInProgress != 0)
                    return;
    
                if (CurrentBars[0] < NBars + 50)
                    return;
    
                 // Set 1
                if (Times[0][0].TimeOfDay == PivotHour.TimeOfDay)
                {
                    HighBar = Convert.ToInt32(HighestBar(Close, NBars));
                    LowBar = Convert.ToInt32(LowestBar(Close, NBars));
                    HighPrice = High[HighBar];
                    LowPrice = Low[LowBar];
                    Draw.Dot(this, @"HighLowBarandPrice Dot_1 " + Convert.ToString(CurrentBars[0]), false, HighBar, High[HighBar], Brushes.Blue);
                    Draw.Dot(this, @"HighLowBarandPrice Dot_2 " + Convert.ToString(CurrentBars[0]), false, LowBar, Low[LowBar], Brushes.BlueViolet);
                }
                
            }
    
            #region Properties
            [NinjaScriptProperty]
            [PropertyEditor("NinjaTrader.Gui.Tools.TimeEditorKey")]
            [Display(Name="PivotHour", Order=1, GroupName="Parameters")]
            public DateTime PivotHour
            { get; set; }
    
            [NinjaScriptProperty]
            [Range(1, int.MaxValue)]
            [Display(Name="NBars", Order=2, GroupName="Parameters")]
            public int NBars
            { get; set; }
            #endregion
    
        }
    }
    ​

    I would like to get some advice. Thanks!

    #2
    Hello daying35,

    Thank you for your post.

    We have an existing sample script that demonstrates calculating the highest high and lowest low for a specified time range:



    I recommend studying this script for guidance, you could even directly edit the script to use Draw.Dot() instead of plotting the highest and lowest price values.

    Please let me know if this doesn't guide you in the right direction.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by NullPointStrategies, Today, 05:17 AM
    0 responses
    52 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    130 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    70 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    43 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    47 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X