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

Sleeping 2 bars before continuing

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

    Sleeping 2 bars before continuing

    Hi, I'm trying to wait 2 candles before performing an action. I have the following code

    HTML 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 test4 : Strategy
        {
            //private NinjaTrader.NinjaScript.Indicators.Sim22.Sim22_DeltaV2 Sim22_DeltaV21;
            private OrderFlowCumulativeDelta CumDelta1m;
            private OrderFlowCumulativeDelta CumDelta15s;
            private double price_15_high;
            private double price_15_low;
    
    
    
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"Joan strategy";
                    Name                                        = "test4";
                    Calculate                                    = Calculate.OnBarClose;
                    EntriesPerDirection                            = 1;
                    EntryHandling                                = EntryHandling.AllEntries;
                    IsExitOnSessionCloseStrategy                = false;
                    ExitOnSessionCloseSeconds                    = 30;
                    IsFillLimitOnTouch                            = true;
                    MaximumBarsLookBack                            = MaximumBarsLookBack.Infinite;
                    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)
                {
                    Calculate = Calculate.OnBarClose;
                    AddDataSeries("YM 03-23", Data.BarsPeriodType.Minute, 1, Data.MarketDataType.Last);
                    AddDataSeries("YM 03-23", Data.BarsPeriodType.Second, 15, Data.MarketDataType.Last);
                    AddDataSeries("YM 03-23", Data.BarsPeriodType.Tick, 1, Data.MarketDataType.Last);
                    AddDataSeries("YM 03-23", Data.BarsPeriodType.Volume, 1, Data.MarketDataType.Last);
                }
                else if (State == State.DataLoaded)
                {                
                    CumDelta1m = OrderFlowCumulativeDelta(BarsArray[1], CumulativeDeltaType.BidAsk, CumulativeDeltaPeriod.Bar, 0);
                    CumDelta15s = OrderFlowCumulativeDelta(BarsArray[2], CumulativeDeltaType.BidAsk, CumulativeDeltaPeriod.Bar, 0);
                }
            }
    
            protected override void OnBarUpdate()
            {                            
    
                // Check if current bar is 1m full candle
                if (BarsInProgress == 1 && CurrentBar > 0){
    
                    double delta = CumDelta1m.DeltaClose[0];
    
                    // obtain current 1m bar timestamp
                    DateTime currentBarTime = Bars.GetTime(Bars.Count-2);
    
                    if (delta > 100 || delta < -100){
                        double maxPrice = double.MinValue;
                        double minPrice = double.MaxValue;                    
                        DateTime futureTime = currentBarTime;
                        DateTime correctTime = futureTime;
    
                        int count = 3;
    
                        for (int i = 0; i < 4; i++){
                            double delta15s = CumDelta15s.DeltaClose[count];
    
                            if (delta15s > 60 || delta15s < -60){
                                double highPrice = Highs[2][count];
                                double lowPrice = Lows[2][count];
    
                                // Update max and min price if necessary
                                if (highPrice > maxPrice)
                                    maxPrice = highPrice;
                                    futureTime = futureTime.AddSeconds(15);
                                    // we save at correctTime variable the candle timestamp where the max is located
                                    correctTime = futureTime;
                                if (lowPrice < minPrice)
                                    minPrice = lowPrice;
                                    //futureTime = futureTime.AddSeconds(15);
                                    correctTime = futureTime;
                            }
    
                            count--;
                            //futureTime = futureTime.AddSeconds(15);
                        }
    
                        // If a 15s candle with delta > 60 or delta < -60 was found, we save the variables on an array
                        if (maxPrice != double.MinValue && minPrice != double.MaxValue){
                            // Save values to an array
                            double[] priceRange = new double[] { maxPrice, minPrice };
                            Print(string.Format("{0} --> MAX price: {1}, MIN price: {2}", correctTime, priceRange[0], priceRange[1]));
                        }
    
                        else{
                            // If no 15 seconds candles with delta > 60 or delta < -60 was found, we save the MAX and MIN value of the 1 min array
                            double highPrice = BarsArray[1].GetHigh(0); // Get highest price of 1 min candle
                            double lowPrice = BarsArray[1].GetLow(0); // Get lowest price of 1 min candle
    
                            // Save LOW and High values into an array
                            double[] priceRange = new double[] { highPrice, lowPrice };
                            Print(string.Format("{0} --> MAX price: {1}, MIN price: {2}", correctTime, priceRange[0], priceRange[1]));
                        }
                    }
                }
            }            
        }
    }
    ​
    So the thing is that after getting the low and high values inside "priceRange" vector, I would like to wait 2 candles before continuing analyizing other parameters. How should be done?

    #2
    Hello speedytrade02,

    Thanks for your post.

    The CurrentBar value could be saved to a variable named something like "myCurrentBar" when you are getting the low and high values. Then you could create a condition that checks if CurrentBar is greater than your "myCurrentBar" variable plus 2 to see if at least 2 bars have passed since you got those values.

    Or, a timer could be implemented in your script by using TriggerCustomEvent() to have the script wait a certain amount of time before performing an action.

    CurrentBar: https://ninjatrader.com/support/help...currentbar.htm
    TriggerCustomEvent(): https://ninjatrader.com/support/help...ustomevent.htm
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Thanks Brandon, that worked fine!

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by jxs_xrj, 01-12-2020, 09:49 AM
      5 responses
      3,290 views
      1 like
      Last Post jgualdronc  
      Started by Touch-Ups, Today, 10:36 AM
      0 responses
      7 views
      0 likes
      Last Post Touch-Ups  
      Started by geddyisodin, 04-25-2024, 05:20 AM
      8 responses
      61 views
      0 likes
      Last Post NinjaTrader_Gaby  
      Started by Option Whisperer, Today, 09:55 AM
      0 responses
      8 views
      0 likes
      Last Post Option Whisperer  
      Started by halgo_boulder, 04-20-2024, 08:44 AM
      2 responses
      24 views
      0 likes
      Last Post halgo_boulder  
      Working...
      X