I want to check every 5 bars from Last Entry and if position is above EMA20, if position is in profit, and EMA200 is 6.5% > EMA20, I add another Long position. I'm using
BarsSinceEntry("LastSignalName") > 5 but it is not working.
From the screenshot, I have position Long3 &Long4 entering before Long2 and only 1 bar after Long1.
Here is my code:
protected override void OnBarUpdate()
{
// Condition set 1
if ((BarsSinceExit() > 2 || BarsSinceExit() == -1)
&& Close[BarsAgo0] >= EMA(EMA200)[BarsAgo0]
&& Close[BarsAgo0] >= EMA(EMA20)[BarsAgo0]
&& Variable1 == 0
&& (EMA(EMA200)[BarsAgo0]) * (1 + 6.5) > EMA(EMA20)[BarsAgo0])
{
EnterLong(Quantity1, "Long1");
SetStopLoss("Long1", CalculationMode.Percent, StopPerc, false);
Variable1 = 1;
}
// Condition set 2
if (Position.GetProfitLoss(Close[0], PerformanceUnit.Currency) > 0
&& BarsSinceEntry("Long1") > 5
&& Close[BarsAgo0] > EMA(EMA20)[BarsAgo0]
&& (EMA(EMA200)[BarsAgo0]) * (1 + 6.5) > EMA(EMA20)[BarsAgo0]
&& Variable2 == 0)
{
EnterLong(Quantity2, "Long2");
SetStopLoss("Long2", CalculationMode.Percent, StopPerc, false);
Variable2 = 1;
}
// Condition set 3
if (Position.GetProfitLoss(Close[0], PerformanceUnit.Currency) > 0
&& BarsSinceEntry("Long2") > 5
&& Close[BarsAgo0] > EMA(EMA20)[BarsAgo0]
&& (EMA(EMA200)[BarsAgo0]) * (1 + 6.5) > EMA(EMA20)[BarsAgo0]
&& Variable3 == 0)
{
EnterLong(Quantity2, "Long3");
SetStopLoss("Long3", CalculationMode.Percent, StopPerc, false);
Variable3 = 1;
}
// Condition set 4
if (Position.GetProfitLoss(Close[0], PerformanceUnit.Currency) > 0
&& BarsSinceEntry("Long3") > 5
&& Close[BarsAgo0] > EMA(EMA20)[BarsAgo0]
&& (EMA(EMA200)[BarsAgo0]) * (1 + 6.5) > EMA(EMA20)[BarsAgo0]
&& Variable4 == 0)
{
EnterLong(Quantity2, "Long4");
SetStopLoss("Long5", CalculationMode.Percent, StopPerc, false);
Variable4 = 1;
}
Comment