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

chartScale.GetYByValue() returning invalid numbers

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

    chartScale.GetYByValue() returning invalid numbers

    Hi,
    I want to print certain calculations beside the high, low and close of the current candle. and previous candle on the chart.
    I was looking at a number of examples that use chartScale.GetYByValue. I have checked the values that I'm entering in and they are matching the y tick value displayed. However the y that is returned is an extremely large negative number so my values aren't displayed. If I override and set y=100 for instance my numbers then start displaying on the screen obviously at a different location.
    Is it possible to tell me what I may be doing wrong with the below code. I've attached the full code as well if that helps.

    I'm using version 8.0.27.1 64-bit

    Code:
    protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
            {
                base.OnRender(chartControl, chartScale);
                if (CurrentBar < 1)
                    return;
                TextFormat textFormat = chartControl.Properties.LabelFont.ToDirectWriteTextFormat();
                SharpDX.Direct2D1.Brush textBrushDx = TextBrush.ToDxBrush(RenderTarget);
                ChartPanel panel = chartControl.ChartPanels[ChartPanel.PanelIndex];
                // Define the parameters
                int fontSize = 10;
                Brush textBrush = Brushes.White;
                double boxWidth = 50;
                double boxHeight = 30;
                var lastBarX = chartControl.GetXByBarIndex(ChartBars, ChartBars.ToIndex);
                var secondLastBarX = chartControl.GetXByBarIndex(ChartBars, ChartBars.ToIndex - 1);
                double high = High[0];
                double low = Low[0];
                var high0_Y = chartScale.GetYByValue(high);
                var low0_Y = chartScale.GetYByValue(low);
                int yByValue = chartScale.GetYByValue(Bars.GetClose(Bars.Count - 1));
                //double boxX = chartControl.Bounds.Right - boxWidth - 10;
                //double boxY1 = chartScale.GetYByValue(CurrentBar - 1) - (boxHeight / 2);
                //double boxY2 = chartScale.GetYByValue(CurrentBar) - (boxHeight / 2);
    
                // Draw pivot text
                System.Windows.Point startPoint = new System.Windows.Point(lastBarX, yByValue);
                TextLayout textLayout = new TextLayout(Globals.DirectWriteFactory,
                                        range1.ToString("0.0"), textFormat,
                                        ChartPanel.W, textFormat.FontSize);
                RenderTarget.DrawTextLayout(startPoint.ToVector2(), textLayout, textBrushDx);
    
            }​
    ​​
    Attached Files

    #2
    Hello cp.trader,

    You have some errors in your logic here, you are using BarsAgo and trying to get the Y value from an invalid price.

    Code:
    double high = High[0];
    double low = Low[0];
    var high0_Y = chartScale.GetYByValue(high);
    var low0_Y = chartScale.GetYByValue(low);
    You cant use High[0] or Low[0] in OnRender, that won't return the bar you intended. You need to use the GetValueAt method whenever you access a series for prices:

    Code:
    var high0_Y = chartScale.GetYByValue(High.GetValueAt(CurrentBar - 1));
    var low0_Y = chartScale.GetYByValue(Low.GetValueAt(CurrentBar - 1));


    In the code you provided you are also using a few different methods at finding bar indexes, some of which are specific bar indexes and some that are related to what part of the chart you are currently looking at. As I am not sure what the overall goal was its hard to say if that would be correct as coded. I would suggest to comment out most of what you have and first focus on collecting the prices and bar indexes that you need. That will make all the other code you are using much easier and less complicated.

    Just to clarify the above statement, the following can all represent the last bar on the chart but all have different outcomes depending on the use and script settings:

    ChartBars.ToIndex // would be the index of the last visible bar on the chart, this changes as you scroll the chart
    CurrentBar // The actual bar being processed from OnBarUpdate, will represent the last bar in the dataset.
    Bars.Count // Similar to using CurrentBar however this is the Count and will change its value depending on the Calculate setting being used, you will have to subtract 1 or 2 to get the last bar index.






    JesseNinjaTrader Customer Service

    Comment


      #3
      Thank you for your response, I changed my code and still had the same problem. I've simplified it to avoid confusion and put some print statements in there.
      I'm trying to create proof of concept. what I'm trying to do is print text at the high and low of the current candle and previous candle.

      On a side note how do I display inverse text ie black background and white text for the give area. or red back ground white text.


      using your example I changed the code to the following
      Code:
              protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
              {
                  if (CurrentBar < 1)
                      return;
      
                  TextFormat textFormat = chartControl.Properties.LabelFont.ToDirectWriteTextFormat();
                  SharpDX.Direct2D1.Brush textBrushDx = TextBrush.ToDxBrush(RenderTarget);
                  ChartPanel panel = chartControl.ChartPanels[ChartPanel.PanelIndex];
      
                  int currentX = chartControl.GetXByBarIndex(ChartBars, CurrentBar);
                  int previousX = chartControl.GetXByBarIndex(ChartBars, CurrentBar - 1);
                  double highCurrent = High.GetValueAt(CurrentBar);
                  double highPrevious = High.GetValueAt(CurrentBar-1);
                  double lowCurrent = Low.GetValueAt(CurrentBar);
                  double lowPrevious = Low.GetValueAt(CurrentBar-1);
      
                  int currentHighY = chartScale.GetYByValue(highCurrent);
                  int currentLowY = chartScale.GetYByValue(lowCurrent);
                  int previousHighY = chartScale.GetYByValue(highPrevious);
                  int previousLowY = chartScale.GetYByValue(lowPrevious);
      
                  DrawText(currentX, currentHighY, "CH", textFormat, textBrushDx);
                  DrawText(currentX, currentLowY, "CL", textFormat, textBrushDx);
                  DrawText(previousX, previousHighY, "PH", textFormat, textBrushDx);
                  DrawText(previousX, previousLowY, "PL", textFormat, textBrushDx);
                  string txtCurrent= String.Format(  "({0}, {1})  -  Current X  =  {2}     CurrentHighY   = {3}      CurrentLowY  = {4}     ", currentX, highCurrent, currentX, currentHighY, currentLowY );
                  string txtPrevious = String.Format("({0}, {1})  -  Previous X  = {2}     PreviousHighY  = {3}      PreviousLowY = {4}     ", previousX, highPrevious, previousX, previousHighY, previousLowY);
                  Print(txtCurrent);
                  Print(txtPrevious);
      
              }
      
              private void DrawText(int x, int y, string text, TextFormat textFormat, SharpDX.Direct2D1.Brush textBrushDx)
              {
                  System.Windows.Point drawAtPoint = new System.Windows.Point(x, y);
                  TextLayout textLayout = new TextLayout(Globals.DirectWriteFactory,
                                          text, textFormat,
                                          ChartPanel.W, textFormat.FontSize);
                  RenderTarget.DrawTextLayout(drawAtPoint.ToVector2(), textLayout, textBrushDx);
              }​



      The output displays
      (1225, 12108.5) - CurrentX = 1225 CurrentHighY = -40778806 CurrentLowY = -40755231
      (1209, 12108.5) - PreviousX = 1209 PreviousHighY = -40778806 PreviousLowY = -40757757
      (1225, 12108.5) - CurrentX = 1225 CurrentHighY = -40778806 CurrentLowY = -40755231
      (1209, 12108.5) - PreviousX = 1209 PreviousHighY = -40778806 PreviousLowY = -40757757
      (1225, 12108.5) - CurrentX = 1225 CurrentHighY = -40778806 CurrentLowY = -40755231
      (1209, 12108.5) - PreviousX = 1209 PreviousHighY = -40778806 PreviousLowY = -40757757
      (1225, 12108.5) - CurrentX = 1225 CurrentHighY = -40778806 CurrentLowY = -40755231
      (1209, 12108.5) - PreviousX = 1209 PreviousHighY = -40778806 PreviousLowY = -40757757

      My chart (NQ) has the value range from 12190 to 12030​. The
      Attached Files

      Comment


        #4
        Thank you for your response, I changed my code and still had the same problem. I've simplified it to avoid confusion and put some print statements in there.
        I'm trying to create proof of concept. what I'm trying to do is print text at the high and low of the current candle and previous candle.

        On a side note how do I display inverse text ie black background and white text for the give area. or red back ground white text.


        (Just a thought can you have a look at my initialization code and see if it is drawing on the correct panel.)

        using your example I changed the code to the following
        Code:
                protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
                {
                    if (CurrentBar < 1)
                        return;
        
                    TextFormat textFormat = chartControl.Properties.LabelFont.ToDirectWriteTextFormat();
                    SharpDX.Direct2D1.Brush textBrushDx = TextBrush.ToDxBrush(RenderTarget);
                    ChartPanel panel = chartControl.ChartPanels[ChartPanel.PanelIndex];
        
                    int currentX = chartControl.GetXByBarIndex(ChartBars, CurrentBar);
                    int previousX = chartControl.GetXByBarIndex(ChartBars, CurrentBar - 1);
                    double highCurrent = High.GetValueAt(CurrentBar);
                    double highPrevious = High.GetValueAt(CurrentBar-1);
                    double lowCurrent = Low.GetValueAt(CurrentBar);
                    double lowPrevious = Low.GetValueAt(CurrentBar-1);
        
                    int currentHighY = chartScale.GetYByValue(highCurrent);
                    int currentLowY = chartScale.GetYByValue(lowCurrent);
                    int previousHighY = chartScale.GetYByValue(highPrevious);
                    int previousLowY = chartScale.GetYByValue(lowPrevious);
        
                    DrawText(currentX, currentHighY, "CH", textFormat, textBrushDx);
                    DrawText(currentX, currentLowY, "CL", textFormat, textBrushDx);
                    DrawText(previousX, previousHighY, "PH", textFormat, textBrushDx);
                    DrawText(previousX, previousLowY, "PL", textFormat, textBrushDx);
                    string txtCurrent= String.Format(  "({0}, {1})  -  Current X  =  {2}     CurrentHighY   = {3}      CurrentLowY  = {4}     ", currentX, highCurrent, currentX, currentHighY, currentLowY );
                    string txtPrevious = String.Format("({0}, {1})  -  Previous X  = {2}     PreviousHighY  = {3}      PreviousLowY = {4}     ", previousX, highPrevious, previousX, previousHighY, previousLowY);
                    Print(txtCurrent);
                    Print(txtPrevious);
        
                }
        
                private void DrawText(int x, int y, string text, TextFormat textFormat, SharpDX.Direct2D1.Brush textBrushDx)
                {
                    System.Windows.Point drawAtPoint = new System.Windows.Point(x, y);
                    TextLayout textLayout = new TextLayout(Globals.DirectWriteFactory,
                                            text, textFormat,
                                            ChartPanel.W, textFormat.FontSize);
                    RenderTarget.DrawTextLayout(drawAtPoint.ToVector2(), textLayout, textBrushDx);
                }​



        The output displays
        (1225, 12108.5) - CurrentX = 1225 CurrentHighY = -40778806 CurrentLowY = -40755231
        (1209, 12108.5) - PreviousX = 1209 PreviousHighY = -40778806 PreviousLowY = -40757757
        (1225, 12108.5) - CurrentX = 1225 CurrentHighY = -40778806 CurrentLowY = -40755231
        (1209, 12108.5) - PreviousX = 1209 PreviousHighY = -40778806 PreviousLowY = -40757757
        (1225, 12108.5) - CurrentX = 1225 CurrentHighY = -40778806 CurrentLowY = -40755231
        (1209, 12108.5) - PreviousX = 1209 PreviousHighY = -40778806 PreviousLowY = -40757757
        (1225, 12108.5) - CurrentX = 1225 CurrentHighY = -40778806 CurrentLowY = -40755231
        (1209, 12108.5) - PreviousX = 1209 PreviousHighY = -40778806 PreviousLowY = -40757757

        My chart (NQ) has the value range from 12190 to 12030​. The
        Attached Files

        Comment


          #5
          Hello cp.trader

          What specific line are you having trouble with? Our support is not going to run your scripts to debug it for you but we can use that information to provide guidance. If you point out what specific line you have trouble with we can focus on that.

          As a side note, please avoid asking separate questions. If you have a unrelated question please open a new post for that.
          JesseNinjaTrader Customer Service

          Comment


            #6
            Sorry I though my last post had enough information in it.
            I have changed onrender to call chartScale.GetYByValue(lowCurrent) as asked.

            howerver it is returning negative numbers. (see the print output I had included in the previous post)

            One of these values was
            Current high => 12108.5 (Ticks)
            GetYByValue => returning -40778806
            therefore when I try and place text on the screen it is not displayed as Y is an invalid value.

            Please note that this value is displayed on the y column so it is on the chart.

            I can't figure out what is wrong GetYByValue and what I have to do differently.
            Last edited by cp.trader; 02-22-2023, 10:32 PM.

            Comment


              #7
              Hello cp.trader,

              In the file you attached you are not using the charts price scale so GetYByValue wouldn't make sense for that use case. You would need to be using the same scale as the chart to use that method to find Y values from bars on the chart.

              The chartScale object that's passed in with OnRender refers to the scale the indicator is using, when you use overlay the indicators scale only uses the plots that the indicator produces for the scale. GetYByValue would work for anything which exists in the same scale as the indicator so you need to switch this to Right instead of Overlay so the charts right hand price scale is used. ​
              JesseNinjaTrader Customer Service

              Comment


                #8
                Thankyou so much that fixed the problem and I would never have found that.
                I think I had copied it from another indicator.
                Where is the best place to find information on all the initialisation parameters.

                Comment


                  #9
                  Hello cp.trader,

                  The best suggestion that I could make would be to generate a new empty indicator which has the standard defaults and then use the help guide to search for each of those properties. The help guide has a dedicated page for every supported property and has descriptions of each.

                  You may encounter other properties being set in State.SetDefaults however those are most often not something you would normally set, the items in a new empty indicator are the main settings that really play a role in how the script works so those are the items you would want to learn about to know how scripts work and the options they have.



                  JesseNinjaTrader Customer Service

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by knighty6508, 05-10-2024, 01:20 AM
                  4 responses
                  25 views
                  0 likes
                  Last Post knighty6508  
                  Started by OllieFeraher, 05-09-2024, 11:14 AM
                  6 responses
                  19 views
                  0 likes
                  Last Post OllieFeraher  
                  Started by PaulMohn, 05-02-2024, 06:59 PM
                  2 responses
                  44 views
                  0 likes
                  Last Post PaulMohn  
                  Started by ETFVoyageur, Today, 02:10 AM
                  0 responses
                  17 views
                  0 likes
                  Last Post ETFVoyageur  
                  Started by rayyyu12, Today, 12:47 AM
                  0 responses
                  12 views
                  0 likes
                  Last Post rayyyu12  
                  Working...
                  X