Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Compare two assets Compare Highs

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

    Compare two assets Compare Highs

    hello,
    I am struggling with this indicator.

    During live markets, it seems to get phantom, random, inaccurate signals. The data window I have open is capturing the correct data, and when I refresh the chart, the indicator is behaving exactly as it should. I just can't trust it during live markets, or I am continually refreshing the chart manually. Would anyone be able to take a quick look and tell me what I have potentially done wrong?
    The indicator should be comparing the current bar with the previous bar. If NQ makes a higher high and the ES makes an equal or lower high, it should print a signal on top of the NQ price bar. If both NQ and ES make higher highs, there is no signal.
    Thank you for any and all input.

    Here is the indicator.




    using System;

    using System.ComponentModel;

    using System.ComponentModel.DataAnnotations;

    using System.Windows.Media;

    using System.Xml.Serialization;

    using NinjaTrader.Cbi;

    using NinjaTrader.Gui;

    using NinjaTrader.Gui.Chart;

    using NinjaTrader.NinjaScript;

    using NinjaTrader.Core.FloatingPoint;

    using NinjaTrader.NinjaScript.DrawingTools;

    using NinjaTrader.Data;


    namespace NinjaTrader.NinjaScript.Indicators

    {

    public class AssetHighComparisonIndicator : Indicator

    {

    private const double TOLERANCE = 0.0001; // Tolerance for price comparisons


    protected override void OnStateChange()

    {

    if (State == State.SetDefaults)

    {

    Description = @"Compares highs of two assets and signals divergence";

    Name = "Asset High Comparison Indicator";

    Calculate = Calculate.OnBarClose;

    IsOverlay = true;

    DisplayInDataBox = true;

    DrawOnPricePanel = true;

    DrawHorizontalGridLines = true;

    DrawVerticalGridLines = true;

    PaintPriceMarkers = true;

    ScaleJustification = ScaleJustification.Right;

    Asset2 = string.Empty;

    SignalOffset = 5;

    PositiveSignalColor = Brushes.Green;

    NegativeSignalColor = Brushes.Red;

    LookbackPeriod = 1;

    TimeFrame = 5;

    }

    else if (State == State.Configure)

    {

    AddDataSeries(Asset2, new BarsPeriod { BarsPeriodType = BarsPeriodType.Minute, Value = TimeFrame });

    }

    }


    protected override void OnBarUpdate()

    {

    if (CurrentBar < LookbackPeriod || CurrentBars[1] < LookbackPeriod || BarsInProgress != 0)

    return;


    if (Calculate != Calculate.OnBarClose || CurrentBar < 1)

    return;


    double asset1CurrentHigh = High[0];

    double asset1PreviousHigh = High[1];

    double asset2CurrentHigh = Highs[1][0];

    double asset2PreviousHigh = Highs[1][1];


    bool asset1HigherHigh = asset1CurrentHigh > asset1PreviousHigh + TOLERANCE;

    bool asset2HigherHigh = asset2CurrentHigh > asset2PreviousHigh + TOLERANCE;


    Print($"Bar: {CurrentBar}, Time: {Time[0]}");

    Print($"Asset1 Current: {asset1CurrentHigh}, Previous: {asset1PreviousHigh}, Higher: {asset1HigherHigh}");

    Print($"Asset2 Current: {asset2CurrentHigh}, Previous: {asset2PreviousHigh}, Higher: {asset2HigherHigh}");

    Print($"Condition Check: (asset1HigherHigh && !asset2HigherHigh) = {asset1HigherHigh && !asset2HigherHigh}");

    Print($"Condition Check: (!asset1HigherHigh && asset2HigherHigh) = {!asset1HigherHigh && asset2HigherHigh}");


    if (asset1HigherHigh && !asset2HigherHigh)

    {

    double signalPrice = asset1CurrentHigh + SignalOffset * TickSize;

    Draw.Diamond(this, $"PositiveSignal{CurrentBar}", false, 0, signalPrice, PositiveSignalColor);

    Print($"Drawing Positive Signal at bar {CurrentBar}");

    }

    else if (!asset1HigherHigh && asset2HigherHigh)

    {

    double signalPrice = asset1CurrentHigh + SignalOffset * TickSize;

    Draw.Diamond(this, $"NegativeSignal{CurrentBar}", false, 0, signalPrice, NegativeSignalColor);

    Print($"Drawing Negative Signal at bar {CurrentBar}");

    }

    else

    {

    Print($"No signal drawn at bar {CurrentBar}");

    }


    Print("--------------------");

    }


    [NinjaScriptProperty]

    [Display(Name="Asset 2", Description="Symbol for the comparison asset", Order=1, GroupName="Parameters")]

    public string Asset2 { get; set; }


    [NinjaScriptProperty]

    [Range(0, 20)]

    [Display(Name="Signal Offset", Description="Distance from high in ticks", Order=2, GroupName="Parameters")]

    public int SignalOffset { get; set; }


    [NinjaScriptProperty]

    [Range(1, 10)]

    [Display(Name="Lookback Period", Description="Number of bars to look back for comparison", Order=3, GroupName="Parameters")]

    public int LookbackPeriod { get; set; }


    [NinjaScriptProperty]

    [Range(1, 60)]

    [Display(Name="Time Frame", Description="Time frame in minutes for both assets", Order=4, GroupName="Parameters")]

    public int TimeFrame { get; set; }


    [NinjaScriptProperty]

    [XmlIgnore]

    [Display(Name="Positive Signal Color", Description="Color for signals when Asset 1 outperforms", Order=5, GroupName="Parameters")]

    public Brush PositiveSignalColor { get; set; }


    [Browsable(false)]

    public string PositiveSignalColorSerializable

    {

    get { return Serialize.BrushToString(PositiveSignalColor); }

    set { PositiveSignalColor = Serialize.StringToBrush(value); }

    }


    [NinjaScriptProperty]

    [XmlIgnore]

    [Display(Name="Negative Signal Color", Description="Color for signals when Asset 2 outperforms", Order=6, GroupName="Parameters")]

    public Brush NegativeSignalColor { get; set; }


    [Browsable(false)]

    public string NegativeSignalColorSerializable

    {

    get { return Serialize.BrushToString(NegativeSignalColor); }

    set { NegativeSignalColor = Serialize.StringToBrush(value); }

    }

    }

    }





    #2
    Hello butch1130,

    What is the specific line of code returning an unexpected value?

    Note, using a variable (Asset2) with AddDataSeries() is not supported. Use null to use the same instrument as the primary or hard code the instrument symbol.

    From the help guide:
    "rguments supplied to AddDataSeries() should be hardcoded and NOT dependent on run-time variables which cannot be reliably obtained during State.Configure (e.g., Instrument, Bars, or user input). Attempting to add a data series dynamically is NOT guaranteed and therefore should be avoided. Trying to load bars dynamically may result in an error similar to: Unable to load bars series. Your NinjaScript may be trying to use an additional data series dynamically in an unsupported manner.​"


    As a tip, to export a NinjaTrader 8 NinjaScript so this can be shared and imported by the recipient do the following:
    1. Click Tools -> Export -> NinjaScript Add-on...
    2. Click the 'add' link -> check the box(es) for the script(s) and reference(s) you want to include
    3. Click the 'Export' button
    4. Enter the script name in the value for 'File name:'
    5. Choose a save location -> click Save
    6. Click OK to clear the export location message
    By default your exported file will be in the following location:
    • (My) Documents/NinjaTrader 8/bin/Custom/ExportNinjaScript/<export_file_name.zip>
    Below is a link to the help guide on Exporting NinjaScripts.
    http://ninjatrader.com/support/helpG...nt8/export.htm
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      It's not a specific issue. It's totally random when Asset 2 is capturing it's data for comparison. I can't pinpoint it, but I guess that is what you are saying with the tips you shared. I've hard coded the second data series now and I'll let it run awhile to see if it's still an issue. The indicator, upon a refresh of data to the chart, works as intended. It' s just not reliable enough.

      Comment


        #4
        Appreciate you getting back to me. Thank You. Should have said that first!

        Comment


          #5
          Hello butch1130,

          I've made a simple test script that prints out the Bar series information and the high of the primary series and an added 5 minute ES 09-24 series to help you identify if the data is what is matching on the chart.
          CompareHighsTest_NT8.zip

          Below is a link to a video of testing the indicator.
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            Thank you so much for this. I'll will check it out. Here is a closer inspection of what I'm seeing randomly.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            647 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            369 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            108 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            572 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            573 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X