Simple Coding Help - No Promise.

Hello Seniors and Fellow traders,

One help required on the below formula, it sometime not calculate the correct SL or TGT in given any scrip here i am pasting the code and example image of DRReddy where its showing buying and TGT rate as same kindly help me to get corrected.


I am attaching code also it is very good to give signals in intraday and in Forex superb but when it comes to low price shares it dont give good idea.

Odd=13;
CoefOdd=round(Odd/2);

Even=12;
Coefeven=Even/2;
Coefeven2=Coefeven+1;

CongestionPercent=2.8;

TriangularOdd=MA(MA(C,CoefOdd),CoefOdd);
TriangularEven=MA(MA(C,Coefeven),Coefeven2);

finalMov_avg=IIf(Odd > even,triangularOdd,TriangularEven);

Color=colorBrightGreen;
tickercolor=colorWhite;

Plot(finalMov_avg,"",IIf(C < finalmov_avg,colorRed,Color),styleLine|styleThick);
Plot(C,"",tickercolor,styleCandle);

Title=Name()+"..."+"( "+WriteIf(Odd > even,WriteVal(Odd,1),WriteVal(even,1))+" ) Period "+EncodeColor(Color)+"Triangular"+WriteIf(Odd > even,"ODD","EVEN")+" Moving Average"+"..."+EncodeColor(colorBlack)+ WriteIf(C < finalMov_avg,"Close is "+EncodeColor(colorRed)+"Below"+EncodeColor(colorBlack)+" Moving Average by ","Close is"+EncodeColor(colorBrightGreen)+" Above"+EncodeColor(colorBlack)+" Moving Average by ")+"("+WriteVal(((C/finalMov_avg)-1)*100,1.1)+"% )"+"\n"+WriteIf(finalmov_avg-Ref(finalmov_avg,-1)>0," Slope Of Average is UP : ","Slope Of Average is DOWN :")+WriteIf((((C/finalMov_avg)-1)*100 <= CongestionPercent AND ((C/finalMov_avg)-1)*100 >= -CongestionPercent),EncodeColor(colorYellow)+" with Price Congestion / Divergence to Average ","")+"\n"+WriteIf(Ref(C,-1) < Ref(finalmov_avg,-1) AND C > finalmov_avg,EncodeColor(colorGreen)+"Possible Change in Trend From Down to Up"+"\n"+" OR Short Term Correction of Previous Trend",WriteIf(Ref(C,-1) > Ref(finalmov_avg,-1) AND C < finalmov_avg,EncodeColor(colorRed)+"Possible Change in Trend From Up to Down "+"\n"+" OR Short Term Correction to Previous Trend",""))+"\n"+WriteIf(C > finalmov_avg,EncodeColor(colorGreen)+"Close has been above Moving Average ( "+WriteVal(BarsSince(C < finalmov_avg),1)+" ) Bars",EncodeColor(colorRed)+"Close has been Below Moving Average ( "+WriteVal(BarsSince(C > finalmov_avg),1)+" ) Bars")+"\n"+EncodeColor(colorBlack)+"The average # of Bars Above ( "+WriteVal(round(Cum(BarsSince(C < finalmov_avg)/Cum(1))),1)+" )"+"\n"+"The average # of Bars Below ( "+WriteVal(round(Cum(BarsSince(C > finalmov_avg)/Cum(1))),1)+" )";
_SECTION_BEGIN("AFL Example");



SetBarsRequired(10000,10000); /* this ensures that the charts include all bars AND NOT just those on screen */
SetFormulaName("Sample System"); /*name it for backtest report identification */
SetTradeDelays( 1, 1, 1, 1 ); /* delay entry/exit by one bar */
SetOption( "initialequity", 100000 ); /* starting capital */
PositionSize = -10; /* trade size will be 10% of available equty */
SetOption( "MaxOpenPositions", 6 ); /* I don't want to comit more than 60% of Equity at any one time */
SetOption( "PriceBoundChecking", 1 ); /* trade only within the chart bar's price range */
SetOption( "CommissionMode", 2 ); /* set commissions AND costs as $ per trade */
SetOption( "CommissionAmount", 32.95 ); /* commissions AND cost */
SetOption( "UsePrevBarEquityForPosSizing", 1 ); /*set the use of last bars equity for trade size*/
PositionScore = 100/C; /*Set the order for which stock trades when get mulitple signals in one bar in backtesting */



LongPer = Param("Long Period", 50, 30, 100, 5 ); /* select periods with parameter window */
ShortPer = Param("Short Period", 5, 3, 10, 1 );

LongMA = EMA( C, LongPer );
ShortMA = EMA( C, ShortPer );
LastHigh = HHV( H, LongPer );

Buy = Cross( ShortMA, LongMA ) AND H > Ref( LastHigh, -1 );

Sell = Cross( LongMA, ShortMA );

Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);



Filter = Buy OR Sell;
AddTextColumn( FullName(), "Company Name" );
AddColumn( Buy, "Buy", 1 );
AddColumn( Sell, "Sell", 1 );
AddColumn( C, "Close", 1.3 );
AddColumn( H, "High", 1.3 );
AddColumn( LastHigh, "HHV", 1.3 );
AddColumn( LongMA, "Long MA", 1,3 );
AddColumn( ShortMA, "Short MA", 1,3 );




GraphXSpace = 10;

//Plot( C, " Close Price", colorGrey50, styleBar );
Plot( LongMA, " EMA(C,"+WriteVal(LongPer,1)+")", colorBrown, styleLine|styleNoRescale );
Plot( ShortMA, " EMA(C,"+WriteVal(ShortPer,1)+")", colorBlue, styleLine|styleNoRescale );
Plot( Ref(Lasthigh,-1), " HHV(H,"+WriteVal(LongPer,1)+")", colorRed, styleNoLine|styleDots|styleNoRescale );

//PlotShapes( shapeUpArrow*Buy, colorGreen, 0, L, -10 );
//PlotShapes( shapeDownArrow*Sell, colorRed, 0, H, -10 );

Title = " {{NAME}} {{DATE}} {{INTERVAL}} "+_DEFAULT_NAME()+" Chart values : {{VALUES}} ";


_SECTION_END();
no=Param( "Swing", 5, 1, 55 );
res=HHV(H,no);
sup=LLV(L,no);
tsl=IIf(ValueWhen(IIf(C>Ref(res,-1),1,IIf(C<Ref(sup,-1),-1,0))!=0,IIf(C>Ref(res,-1),1,IIf(C<Ref(sup,-1),-1,0)),1)==1,sup,res);

Buy = Cross(C,res) ;
Sell = Cross(sup,C) ;
_SECTION_END();

a=C;
g=(EMA(Close,3) * (2 / 4 - 1)-EMA(Close,5) * (2 / 6 - 1)) / (2 /4- 2 /6);
e=Ref(tsl,-1);
Buy = Cross(C,tsl) ;
Sell = Cross(tsl,C) ;
SellPrice=ValueWhen(Sell,e,1);
BuyPrice=ValueWhen(Buy,e,1);
Long=Flip(Buy,Sell);
Shrt=Flip(Sell,Buy );
Filter=Buy OR Sell;
Buy = Cross(C,tsl) ;
Sell = Cross(tsl,C) ;
shape = Buy * shapeUpArrow + Sell * shapeDownArrow;
PlotShapes( shape, IIf( Buy, colorGreen, colorRed ),0, IIf( Buy, Low, High ) );
a1=Ref(tsl,-1);

dist = 0.8*ATR(1); //0.8
dist1 = 1.8*ATR(1); //1.2
for( i = 0; i < BarCount; i++ )
{
if( Buy )
{
PlotText( "Buy:" + L[ i ] + "\nTgt: " + (a1*1.005) + "\nSL: " + (tsl*0.9975), i, L[ i ]-dist, colorLime);
}
if( Sell )
{
PlotText( "Sell:" + H[ i ] + "\nT: " + (a1*0.995) + "\nSL: " + (tsl*1.0025), i, H[ i ]+dist1, colorGreen);
}
}
 

Attachments

Hello Seniors and Fellow traders,

One help required on the below formula, it sometime not calculate the correct SL or TGT in given any scrip here i am pasting the code and example image of DRReddy where its showing buying and TGT rate as same kindly help me to get corrected.
That is flaw in the trading system/method not in the code.
No mechanical system with out discretion, wont do good in long run.
 
Hi,
I want to ask question with a
lets say,
today is tuesday ..we seeing crude oil chart on 5 minute timeframe .. i only trade when there's shooting star.and there's no shooting star on 5 minute timeframe,( current timeframe that is on my chart) As per his rule he only trade when there's shooting star,but there's shooting star on 23 minute timeframe. but he don't know there's shooting star.

I need an afl , which can output timeframe of candle pattern.

I simplied question, i guess it's clear.

shootingstar = (((H-L)>4*(O-C)) AND ((H-C)/(.001+H-L)>= 0.75) AND ((H-O)/(.001+H-L)>= 0.75));

Thank you
anyone?

Thank you
 
Code:
_SECTION_BEGIN("Candle Identification");
	Plot(C,"",1,64);
	O1 = Ref(O,-1);O2 = Ref(O,-2);
	H1 = Ref(H,-1);H2 = Ref(H,-2);
	L1 = Ref(L,-1);L2 = Ref(L,-2);
	C1 = Ref(C,-1);C2 = Ref(C,-2);
	function CandlePattern(P) {
		global PatternName;
		if(P == 0) {
			PatternName = "NearDoji"; Pv = (abs(O-C)<= ((H-L)*0.1));
		}
		else if(P == 1) { 
			PatternName = "BlackCandle"; Pv = (O>C);
		}
		else if(P == 2) {
			PatternName = "LongBlackCandle"; Pv = (O>C AND	(O-C)/(.001+H-L)>.6);
		}
		else if(P == 3) {
			PatternName = "SmallBlackCandle"; Pv = ((O>C) AND	((H-L)>(3*(O-C))));
		}
		else if(P == 4) { 
			PatternName = "WhiteCandle"; Pv = (C>O);
		}
		else if(P == 5) { 
			PatternName = "LongWhiteCandle"; Pv = ((C>O) AND	((C-O)/(.001+H-L)>.6)); 
		}
		else if(P == 6) { 
			PatternName = "SmallWhiteCandle"; Pv = ((C>O) AND	((H-L)>(3*(C-O)))); 
		}
		else if(P == 7) { 
			PatternName = "BlackMaubozu"; Pv = (O>C AND H==O AND	C==L); 
		}
		else if(P == 8) { 
			PatternName = "WhiteMaubozu"; Pv = (C>O AND H==C AND	O==L);
		}
		else if(P == 9) {
			PatternName = "BlackClosingMarubozu"; Pv = (O>C AND	C==L); 
		}
		else if(P == 10) { 
			PatternName = "WhiteClosingMarubozu"; Pv = (C>O AND	C==H);
		}
		else if(P == 11) { 
			PatternName = "BlackOpeningMarubozu"; Pv = (O>C AND	O==H); 
		}
		else if(P == 12) {
			PatternName = "WhiteOpeningMarubozu"; Pv = (C>O AND	O==L); 
		}
		else if(P == 13) { 
			PatternName = "HangingMan"; Pv = (((H-L)>4*(O-C)) AND	((C-L)/(.001+H-L)>= 0.75) AND ((O-L)/(.001+H-L)>= 0.75));
		}
		else if(P == 14) { 
			PatternName = "Hammer"; Pv = (((H-L)>3*(O-C)) AND	((C-L)/(.001+H-L)>0.6) AND ((O-L)/(.001+H-L)>0.6)); 
		}
		else if(P == 15) { 
			PatternName = "InvertedHammer"; Pv = (((H-L)>3*(O-C))	AND ((H-C)/(.001+H-L)>0.6) AND ((H-O)/(.001+H-L)>0.6)); 
		}
		else if(P == 16) { 
			PatternName = "ShootingStar"; Pv = (((H-L)>4*(O-C))	AND ((H-C)/(.001+H-L)>= 0.75) AND ((H-O)/(.001+H-L)>= 0.75));
		}
		else if(P == 17) { 
			PatternName = "BlackSpinningTop"; Pv = ((O>C) AND	((H-L)>(3*(O-C))) AND (((H-O)/(.001+H-L))<.4) AND	(((C-L)/(.001+H-L))<.4)); 
		}
		else if(P == 18) { 
			PatternName = "WhiteSpinningTop"; Pv = ((C>O) AND	((H-L)>(3*(C-O))) AND (((H-C)/(.001+H-L))<.4) AND	(((O-L)/(.001+H-L))<.4));
		}
		else if(P == 19) {
			PatternName = "BearishAbandonedBaby"; Pv = ((C1 == O1)	AND (C2>O2) AND (O>C) AND (L1>H2) AND (L1>H));
		}
		else if(P == 20) { 
			PatternName = "BearishEveningDojiStar"; Pv = ((C2>O2)	AND ((C2-O2)/(.001+H2-L2)>.6) AND (C2<O1) AND (C1>O1) AND	((H1-L1)>(3*(C1-O1))) AND (O>C) AND (O<O1)); 
		}
		else if(P == 21) { 
			PatternName = "DarkCloudCover"; Pv = (C1>O1 AND	((C1+O1)/2)>C AND O>C AND O>C1 AND C>O1 AND (O-C)/(.001+(H-L)>0.6)); 
		}
		else if(P == 22) { 
			PatternName = "BearishEngulfing"; Pv = ((C1>O1) AND	(O>C) AND (O>= C1) AND (O1>= C) AND ((O-C)>(C1-O1))); 
		}
		else if(P == 23) { 
			PatternName = "ThreeOutsideDownPattern"; Pv = ((C2>O2)	AND (O1>C1) AND (O1>= C2) AND (O2>= C1) AND ((O1-C1)>(C2-O2)) AND (O>C) AND	(C<C1)); 
		}
		else if(P == 24) { 
			PatternName = "BullishAbandonedBaby"; Pv = ((C1 == O1)	AND (O2>C2) AND (C>O) AND (L2>H1) AND (L>H1)); 
		}
		else if(P == 25) {
			PatternName = "BullishMorningDojiStar"; Pv = ((O2>C2)	AND ((O2-C2)/(.001+H2-L2)>.6) AND (C2>O1) AND (O1>C1) AND	((H1-L1)>(3*(C1-O1))) AND (C>O) AND (O>O1)); 
		}
		else if(P == 26) {
			PatternName = "BullishEngulfing"; Pv = ((O1>C1) AND	(C>O) AND (C>= O1) AND (C1>= O) AND ((C-O)>(O1-C1)));
		}
		else if(P == 27) { 
			PatternName = "ThreeOutsideUpPattern"; Pv = ((O2>C2)	AND (C1>O1) AND (C1>= O2) AND (C2>= O1) AND ((C1-O1)>(O2-C2)) AND (C>O) AND	(C>C1)); 
		}
		else if(P == 28) { 
			PatternName = "BullishHarami"; Pv = ((O1>C1) AND (C>O)	AND (C<= O1) AND (C1<= O) AND ((C-O)<(O1-C1))); 
		}
		else if(P == 29) { 
			PatternName = "ThreeInsideUpPattern"; Pv = ((O2>C2)	AND (C1>O1) AND (C1<= O2) AND (C2<= O1) AND ((C1-O1)<(O2-C2)) AND (C>O) AND	(C>C1) AND (O>O1)); 
		}
		else if(P == 30) {
			PatternName = "PiercingLine"; Pv = ((C1<O1) AND	(((O1+C1)/2)<C) AND (O<C) AND (O<C1) AND (C<O1) AND	((C-O)/(.001+(H-L))>0.6)); 
		}
		else if(P == 31) {
			PatternName = "BearishHarami"; Pv = ((C1>O1) AND (O>C)	AND (O<= C1) AND (O1<= C) AND ((O-C)<(C1-O1)));
		}
		else if(P == 32) { 
			PatternName = "ThreeInsideDownPattern"; Pv = ((C2>O2)	AND (O1>C1) AND (O1<= C2) AND (O2<= C1) AND ((O1-C1)<(C2-O2)) AND (O>C) AND	(C<C1) AND (O<O1));
		}
		else if(P == 33) { 
			PatternName = "ThreeWhiteSoldiers"; Pv = (C>O*1.01)	AND (C1>O1*1.01) AND (C2>O2*1.01) AND (C>C1) AND (C1>C2) AND (O<C1) AND	(O>O1) AND (O1<C2) AND (O1>O2) AND (((H-C)/(H-L))<.2) AND	(((H1-C1)/(H1-L1))<.2) AND (((H2-C2)/(H2-L2))<.2); 
		}
		else if(P == 34) { 
			PatternName = "DarkCloudCover"; Pv = (C1>O1*1.01) AND	(O>C) AND (O>H1) AND (C>O1) AND (((C1+O1)/2)>C) AND (C>O1) AND	(MA(C,13)-Ref(MA(C,13),-4)>0);
		}
		else if(P == 35) { 
			PatternName = "ThreeBlackCrows"; Pv = (O>C*1.01) AND	(O1>C1*1.01) AND (O2>C2*1.01) AND (C<C1) AND (C1<C2) AND (O>C1) AND (O<O1)	AND (O1>C2) AND (O1<O2) AND (((C-L)/(H-L))<.2) AND (((C1-L1)/(H1-L1))<.2)	AND (((C2-L2)/(H2-L2))<.2); 
		}
		else if(P == 36) {
			PatternName = "doji"; Pv = (O == C); 
		}
		else if(P == 37) { 
			PatternName = "GapUp"; Pv = GapUp(); 
		}
		else if(P == 38) { 
			PatternName = "GapDown"; Pv = GapDown();
		}
		else if(P == 39) { 
			PatternName = "BigGapUp"; Pv = L>1.01*H1;
		}
		else if(P == 40) { 
			PatternName = "BigGapDown"; Pv = H<0.99*L1;
		}
		else if(P == 41) { 
			PatternName = "HugeGapUp"; Pv = L>1.02*H1; 
		}
		else if(P == 42) {
			PatternName = "HugeGapDown"; Pv = H<0.98*L1;
		}
		else if(P == 43) { 
			PatternName = "DoubleGapUp"; Pv = GapUp() AND	Ref(GapUp(),-1); 
		}
		else if(P == 44) {
			PatternName = "DoubleGapDown"; Pv = GapDown() AND	Ref(GapDown(),-1); 
		}
		return Pv;
	}

	PatternNameList = "";
	for(Cp=0; Cp<=44; Cp++)	{
		VarSet("Pattern"+NumToStr(Cp,1.0),CandlePattern(cP));
		PatternNameList = PatternNameList +PatternName+","; 
	}

	BI = BarIndex();
	SelectedBar = SelectedValue(BI) -BI[0];
	//Selectedbar = Status("lastvisiblebar")-1;
	PStr="";
	for(Cp=0; Cp<=44; Cp++)	{
		Temp = VarGet("Pattern"+NumToStr(Cp,1.0));
		if(temp[SelectedBar]) Pstr=Pstr+"#"+NumToStr(Cp,1.0)+" - "+StrExtract(PatternNameList,Cp)+"\n";
	}

	Title = Name() +" {" +Date()+  "} " + "Open: "+ O + " High: "+ H+" Low: "+L +" Close: "+C+"\nCandleStick Analysis-\n"+ EncodeColor(colorGreen)+Pstr;
_SECTION_END();

//~manoj
 
Last edited:
I use following code for BB.

mid = WMA( C, Period );
sd = StDev( C, Period );
Top = mid + Width * sd;
Bot = mid - Width * sd;

I want pale green color for Bot for Cond1 ,Green color for Cond2 and DarkOliveGreen for Cond3.

I want Pink color for Top for Cond1 ,Red color for Cond2 and DarkRed for Cond3.

pl. help
 
Hello Seniors and Fellow traders,

One help required on the below formula, it sometime not calculate the correct SL or TGT in given any scrip here i am pasting the code and example image of DRReddy where its showing buying and TGT rate as same kindly help me to get corrected.



Please provide any solution seniors...ty
 

Similar threads