My visual effect afl collection.

yasu222

Active Member
barssince , state syntax in AFL

The following AFL code is to avoid multiple entries .May be useful for future reference.


state1=IIf(BarsSince(BUY1)<BarsSince(SELL1),1,0);
s1=state1>Ref(state1,-1);
ss1=state1<Ref(state1,-1);
sss1=state1==Ref(state1,-1);
col=IIf(state1 == 1 ,51,IIf(state1 ==0,4,1));
Plot(C,"",Col,128);
PlotShapes( shapeUpArrow * s1 ,6,0,L);
PlotShapes( shapeDownArrow *ss1 ,4,0,H);


state2=IIf(BarsSince(BUY2)<BarsSince(SELL2),1,0);
s2=state2>Ref(state2,-1);
ss2=state2<Ref(state2,-1);
sss2=state2==Ref(state2,-1);
col=IIf(state2 == 1 ,51,IIf(state2 ==0,4,1));
Plot(C,"",Col,128);
PlotShapes( shapeUpArrow * s2 ,6,0,L);
PlotShapes( shapeDownArrow *ss2 ,4,0,H);
 

yasu222

Active Member
Is the back test is true?!!!

ATTACHED BACK TEST REPORT. IS IT TRUE?!!! I GOT IT FROM FRIEND.

PHP:
Summary
            (BANKNIFTY-1M)
             Daily Bars 13-06-2005 Through 23-10-2015 (3784 Days)
            Optimized System Points Only Test

            Performance
            Profit128536.6846 Pts
            PerformanceN/A
            Annualized PerformanceN/A
            Buy & Hold Profit14325.5000 Pts
            Buy & Hold PerformanceN/A
            Buy & Hold Annualized PerformanceN/A

       
            Trade Summary
            Total Trades222
            Trade Efficiency46.82 %
            Average Profit/Average LossN/A

       
            Profitable Trades
            Total200
            Long101
            Short99

            Average Profit667.4747 Pts
            Highest Profit2910.0000 Pts
            Lowest Profit4.0000 Pts
            Most Consecutive28

       
            Unprofitable Trades
            Total22
            Long10
            Short12

            Average Loss-225.3751 Pts
            Highest Loss-757.6504 Pts
            Lowest Loss0.0000 Pts
            Most Consecutive3

       
            Maximum Position Excursions
            Long Favorable3497.7998 Pts
            Short Favorable3402.1494 Pts
            Long Adverse-639.0000 Pts
            Short Adverse-1126.0000 Pts
 

yasu222

Active Member
At Price Stoploss

Stop Loss line start from desired price level / our set price level.

PHP:
_SECTION_BEGIN("ATR@PRICE");
dojidiv =5;
CloseEqualOpen  = ( High - Low ) / dojidiv > abs( Open - Close );
RefC = Ref( C, -1 );
RefO = Ref( O, -1 ); 
Up = C > O;
Down = C < O ;
UpDoji = CloseEqualOpen AND RefC > RefO;
DownDoji = CloseEqualOpen AND RefC < RefO;
colcond = IIf( up || updoji, colorBrightGreen, colorRed );
SetBarFillColor( colcond );
Plot( C, "Price", colcond, styleCandle );

MY_KISS = Param("KISS_PRICE",  10, 0.05, 45000, 0.05 );
Start = Cross( C, MY_KISS ); 
Started = Flip( Start, 0 ); 

StopMode = ParamToggle("Stop Mode", "Fixed|Chandelier" ); 
StopLevel = Param("Fixed perc %", 14, 0.1, 50, 0.1)/100; 
StopATRFactor = Param("Chandelier ATR multiple", 4, 0.5, 10, 0.1 ); 
StopATRPeriod = Param("Chandelier ATR period", 14, 3, 50 ); 

// calculate support and resistance levels 
if( StopMode == 0 ) // fixed percent trailing stop 
{ 
 sup = C * ( 1 - stoplevel ); 
 res = C * ( 1 + stoplevel ); 
} 
else // Chandelier ATR-based stop 
{ 
 sup = C - StopATRFactor * ATR( StopATRPeriod ); 
 res = C + StopATRFactor * ATR( StopATRPeriod ); 
} 

// calculate trailing stop line 
trailARRAY = Null; 
trailstop = 0; 
for( i = 1; i < BarCount; i++ ) 
{ 
if( Started[ i ] == 0 ) continue; 

if( C[ i ] > trailstop AND C[ i - 1 ] > trailstop ) 
   trailstop = Max( trailstop, sup[ i ] ); 
else 
if( C[ i ] < trailstop AND C[ i - 1 ] < trailstop ) 
   trailstop = Min( trailstop, res[ i ] ); 
else 
   trailstop = IIf( C[ i ] > trailstop, sup[ i ], res[ i ] );       

trailARRAY[ i ] = trailstop; 
} 

// generate buy/sell signals based on crossover with trail stop line 
Buy = Start OR Cross( C, trailArray ); 
Sell = Cross( trailArray, C ); 

PlotShapes(Buy*shapeUpArrow,colorGreen,0,trailarray); 
PlotShapes(Sell*shapeDownArrow,colorRed,0,trailarray); 

Plot( Close,"Price",colorBlack,styleBar); 
Plot( trailARRAY,"trailing stop level", colorYellow );
_SECTION_END();
 
Last edited:
Is it possible convert this assigment
to the MetaStock format..???
Thanks in advance..Good luck


A = Param("Average Periods",5,1,10,1);
N = Param("Short Periods",15,1,500,1);
Average = (Low+High+(2*Close))/4;
OP = EMA(Average,A);
B1 = HHV(OP,N);
S1 = LLV(OP,N);
avd=IIf(C>Ref(B1,-1),1,IIf(C<Ref(S1,-1),-1,0));
avn=ValueWhen(avd!=0,avd,1);
tsl=IIf(avn==1,S1,B1);

Plot(tsl, _DEFAULT_NAME(), colorTan, styleStaircase);
 

KelvinHand

Well-Known Member
Is it possible convert this assigment
to the MetaStock format..???
Thanks in advance..Good luck


A = Param("Average Periods",5,1,10,1);
N = Param("Short Periods",15,1,500,1);
Average = (Low+High+(2*Close))/4;
OP = EMA(Average,A);
B1 = HHV(OP,N);
S1 = LLV(OP,N);
avd=IIf(C>Ref(B1,-1),1,IIf(C<Ref(S1,-1),-1,0));
avn=ValueWhen(avd!=0,avd,1);
tsl=IIf(avn==1,S1,B1);

Plot(tsl, _DEFAULT_NAME(), colorTan, styleStaircase);

I convert based on "MetaStock-Formula-Primer.pdf", No verification is done, cause i no metastock knowledge anymore. So don't look for me if syntax error

PHP:
A := Input("Average Periods",1,10,5); 
N := Input("Short Periods",1,500,15); 
Average := (Low+High+(2*Close))/4; 
OP      := Mov(Average,A, E); 
B1      := HHV(OP,N); 
S1      := LLV(OP,N); 

avd :=If(C>Ref(B1,-1),1,If(C<Ref(S1,-1),-1,0)); 
avn :=ValueWhen(1,avd<>0,avd); 
tsl   :=If(avn=1,S1,B1); 

tsl;
 
Thank you so much brother KelvinHand ...
It works and no problem so far....
You are great...
Best regards..............
 

amibrokerfans

Well-Known Member
one contribution from my end.

value chart

PHP:
_SECTION_BEGIN("Price 29");
SetChartOptions(0,chartShowArrows|chartShowDates);
    SetChartBkColor(ParamColor("Outer panel",colorBlack)); // color of outer border
    SetChartBkGradientFill( ParamColor("Inner panel upper",colorBlack),ParamColor("Inner panel lower",colorBlack));
    tchoice=Param("Title Selection ",2,1,2,1);
 
////  Vchart
//This was converted from code created for TradeStation by Mark W. Helweg of ValueCharts.com
VarNumbars = Param("Number of candlestick",5,2,1000,1);
Color = Param("Bar Color",colorBlue);
Top = Param("Top Chart Line",8);
TopMid = Param("Top Mid Chart Line",4);
BottomMid = Param("Bottom Mid Chart Line",-4);
Bottom = Param("Bottom Chart Line",-8);
Plot(Top,"Top Chart Line",colorRed);
Plot(TopMid,"Top Mid Chart Line",colorLightGrey);
Plot(BottomMid,"Bottom Mid Chart Line",colorLightGrey);
Plot(Bottom,"Bottom Chart Line",colorRed);
 
LRange = 0;
    VarP = round(VarNumBars/5);
if (VarNumBars > 7)
{
    VarA=HHV(H,VarP)-LLV(L,VarP);
    VarR1 = IIf(VarA==0 AND VarP==1,abs(C-Ref(C,-Varp)),VarA);
    VarB=Ref(HHV(H,VarP),-VarP+1)-Ref(LLV(L,VarP),-VarP);
    VarR2 = IIf(VarB==0 AND VARP==1,abs( Ref(C,-VarP)-Ref(C,-Varp*2) ),VarB);
    VarC=Ref(HHV(H,VarP),-VarP*2)-Ref(LLV(L,VarP),-VarP*2);
    VarR3 = IIf(VarC == 0 AND VarP==1,abs(Ref(C,-Varp*2)-Ref(C,-Varp*3)),VarC); 
    VarD = Ref(HHV(H,VarP),-VarP*3)-Ref(LLV(L,VarP),-VarP*3);
    VarR4 = IIf(VarD == 0 AND VarP==1,abs(Ref(C,-Varp*3)-Ref(C,-Varp*4)),VarD);
    VarE = Ref(HHV(H,VarP),-VarP*4)-Ref(LLV(L,VarP),-VarP*4);
    VarR5 = IIf(VarE == 0 AND VarP==1,abs(Ref(C,-Varp*4)-Ref(C,-Varp*5)),VarE);
    LRange=((VarR1+VarR2+VarR3+VarR4+VarR5)/5)*.2;
};
 
 
CDelta = abs(C - Ref(C,-1));
if (VarNumBars <=7)
{
    Var0 = IIf(CDelta > (H-L) OR H==L,CDelta,(H-L));
    LRange = MA(Var0,5)*.2;
}
MidBarAverage = MA( (H+L)/2,VarNumbars);
VOpen = (Open- MidBarAverage)/LRange;
VHigh = (High-MidBarAverage)/LRange;
VLow = (Low-MidBarAverage)/LRange;
VClose = (Close-MidBarAverage)/LRange;
PlotOHLC(VOpen,VHigh,VLow,VClose,"ValueChart",color,styleCandle| styleThick ,-12,12);
Title= Name() + " " + WriteVal( DateTime(), formatDateTime )+
"\nVOpen " + VOpen + "\n"+ " VHigh "+ VHigh + "\n"+ " VLow " + Vlow  + "\n"+ " VClose " + VClose;
Filter = Volume > 50000;
AddColumn( VOpen, "VOpen");
AddColumn( VHigh, "VHigh");
AddColumn( VLow, "VLow");
AddColumn( VClose, "VClose");
_SECTION_END();
_SECTION_END();
 

Similar threads