using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Data;
using NinjaTrader.Gui.Chart;
#endregion
// This namespace holds all indicators and is required. Do not change it.
namespace NinjaTrader.Indicator
{
/// <summary>
/// Sends an email when a specified price is reached.
/// </summary>
[Description("Sends an email when a specified price is reached.")]
public class EmailPriceAlert : Indicator
{
#region Variables
// Wizard generated variables
private double priceLevel = 1; // Default setting for PriceLevel
private string toEmail = "[email protected]"; // Default setting for ToEmail
private string fromEmail = "[email protected]"; // Default setting for FromEmail
// User defined variables (add any user defined variables below)
#endregion
/// <summary>
/// This method is used to configure the indicator and is called once before any bar data is loaded.
/// </summary>
protected override void Initialize()
{
Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.HLine, "PriceAlert"));
Overlay = false;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
if(CurrentBar < 1)
return;
PriceAlert.Set(Close[0]);
if(Close[0] == Close[0])
{
SendMail(fromEmail, toEmail, "", " " + Close[0]);
}
}
#region Properties
[Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
[XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
public DataSeries PriceAlert
{
get { return Values[0]; }
}
[Description("")]
[GridCategory("Parameters")]
public double PriceLevel
{
get { return priceLevel; }
set { priceLevel = Math.Max(1, value); }
}
[Description("")]
[GridCategory("Parameters")]
public string ToEmail
{
get { return toEmail; }
set { toEmail = value; }
}
[Description("")]
[GridCategory("Parameters")]
public string FromEmail
{
get { return fromEmail; }
set { fromEmail = value; }
}
#endregion
}
}
#region NinjaScript generated code. Neither change nor remove.
// This namespace holds all indicators and is required. Do not change it.
namespace NinjaTrader.Indicator
{
public partial class Indicator : IndicatorBase
{
private EmailPriceAlert[] cacheEmailPriceAlert = null;
private static EmailPriceAlert checkEmailPriceAlert = new EmailPriceAlert();
/// <summary>
/// Sends an email when a specified price is reached.
/// </summary>
/// <returns></returns>
public EmailPriceAlert EmailPriceAlert(string fromEmail, double priceLevel, string toEmail)
{
return EmailPriceAlert(Input, fromEmail, priceLevel, toEmail);
}
/// <summary>
/// Sends an email when a specified price is reached.
/// </summary>
/// <returns></returns>
public EmailPriceAlert EmailPriceAlert(Data.IDataSeries input, string fromEmail, double priceLevel, string toEmail)
{
if (cacheEmailPriceAlert != null)
for (int idx = 0; idx < cacheEmailPriceAlert.Length; idx++)
if (cacheEmailPriceAlert[idx].FromEmail == fromEmail && Math.Abs(cacheEmailPriceAlert[idx].PriceLevel - priceLevel) <= double.Epsilon && cacheEmailPriceAlert[idx].ToEmail == toEmail && cacheEmailPriceAlert[idx].EqualsInput(input))
return cacheEmailPriceAlert[idx];
lock (checkEmailPriceAlert)
{
checkEmailPriceAlert.FromEmail = fromEmail;
fromEmail = checkEmailPriceAlert.FromEmail;
checkEmailPriceAlert.PriceLevel = priceLevel;
priceLevel = checkEmailPriceAlert.PriceLevel;
checkEmailPriceAlert.ToEmail = toEmail;
toEmail = checkEmailPriceAlert.ToEmail;
if (cacheEmailPriceAlert != null)
for (int idx = 0; idx < cacheEmailPriceAlert.Length; idx++)
if (cacheEmailPriceAlert[idx].FromEmail == fromEmail && Math.Abs(cacheEmailPriceAlert[idx].PriceLevel - priceLevel) <= double.Epsilon && cacheEmailPriceAlert[idx].ToEmail == toEmail && cacheEmailPriceAlert[idx].EqualsInput(input))
return cacheEmailPriceAlert[idx];
EmailPriceAlert indicator = new EmailPriceAlert();
indicator.BarsRequired = BarsRequired;
indicator.CalculateOnBarClose = CalculateOnBarClose;
#if NT7
indicator.ForceMaximumBarsLookBack256 = ForceMaximumBarsLookBack256;
indicator.MaximumBarsLookBack = MaximumBarsLookBack;
#endif
indicator.Input = input;
indicator.FromEmail = fromEmail;
indicator.PriceLevel = priceLevel;
indicator.ToEmail = toEmail;
Indicators.Add(indicator);
indicator.SetUp();
EmailPriceAlert[] tmp = new EmailPriceAlert[cacheEmailPriceAlert == null ? 1 : cacheEmailPriceAlert.Length + 1];
if (cacheEmailPriceAlert != null)
cacheEmailPriceAlert.CopyTo(tmp, 0);
tmp[tmp.Length - 1] = indicator;
cacheEmailPriceAlert = tmp;
return indicator;
}
}
}
}
// This namespace holds all market analyzer column definitions and is required. Do not change it.
namespace NinjaTrader.MarketAnalyzer
{
public partial class Column : ColumnBase
{
/// <summary>
/// Sends an email when a specified price is reached.
/// </summary>
/// <returns></returns>
[Gui.Design.WizardCondition("Indicator")]
public Indicator.EmailPriceAlert EmailPriceAlert(string fromEmail, double priceLevel, string toEmail)
{
return _indicator.EmailPriceAlert(Input, fromEmail, priceLevel, toEmail);
}
/// <summary>
/// Sends an email when a specified price is reached.
/// </summary>
/// <returns></returns>
public Indicator.EmailPriceAlert EmailPriceAlert(Data.IDataSeries input, string fromEmail, double priceLevel, string toEmail)
{
return _indicator.EmailPriceAlert(input, fromEmail, priceLevel, toEmail);
}
}
}
// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
public partial class Strategy : StrategyBase
{
/// <summary>
/// Sends an email when a specified price is reached.
/// </summary>
/// <returns></returns>
[Gui.Design.WizardCondition("Indicator")]
public Indicator.EmailPriceAlert EmailPriceAlert(string fromEmail, double priceLevel, string toEmail)
{
return _indicator.EmailPriceAlert(Input, fromEmail, priceLevel, toEmail);
}
/// <summary>
/// Sends an email when a specified price is reached.
/// </summary>
/// <returns></returns>
public Indicator.EmailPriceAlert EmailPriceAlert(Data.IDataSeries input, string fromEmail, double priceLevel, string toEmail)
{
if (InInitialize && input == null)
throw new ArgumentException("You only can access an indicator with the default input/bar series from within the 'Initialize()' method");
return _indicator.EmailPriceAlert(input, fromEmail, priceLevel, toEmail);
}
}
}
#endregion
Hi Support,
the above is the code for the EmailPriceAlert indicator,i`ve only change the Price Value logic.What i`d like to ask is to add the logic that will Alert me in the message if the price actually closed above or below relative to the prior bar and hence,if the current price closes above the prior bar,i`d like "Sell at X" text int the alert message.
anyone willing to help?
Thanks a lot in advance!
Comment