Swing counter does't work

Hi i'am new to the community, but already on reading this forum for 7 month's. I like amibroker but my ideas go further than my coding knowledge. So thanks for all the helpfull stuff.

I have a question.
I would like to display shapedigit1 through 9 in the chart on each bar if there is a positive trend, for wich the condition is close>ref(close,-4) is true. this on a weekly timeframe. So if this is true in week 1 then shapedigit1 is displayed on the bar and if this is true on the next week shapedigit2 is displayed on the next bar and so on until 9. After nine times the counter is back to zero and the counting can start again as long as the trend is positive.

I have already read en tried a lot. Iám getting close, but i don't get this working.
Who is willing to help me ?

Kind regards from the Netherlands,

//swingcounter code
for(i=0; i<barcount; i++);
while (s<10)

IIf(Close>Ref(Close,-4) AND trend==1,s=1,s=11);


The code above plots a shapedigit1 and shapedigit2 on every bar in positieve and negative trend. Negative trend = 0

1 Like

There may be more elegant ways to achieve this, but here's one possibility. I added my own Up Trend definition since you did not provide one.

isUpTrend = C > MA(C,50);
isUpClose = Close>Ref(Close,-4);

cntUpClose = SumSince(Cross(isUpTrend,0.5), isUpClose);
cntUpClose = IIf(isUpTrend, cntUpClose % 9 + 1, -1);

shape = IIf (cntUpClose == 1, shapeDigit1, 
		IIf (cntUpClose == 2, shapeDigit2,
		IIf (cntUpClose == 3, shapeDigit3,
		IIf (cntUpClose == 4, shapeDigit4,
		IIf (cntUpClose == 5, shapeDigit5,
		IIf (cntUpClose == 6, shapeDigit6,
		IIf (cntUpClose == 7, shapeDigit7,
		IIf (cntUpClose == 8, shapeDigit8,
		IIf (cntUpClose == 9, shapeDigit9, shapeNone)))))))));

PlotShapes(IIf(isUpClose, shape, shapeNone), colorWhite, 0, High, 24);

Note that you should generally try to avoid using loops whenever possible, as AmiBroker's built in array (vector) processing is much faster. And you should (almost) never perform array functions while inside a bar-by-bar loop, as this is very inefficient.


mradtke Thanks for your effort. Your code is way better than mine.

The only thing i have to solve now is that if the condition Close>Ref(Close,-4) is not true the counter is set to null. In your code it counts to two for an example, wait a few bars when condition is not true, and then start counting again from 3 and so on.


If condition is not true the swing counter has to go to zero again.

Iám still puzzeling it out.

Try just changing this:

cntUpClose = SumSince(Cross(isUpTrend,0.5), isUpClose);
cntUpClose = IIf(isUpTrend, cntUpClose % 9 + 1, -1);

to this:

cntUpClose = SumSince(!isUpClose, isUpClose);
cntUpClose = IIf(isUpTrend, cntUpClose % 9, -1);
1 Like

Thanks !

You are amazing.

Kind regards,