Welcome to NexusFi: the best trading community on the planet, with over 150,000 members Sign Up Now for Free
Genuine reviews from real traders, not fake reviews from stealth vendors
Quality education from leading professional traders
We are a friendly, helpful, and positive community
We do not tolerate rude behavior, trolling, or vendors advertising in posts
We are here to help, just let us know what you need
You'll need to register in order to view the content of the threads and start contributing to our community. It's free for basic access, or support us by becoming an Elite Member -- see if you qualify for a discount below.
-- Big Mike, Site Administrator
(If you already have an account, login at the top of the page)
NT8 - Horizontal line label with sound alert function
I recently switched to NT8 and I've been trying to mess with the code of an indicator I found to label the value on the horizontal line drawing tool (F6). The indicator works perfect, however I'm trying to add a function when price crosses that horizontal line that it plays a sound file (.wav file from my NT8 folder).
I've attached the NT8 version as zip and the NT7 version as the .cs file which I was using and worked well.
Here is the NT8 code and any suggestion would be highly appreciated, thanks in advance:
#region Using declarations
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Gui;
using NinjaTrader.Gui.Chart;
using NinjaTrader.Gui.SuperDom;
using NinjaTrader.Data;
using NinjaTrader.NinjaScript;
using NinjaTrader.Core.FloatingPoint;
using NinjaTrader.NinjaScript.DrawingTools;
using SharpDX;
using SharpDX.DirectWrite;
using SharpDX.Direct2D1;
#endregion
//This namespace holds Indicators in this folder and is required. Do not change it.
namespace NinjaTrader.NinjaScript.Indicators
{
public class LabelHorizLinesCL : Indicator
{
public enum SideEnum { Left, Right }
public enum PositionEnum { Below, Above }
protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
{
foreach (Gui.NinjaScript.IChartObject co in ChartControl.ChartObjects)
{
if (co is HorizontalLine)
{
HorizontalLine l1 = (co as HorizontalLine);
string priceformat = Core.Globals.GetTickFormatString(TickSize);
float x, y;
System.Windows.Media.Color col = ((System.Windows.Media.SolidColorBrush)ChartControl.Properties.ChartBackground).Color;
using (SharpDX.Direct2D1.Brush backbr = new SharpDX.Direct2D1.SolidColorBrush(RenderTarget, new SharpDX.Color() { A = Opacity, R = col.R, G = col.G, B = col.B }))
using (SharpDX.Direct2D1.Brush br = l1.Stroke.Brush.ToDxBrush(RenderTarget))
using (var factory = new SharpDX.DirectWrite.Factory(SharpDX.DirectWrite.FactoryType.Shared))
using (var textFormat = new TextFormat(factory, "Arial", (Bold) ? SharpDX.DirectWrite.FontWeight.Bold : SharpDX.DirectWrite.FontWeight.Normal, SharpDX.DirectWrite.FontStyle.Normal, Size * 96 / 72))
{
string text = l1.Anchors.First().Price.ToString(priceformat);
using (var textLayout = new SharpDX.DirectWrite.TextLayout(factory, text, textFormat, float.MaxValue, float.MaxValue))
{
if (Side == SideEnum.Right)
x = ChartPanel.W - textLayout.Metrics.Width;
else
x = 0;
y = chartScale.GetYByValue(l1.Anchors.First().Price);
if (Position == PositionEnum.Above)
{
y -= textLayout.Metrics.Height + l1.Stroke.Width / 2.0f;
}
else
{
y += l1.Stroke.Width / 2.0f;
}
RenderTarget.FillRectangle(new RectangleF(x, y, textLayout.Metrics.Width, textLayout.Metrics.Height), backbr);
RenderTarget.DrawText(text, textFormat, new SharpDX.RectangleF(x, y, textLayout.Metrics.Width, textLayout.Metrics.Height), br);
}
{ // Here I want to add a code which will trigger the sound file when the horizontal line is touched, however I couldnt fint a solution. I know in NT7 you could use linetriggered but it just doesnt work when I use it in NT8..
// if (CurrentBars = l1)
// { Alert("Alert", Priority.High, SoundFile);}
}
}
}
}
}
#region Properties
[Display(Name = "Font Size", GroupName = "Settings", Order = 0)]
public int Size
{ get; set; }
[Display(Name = "Bold", GroupName = "Settings", Order = 1)]
public bool Bold
{ get; set; }
[Display(Name = "Side", GroupName = "Settings", Order = 2)]
public SideEnum Side
{ get; set; }
[Display(Name = "Position", GroupName = "Settings", Order = 3)]
public PositionEnum Position
{ get; set; }
[Display(Name = "BackGround Opacity(0-255)", GroupName = "Settings", Order = 4)]
public byte Opacity
{ get; set; }
Broker: NT Brokerage, Kinetick, IQFeed, Interactive Brokers
Trading: ES
Posts: 159 since Dec 2014
Thanks Given: 40
Thanks Received: 166
The documentation has an example of how to do this. Of note, indicators handle the call to PlaySound method a bit differently so watch out for that. Here is the link.
Essentially, you define your logic, such as with if statements, then if your criteria is met, put your call to PlaySound in that conditional logic block.
Remember, it won't work during a backtest or when you're loading data. (State.Historical)