Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Help with Anchored Regression channel
Collapse
X
-
Help with Anchored Regression channel
I would like to anchor the Regression Channel between 4 am and 9:30 am and have it extended until 3:59 pm. I am stuck with the anchoring logic and am not sure how to extend the channel. When I put the if condition between time range to calculate regression it draws weird lines. I have attached my code. Any help will be so appreciated. Thanks!!Tags: None
-
Thanks so much Brandon for your help. I tried using the SharpDX GetXByTime as suggested, however not no lines are plotted. Can you please tell me what I am doing wrong?
protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
{
if (Bars == null || chartControl == null) return; // Ensure valid conditions
//RenderTarget renderTarget = chartControl.RenderTarget;
RenderTarget.AntialiasMode = AntialiasMode.PerPrimitive;
// Define anchor times for custom rendering
TimeSpan startTime = new TimeSpan(4, 0, 0); // 4:00 am
TimeSpan endTime = new TimeSpan(14, 30, 0); // 2:30 pm
DateTime chartDate = Times[0][0].Date;
// Calculate x-coordinates for start and end anchor points
int startX = chartControl.GetXByTime(chartDate.Add(startTime)); // Start anchor at 4:00 am
int endX = chartControl.GetXByTime(chartDate.Add(endTime)); // End anchor at 2:30 pm
if (startX >= endX) return; // Ensure valid rendering range
// Retrieve regression parameters
int idx = BarsArray[0].Count - 1 - (Calculate == Calculate.OnBarClose ? 1 : 0);
double intercept = interceptSeries.GetValueAt(idx);
double slope = slopeSeries.GetValueAt(idx);
double stdDev = stdDeviationSeries.GetValueAt(idx);
// Calculate pixel-based standard deviation
ChartPanel panel = chartControl.ChartPanels[ChartPanel.PanelIndex];
int stdDevPixels = (int) Math.Round(((stdDev * Width) / (chartScale.MaxValue - chartScale.MinValue)) * panel.H, 0);
// Calculate y-coordinates for middle, upper, and lower lines
int startY = chartScale.GetYByValue(intercept);
int endY = chartScale.GetYByValue(intercept + slope * (Period - 1));
// Define vectors for rendering
Vector2 startVector = new Vector2(startX, startY);
Vector2 endVector = new Vector2(endX, endY);
RenderTarget.DrawLine(startVector, endVector, Plots[0].BrushDX, Plots[0].Width, Plots[0].StrokeStyle);
// Upper
RenderTarget.DrawLine(new Vector2(startVector.X, startVector.Y - stdDevPixels), new Vector2(endVector.X, endVector.Y - stdDevPixels), Plots[1].BrushDX, Plots[1].Width, Plots[1].StrokeStyle);
// Lower
RenderTarget.DrawLine(new Vector2(startVector.X, startVector.Y + stdDevPixels), new Vector2(endVector.X, endVector.Y + stdDevPixels), Plots[2].BrushDX, Plots[2].Width, Plots[2].StrokeStyle);
// Draw middle regression line
// renderTarget.DrawLine(
// startVector,
// endVector,
// Plots[0].BrushDX,
// lots[0].Width, Plots[0].StrokeStyle
// );
// // Draw upper and lower regression lines for the channel
// renderTarget.DrawLine(
// new Vector2(startVector.X, startVector.Y - stdDevPixels),
// new Vector2(endVector.X, endVector.Y - stdDevPixels),
// new SolidColorBrush(renderTarget, SharpDX.Color.Blue),
// 2.0f,
// new SharpDX.Direct2D1.StrokeStyle(chartControl.Factory , new SharpDX.Direct2D1.StrokeStyleProperties { DashStyle = SharpDX.Direct2D1.DashStyle.Solid })
// );
// renderTarget.DrawLine(
// new Vector2(startVector.X, startVector.Y + stdDevPixels),
// new Vector2(endVector.X, endVector.Y + stdDevPixels),
// new SolidColorBrush(renderTarget, SharpDX.Color.Blue),
// 2.0f,
// new SharpDX.Direct2D1.StrokeStyle(chartControl.Factory , new SharpDX.Direct2D1.StrokeStyleProperties { DashStyle = SharpDX.Direct2D1.DashStyle.Solid })
// );
}
-
Thanks again for clarifying. I changed the time to datetime and added print statement and still nothing plots. How many times does OnRender method get called. I have a one minute chart and added the print statement in the OnRender and it printed only 6 times.Originally posted by NinjaTrader_BrandonHHello Graci117,
Thanks for your notes.
A DateTime object should be passed into the GetXByTime() method. The TimeSpan objects in your script should be changed to DateTime objects and then you would pass that object into the GetXByTime() method as seen in the sample code on the GetXByTime() help guide linked in post # 2.
To understand exactly how your logic in the script is behaving you would have to add debugging prints to the script that prints out all the values in the logic to custom render the lines on the chart to understand how your logic is evaluating and behaving. Once you understand exactly how the logic is behaving, you could modify the logic of the script accordingly.
Add prints to the script that print out all the logic used for custom rendering the lines on the chart along with labels to know what is being compared to understand behavior.
Below is a link to a forum post that demonstrates how to use prints to understand behavior.
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
599 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
345 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
103 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
558 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
558 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|

Comment