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

Price Line

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

    Price Line

    Hello,

    I am using the code below to get the price value of a 'user drawn' horizontal line on a chart. It seems to work okay on a blank chart (i.e. gets the correct price value). However, when I tried it on the chart I actually want to use it on, it comes up with the wrong price value, actually stuck on some arbitrary value from no where. The chart has got other indicators on it that plot lines like the current day low/high etc, but none of these lines are 'user drawn' (i.e. not drawn manually by myself). I wonder whether some how, these indicators' plotted lines are affecting my code below that's meant to get values from 'user drawn' lines only. If that's the case, how can I circumvent this please, is there a code I could further add to sort of identify/specify the horizontal line I want the y value (price) obtained from?

    Many thanks
    Dan

    Code:
    protected override void OnStartUp()
            {
              foreach (IDrawObject draw in DrawObjects)
                 {
                   if (draw.DrawType == DrawType.HorizontalLine && draw.UserDrawn)
                     {
                     IHorizontalLine hLine = (IHorizontalLine) draw;
                     yvalue = Math.Round(hLine.Y, 2);    // This is Price value
                     }
                    DrawTextFixed("Price","Price is " + yvalue, TextPosition.BottomLeft);
                 }
                
            }

    #2
    Hello rabcnesbit,
    There my be other user drawn horizontal lines in the chart. Please filter it further by using the tag name property.

    Code:
    if (hLine.Tag = "user drawn tag name")
    {
      //do something;
    }
    Please let me know if I can assist you any further.
    JoydeepNinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Joydeep View Post
      Hello rabcnesbit,
      There my be other user drawn horizontal lines in the chart. Please filter it further by using the tag name property.

      Code:
      if (hLine.Tag = "user drawn tag name")
      {
        //do something;
      }
      Please let me know if I can assist you any further.

      Hello, NT_Joydeep,

      Thanks for your reply. How do I incorporate this with my original code please, and also I am assuming I need to replace "user drawn tag name" with a tag name, where would I get this tag name from?

      Many thanks
      Dan

      Comment


        #4
        Hello Dan,
        Please use the below code. Here in the code I am looking for a horizontal line with the tag name 0 (zero).

        Code:
        protected override void OnStartUp()
        {
            foreach (IDrawObject draw in DrawObjects)
            {
               	if (draw.DrawType == DrawType.HorizontalLine && draw.UserDrawn)
                {
                 	IHorizontalLine hLine = (IHorizontalLine) draw;
        			[B][COLOR="Blue"]if (hLine.Tag == "0")[/COLOR][/B]
                                 {
                 		     yvalue = Math.Round(hLine.Y, 2);    // This is Price value
                                 }
                }
            	    
        		DrawTextFixed("Price","Price is " + yvalue, TextPosition.BottomLeft);
            }
        }
        Please manually draw a horizontal line, and rename it as 0 (zero). To rename it please follow the below procedures:
        • Select the horizontal line and right click on it.
        • In the context menu click on Properties...
        • In the Horizontal Line Properties dialog, click on the Data tab.
        • In the Data tab, please reset the Tag textbox to 0
        • Click Ok.




        Please let me know if I can assist you any further.
        JoydeepNinjaTrader Customer Service

        Comment


          #5
          Many thanks,

          That worked fine.

          Please manually draw a horizontal line, and rename it as 0 (zero). To rename it please follow the below procedures:
          • Select the horizontal line and right click on it.
          • In the context menu click on Properties...
          • In the Horizontal Line Properties dialog, click on the Data tab.
          • In the Data tab, please reset the Tag textbox to 0
          • Click Ok.
          I thought that was probably what you meant, but I was hoping there was a better way to name (tag) the manually drawn horizontal line without the above long process, as I monitor many instruments and switch from one instrument to another continually using the same chart. Is there a way around this, or a code that automatically name the drawn line to 0 when I switch from one instrument to another and draw a new line?

          Many thanks.
          Dan

          Comment


            #6
            Hello Dan,
            The manually drawn lines will have a tag name automatically. However if you further want to rename it then you have to change it from the properties dialog and there is no alternative as of now.

            In NinjaScript, Tag is a read only property and you cannot set it.

            Please let me know if I can assist you any further.
            JoydeepNinjaTrader Customer Service

            Comment


              #7
              Ok, will just have to manage it the way it is.
              Many thanks for all your assistance, much obliged.

              Dan

              Comment


                #8
                Hello Dan,
                Thanks for your note.
                Please let me know if I can assist you any further.
                JoydeepNinjaTrader Customer Service

                Comment


                  #9
                  Hello,

                  I am using the following code, which works ok (does what I want it to do).

                  Code:
                  protected override void OnStartUp()
                          {
                            foreach (IDrawObject draw in DrawObjects)
                               {
                                 if (draw.DrawType == DrawType.HorizontalLine && draw.UserDrawn)
                                  
                                   {    
                                   IHorizontalLine hLine = (IHorizontalLine) draw;
                                    if (hLine.Tag == "0")
                                     {
                                     yvalue = Math.Round(hLine.Y, 2);    // This is Price value    
                                     }
                                   }
                                  
                               }
                              
                              
                              double StopLossP, StopLossPATR, StopLossP2, StopLossPATR2, SharesMxS, SharesMxS2, SharesATR, SharesATR2, SharesPC, SharesPC2;
                              
                              
                              StopLossP = yvalue - LossPerUnit6;
                              StopLossPATR = Math.Round(yvalue - ATR(14)[0],2);
                              SharesATR = Math.Round(Math.Abs(LossPerTrade6/(yvalue - StopLossPATR)),2);
                              SharesPC = Math.Round(Math.Abs(LossPerTrade6/(yvalue - [B][COLOR=Blue]Low[0][/COLOR][/B])),2);
                              SharesMxS = Math.Round(Math.Abs(LossPerTrade6/(yvalue - StopLossP)),2);
                              
                              StopLossP2 = yvalue + LossPerUnit6;
                              StopLossPATR2 = Math.Round(yvalue + ATR(14)[0],2);
                              SharesATR2 = Math.Round(Math.Abs(LossPerTrade6/(yvalue - StopLossPATR2)),2);
                              SharesPC2 = Math.Round(Math.Abs(LossPerTrade6/(yvalue - [B][COLOR=Blue]High[0][/COLOR][/B])),2);
                              SharesMxS2 = Math.Round(Math.Abs(LossPerTrade6/(yvalue - StopLossP2)),2);
                              
                              DrawTextFixed("Stop Values","EP " + yvalue + "  ATR " + Math.Round(ATR(14)[0],2) + "  BUY(MS/AS/PS) " + StopLossP + " [" + SharesMxS + "] / " + StopLossPATR + " [" + SharesATR + "] / " + [B][COLOR=Blue]Low[0][/COLOR][/B] + " [" + SharesPC + "]  " + "  SELL(MS/AS/PS) " + StopLossP2 + " [" + SharesMxS2 + "] / " + StopLossPATR2 + " [" + SharesATR2 + "] / " + [B][COLOR=Blue]High[0][/COLOR][/B] + " [" + SharesPC2 + "]" , TextPosition.BottomLeft, Color.White, textFont, Color.Transparent, Color.Transparent, 0);
                              
                              
                              
                              
                          }
                  However, when I change the Low[ ] /High [ ] (highlighted in blue in the code above for easy reference) to 1 or 2 or 3 (i.e. any other figure apart from 0), nothing shows. My chart is a 5-min chart and got several bars, so I am not quite sure what the problem is and completely baffled!

                  Hope you can help please?

                  Thanks.

                  Comment


                    #10
                    Hello Dan,
                    Please use the Print function to debug your code and see what is the value of SharesPC, SharesPC2 etc fields are.

                    Please refer to this post which will guide to how to effectively debug a NinjaScript code


                    Please let me know if I can assist you any further.
                    JoydeepNinjaTrader Customer Service

                    Comment


                      #11
                      Originally posted by rabcnesbit View Post
                      Hello,

                      I am using the following code, which works ok (does what I want it to do).

                      Code:
                      protected override void OnStartUp()
                              {
                                foreach (IDrawObject draw in DrawObjects)
                                   {
                                     if (draw.DrawType == DrawType.HorizontalLine && draw.UserDrawn)
                                      
                                       {    
                                       IHorizontalLine hLine = (IHorizontalLine) draw;
                                        if (hLine.Tag == "0")
                                         {
                                         yvalue = Math.Round(hLine.Y, 2);    // This is Price value    
                                         }
                                       }
                                      
                                   }
                                  
                                  
                                  double StopLossP, StopLossPATR, StopLossP2, StopLossPATR2, SharesMxS, SharesMxS2, SharesATR, SharesATR2, SharesPC, SharesPC2;
                                  
                                  
                                  StopLossP = yvalue - LossPerUnit6;
                                  StopLossPATR = Math.Round(yvalue - ATR(14)[0],2);
                                  SharesATR = Math.Round(Math.Abs(LossPerTrade6/(yvalue - StopLossPATR)),2);
                                  SharesPC = Math.Round(Math.Abs(LossPerTrade6/(yvalue - [B][COLOR=Blue]Low[0][/COLOR][/B])),2);
                                  SharesMxS = Math.Round(Math.Abs(LossPerTrade6/(yvalue - StopLossP)),2);
                                  
                                  StopLossP2 = yvalue + LossPerUnit6;
                                  StopLossPATR2 = Math.Round(yvalue + ATR(14)[0],2);
                                  SharesATR2 = Math.Round(Math.Abs(LossPerTrade6/(yvalue - StopLossPATR2)),2);
                                  SharesPC2 = Math.Round(Math.Abs(LossPerTrade6/(yvalue - [B][COLOR=Blue]High[0][/COLOR][/B])),2);
                                  SharesMxS2 = Math.Round(Math.Abs(LossPerTrade6/(yvalue - StopLossP2)),2);
                                  
                                  DrawTextFixed("Stop Values","EP " + yvalue + "  ATR " + Math.Round(ATR(14)[0],2) + "  BUY(MS/AS/PS) " + StopLossP + " [" + SharesMxS + "] / " + StopLossPATR + " [" + SharesATR + "] / " + [B][COLOR=Blue]Low[0][/COLOR][/B] + " [" + SharesPC + "]  " + "  SELL(MS/AS/PS) " + StopLossP2 + " [" + SharesMxS2 + "] / " + StopLossPATR2 + " [" + SharesATR2 + "] / " + [B][COLOR=Blue]High[0][/COLOR][/B] + " [" + SharesPC2 + "]" , TextPosition.BottomLeft, Color.White, textFont, Color.Transparent, Color.Transparent, 0);
                                  
                                  
                                  
                                  
                              }
                      However, when I change the Low[ ] /High [ ] (highlighted in blue in the code above for easy reference) to 1 or 2 or 3 (i.e. any other figure apart from 0), nothing shows. My chart is a 5-min chart and got several bars, so I am not quite sure what the problem is and completely baffled!

                      Hope you can help please?

                      Thanks.
                      If you want to refer to any bar other than 0 in a barSeries, you need a bars escape to prevent an exception when NT tries to read data from a non-existent bar.

                      Code:
                      private int barsBack = 0; //default. Make this a property, and you can change it from the GUI
                      Code:
                      protected override void OnBarUpdate()
                      {
                      if (CurrentBar < barsBack) return;
                      // Now you can reference any barSeries[barsBack]
                      }

                      Comment


                        #12
                        Originally posted by koganam View Post
                        If you want to refer to any bar other than 0 in a barSeries, you need a bars escape to prevent an exception when NT tries to read data from a non-existent bar.

                        Code:
                        private int barsBack = 0; //default. Make this a property, and you can change it from the GUI
                        Code:
                        protected override void OnBarUpdate()
                        {
                        if (CurrentBar < barsBack) return;
                        // Now you can reference any barSeries[barsBack]
                        }

                        Hello koganam,

                        Thank you very much for your time and assistance, I very grateful indeed. I did follow your advice above (which is a very good tip by the way and something I will definitely bear in mind in my future attempts at coding ), however, it did not seem to solve the problem. Cheers

                        Comment


                          #13
                          Originally posted by NinjaTrader_Joydeep View Post
                          Hello Dan,
                          Please use the Print function to debug your code and see what is the value of SharesPC, SharesPC2 etc fields are.

                          Please refer to this post which will guide to how to effectively debug a NinjaScript code


                          Please let me know if I can assist you any further.
                          Hello NT_Joydeep,

                          I followed your suggestion above, and below is the error I get:

                          Error on calling 'OnStartUp' method for indicator 'KayYLinePrice': You are accessing an index with a value that is invalid since its out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.

                          I do not understand why I am getting this as the chart is full of bars (5min bars) and I was only trying to reference the previous bar [1]. I tried it on several instruments (all with loads of bars, and I still get the same error). Hope you can assist?

                          Many thanks
                          Dan

                          Comment


                            #14
                            Dan, can you please post the OnStartUp() of this indicator script? Likely you're running into what koganam mentioned, but your check only covers the OnBarUpdate().
                            BertrandNinjaTrader Customer Service

                            Comment


                              #15
                              Originally posted by rabcnesbit View Post
                              Hello koganam,

                              Thank you very much for your time and assistance, I very grateful indeed. I did follow your advice above (which is a very good tip by the way and something I will definitely bear in mind in my future attempts at coding ), however, it did not seem to solve the problem. Cheers
                              If the code compiled, and you refreshed the chart, but still do not have the expected output, then you likely have another problem. What, if any, errors are in your log?
                              Last edited by koganam; 04-06-2012, 03:00 PM. Reason: Corrected spelling

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by George21, Today, 10:07 AM
                              0 responses
                              1 view
                              0 likes
                              Last Post George21  
                              Started by Stanfillirenfro, Today, 07:23 AM
                              8 responses
                              23 views
                              0 likes
                              Last Post Stanfillirenfro  
                              Started by DayTradingDEMON, Today, 09:28 AM
                              2 responses
                              15 views
                              0 likes
                              Last Post DayTradingDEMON  
                              Started by navyguy06, Today, 09:28 AM
                              1 response
                              6 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by cmtjoancolmenero, Yesterday, 03:58 PM
                              8 responses
                              32 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Working...
                              X