using Bar count as timer
Created at 24 Aug 2021, 19:53
NI
using Bar count as timer
24 Aug 2021, 19:53
hello guys
please i am trying to use a certain number of bar closes after the previous moving average cross over to determine whether to enter a trade or not but i have trouble doing it .i am a newbie and still learning. below is my code. MA5C ==current 5 moving average value&& MA5P==previous 5 moving average value .... i would be much grateful for any help.
}
protected override void OnTick()
{
var longPosition = Positions.Find(label, SymbolName, TradeType.Buy);
var shortPosition = Positions.Find(label, SymbolName, TradeType.Sell);
var MA50C = MA50.Result.Last(0);
var MA5C = MA5.Result.Last(0);
var MA20C = MA20.Result.Last(0);
var MA5P = MA5.Result.Last(1);
var MA50P = MA50.Result.Last(1);
var MA20P = MA20.Result.Last(1);
if (MA20C > MA50C && MA20P < MA50P && longPosition == null && shortPosition == null &&"//there should be nBars after the last MAcross")
{
ExecuteMarketOrder(TradeType.Buy, SymbolName, VolumeInUnits, label);
}
Replies
ninsonphilip2
25 Aug 2021, 16:31
RE: thank you very much
amusleh said:
Hi,
You can use this method to get the number of bars since last cross between two moving averages:
private int GetNumberOfBarsFromLastCross(MovingAverage fastMa, MovingAverage slowMa) { var index = Bars.Count - 1; var barCount = 0; for (int iBarIndex = index; iBarIndex >= 1; iBarIndex--) { if ((fastMa.Result[iBarIndex] > slowMa.Result[iBarIndex] && fastMa.Result[iBarIndex - 1] <= slowMa.Result[iBarIndex - 1]) || (fastMa.Result[iBarIndex] < slowMa.Result[iBarIndex] && fastMa.Result[iBarIndex - 1] >= slowMa.Result[iBarIndex - 1])) { break; } barCount++; } return barCount; }
@ninsonphilip2
amusleh
25 Aug 2021, 15:57
Hi,
You can use this method to get the number of bars since last cross between two moving averages:
@amusleh