Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Value refrerenced from Indicator in a Stragey is different from indicator data

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

    Value refrerenced from Indicator in a Stragey is different from indicator data

    Hi,

    I am developing a strategy based on an indicator that I modified a little bit from the the source. Basically I added a few plots with some calculations. The indicator displays correctly on the screen, and when debuged the Dataseries holds the correct values.

    For the strategy, I added 2 additional DataSeries, using AddDataSeries, and called the indicator 3 times using the original Close[0] and the 2 new ones. So basically I have the same indicator in 3 different time frames (15/60/240 minutes).

    In this strategy, when calculating the signal, I am getting values that are a little different from what I have in the indicator for the same bar. See the debug printout and the relevant pieces of code.


    Your help is very appricated

    Debug output from the Indicator:
    05/03 16:00:00 000 BWIC(60 Minute)@183, fastSum=26394, mediumSum=26394, SenkouSpanA=13197, SenkouSpanB=13221.75, KumoDir=-1


    Debug output from the Strategy:
    05/03 16:00:00 000 1-BWIC @183={ D=-1, TenkanSen=13197, KijunSen=13197, ChikouSpan=0, SenkouSpanA=13197, SenkouSpanB=13197, KumoDir=0, PriceKumoDir=-1, PriceBaseLineDir=-1}
    See that the (bar 183 on the 60 minute, 05/03 16:00:00 000) SenkouSpanA are indentical, but SenkouSpanB and KumoDir are different.

    Here is the Indicator relevant code:
    Code:
            protected override void OnStateChange()
            {            
                if (State == State.SetDefaults)
                {
                    Description                                    = @"BWIC";
                    Name                                        = "BWIC";
                    Calculate                                    = Calculate.OnBarClose;
                    IsOverlay                                    = true;
                    ...
                    AddPlot(Brushes.Red,         "TenkanSen (Conversion Line)");
                    AddPlot(Brushes.Purple,     "KijunSen (Base Line)");
                    AddPlot(Brushes.MediumBlue, "ChikouSpan (Lagging Span)");
    
                    AddPlot(CloudAreaColorUp,     "SenkouSpanA (Leading Span A)");
                    AddPlot(CloudAreaColorDown, "SenkouSpanB (Leading Span B)");
    
                    ShowTransparentPlotsInDataBox = true;
                    AddPlot(Brushes.Transparent,"PriceKumoDir");
                    AddPlot(Brushes.Transparent,"PriceBaseLineDir");
                    AddPlot(Brushes.Transparent,"KumoDir");
                    AddPlot(Brushes.Transparent,"KumoWidthTicks");
                    AddPlot(Brushes.Transparent,"CurBar");
                }
                else if (State == State.DataLoaded)
                {                
                }
            }
    
            protected override void OnBarUpdate()
            {
                CurBar[0] = CurrentBar;
                if ((CurrentBar < PeriodFast) || (CurrentBar < PeriodMedium) || (CurrentBar < PeriodSlow)) return;
    
                double fastSum                = MAX(High, PeriodFast)[0]+MIN(Low, PeriodFast)[0];
                double mediumSum            = MAX(High, PeriodMedium)[0]+MIN(Low, PeriodMedium)[0];
    
                 TenkanSen[0]                 = fastSum/2.0;
                KijunSen[0]                 = mediumSum/2.0;
                ChikouSpan[PeriodMedium]     = Close[0];
    
                SenkouSpanA[0]                 = (fastSum+mediumSum)/4.0;
                SenkouSpanB[0]                 = (MAX(High, PeriodSlow)[0]+MIN(Low, PeriodSlow)[0])/2.0;    
    
                KumoDir[0]                     = SenkouSpanA[0].ApproxCompare(SenkouSpanB[0]);
                PriceKumoDir[0]             = Close[0].ApproxCompare(KumoDir[0]>0?SenkouSpanA[0]:SenkouSpanB[0]);
                PriceBaseLineDir[0]         = Close[0].ApproxCompare(KijunSen[0]);
                KumoWidthTicks[0]             = Math.Round(Math.Abs(SenkouSpanA[0]-SenkouSpanB[0])/TickSize,0);
    
                Print(Time[0].ToString("MM/dd HH:mm:ss fff")+" BWIC("+Bars.BarsPeriod.Value+" "+Bars.BarsPeriod.BarsPeriodTypeName+")@"+CurrentBar+", fastSum="+fastSum+", mediumSum="+mediumSum
                            +", SenkouSpanA="+SenkouSpanA[0]+", SenkouSpanB="+SenkouSpanB[0]+", KumoDir="+KumoDir[0]);
            }
    
            #region Output Properties
            [Browsable(false)][XmlIgnore]public Series<double> TenkanSen         { get { return Values[0];         }}
            [Browsable(false)][XmlIgnore]public Series<double> KijunSen         { get { return Values[1];         }}
            [Browsable(false)][XmlIgnore]public Series<double> ChikouSpan         { get { return Values[2];         }}
            [Browsable(false)][XmlIgnore]public Series<double> SenkouSpanA         { get { return Values[3];         }}
            [Browsable(false)][XmlIgnore]public Series<double> SenkouSpanB         { get { return Values[4];         }}
            [Browsable(false)][XmlIgnore]public Series<double> PriceKumoDir     { get { return Values[5];         }}
            [Browsable(false)][XmlIgnore]public Series<double> PriceBaseLineDir { get { return Values[6];         }}
            [Browsable(false)][XmlIgnore]public Series<double> KumoDir             { get { return Values[7];         }}
            [Browsable(false)][XmlIgnore]public Series<double> KumoWidthTicks     { get { return Values[8];         }}
            [Browsable(false)][XmlIgnore]public Series<double> CurBar             { get { return Values[9];         }}
             #endregion
    ​
    Here is the Strategy relevant code:
    Code:
        protected int TFH1                 = -1;
        protected int TFH2                 = -1;
        protected BWIC[] BWICs;
        protected override void OnStateChange()
        {
            if (State == State.SetDefaults)
            {
                Description        = @"Enter the description for your new custom Strategy here.";
                Name            = "BWAAIC";
            }
            else if (State == State.Configure)
            {
                TFH1 = 1;
                TFH2 = 2;
                AddDataSeries(BarsPeriodType.Minute, 60);
                AddDataSeries(BarsPeriodType.Minute, 240);
            }
            else if (State==State.DataLoaded)
            {        
                BWICs = new BWIC[]
                                    {BWIC(Closes[0], 9, 26, 5, 40, Brushes.Green, Brushes.Red, 26),
                                        BWIC(Closes[TFH1], 9, 26, 5, 40, Brushes.Green, Brushes.Red, 26),
                                            BWIC(Closes[TFH2], 9, 26, 5, 40, Brushes.Green, Brushes.Red, 26)};
                AddChartIndicator(BWICs[0]);
            }
        }
    
        protected override void OnBarUpdate()
        {
            if (BarsInProgress==0)
            {
                int[] dirs = { Closes[0][0].ApproxCompare(Opens[0][0]),
                                Closes[TFH1][0].ApproxCompare(Opens[TFH1][0]),
                                    Closes[TFH2][0].ApproxCompare(Opens[TFH2][0])};
                Print(Time[0].ToString("MM/dd HH:mm:ss fff")+" @"+CurrentBars[0]+" ======================== ");                
                Print(Time[0].ToString("MM/dd HH:mm:ss fff")+" @"+CurrentBars[0]+" 2-BWIC @"+CurrentBars[TFH2]+"={ D="+dirs[2]
                        +", TenkanSen="+BWICs[2].TenkanSen[0]+", KijunSen="+BWICs[2].KijunSen[0]+", ChikouSpan="+BWICs[2].ChikouSpan[0]
                        +", SenkouSpanA="+BWICs[2].SenkouSpanA[0]+", SenkouSpanB="+BWICs[2].SenkouSpanB[0]+", KumoDir="+BWICs[2].KumoDir[0]
                        +", PriceKumoDir="+BWICs[2].PriceKumoDir[0]+", PriceBaseLineDir="+BWICs[2].PriceBaseLineDir[0]+"}");
                Print(Time[0].ToString("MM/dd HH:mm:ss fff")+" @"+CurrentBars[0]+" 1-BWIC @"+CurrentBars[TFH1]+"={ D="+dirs[1]
                        +", TenkanSen="+BWICs[1].TenkanSen[0]+", KijunSen="+BWICs[1].KijunSen[0]+", ChikouSpan="+BWICs[1].ChikouSpan[0]
                        +", SenkouSpanA="+BWICs[1].SenkouSpanA[0]+", SenkouSpanB="+BWICs[1].SenkouSpanB[0]+", KumoDir="+BWICs[1].KumoDir[0]
                        +", PriceKumoDir="+BWICs[1].PriceKumoDir[0]+", PriceBaseLineDir="+BWICs[1].PriceBaseLineDir[0]+"}");
                Print(Time[0].ToString("MM/dd HH:mm:ss fff")+" @"+CurrentBars[0]+" 0-BWIC @"+CurrentBars[0]+"={ D="+dirs[0]
                        +", TenkanSen="+BWICs[0].TenkanSen[0]+", KijunSen="+BWICs[0].KijunSen[0]+", ChikouSpan="+BWICs[0].ChikouSpan[0]
                        +", SenkouSpanA="+BWICs[0].SenkouSpanA[0]+", SenkouSpanB="+BWICs[0].SenkouSpanB[0]+", KumoDir="+BWICs[0].KumoDir[0]
                        +", PriceKumoDir="+BWICs[0].PriceKumoDir[0]+", PriceBaseLineDir="+BWICs[0].PriceBaseLineDir[0]+"}");
            }
        }        ​
    Here is the chart with the strategy:
    Click image for larger version

Name:	Screenshot 2023-05-25 at 0.29.38.png
Views:	138
Size:	923.3 KB
ID:	1252884

    #2
    Hello Shai Samuel,

    Thank you for writing in.

    I see your strategy is only printing when BarsInProgress == 0 and you are only adding the indicator calculated on the primary series visually with AddChartIndicator(BWICs[0]);
    I am curious what the results would be if you added the indicator calculated on the 60-minute series visually with AddChartIndicator(BWICs[1]); and isolated your print statements for each BarsInProgress calling OnBarUpdate() (for example, printing the values from the indicator based on the 60-minute series when BarsInProgress ==1).

    The following page in the help guide is a useful overview of working with multi-time frame & instruments:Another option to test would be to comment out your currently added instances of the BWIC indicator and add only the BWIC based on the added 60-minute series and double-check the results. Isolating this time frame might help to narrow down and better understand how the strategy is calculating.

    Please let us know if we may be of further assistance.
    Emily C.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by renner1984, 06-16-2024, 01:50 PM
    6 responses
    22 views
    0 likes
    Last Post renner1984  
    Started by hardworkinguy, 02-07-2024, 09:19 PM
    8 responses
    413 views
    0 likes
    Last Post DavidHunter  
    Started by ender_wiggum, Today, 07:56 AM
    6 responses
    20 views
    0 likes
    Last Post ender_wiggum  
    Started by admiralpanda, 02-16-2024, 06:59 AM
    2 responses
    133 views
    0 likes
    Last Post pacpac
    by pacpac
     
    Started by Vikuno1, 06-15-2024, 10:22 PM
    16 responses
    58 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Working...
    X