# Generate Buy Signal with Camarila Pivots

Hiii,
Trying to generate Buy Signal when Current Month ML3 of Camarilla is greater than last month MH4 Camarilla pivot.
Using this code but not getting any signal

``````PMH =  TimeFrameGetPrice("H", inMonthly, -1);
PML =  TimeFrameGetPrice("L", inMonthly, -1);
PMC =  TimeFrameGetPrice("C", inMonthly, -1);

MH5 = (PMH/PML) * PMC ;
MH4 = PMC + (PMH - PML) * 1.1 / 2.0;
MH3 = PMC + (PMH - PML) * 1.1 / 4.0;
ML3 = PMC - (PMH - PML) * 1.1 / 4.0;
ML4 = PMC - (PMH - PML) * 1.1 / 2.0;
ML5 = PMC - (MH5 - PMC);

``````

`ML3` being greater than `Ref(ML3,-1)` of posted code can only differ at start of each new month because ML3 is a "straight line" (if ML3 being drawn on chart) for entire month in shorter intervals. So Cross (which is impulse signal) would have to return `TRUE` at start of month too in order to make Buy returning `TRUE` (if current cam is greater than previous one).

So how likely is that one to occur? Not so much, isn't it.

So you have to set start of month flag and use ValueWhen to keep cam result of start of month available from start to end of month (in your code case).

``````PMH =  TimeFrameGetPrice("H", inMonthly, -1);
PML =  TimeFrameGetPrice("L", inMonthly, -1);
PMC =  TimeFrameGetPrice("C", inMonthly, -1);

MH5 = (PMH/PML) * PMC ;
MH4 = PMC + (PMH - PML) * 1.1 / 2.0;
MH3 = PMC + (PMH - PML) * 1.1 / 4.0;
ML3 = PMC - (PMH - PML) * 1.1 / 4.0;
ML4 = PMC - (PMH - PML) * 1.1 / 2.0;
ML5 = PMC - (MH5 - PMC);

mth = Month();
new_mth = mth != Ref(mth,-1);
Buy = ValueWhen(new_mth, ML3>ref (MH3,-1)) AND Cross(C,MH3);
``````
1 Like

Thanks @fxshrat ....

How to refer value of Last to Last month? I was using below code but not working.

``````mth = Month();
new_mth = mth != Ref(mth,-1);
new_mth1 = Month(1)!=(Ref(mth,-2);
Longtrade = (ValueWhen(new_mth,MH3<Ref(MH3,-1)) AND ValueWhen(new_mth1,Ref(MH3,-1)<Ref(MH3,-2)) AND Cross(C,MH3));
``````

@ranahimanshu57 your formula is not working the way you think so you need to learn how to debug your code.

In your case specifically perhaps you could learn how to properly use `ValueWhen`.
https://www.amibroker.com/guide/afl/valuewhen.html

``````// your code was using
ValueWhen(new_mth, ML3>ref (MH3,-1))
``````

I'm not sure I understand what you mean "Last to Last month". It appears to me that you want to know value the previous time your Expression was true.
ValueWhen(EXPRESSION, ARRAY, n = 2 )

No, multiple Valuewhen calls are not good way to get to sum of values / occurences.
Doing this

``````Buy = ValueWhen(new_mth,MH3<Ref(MH3,-1)) AND
ValueWhen(new_mth,MH3<Ref(MH3,-1), 2) AND
ValueWhen(new_mth,MH3<Ref(MH3,-1), 3) AND
ValueWhen( ....
``````

is just awful and inflexible way of handling repetitions.

Instead (in case of existing code of post #1) use `Sparse*` and `Sum`.

Or simply do complete rewrite and use `TimeFrameSet` and `Sum` but not `TimeFrameGetPrice` as you did in first post.

Isn't this much cleaner than multiple ValueWhen and new_mth and what not code mumbo jumbo?

``````period = 3;
TimeFrameSet(tmfrm = inMonthly);
MH5 = (H/L) * C;
perc_range = (H - L) * 1.1;
MH4 = C + perc_range / 2.0;
MH3 = C + perc_range / 4.0;
ML3 = C - perc_range / 4.0;
ML4 = C - perc_range / 2.0;
ML5 = C - (MH5 - C);
rc_cond = Sum(ROC(ML3,1)<0, period) == period;
TimeFrameRestore();

MH3 = TimeFrameExpand(MH3, tmfrm);
rc_cond = TimeFrameExpand(rc_cond, tmfrm);

``````

@fxshrat, @portfoliobuilder Thanks.... I can use both the codes...........

Can you please suggest some book/site for learning the AFL programming with examples?? I am novice in AFL programming.

1 Like

@ranahimanshu57 many resources listed in the posts of this thread,

You don't need books by chief advertisers. All you need is AB manual and this forum.
All those books guys try to make money from existing information (and even copy existing code 1:1).

i use Camarilla pivots also and I think you mean something different than is answered here.

first off all your question is not clear because you first talk about current month ML3 and last month MH4, but then in the buy line you use the current month ML3 and last month MH3. So I assume you mean current month ML3 and last month MH3. Then below the code how you calculate this.

This months levels MH3 level is called MH30 and last months MH3 level is called MH31

``````// current month's levels
PMH =  TimeFrameGetPrice("H", inMonthly, -1);
PML =  TimeFrameGetPrice("L", inMonthly, -1);
PMC =  TimeFrameGetPrice("C", inMonthly, -1);

MH50 = (PMH/PML) * PMC ;
MH40 = PMC + (PMH - PML) * 1.1 / 2.0;
MH30 = PMC + (PMH - PML) * 1.1 / 4.0;
ML30 = PMC - (PMH - PML) * 1.1 / 4.0;
ML40 = PMC - (PMH - PML) * 1.1 / 2.0;
ML50 = PMC - (MH50 - PMC);

// the levels 1 month before current month
PMH =  TimeFrameGetPrice("H", inMonthly, -2);
PML =  TimeFrameGetPrice("L", inMonthly, -2);
PMC =  TimeFrameGetPrice("C", inMonthly, -2);

MH51 = (PMH/PML) * PMC ;
MH41 = PMC + (PMH - PML) * 1.1 / 2.0;
MH31 = PMC + (PMH - PML) * 1.1 / 4.0;
ML31 = PMC - (PMH - PML) * 1.1 / 4.0;
ML41 = PMC - (PMH - PML) * 1.1 / 2.0;
ML51 = PMC - (MH51 - PMC);

GraphXSpace = 5;
SetChartOptions( 0, chartShowDates );
Plot( C, "\nPrice", IIf( C > O, ColorRGB( 0, 255, 0 ), IIf( C <= O, ColorRGB( 255, 0, 0 ), colorLightGrey ) ), 64, Null, Null, 0, 0 );

Plot( MH30, "MH30", ColorRGB( 0, 255, 0 ), styleThick, Null, Null, 0, -1 );
Plot( ML30, "ML30", ColorRGB( 255 - 0, 255 - 255, 255 - 0 ), styleThick, Null, Null, 0, -1 );
Plot( MH40, "MH40", ColorRGB( 0, 100, 0 ), styleThick, Null, Null, 0, -1 );
Plot( ML40, "ML40", ColorRGB( 255 - 0, 255 - 100, 255 - 0 ), styleThick, Null, Null, 0, -1 );

Plot( MH31, "MH31", ColorWhite, styleThick | styleDots, Null, Null, 0, -1 );

Buy = Cross( C, MH30 ) AND ML30 > MH31;

PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), colorDarkGreen, 0, L, -15 );

``````

No Ed, it is not different than what he was posting as code (with syntax errors).

This is what he posted

``````mth = Month();
new_mth = mth != Ref(mth,-1);
new_mth1 = Month(1)!=(Ref(mth,-2);
ValueWhen(new_mth1,Ref(MH3,-1)<Ref(MH3,-2)) AND
Cross(C,MH3));
``````

Now (different example for explanation).... this one

`cond1 = C<Ref(C,-1) AND Ref(C,-1)<Ref(C,-2);`

is getting the same end result as this one

`cond2 = Sum(ROC(C,1)<0, 2) == 2;`

First one is inflexible complicated way while second one can be applied to any period quickly.

Here is proof that both are the same (you may use explorer also) And your one is different one than what he posted later yesterday as it it just compares one month to previous one time (also you use ML3 > previous MH3 while in his code he was using MH3 only and smaller as).

But I guess you refer to first post of thread.

But also your long one can be simplified to just this

``````TimeFrameSet(tmfrm = inMonthly);
MH5 = (H/L) * C;
perc_range = (H - L) * 1.1;
MH4 = C + perc_range / 2.0;
MH3 = C + perc_range / 4.0;
ML3 = C - perc_range / 4.0;
ML4 = C - perc_range / 2.0;
ML5 = C - (MH5 - C);
rc_cond = ML3 > Ref(MH3,-1);
TimeFrameRestore();

MH3 = TimeFrameExpand(MH3, tmfrm);
rc_cond = TimeFrameExpand(rc_cond, tmfrm);

Plot( C, "Price", colorDefault, styleBar );
PlotShapes( Buy * shapeUpArrow,colorGreen, layer = 0, L );
``````

Or by using `expandFirst` (as the codes of this thread using TimeFrameGetPrice use expandFirst also, so just showing that you can do it differently also)

``````TimeFrameSet(tmfrm = inMonthly);
H = Ref(H,-1);
L = Ref(L,-1);
C = Ref(C,-1);
MH5 = (H/L) * C;
perc_range = (H - L) * 1.1;
MH4 = C + perc_range / 2.0;
MH3 = C + perc_range / 4.0;
ML3 = C - perc_range / 4.0;
ML4 = C - perc_range / 2.0;
ML5 = C - (MH5 - C);
rc_cond = ML3 > Ref(MH3, -1);
TimeFrameRestore();

MH3 = TimeFrameExpand(MH3, tmfrm, expandFirst);
rc_cond = TimeFrameExpand(rc_cond, tmfrm, expandFirst);

Plot( C, "Price", colorDefault, styleBar );
PlotShapes( Buy * shapeUpArrow,colorGreen, layer = 0, L);
``````

Results are same ones as of your longer code

(BTW code of 2nd post is getting same result too already) I didn't read your whole post but I didn't look at his code but at his description. Maybe I am wrong but what i think he means is that he wants to buy at the cross of MH3 only if this months ML3 is greater than the MH3 level of the prior month.

So because he uses Monthly intervals he probably trades within the daily or weekly interval. So the chart in the daily will look like this (see below). So the price crosses MH30 and is only valid because ML30 is greater than MH31. If I misunderstood then my apologies but the question was also not particularly clear (as per usual). Prior to my first answer I quickly ran your code and I was not getting the same signals my code produces and therefor I assumed you interpreted his question differently. But since I use Camarilla levels myself I think he means it like I showed in my code. Once again...
code of 2nd post returns same result as your code (just with difference of yours being more code). Also code of post #11 returns same results. I have put all three ones into one AFL with explorer output.

Just do explore:

``````/// version 1
PMH =  TimeFrameGetPrice("H", inMonthly, -1);
PML =  TimeFrameGetPrice("L", inMonthly, -1);
PMC =  TimeFrameGetPrice("C", inMonthly, -1);

MH5 = (PMH/PML) * PMC ;
MH4 = PMC + (PMH - PML) * 1.1 / 2.0;
MH3 = PMC + (PMH - PML) * 1.1 / 4.0;
ML3 = PMC - (PMH - PML) * 1.1 / 4.0;
ML4 = PMC - (PMH - PML) * 1.1 / 2.0;
ML5 = PMC - (MH5 - PMC);

mth = Month();
new_mth = mth != Ref(mth,-1);
Buy1 = ValueWhen(new_mth, ML3>ref (MH3,-1)) AND Cross(C,MH3);

/// ################################################################
/// version 2

TimeFrameSet(tmfrm = inMonthly);
H = Ref(H,-1);
L = Ref(L,-1);
C = Ref(C,-1);
MH5 = (H/L) * C;
perc_range = (H - L) * 1.1;
MH4 = C + perc_range / 2.0;
MH3 = C + perc_range / 4.0;
ML3 = C - perc_range / 4.0;
ML4 = C - perc_range / 2.0;
ML5 = C - (MH5 - C);
rc_cond = ML3 > Ref(MH3, -1);
TimeFrameRestore();

MH3 = TimeFrameExpand(MH3, tmfrm, expandFirst);
rc_cond = TimeFrameExpand(rc_cond, tmfrm, expandFirst);

/// ################################################################
/// @empottasch version

// current month's levels
PMH =  TimeFrameGetPrice("H", inMonthly, -1);
PML =  TimeFrameGetPrice("L", inMonthly, -1);
PMC =  TimeFrameGetPrice("C", inMonthly, -1);

MH50 = (PMH/PML) * PMC ;
MH40 = PMC + (PMH - PML) * 1.1 / 2.0;
MH30 = PMC + (PMH - PML) * 1.1 / 4.0;
ML30 = PMC - (PMH - PML) * 1.1 / 4.0;
ML40 = PMC - (PMH - PML) * 1.1 / 2.0;
ML50 = PMC - (MH50 - PMC);

// the levels 1 month before current month
PMH =  TimeFrameGetPrice("H", inMonthly, -2);
PML =  TimeFrameGetPrice("L", inMonthly, -2);
PMC =  TimeFrameGetPrice("C", inMonthly, -2);

MH51 = (PMH/PML) * PMC ;
MH41 = PMC + (PMH - PML) * 1.1 / 2.0;
MH31 = PMC + (PMH - PML) * 1.1 / 4.0;
ML31 = PMC - (PMH - PML) * 1.1 / 4.0;
ML41 = PMC - (PMH - PML) * 1.1 / 2.0;
ML51 = PMC - (MH51 - PMC);

Buy3 = Cross( C, MH30 ) AND ML30 > MH31;

/// ################################################################
/// Explorer output

for( i = 1; i <= 3; i++ )
``````

Exploration result run on 500 symbols
So it is rock solid clear that all those three versions return same signals at same time. ah ok, sorry about that. I didn't go into details of your code, I just ran it and it seemed to give different signals than mine. But we have the same interpretation of his question so thats good

Hii, I am not getting same results for the solution provided earlier. Please find below both the codes. I have mentioned below my code.

``````//Monthly High, Low, Close
PMH =  TimeFrameGetPrice("H", inMonthly, -1);
PML =  TimeFrameGetPrice("L", inMonthly, -1);
PMC =  TimeFrameGetPrice("C", inMonthly, -1);
PMH1 =  TimeFrameGetPrice("H", inMonthly, -2);
PML1 =  TimeFrameGetPrice("L", inMonthly, -2);
PMC1 =  TimeFrameGetPrice("C", inMonthly, -2);

// Monthly Camarilla

MH5 = (PMH/PML) * PMC ;
MH4 = PMC + (PMH - PML) * 1.1 / 2.0;
MH3 = PMC + (PMH - PML) * 1.1 / 4.0;
ML3 = PMC - (PMH - PML) * 1.1 / 4.0;
ML4 = PMC - (PMH - PML) * 1.1 / 2.0;
ML5 = PMC - (MH5 - PMC);
MH32 = PMC1 + (PMH1 - PML1) * 1.1 / 4.0;

mth = Month();
new_mth = mth != Ref(mth,-1);
Buy1 = (ValueWhen(new_mth,MH3<Ref(MH3,-1)) AND ValueWhen(new_mth,MH3<Ref(MH3,-2),2) AND Cross(C,MH3));

===============================================================

period = 3;
TimeFrameSet(tmfrm = inMonthly);
MH5 = (H/L) * C;
perc_range = (H - L) * 1.1;
MH4 = C + perc_range / 2.0;
MH3 = C + perc_range / 4.0;
ML3 = C - perc_range / 4.0;
ML4 = C - perc_range / 2.0;
ML5 = C - (MH5 - C);
rc_cond = Sum(ROC(ML3,1)<0, period) == period;
TimeFrameRestore();

MH3 = TimeFrameExpand(MH3, tmfrm);
rc_cond = TimeFrameExpand(rc_cond, tmfrm);

for( i = 1; i <= 2; i++ )
=========================================================

Stoplength = 4*ATR(10);

Sell = 0;

ApplyStop(stopTypeTrailing,stopModePoint,Stoplength,2,True,1);

Equity(1,0);
SetOption("Everybarnullcheck", True);
Plot(stopline,"Stoploss",colorRed,styleLine);

`````` You should not just copy and paste but actually understand code.

1. I have used `period = 3` on purpose. And we can see expected result now of you just copy & pasting without actually understanding. You just look back at two occurrences and compare it to three lookback periods.
2. I was using `ML3` variable. You use `MH3` variable
3. You use default `expandFirst` (in TimeFrameGetPrice). I use default `expandLast`.

Again, don't just copy & paste but read documentation and learn basics.

And again Sum function is way to go to for getting results of whatever lookback period. One line of code and one single function.

So in the end if you compare apples to apples you will get same results on e.g. 500 symbols. 1 Like

Got it thanks.. By Using "expandlast" in "Timeframeexpand", it is not providng signals on the last day of the month.

Unable to remove excess Buy signals using below code. Can you please provide some hint. I am using Exrem but in this case Sell =0. How to apply exrem for Applystop

``````Stoplength = 4*ATR(10);

Sell = 0;

ApplyStop(stopTypeTrailing,stopModePoint,Stoplength,2,True,1);

Equity(1,0);