Consultation on right way to code a certain indicator

Hi
I am a new amibroker user
I try these days to build an indicator , its purpose is to connect
in chronological order between highest high & lowest low forming during each month ,
creating a trendline extend for a 10 day period

However i face a small problem which is each line connecting High and Low ending with an a ray line that bear the last value for my indicator
I want to clear that ray , i tried to use REF() Function but no thing have changed

The second issue relating to continuousness and repeating
I write the code and i want it to be repeated for next months

How can i do this ?

Thanks in advance

Y=Param("Year",2016,1900,2100);
M=Param("Month",1,1,12);


AR=Year()==Y AND Month()==M;
P1=LastValue(Highest(ValueWhen(AR,H,1)));
P2=LastValue(Lowest(ValueWhen(AR,L,1)));
T1=Cum(IIf(Cum(AR AND H==P1)>=1,1,IIf(Cum(AR AND H==P1)<1,0,1)));
T2=Cum(IIf(Cum(AR AND L==P2)>=1,1,IIf(Cum(AR AND L==P2)<1,0,1)));
TD=LastValue(T1-T2);
XP=IIf(IIf(TD>0,1,2)==1,P1,P2);
YP=IIf(IIf(TD>0,1,2)==2,P1,P2);
XT=IIf(XP==P1,T1,T2);
YT=IIf(XP==P2,T1,T2);
MW=XP+IIf(XP>YP,(-1*((abs(LastValue((XP-YP)/10))*YT))),(abs(LastValue((XP-YP)/10))*YT));
MWT=Ref(Ref(Ref(MW,LastValue(-1*(YT))+10),LastValue(Cum(1)-11)),LastValue(-1*(Cum(1)-(Cum(AR)+BarsSince(AR)))));

MWT;

Plot(MWT,"Neowave",colorRed);

A=Year()==IIf(M==12,Y+1,Y) AND Month()==IIf(M==12,1,M+1);
P1=LastValue(Highest(ValueWhen(A,H,1)));
P2=LastValue(Lowest(ValueWhen(A,L,1)));
T1=Cum(IIf(Cum(A AND H==P1)>=1,1,IIf(Cum(A AND H==P1)<1,0,1)));
T2=Cum(IIf(Cum(A AND L==P2)>=1,1,IIf(Cum(A AND L==P2)<1,0,1)));
TD=LastValue(T1-T2);
XP1=IIf(IIf(TD>0,1,2)==1,P1,P2);
YP1=IIf(IIf(TD>0,1,2)==2,P1,P2);
XT1=IIf(XP1==P1,T1,T2);
YT1=IIf(XP1==P2,T1,T2);
MW=YP+IIf(XP1<YP,(-1*((abs(LastValue((XP1-YP)/10))*XT1))),(abs(LastValue((XP1-YP)/10))*XT1));
MWT=Ref(Ref(Ref(MW,LastValue(-1*(XT1))+10),LastValue(Cum(1)-11)),LastValue(-1*(Cum(1)-(Cum(AR)+BarsSince(AR))+10)));

MWT;

Plot(MWT,"Neowave",colorRed);

A=Year()==IIf(M==12,Y+1,Y) AND Month()==IIf(M==12,1,M+1);
P1=LastValue(Highest(ValueWhen(A,H,1)));
P2=LastValue(Lowest(ValueWhen(A,L,1)));
T1=Cum(IIf(Cum(A AND H==P1)>=1,1,IIf(Cum(A AND H==P1)<1,0,1)));
T2=Cum(IIf(Cum(A AND L==P2)>=1,1,IIf(Cum(A AND L==P2)<1,0,1)));
TD=LastValue(T1-T2);
XP=IIf(IIf(TD>0,1,2)==1,P1,P2);
YP=IIf(IIf(TD>0,1,2)==2,P1,P2);
XT=IIf(XP==P1,T1,T2);
YT=IIf(XP==P2,T1,T2);
MW=XP+IIf(XP>YP,(-1*((abs(LastValue((XP-YP)/10))*YT))),(abs(LastValue((XP-YP)/10))*YT));
MWT=Ref(Ref(Ref(MW,LastValue(-1*(YT))+10),LastValue(Cum(1)-11)),LastValue(-1*(Cum(1)-(Cum(AR)+BarsSince(AR))+20)));

MWT;

Plot(MWT,"Neowave",colorRed);


That's My Code and here is a screen shoot for the result 1q

Try drawing (manually) the lines on the chart the way you want them to appear (desired result). It would be easier for person trying to help you with coding if he/she sees the example what you are after.

Thank you very much Tomaz for your response

The idea is very simple and easy to grasp , here's an example for Microsoft Chart (MSFT)
From January,2012 to April,2012

The Purpose Of Indicator represent in the next few points ::

1-
First .. Indicator should determine the Highest High & Lowest Low during one month
Let's say from 1 January to 31 January

2-
Second .. It should determine which is earlier ,
If Highest High came first it plot it on chart and then Lowest Low come as a seconed
If Lowest Low came first it plot it on chart and then Highest High come as a seconed

Third.. it Connect between them with a separation period equal to ten time unit (Ten Days),
Since this consider the first trendline to be drawn indicator will plot its first point in the first day of the month
Here in our example it would be 1/3/2012

Fourth..Indicator repeat the First & Second step for the next month , so it should determine again
the highest high and the lowest low occured during febraury then plot them in order of their occurence
However the beginning point will not be plot on first day of febraury as we did in previous time
here the beginning point for febraury will be 20 day far from the beginnig point of first trendline
( The beginning point was 1/3/2012 ) .. that make a separation period equal to ten day between them
This trend also will extend forward for a ten day

Between these trendline there will be a secondary trendline that connect both of them

Now the process should be repeated with the next months till end of chart
Find highest high and lowest low , plot them in order ,
First day of First month be beginnig point for first trendline and also would be consider as a
refrence point for the beginning point of next month

Note::All Trendline should Extend for a period equal to ten day
Monowave_1

Hoping that is clear enough , thank you very much :slight_smile:

This may not be the most elegant solution, but I think it does what you want:

function BarsUntil(array)
{
	bu = Reverse(BarsSince(Reverse(array)));
	return bu;
}

bi = BarIndex();
isStartOfMonth = Month() != Ref(Month(), -1);
barsSinceStartOfMonth = BarsSince(isStartOfMonth);
barsUntilNextMonth = BarsUntil(isStartOfMonth);

monthlyHigh = TimeFrameGetPrice("H", inMonthly);
barsUntilMH = BarsUntil (H == monthlyHigh);

monthlyLow = TimeFrameGetPrice("L", inMonthly);
barsUntilML = BarsUntil (L == monthlyLow);

firstValueIsLow = ValueWhen(isStartOfMonth, barsUntilML < barsUntilMH);
firstValueCurrMonth = IIf(firstValueIsLow, monthlyLow, monthlyHigh);
secondValueCurrMonth = IIf(firstValueIsLow, monthlyHigh, monthlyLow);
firstValueNextMonth = IIf(Ref(firstValueIsLow, barsUntilNextMonth), Ref(monthlyLow, barsUntilNextMonth), Ref(monthlyHigh, barsUntilNextMonth));
slope1 = (secondValueCurrMonth - firstValueCurrMonth) / 10;
slope2 = (firstValueNextMonth - secondValueCurrMonth) / ValueWhen(barsSinceStartOfMonth == 10, BarsUntilNextMonth);

MWT = IIf(barsSinceStartOfMonth <= 10, firstValueCurrMonth + barsSinceStartOfMonth * slope1, firstValueNextMonth - barsUntilNextMonth * slope2);
													  
Plot(MWT, "Neowave", colorRed, styleLine);
4 Likes

Thank you very much mradtke , I really appreciate your help

However the code does not work , i have an earlier edition of Amibroker (5.6) which does not support
[ Reverse function ] and i don't know what it do exactly.

I began to learn programming from just a couple of days and i have no idea about this function , i read a brief explanation about it on AFL Function List page
but i am not sure that i comprehend it completely

I think the most important part of your formula that is required to be studied by me is function & return
I make a search to get some help and i find this example

function RiseToAPower( x )

{ 
X==2                <<--  I added this part
return x ^ 2;
}


Plot(RiseToAPowerOf2,"MO",colorRed,styleLine); <<-- I added this part

but every time i paste it in formula editor it give me an error

So i ask you kindly to put a simple application for [ Function and Return ] and i will do the rest of job

Best Regards ,
Sasha

AmiBroker v5.6 is very old, and I believe no longer supported. AB is a powerful tool and very reasonably priced, so my suggestion is to pay for the most recent version and start from there.

In the example you provided, your function is named RiseToAPower(). But in your plot, you incorrectly referenced RiseToAPowerOf2, and did not provide any arguments. It should be something like RiseToAPower(Close) if you want the square of the closing price.

2 Likes

@Sasha_99, I wholeheartedly agree with @mradtke that, as usual, was very quick and smart to provide you a working answer to your (not so simple...) problem.

But, independently of the old AmiBroker version you are using, I suspect that this code is probably too advanced for your current programming knowledge.

If you want to properly learn how to program AFL start here.

Then come back to this thread and, as an exercise, try to code an alternative function to the missing one: Reverse(array) that returns a new array with the order of the elements in specified range reversed and post your coding progress.

P.S. @mradtke I'm learning so much from your posts! Thanks a lot.

4 Likes

Thanks mradtke for your sincere advice , I can't hide my impression with the AB and its powerful capabilities
that i need urgently to boost my trading performance ... so it deserve the money spend on it

For writing code i think i need more time and more search for practical examples to understand this function in proper way and the forum here provide many examples for beginner
So Big thank to all admin and supervisor here

I agree with you .. cause before two week i did not know any thing about programming
during the last week i try hard to write the code and i succeeded in that partially but it look like
I need more time and practice to grow my skills and that was just a start :slight_smile: