NexusFi: Find Your Edge


Home Menu

 





Ninja script question - Text Placement on Chart


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one markbb10 with 9 posts (1 thanks)
    2. looks_two sg72 with 4 posts (4 thanks)
    3. looks_3 Stevea94 with 2 posts (1 thanks)
    4. looks_4 Epistemophilic with 1 posts (1 thanks)
      Best Posters
    1. looks_one sg72 with 1 thanks per post
    2. looks_two utilizator with 1 thanks per post
    3. looks_3 NT2005 with 1 thanks per post
    4. looks_4 markbb10 with 0.1 thanks per post
    1. trending_up 6,699 views
    2. thumb_up 9 thanks given
    3. group 8 followers
    1. forum 19 posts
    2. attach_file 2 attachments




 
Search this Thread

Ninja script question - Text Placement on Chart

  #11 (permalink)
 sg72 
Orange County, CA, USA
 
Experience: Intermediate
Platform: NinjaTrader, Fidelity ATP
Trading: ES/NQ/RTY, Equity ETFs, Options
Posts: 59 since Sep 2018
Thanks Given: 44
Thanks Received: 48

I think your issue may be that you have the same draw tags overwriting each other. Make each draw tag unique. If you need to place text on multiple bars and you're generating no more than one text object per bar, then one way to make sure your tags are unique is to use CurrentBar in constructing your tag.

Ex:

Draw.Text(this,"DT_"+CurrentBar,false, … the rest of your code

If you need more than one text object per bar, then use "DT1_"+CurrentBar, "DT2_"+CurrentBar, so on and so forth.

If you keep using the same draw tag with successive calls to Draw.Text() on different bars, each time the function is executed, it will operate on the same text object instead of generating a new text object for the new bar. Which means the older text will disappear and get replaced with the new text at the new location. If I'm not mistaken, this is what you're seeing.

Hope this is clear.

Good luck.

Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Quantum physics & Trading dynamics
The Elite Circle
About a successful futures trader who didnt know anythin …
Psychology and Money Management
What broker to use for trading palladium futures
Commodities
Pivot Indicator like the old SwingTemp by Big Mike
NinjaTrader
REcommedations for programming help
Sierra Chart
 
  #12 (permalink)
 markbb10 
Treasure Coast, Florida
 
Experience: Advanced
Platform: Ninja 8
Trading: Futures
Posts: 135 since Aug 2012
Thanks Given: 57
Thanks Received: 50

@sg72 Thank for the info. I have experimented with tags and that does not appear to make
a difference. When I use the two instances of the Draw.Text not only does the desired text not
appear but all output from the indicator ceases as well.

Thank for the suggestions.

Mark

Started this thread Reply With Quote
  #13 (permalink)
 Stevea94 
Newbury, UK
 
Experience: Intermediate
Platform: NinjaTrader
Broker: NinjaTrader, Kinetick
Trading: NQ, CL, 6E
Posts: 78 since Oct 2015
Thanks Given: 1
Thanks Received: 150



markbb10 View Post
I want to thank everyone for their ideas and guidance. I am slowly learning more but I would be lying if I did not say it is driving me crazy.

Here is my latest issue. I have a short code snippet that will print the status of a indicator depending on the value of the variable called trend at a defined location on the chart.

In the format below I get printing of the status correctly in the defined locations. Please note that the first if statement is a Draw.TextFixed call and the second is Draw.Text.

if (trend == 1)
Draw.TextFixed(this,"status","Weis Wave is Green", TextPosition.TopRight,Brushes.White, ChartControl.Properties.LabelFont,Brushes.Black,Brushes.Green,100);

if (trend == -1)
Draw.Text(this,"drawtag1",false,"Weis Wave - Red",18,Close[0],-80,Brushes.Red,myFont,TextAlignment.Center,Brushes.Red,Brushes.LightGray,80);

If I change the first If statement to a Draw.Text, using proper syntax the indicator will compile without error but produces NOTHING on the screen. I have never seen anything more frustrating. Below is the code sequence that will compile but prints no data on the chart.

if (trend == 1)
Draw.Text(this,"drawtag1",false,"Weis Wave - Green",18,Close[0],-70, Brushes.Green,myFont, TextAlignment.Center,Brushes.Green,Brushes.LightGray,80);

if (trend == -1)
Draw.Text(this,"drawtag1",false,"Weis Wave - Red",18, Close[0],-80, Brushes.Red, myFont, TextAlignment.Center,Brushes.Red,Brushes.LightGray,80);

This prints nothing

You might want to check you've initialised 'myFont' correctly as this doesn't seem to be used in your TextFixed call.

This is a code snippet I use (as I don't use borders or background):

 
Code
		
else if (State == State.Configure)
{
	if (TextFont == null) TextFont = new SimpleFont("Arial", InfoTextSize);
}


private void DrawText(string Tag, string Text, bool IsAutoScale, int BarOffset, double TickOffset, int PixelOffset, int FontSize, Brush Color)
{
	TextFont.Size = FontSize;
	Draw.Text(this, Tag, IsAutoScale, Text, BarOffset, TickOffset, PixelOffset, Color, TextFont, TextAlignment.Left, Brushes.Transparent, Brushes.Transparent, 0);  
}
I use this in various ways and occasionally with a -15 BarOffset which prints to the right of the price bars.

Steve

Reply With Quote
  #14 (permalink)
 markbb10 
Treasure Coast, Florida
 
Experience: Advanced
Platform: Ninja 8
Trading: Futures
Posts: 135 since Aug 2012
Thanks Given: 57
Thanks Received: 50

Thanks Steve, myFont is initialized properly as a SimpleFont() and works correctly.

Started this thread Reply With Quote
  #15 (permalink)
 sg72 
Orange County, CA, USA
 
Experience: Intermediate
Platform: NinjaTrader, Fidelity ATP
Trading: ES/NQ/RTY, Equity ETFs, Options
Posts: 59 since Sep 2018
Thanks Given: 44
Thanks Received: 48

It just occurred to me that you're trying to place your text several bars back. When you loop through the logic inside OnBarUpdate(), that goes through all the bars on the chart starting with the first bar that prints. If for example, OnBarUpdate() happens to be on bar #5 from the beginning and you're trying to place a text 15 bars ago, you're going to get an indexing error. If you haven't don't so already, please open a NinjaScript Output window and watch for errors/warnings there. That might give you a clue as to what's happening.

If this is the issue you're having, you can get around it by having a statement like below so that you'll skip over the logic for the first few bars until you have enough bars that if you attempt to go back a few, it won't give you the indexing error.

protected override void OnBarUpdate()
{
if(CurrentBar < 10 ) { return ; } // Skip the first few bars to let things settle down.
The rest of your logic here
}

You'll have to pick a number that works for you.

Hope this helps. I'm running out of ideas .

Reply With Quote
Thanked by:
  #16 (permalink)
 markbb10 
Treasure Coast, Florida
 
Experience: Advanced
Platform: Ninja 8
Trading: Futures
Posts: 135 since Aug 2012
Thanks Given: 57
Thanks Received: 50

Hi @sg72

Thanks for that, I actually have that test in my code, I had not included it here for clarity.
Thanks for the input but that does not seem to be the issue.

Mark

Started this thread Reply With Quote
  #17 (permalink)
 sg72 
Orange County, CA, USA
 
Experience: Intermediate
Platform: NinjaTrader, Fidelity ATP
Trading: ES/NQ/RTY, Equity ETFs, Options
Posts: 59 since Sep 2018
Thanks Given: 44
Thanks Received: 48

If you'd like, you can copy the relevant parts of your code into a shorter script and test to make sure it has the same issue and then attach it here and I'll look at it and see if I can figure out what the issue is. Without seeing the code, I'm running out of ideas as to what it could be.

Reply With Quote
Thanked by:
  #18 (permalink)
 markbb10 
Treasure Coast, Florida
 
Experience: Advanced
Platform: Ninja 8
Trading: Futures
Posts: 135 since Aug 2012
Thanks Given: 57
Thanks Received: 50

@sg92 you were right. It is due to number of bars being tested. I was not looking at the logic correctly due to my incompetence.

Thanks so much for all the help as I attempt to learn this.

Always much appreciated.

Mark

Started this thread Reply With Quote
Thanked by:
  #19 (permalink)
IanC55
Aberdeen Scotland
 
Posts: 5 since Feb 2019
Thanks Given: 0
Thanks Received: 0


sg72 View Post
I think your issue may be that you have the same draw tags overwriting each other. Make each draw tag unique. If you need to place text on multiple bars and you're generating no more than one text object per bar, then one way to make sure your tags are unique is to use CurrentBar in constructing your tag.

Ex:

Draw.Text(this,"DT_"+CurrentBar,false, … the rest of your code

If you need more than one text object per bar, then use "DT1_"+CurrentBar, "DT2_"+CurrentBar, so on and so forth.

If you keep using the same draw tag with successive calls to Draw.Text() on different bars, each time the function is executed, it will operate on the same text object instead of generating a new text object for the new bar. Which means the older text will disappear and get replaced with the new text at the new location. If I'm not mistaken, this is what you're seeing.

Hope this is clear.

Good luck.

I agree it is your tag. They need to be unique. However using CurrentBar will give you more than two bits of text. Not sure if that is your intention. If not just use two unique Tags.

Reply With Quote
  #20 (permalink)
 jalley 
La Grange, CA USA
 
Experience: Intermediate
Platform: Ninjatrader, MetaTrader
Broker: NinjaTrader Brokerage
Trading: MES, MNQ, M2K, MYM
Posts: 25 since Jun 2020
Thanks Given: 14
Thanks Received: 7


Here is a solution: (I see it reformats my code wrong when posting as I indent my code between brackets)

protected void DrawTextFixed(TextPosition textPosition, int row, string tag, string txt, Brush myBrush, int opacity = 85)
{
string output = "";

for (int i = 0; i < row; i++) output += "\r";
output += txt;
Draw.TextFixed(this, tag, output, textPosition, myBrush, myFont, Brushes.Transparent, Brushes.Transparent,
opacity);
}

protected void RenderTextInfo()
{
// This next line draws the box and I use I put \r for each line
Draw.TextFixed(this, "Box","l l\r\r\r\r\r\r\r\r", TextPosition.TopLeft,
Brushes.DarkOliveGreen, myFont, Brushes.DarkOliveGreen, Brushes.Black, 85);
DrawTextFixed(TextPosition.TopLeft, 1, "ATR", String.Format("ATR: {0:0.00}", myATR[0]),
Brushes.Yellow);
// And so on for other lines
}

Visit my NexusFi Trade Journal Reply With Quote




Last Updated on February 25, 2021


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts