Simple Coding Help - No Promise.

pratapvb

Well-Known Member
Its an egg/hen problem :D

Your MO is based on MC and MC is based on MO, circular reference . . .


Code:
Modified close = ([B]modified open[/B] + high + low + close)/4

substituting for MO

Modified close = ([B][I](previous modified open + previous modified close)/2[/I][/B] + high + low + close)/4
but there is a way around to this

define first bar values as equal to open & close and then derive rest in a for barcount loop


:) Happy
Please tell me if this is correct

MO = (open+close)/2
MC = (MO+High+Low+Close)/4
MO = AMA (Ref(MC,-1),0.5)
 

ethan hunt

Well-Known Member
Hi,

Below is an AFL.

I would like to incoporate following conditions/options into the AFL.

1) BUY CONDITION: Green Arrow when MA (Close,50) crosses MA (Close,100) from below & goes up.

2) SHORT CONDITION: Red Arrow when MA (Close,100) crosses MA (Close,50) from below & goes up.


3) BUY EXECUTION: Buy at open of next candle after point 1 candle ; TARGET: 50 points; SL: 25 points

4) SHORT EXECUTION: Short at open of next candle after point 2 candle; TARGET: 50 points; SL 25 points

5) Draws automatically HORIZONTAL LINES for TARGET & SL with respective values as soon as BUY /SHORT is triggered.

6) Shows BUY@ PRICE ; SHORT@ PRICE on chart above candles of points 3 & 4.

7) VOICE ALERTS as soon as point 1 & 2 are met.

8) VOICE ALERTS as soon as TARGET or SL is hit.

9) POP-UPS as soon as point 1 & 2 are met.

10) POP-UPS as soon as point TARGET / SL is hit.

11) Back testing facility.

12) Scanning facility.

Thanks a lot.

AFL:


_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
_SECTION_END();

_SECTION_BEGIN("MA");
P = ParamField("Price field",-1);
Periods = Param("Periods", 15, 2, 300, 1, 10 );
Plot( MA( P, Periods ), _DEFAULT_NAME(), ParamColor( "Color", colorCycle ), ParamStyle("Style") );
_SECTION_END();

_SECTION_BEGIN("MA1");
P = ParamField("Price field",-1);
Periods = Param("Periods", 15, 2, 300, 1, 10 );
Plot( MA( P, Periods ), _DEFAULT_NAME(), ParamColor( "Color", colorCycle ), ParamStyle("Style") );
_SECTION_END();

A=MA(Close,50);
B=MA(Close,100);
Buy=Cross(A,B);
Sell=Cross(B,A);
PlotShapes(IIf(Sell==1, shapeDownArrow, shapeNone), colorRed, 0,High, Offset=-50);
PlotShapes(IIf(Buy==1, shapeUpArrow , shapeNone), colorGreen, 0,Low, Offset=-50);
 

pratapvb

Well-Known Member
Yes quite similar. Could you please share the afl.
not sure if it is exactly correct because actually reducing you formula results in modopen dependent on prevmodopen....so not sure of AMA use

Code:
_SECTION_BEGIN("modopen");
//ModOpen = (prevmodOpen + prevmodClose)/2
//ModClose = (modOpen + High + Low + Close)/4


//2*ModOpen = 5/4*prevmodOpen + (prevhigh+prevlow+Prevclose)/4
//ModOpen = 5/8*prevmodOpen + (prevhigh++prevlow+Prevclose)/8
//ModOpen = 6/8*(5/6*prevmodOpen + 1/6*(prevhigh++prevlow+Prevclose))


modopen = O ;

prevhlc = Ref(H, -1)+Ref(L, -1)+Ref(C, -1) ;
modopen = AMA(prevhlc, 1.0/6) /3 ; // * 6.0/8;

modclose = (modopen+H+L+C)/4;
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} " + EncodeColor( colorGreen ) + " ModOpen %g," + EncodeColor( colorBlue ) + " Hi %g,  " + EncodeColor( colorRed ) + " Lo %g,  " + EncodeColor( colorBlack ) + " ModClose %g (%.1f%%){{VALUES}}", modopen, H, L, modclose, SelectedValue( ROC( C, 1 ) ) ));

PlotOHLC(modopen, H, L, modclose, "", colorBlack) ;
_SECTION_END();
 
Please help with coding this

Modified open = (previous modified open + previous modified close)/2

Modified close = (modified open + high + low + close)/4

Code:
//Initialization
MO[0] = Open[0]; MC[0] = Close[0];
for(i = 1; i < BarCount; i++ ) 
{
	MO[i] = (MO[i-1]+MC[i-1])/2;
	MC[i] = (MO[i-1]+High[i]+Low[i]+Close[i])/4;
}
UP = MC > MO AND C > MC AND C > Ref(H,-1);	DN = MC < MO AND C < MO AND C < Ref(L,-1);
UP = ExRem(UP,DN); DN = ExRem(DN,UP);

Plot(MC, "\nMC",colorRed,  styleStaircase|styleThick,0,0,1); 
Plot(MO, "MO",colorBlue, styleStaircase|styleThick,0,0,1); 

PlotShapes(UP+2*DN,colorWhite,0,IIf(UP,L,H)); 

//Color=IIf(MC>MO, colorBlue, colorRed);
//Plot((MO+MC)/2,"",Color, styleStaircase|styleThick|styleNoLabel|styleNoTitle,0,0,1,0,5);

:) Happy
 
Last edited:
not sure if it is exactly correct because actually reducing you formula results in modopen dependent on prevmodopen....so not sure of AMA use
Yes, me thinks we should just do away with the AMA code . . . :D

:) Happy
 

pratapvb

Well-Known Member
Yes, me thinks we should just do away with the AMA code . . . :D

:) Happy
I tend to avoid writing FOR loop code as far as possible, as it makes the AFL sluggish, though in this case AMA maybe using for loop internally (but it could be optimized for better execution)
 
Pratap Sir, Happy Sir

please guide me on this

Display out and compare the HPPL and HPPU values, you will know the reason of the problem.

If you use different color to plot HPPU & HHPL, you will get the mistake

use this and see if this is what you want . . .

Code:
HPP  = (HH + HL + HC)/3;
HPP2 = (HH + HL)/2;
Dif  = abs(HPP-HPP2);
HPPU = HPP + Dif;
HPPL = HPP - Dif;

:) Happy
 

Similar threads