#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]));
}
}
}
}
}
}
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Sleeping 2 bars before continuing
Collapse
X
-
Sleeping 2 bars before continuing
Hi, I'm trying to wait 2 candles before performing an action. I have the following code
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?HTML Code:Tags: None
-
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<span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>
-
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
639 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
366 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
107 views
0 likes
|
Last Post
by Mindset
02-09-2026, 11:44 AM
|
||
|
Started by Geovanny Suaza, 02-02-2026, 12:30 PM
|
0 responses
569 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
572 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|

Comment