Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Help with Range Based Strategy
Collapse
X
-
Not sure where to find the "other one"?
Here's what is in the using declarations section:
// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
///<summary>
/// Simple strategy that monitors for a breakout.
///</summary>
[Description("Simple strategy that monitors for a breakout.")]
publicclass SamBreakOut : Strategy
Here's the variables section:
///<summary>
/// This method is used to configure the strategy and is called once before any strategy method is called.
///</summary>
privatedouble highestHigh = 0;
protectedoverridevoid Initialize()
{
CalculateOnBarClose = false;
}
Comment
-
And the OnBarUpdate section:
///<summary>
/// Called on each bar update event (incoming tick)
///</summary>
protectedoverridevoid OnBarUpdate()
{
{
// recording the first highest high
if (ToTime(Time[0]) >= 93000 && ToTime(Time[0]) <= 94500)
{
if (High[0] > highestHigh)
highestHigh = High[0];
}
}
// Entry Condition: Submit a buy stop order two ticks above the highest high after 9:30am
if (ToTime(Time[0]) > 94500 && ToTime(Time[0]) <= 100000)
{
// EnterLongStop() can be used to generate buy stop orders.
EnterLongStop(highestHigh + TickSize * 2);
}
// Exit Condition: Close positions after 10 bars have passed
if (BarsSinceEntry() > 10)
ExitLong();
}
}
}
Comment
-
Ok . . here it is, so which one should I delete the first declaration or the second one?
#region Variables
privatedouble highestHigh = 0;
#endregion
///<summary>
/// This method is used to configure the strategy and is called once before any strategy method is called.
///</summary>
privatedouble highestHigh = 0;
protectedoverridevoid Initialize()
{
CalculateOnBarClose = false;
Comment
-
This might help. It is basically your sample indicator but works great for what Insearch is trying to accomplish.
Now you can call HighestHigh and LowestLow for whatever time period you select with the variables StartHours/Minute and EndHour/Minute.Code:protected override void OnBarUpdate() { // Check to make sure the end time is not earlier than the start time if (EndHour < StartHour) return; //Do not calculate the high or low value when the ending time of the desired range is less than the current time of the bar being processed if (ToTime(EndHour, EndMinute, 0) > ToTime(Time[0])) return; // If the stored date time date is not the same date as the bar time date, create a new DateTime object if (startDateTime.Date != Time[0].Date) { startDateTime = new DateTime(Time[0].Year, Time[0].Month, Time[0].Day, StartHour, StartMinute, 0); endDateTime = new DateTime(Time[0].Year, Time[0].Month, Time[0].Day, EndHour, EndMinute, 0); } // Calculate the number of bars ago for the start and end bars of the specified time range int StartBarsAgo = GetBar(startDateTime); int EndBarsAgo = GetBar(endDateTime); // Now that we have the start end end bars ago values for the specified time range we can calculate the highest high for this range double HighestHigh = MAX(High, StartBarsAgo - EndBarsAgo)[EndBarsAgo]; double LowestLow = MIN(Low, StartBarsAgo - EndBarsAgo)[EndBarsAgo]; //Entry Logic added here! }
Comment
-
Thx. Compiled it at ran it on the analyzer . . . Why is the strategy not taking longs in some cases after 9:45 and at other times it is taking it late?
For example on 2/11 the high between 9:30-9:45 was 833.75. 2 ticks above that would be 834.25, which the market had touched by 9:50, but there was no long taken . . . .
On 2/6 the high was 856. Again by 9:50 the market had touched 856.50, but the long was taken in the 9:55 bar . . .
Also, I see that the code allows for multiple entries, how can I limit the entry to only 1 instance?
Comment
-
Thx GreenTrade, I'm very new to programming . . . I'd seen that indicator code, but wasn't sure how to modify it for the strategy . . .
Of course it doesn't help that I don't know a lot of the syntax and available commands, but learning as I go along . . .
In your suggestion at the end, do you imply that I'd have to set the variables StartHours/Minute and EndHour/Minute in the variables section, ie. specify the start and end times which would capture the high/low values in that time range and then in the entry logic specify entry based off of those high/lows?
Originally posted by GreenTrade View PostThis might help. It is basically your sample indicator but works great for what Insearch is trying to accomplish.
Now you can call HighestHigh and LowestLow for whatever time period you select with the variables StartHours/Minute and EndHour/Minute.Code:protected override void OnBarUpdate() { // Check to make sure the end time is not earlier than the start time if (EndHour < StartHour) return; //Do not calculate the high or low value when the ending time of the desired range is less than the current time of the bar being processed if (ToTime(EndHour, EndMinute, 0) > ToTime(Time[0])) return; // If the stored date time date is not the same date as the bar time date, create a new DateTime object if (startDateTime.Date != Time[0].Date) { startDateTime = new DateTime(Time[0].Year, Time[0].Month, Time[0].Day, StartHour, StartMinute, 0); endDateTime = new DateTime(Time[0].Year, Time[0].Month, Time[0].Day, EndHour, EndMinute, 0); } // Calculate the number of bars ago for the start and end bars of the specified time range int StartBarsAgo = GetBar(startDateTime); int EndBarsAgo = GetBar(endDateTime); // Now that we have the start end end bars ago values for the specified time range we can calculate the highest high for this range double HighestHigh = MAX(High, StartBarsAgo - EndBarsAgo)[EndBarsAgo]; double LowestLow = MIN(Low, StartBarsAgo - EndBarsAgo)[EndBarsAgo]; //Entry Logic added here! }
Comment
-
Yes in the variables section you would add the times to start and end the calculation of the highs and lows.
Code:#region Variables // Wizard generated variables private int startHour = 12; // Default setting for StartHour private int startMinute = 00; // Default setting for StartMinute private int endHour = 13; // Default setting for EndHour private int endMinute = 00; // Default setting for EndMinute #endregion
Comment
-
Ok. The below code seems to work, but I still have in some cases multiple entries in one day . . . How can I limit this to only 1 per day?
Also, I still have instances where the execution is late or is not happening when the price (according to the chart in the analyzer) seems to have hit the stops. Any idea what could be going on?
protectedoverridevoid Initialize()
{
/* The following settings configure your strategy to execute only one entry for each uniquely named entry signal.
This can be configured in the Initalize() method or via the Strategy dialog window when running or backtesting a strategy */
EntriesPerDirection = 1;
EntryHandling = EntryHandling.UniqueEntries;
CalculateOnBarClose = false;
/* These Set methods will place Profit Target and Stop Loss orders for our entry orders.
*/
SetProfitTarget(CalculationMode.Ticks, 4);
SetStopLoss(CalculationMode.Ticks, 10);
}
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
650 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
370 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
109 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
574 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
577 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|

Comment