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

Delta value printed from strategy not matching chart data window

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

    Delta value printed from strategy not matching chart data window

    Code:
    deltaClose = OrderFlowCumulativeDelta(BarsArray[0], CumulativeDeltaType.BidAsk, CumulativeDeltaPeriod.Session, 0).DeltaClose[0];
    Print(Time[0]);
    Print("deltaClose: " + deltaClose);
    Print("deltaSMA: " + deltaSMA);
    1/10/2022 8:55:17 AM
    deltaClose: -7987
    deltaSMA: -8099.3
    Here are screenshots of the data window and the settings.

    Any idea why these don't match?
    Attached Files

    #2
    Hello WalterSkinner,

    Thanks for your post.

    You will need to make sure the additional code is added to update the single tick data series of the Order Flow indicator.

    Code:
    // OnBarUpdate() logic
    if (BarsInProgress == 0)
    {
        // Print the close of the cumulative delta bar with a delta type of Bid Ask and with a Session period
        Print("Delta Close: " + OrderFlowCumulativeDelta(BarsArray[0], CumulativeDeltaType.BidAsk, CumulativeDeltaPeriod.Session, 0).DeltaClose[0]);
    }
    else if (BarsInProgress == 1)
    {
        // We have to update the secondary series of the cached indicator to make sure the values we get in BarsInProgress == 0 are in sync
        OrderFlowCumulativeDelta(BarsArray[0], CumulativeDeltaType.BidAsk, CumulativeDeltaPeriod.Session, 0).Update(OrderFlowCumulativeDelta(Bars Array[0], CumulativeDeltaType.BidAsk, CumulativeDeltaPerio d.Session, 0).BarsArray[1].Count - 1, 1);
    }


    After this code is added, the results will align. Please note that when processing realtime data, the indicator behaves similarly to a Tick Replay indicator (only in the fact that it is one tick behind, and the value on the chart is updated with the bar close value, after the bar closes.)

    Let us know if you have any questions.
    JimNinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Jim View Post
      Hello WalterSkinner,

      Thanks for your post.

      You will need to make sure the additional code is added to update the single tick data series of the Order Flow indicator.

      Code:
      // OnBarUpdate() logic
      if (BarsInProgress == 0)
      {
      // Print the close of the cumulative delta bar with a delta type of Bid Ask and with a Session period
      Print("Delta Close: " + OrderFlowCumulativeDelta(BarsArray[0], CumulativeDeltaType.BidAsk, CumulativeDeltaPeriod.Session, 0).DeltaClose[0]);
      }
      else if (BarsInProgress == 1)
      {
      // We have to update the secondary series of the cached indicator to make sure the values we get in BarsInProgress == 0 are in sync
      OrderFlowCumulativeDelta(BarsArray[0], CumulativeDeltaType.BidAsk, CumulativeDeltaPeriod.Session, 0).Update(OrderFlowCumulativeDelta(Bars Array[0], CumulativeDeltaType.BidAsk, CumulativeDeltaPerio d.Session, 0).BarsArray[1].Count - 1, 1);
      }


      After this code is added, the results will align. Please note that when processing realtime data, the indicator behaves similarly to a Tick Replay indicator (only in the fact that it is one tick behind, and the value on the chart is updated with the bar close value, after the bar closes.)

      Let us know if you have any questions.
      I forgot about this thank you so much!

      Comment


        #4
        Quick followup.

        If I wanted to use the close value in real time (for delta), should I then change my entry logic to the previous bar?

        For example if my entry logic is: Closes[0][0] > Opens[0][0] && deltaClose > deltaSMA

        Would it make sense to change it to: Closes[0][1] > Opens[0][1] && prevDeltaClose > prevDeltaSMA

        Years ago I used to work with MQL4 in MetaTrader and all of the entry logic was based on the prior bar's close so this would be similar to that, but I'm not sure if there are any issues that would pop up if I make this switch.

        If I understand you correctly: a strategy running on real-time data will (possibly/likely) have a different value for delta as of the close of bar index [0] compared to what the closing value is on the chart since the value is updated after the close of that bar.

        Last edited by WalterSkinner; 02-10-2022, 10:42 AM.

        Comment


          #5
          Hello WalterSkinner,

          While processing realtime data, you would want to reference the previous bar values on BIP 1 just after the bar closes on BIP 0. In this case, the Close value of the primary bar, and the DeltaClose value from Order Flow Cumulative Delta would be the bar close values.

          I have attached an example with some comments to demonstrate.

          Attached Files
          JimNinjaTrader Customer Service

          Comment


            #6
            Thank you so much!

            Comment


              #7
              I'm trying to call Order Flow Cumulative Delta in a strategy and the Print("Delta Close: " + cumulativeDelta.DeltaClose[0]); output is much different than the indicator on the chart. How do I get the values that I see on the indicator on the chart?

              public class DeltaPush : Strategy
              {
              private double Bid;
              private double Ask;
              private OrderFlowCumulativeDelta cumulativeDelta;
              private NinjaTrader.NinjaScript.Indicators.App_DSP_Visual. PriceVelocityBeta PriceVelocityBeta1;


              protected override void OnStateChange()
              {
              if (State == State.SetDefaults)
              {
              Description = @"Enter the description for your new custom Strategy here.";
              Name = "DeltaPush";
              Calculate = Calculate.OnEachTick;
              EntriesPerDirection = 1;
              EntryHandling = EntryHandling.AllEntries;
              IsExitOnSessionCloseStrategy = true;
              ExitOnSessionCloseSeconds = 30;
              IsFillLimitOnTouch = false;
              MaximumBarsLookBack = MaximumBarsLookBack.Infinite;
              OrderFillResolution = OrderFillResolution.Standard;
              Slippage = 0;
              StartBehavior = StartBehavior.WaitUntilFlatSynchronizeAccount;
              TimeInForce = TimeInForce.Gtc;
              TraceOrders = true;
              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;
              TradeTaken = false;
              Profit = 2;
              Stop = 20;
              DeltaLong = 200;
              DeltaShort = -200;
              VelocitySDLong = 1;
              VelocitySDShort = -1;
              }
              else if (State == State.Configure)
              {
              AddDataSeries("^TICK", Data.BarsPeriodType.Minute, 1, Data.MarketDataType.Last);
              AddDataSeries(Data.BarsPeriodType.Tick, 1);
              }
              else if (State == State.DataLoaded)
              {
              PriceVelocityBeta1 = PriceVelocityBeta(Closes[1], SAMethod.Autocorrelation);
              BarTimes1 = BarTimes(Close, AvgCalcMode.WMA, 2);
              SetProfitTarget(@"TVLong", CalculationMode.Ticks, Profit);
              SetProfitTarget(@"DeltaLong", CalculationMode.Ticks, Profit);
              SetStopLoss(@"TVLong", CalculationMode.Ticks, Stop, false);
              SetStopLoss(@"DeltaLong", CalculationMode.Ticks, Stop, false);
              SetProfitTarget(@"DeltaShort", CalculationMode.Ticks, Profit);
              SetStopLoss(@"DeltaShort", CalculationMode.Ticks, Stop, false);
              SetProfitTarget(@"TVShort", CalculationMode.Ticks, Profit);
              SetStopLoss(@"TVShort", CalculationMode.Ticks, Stop, false);
              cumulativeDelta = OrderFlowCumulativeDelta(BarsArray[0], CumulativeDeltaType.BidAsk, CumulativeDeltaPeriod.Bar, 0);
              }
              }

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

              if (CurrentBars[0] < 1
              || CurrentBars[1] < 1 || CurrentBars[2] <1)
              return;

              if (State == State.Historical)
              return;
              if (BarsInProgress == 0)
              {
              Bid = GetCurrentBid(1);
              Ask = GetCurrentAsk(1);
              }
              Print("TICK Velocity:" + PriceVelocityBeta1.Vel[0]);

              if (BarsInProgress == 0)
              {
              // Print the close of the cumulative delta bar with a delta type of Bid Ask and with a Bar period
              Print("Delta Close: " + cumulativeDelta.DeltaClose[0]);
              }
              else if (BarsInProgress == 2)
              {
              // We have to update the secondary series of the hosted indicator to make sure the values we get in BarsInProgress == 0 are in sync
              cumulativeDelta.Update(cumulativeDelta.BarsArray[2].Count - 1, 1);
              }

              // Set 1
              if ((PriceVelocityBeta1.Vel[0] >= VelocitySDLong) && (cumulativeDelta.DeltaClose[0] >= DeltaLong))
              {
              EnterLongLimit(Convert.ToInt32(DefaultQuantity), Bid, @"Long1");
              }

              // Set 2
              if ((PriceVelocityBeta1.Vel[0] <= VelocitySDShort) && (cumulativeDelta.DeltaClose[0] <= DeltaShort))
              {
              EnterShortLimit(Convert.ToInt32(DefaultQuantity), Ask, @"Short1");
              }

              // Set 3
              if ((PriceVelocityBeta1.Vel[0] <= VelocitySDShort/2) && (cumulativeDelta.DeltaClose[0] <= DeltaShort/2))

              {
              ExitLong(Convert.ToInt32(DefaultQuantity), "ExitLong1", @"Long1");
              }

              // Set 4
              if ((PriceVelocityBeta1.Vel[0] >= VelocitySDLong/2) && (cumulativeDelta.DeltaClose[0] >= DeltaLong/2))

              {
              ExitShort(Convert.ToInt32(DefaultQuantity), "ExitShort1", @"Short1");
              }

              Comment


                #8
                Hello Lance El Camino,

                At the top you have:

                if (BarsInProgress != 0)
                return;​

                This means the code that should run on each tick will never be reached.
                else if (BarsInProgress == 2)
                {
                // We have to update the secondary series of the hosted indicator to make sure the values we get in BarsInProgress == 0 are in sync
                cumulativeDelta.Update(cumulativeDelta.BarsArray[2].Count - 1, 1);
                }​

                Perhaps move if (BarsInProgress != 0) return;​​ below that point?
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Thank you for getting back to me so quickly. After moving if (BarsInProgress != 0) return;​​ below that point I'm getting Strategy 'DeltaPush': Error on calling 'OnBarUpdate' method on bar 1776: Index was outside the bounds of the array. I'm loading 10 days of a 5000 Volume chart.

                  Another quick question: what is more efficient, calling Order Flow Cumulative Delta directly or referencing the indicator? Thanks!

                  Comment


                    #10
                    Here's what Visual Studio shows. Is it because of the if (Current Bars[0] < 1 || Current Bars[1] < 1 || CurrentBars[2] < 1) return" is causing an issue with that particular line?

                    Comment


                      #11
                      Click image for larger version

Name:	image.png
Views:	138
Size:	254.8 KB
ID:	1218327​Sorry I printed both monitors on last post.

                      Comment


                        #12
                        Click image for larger version

Name:	image.png
Views:	137
Size:	226.7 KB
ID:	1218331​I input the correct BIP index and still get error?

                        Comment


                          #13
                          So putting if (BarsInProgress != 0) return;​ back above just under State.OnBarUpdate removed the outside the bounds of the array error. Also, after a couple of bars of BIP 0 completed the delta output is matching the indicator. I believe my error was in the section:

                          else if (BarsInProgress == 2)
                          {
                          // We have to update the secondary series of the hosted indicator to make sure the values we get in BarsInProgress == 0 are in sync
                          cumulativeDelta.Update(cumulativeDelta.BarsArray[2].Count - 1, 2);
                          }


                          I originally had:

                          else if (BarsInProgress == 2)
                          {
                          // We have to update the secondary series of the hosted indicator to make sure the values we get in BarsInProgress == 0 are in sync
                          cumulativeDelta.Update(cumulativeDelta.BarsArray[2].Count - 1, 1);
                          }

                          ​​
                          It appears the BIP index I orignally called in Update() was BIP 1 which is the ^TICK index data series in this strategy. After changing it to BIP 2, it finally worked properly after a couple of bars printed for BIP 0.

                          Can you confirm this makes sense? I appreciate the support! I will let it run more bars to make sure it stays consistent. Would you like any updates?

                          THANK YOU!!!!

                          Comment


                            #14
                            Hello Lance El Camino,

                            Indicator instances are cached in NinjaTrader. Using a reference or calling is the same in efficiency due to this. It's easier to code with reference variables, and generally that would be more efficient (if NinjaTrader wasn't already caching)

                            It appears you are trying to update the indicator with BarsInProgress 2, which appears to be correct if the added tick series is BarsInProgress 2.

                            What is the value of CurrentBars[2] before this is called?
                            Last edited by NinjaTrader_ChelseaB; 10-06-2022, 11:27 AM.
                            Chelsea B.NinjaTrader Customer Service

                            Comment


                              #15
                              Just to report that after several bars there is still differences that emerge. How do get it to line up?

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by frankthearm, Today, 09:08 AM
                              7 responses
                              29 views
                              0 likes
                              Last Post NinjaTrader_Clayton  
                              Started by NRITV, Today, 01:15 PM
                              1 response
                              5 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by maybeimnotrader, Yesterday, 05:46 PM
                              5 responses
                              25 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by quantismo, Yesterday, 05:13 PM
                              2 responses
                              17 views
                              0 likes
                              Last Post quantismo  
                              Started by adeelshahzad, Today, 03:54 AM
                              5 responses
                              33 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Working...
                              X