Draw.Text(this, "NinjaScriptInfo", timeLeft, 0, Bars.GetClose(Bars.Count - 1));
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!
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
NinjaTrader
Is this correct for drawing a bar timer at current price line?
Collapse
X
-
-
Hello funk10101,
Thanks for your post and welcome to the NinjaTrader Forums!
To clarify, are you trying to modify a copy of the BarTimer indicator that comes with NinjaTrader?
Draw.Text() would be the correct method to use to draw text on the chart.
To mimic the type of syntax the BarTimer Draw.TextFixed() method uses, you could use the Draw.Text() syntax below.
Draw.Text(NinjaScriptBase owner, string tag, bool isAutoScale, string text, int barsAgo, double y, int yPixelOffset, Brush textBrush, SimpleFont font, TextAlignment alignment, Brush outlineBrush, Brush areaBrush, int areaOpacity)
See this help guide page for more information about Draw.Text(): https://ninjatrader.com/support/help.../draw_text.htm
I would also recommend adding prints to your script to see exactly how the code is behaving, such as printing out Bars.GetClose(Bars.Count-1) to see what price you are passing into the Draw.Text() method. Prints will appear in a New > NinjaScript Output window.
And, keep an eye on the Log tab of the Control Center for any error messages that may appear.
Below is a link to a forum post that demonstrates how to use prints to understand behavior.
Brandon H.NinjaTrader Customer Service
-
Thanks for your response. I've made the changes and printed out the results. They are correct, however for some reason, Draw.Text() is just not displaying. Draw.TextField() with the "similar" variables works as intended.
Code:Draw.Text(this, "NinjaScriptInfo", false, timeLeft, 0, Bars.GetClose(Bars.Count-1), 0, Brushes.White, MyFont, System.Windows.TextAlignment.Right, Brushes.Transparent, thisBrush, 100); Print(timeLeft);
Comment
-
Hello funk10101,
Thanks for your notes.
You will need to use the Draw.Text() syntax that allows you to pass in a DateTime value when calling this method instead of a barsAgo value.
Draw.Text(NinjaScriptBase owner, string tag, bool isAutoScale, string text, DateTime time, double y, int yPixelOffset, Brush textBrush, SimpleFont font, TextAlignment alignment, Brush outlineBrush, Brush areaBrush, int areaOpacity)
You could pass in Bars.GetTime(CurrentBar) in for the Datetime time argument when calling this method in the script.
For example:
Draw.Text(this, "NinjaScriptInfo1", true, timeLeft, Bars.GetTime(CurrentBar), Bars.GetClose(Bars.Count-1), 0, Brushes.Blue, myFont, System.Windows.TextAlignment.Left, Brushes.Transparent, Brushes.Transparent, 0);
When testing this on my end I see the timer being drawn in the current bar at the close price of the bar.
Please let us know if we may assist further.Brandon H.NinjaTrader Customer Service
Comment
-
Hello funk10101,
Thanks for your notes.
I see that the bar timer in the modified script is updating at the same time as the NinjaTrader Bar Timer indicator which is expected.
The Draw.Text() method is being called based on the timer and will draw at the Close price calculated when the timer is triggered.
You could consider implementing custom logic in the script to modify how often the timer is called if you want it to update differently.Brandon H.NinjaTrader Customer Service
Comment
-
I've decided to modify "Priceline". I've added this where RenderTarget.DrawLine() is:
Code:RenderTarget.DrawText(timeLeft, new SharpDX.DirectWrite.TextFormat(NinjaTrader.Core.Globals.DirectWriteFactory, "Arial", 12f), new RectangleF(startX, y, 150.00f, 100.00f), fgBrush.ToDxBrush(RenderTarget));
Code:Indicator 'MyPriceLine': Error on calling 'OnRender' method on bar 17231: Object reference not set to an instance of an object.
Comment
-
Hello funk10101,
Thanks for your notes.
This error message "Object reference not set to an instance of an object." is advising that something you are accessing has not been created at the time you are accessing it.
Before calling RenderTarget.DrawText() I suggest creating the values you are trying to pass into the method prior to using them in the RenderTarget.DrawText() method.
For example, create a TextFormat prior to passing the value into the RenderTarget.DrawText() method.
The code below from the Using SharpDX for Custom Chart Rendering help guide demonstrates this.
Code:// define the point for the text to render SharpDX.Vector2 startPoint = new SharpDX.Vector2(ChartPanel.X, ChartPanel.Y); // construct the text format with desired font family and size SharpDX.DirectWrite.TextFormat textFormat = new SharpDX.DirectWrite.TextFormat(Core.Globals.DirectWriteFactory, "Arial", 36); // construct the rectangleF struct to describe the position and size the text SharpDX.RectangleF rectangleF = new SharpDX.RectangleF(startPoint.X, startPoint.Y, ChartPanel.W, ChartPanel.H); // define the brush used for the text SharpDX.Direct2D1.SolidColorBrush customDXBrush = new SharpDX.Direct2D1.SolidColorBrush(RenderTarget, SharpDX.Color.DodgerBlue); // execute the render target text command with desired values RenderTarget.DrawText("I am some text", textFormat, rectangleF, customDXBrush); // always dispose of textFormat when finished textFormat.Dispose(); // always dipose of brush when finished customDXBrush.Dispose();
RenderTarget.DrawText(): https://ninjatrader.com/support/help...t_drawtext.htm
I suggest studying the Using SharpDX for Custom Chart Rendering help guide documentation below for detailed information and sample code.
Using SharpDX for Custom Chart Rendering: https://ninjatrader.com/support/help..._rendering.htmBrandon H.NinjaTrader Customer Service
Comment
-
Ok, got it! I have one last question(I believe), I'm not sure where to put the "Dispose()" calls. In my "OnRender()" I have this code which they get called immediately after displaying the text. Is this correct?:
Code:RenderTarget.DrawLine(new SharpDX.Vector2(startX, y), new SharpDX.Vector2(endX, y), LastStroke.BrushDX, LastStroke.Width, LastStroke.StrokeStyle); RenderTarget.DrawText(timeLeft, textFormat, rectangleF, customDXBrush); // always dispose of textFormat when finished textFormat.Dispose(); // always dipose of brush when finished customDXBrush.Dispose();
Comment
-
Hello funk10101,
Thanks for your notes.
You would dispose of the device dependent resources at the end of the OnRender() section of code so they are disposed on each render pass.
An example of this could be seen in the SampleCustomRender reference sample that comes with NinjaTrader.
To view the SampleCustomRender reference sample code, open a New > NinjaScript Editor window, open the Indicators folder, and double-click on the SampleCustomRender file.
Please let us know if we may assist further.Brandon H.NinjaTrader Customer Service
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by cre8able, Yesterday, 01:16 PM
|
3 responses
11 views
0 likes
|
Last Post
by cre8able
Today, 08:28 AM
|
||
Started by ChartTourist, Today, 08:22 AM
|
0 responses
5 views
0 likes
|
Last Post
by ChartTourist
Today, 08:22 AM
|
||
Started by LiamTwine, Today, 08:10 AM
|
0 responses
2 views
0 likes
|
Last Post
by LiamTwine
Today, 08:10 AM
|
||
Started by Balage0922, Today, 07:38 AM
|
0 responses
5 views
0 likes
|
Last Post
by Balage0922
Today, 07:38 AM
|
||
Started by JoMoon2024, Today, 06:56 AM
|
0 responses
6 views
0 likes
|
Last Post
by JoMoon2024
Today, 06:56 AM
|
Comment