Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

X Offset when Rendering Higher Time Frame data on the chart

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

    X Offset when Rendering Higher Time Frame data on the chart

    Hi,

    I am trying to render data that was calculated on a higher time frame on the same chart. The price is correct but the bar numbers are of course different.

    I was trying to use DateTime timeslot1=Times[1].GetValueAt(startBar) of the higher time frame to get the DateTime, and convert it to chart X using double val1=(double)chartControl.GetXByTime(timeslot1). I'm then converting the price to char y using PriceToY(val1, chartScale), and I get to see the line, but with X offset.

    Please see the image and the relevant code below.

    Your help is much appreciated.


    Click image for larger version

Name:	Screenshot 2023-03-08 at 3.07.14.png
Views:	250
Size:	1,006.9 KB
ID:	1238685

    Code:
    ...
    
    DrawDXLine(Times[timeFrame].GetValueAt(startBar),Times[timeFrame].GetValueAt(endBar),
        startPrice,endPrice,
        (dir>0?SwingsUpStroke:indicator.SwingsDownStroke),chartControl,chartScale);
    
    ...
    
    protected void DrawDXLine(DateTime timeslot1, DateTime timeslot2, double val1, double val2,
        Stroke stroke, ChartControl chartControl, Gui.Chart.ChartScale chartScale)
    {
        DrawDXLine((double)chartControl.GetXByTime(timeslot1),(double)chartControl.GetXByTime(timeslot2),
                    PriceToY(val1, chartScale), PriceToY(val2, chartScale),
                    stroke.Brush, stroke.DashStyleHelper, chartControl, chartScale, stroke.Width, stroke.Opacity);
    }
    
    protected void DrawDXLine(double x1, double x2, double y1, double y2, Brush brush, DashStyleHelper dashStyle,
        Gui.Chart.ChartControl chartControl, Gui.Chart.ChartScale chartScale, float width=1f, int opacity = 100)
    {
        Point p1 = new Point(x1, y1);
        Point p2 = new Point(x2, y2);
        DrawDXLine(p1, p2, brush, dashStyle, chartControl, chartScale, width, opacity);
    }
    
    protected void DrawDXLine(Point p1, Point p2, Brush brush, DashStyleHelper dashStyle,
        ChartControl chartControl, Gui.Chart.ChartScale chartScale, float width=1f, int opacity=100)
    {
        SharpDX.Direct2D1.DashStyle tempDashStyle;
        if (!Enum.TryParse(dashStyle.ToString(), true, out tempDashStyle)) tempDashStyle = SharpDX.Direct2D1.DashStyle.Dash;
        SharpDX.Direct2D1.StrokeStyleProperties properties = new SharpDX.Direct2D1.StrokeStyleProperties() {DashStyle=tempDashStyle};
        SharpDX.Direct2D1.StrokeStyle strokeStyle = new SharpDX.Direct2D1.StrokeStyle(Core.Globals.D2DFactory, properties);
    
        SharpDX.Direct2D1.Brush dxBrush = brush.ToDxBrush(RenderTarget);
        dxBrush.Opacity = opacity/100f;
    
        RenderTarget.DrawLine(p1.ToVector2(), p2.ToVector2(), dxBrush, width, strokeStyle);
    
        dxBrush.Dispose();
        strokeStyle.Dispose();
    }
    ​
    ​

    #2
    Hello Shai Samuel,

    Thank you for your post.

    Typically, rendering a value from a secondary series can be achieved by storing the data in a plot or a series. You can then use GetValueAt() to get the values that need to be rendered. My colleague has created an example demonstrating this idea that may be found in the following post:


    If this does not address your inquiry, please clarify what you are looking to achieve with the higher time frame data so I may better assist you.

    Thank you for using NinjaTrader.

    Comment


      #3
      Thank you Emily, unfortunately this example doesn't address my question. The issue I raised wasn't about accessing the data from the additional DataSeries, but about rendering.

      When rendering, you need to convert the bars/time to the chart x axis. I did that using time, as I am not aware of any other alternative, but the result was with significantly offset as you can see in the image I added above (this is the swing zigzag of the higher timeframe (3 times bigger). I also added the relevant short code that shows my steps.

      This is an advanced issue, as rendering requires much more than a simple draw.

      Comment


        #4
        Hello Shai Samuel,

        Thank you for your reply.

        If you are working with the swing zigzag of the higher timeframe, those values should be saved to a series or plot. The examples in the forum post I linked show how to make sure there are values for each index of the primary series, even if the calculation is being done on a higher timeframe. You can then loop through the visible bars on the chart, get the value from the series/plot for each bar index, and render that value how you would like. Getting the value at a specific time from the secondary series will result in the script trying to match that time up with a time on the primary series, though it may not be the same x value and I suspect that is why you are seeing an offset with your current script. You may see different results if you get the values from times on the primary bars, because then the values would likely match the OnRender() slots for the primary series.

        Here is an example of how you could loop through and get the value from a series for each visible bar on the chart:
        protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
        {
        // make sure there are bars displayed on the chart and the chart control is ready before running
        if (Bars == null || chartControl == null)
        return;

        // loop through all the visible bars on the chart
        for (int i = ChartBars.FromIndex; i <= ChartBars.ToIndex; i++)
        {
        double swingZigZagValue = swingZigZagSeries.GetValueAt(i);
        // render the value here
        }
        }

        I hope this helps to clarify why I mentioned the previous examples of adding values to a plot or series from a higher timeframe. You could then use that plot or series to get the values that need to be rendered.

        Please let me know if I may be of further assistance.​

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        571 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        331 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        101 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        549 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        549 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X