Hope you guys are doing well,
I am trying to work on a counter to count crosses in the last specified period of bars, I have done counters before but none were counting crosses, just green or red bars, but because of the nature of cross above or below, as you have to specify the lookback period as an int, so I can not include my desired look back.
Pasting the code below :
if (CurrentBar <= LookBack)
return;
int manycrosses = 0;
int signalState = 0;
if(RuleSetNumber == 1)
for (var i = LookBack; i >= 0 ; i--)
{
if ((CrossAbove(SMA(Period1), SMA(Period2), i))
|| (CrossBelow(SMA(Period1), SMA(Period2), i)))
manycrosses++;
//(CountIf(() => (CrossAbove(SMA(Period1), SMA(Period2),i ) > 1)
}
if(RuleSetNumber == 2)
for (var i = LookBack; i >= 0 ; i--)
{
if ((CrossAbove(EMA(Period1), EMA(Period2), i))
|| (CrossBelow(EMA(Period1), EMA(Period2), i)))
manycrosses++;
}
if (manycrosses >= RequiredCount)
signalState = 1;
if( manycrosses < RequiredCount)
signalState = 0;
SignalState[0] = (double)signalState;
ManyCrosses[0] = (double)manycrosses;
}
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name="LookBack", Order=1, GroupName="Parameters")]
public int LookBack
{ get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name="RequiredCount", Order=2, GroupName="Parameters")]
public int RequiredCount
{ get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name="minimumBarSizeTick", Order=3, GroupName="Parameters")]
public int minimumBarSizeTick
{ get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name="Period1", Order=7, GroupName="Parameters")]
public int Period1
{ get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name="Period2", Order=8, GroupName="Parameters")]
public int Period2
{ get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name="RuleSetNumber", Order=4, GroupName="Parameters")]
public int RuleSetNumber
{ get; set; }
[Browsable(false)]
[XmlIgnore]
public Series<double> SignalState
{
get { return Values[0]; }
}
[Browsable(false)]
[XmlIgnore]
public Series<double> ManyCrosses
{
get { return Values[1]; }
}
}
}
My problem is that it is counting max counter every time a cross happens in the last (Lookback) period, while I want to count every cross that happened 1 bar ago for x number of bars which is the lookback defined as an input.
So again, I want to count how many crosses happen in (Lookback) and if the amount required happens I want the signal state to be 1, if not 0, while this code is signaling 1 every time a cross happens in the last 20 bars.
please help.
thanks,
Comment