I know I can use SetStopLoss on initialize but, I'm just trying to understand the OnMarketData object for further developing code...
Thanks in advance...
#region Variables
//Stop Loss
private double sT = 0.001;
#endregion
protected override void Initialize()
{
//Settings
ExitOnClose = false;
CalculateOnBarClose = true;
Add(PeriodType.Day, 1);
}
protected override void OnBarUpdate()
{
if (CurrentBars[0] > 50
&& CurrentBars[1] > 5);
if (BarsInProgress == 0)
{
if (Positions[0].MarketPosition == MarketPosition.Flat)
if (Close[0] >= Open[0])
{
EnterLong(100000, "Long");
}
}
}
protected override void OnMarketData(MarketDataEventArgs e)
{
if (BarsInProgress == 0)
{
//Exit Long-----------------------------------------------------------------------
if (Positions[0].MarketPosition == MarketPosition.Long)
if (e.MarketDataType == MarketDataType.Last)
{
//Stop Loss ---------------------------------------------------------------------
if (Position.GetProfitLoss(e.Price, PerformanceUnit.Percent) < -ST)
{
ExitLong("Stop Loss", "Long");
}
}
}
}
#region Properties
[Description("Stop Loss")]
[GridCategory("Parameters")]
private double ST
{
get { return sT; }
set { sT = Math.Max(0, value); }
}
#endregion

Comment