want to convert metastock code to amibroker AFL

#1
I want to convert following metastock code to amibroker AFL
metastock code is given below.

I am enclosing metastock chart using following code for reference

Stop1:=If( PREV < L,
If(( H - 4.5*ATR(14) ) >= PREV,
( H - 4.5*ATR(14) ),
PREV),
( H - 4.5*ATR(14) ));

Stop2:=If( PREV < L,
If(( C - 3.5*ATR(14) ) >= PREV,
( C - 3.5*ATR(14) ),
PREV),
( C - 3.5*ATR(14) ));

StopLong:=If(Stop1>Stop2,Stop1,Stop2);

Stop1:=If( PREV > H,
If(( L + 4*ATR(14) ) <= PREV,
( L + 4*ATR(14) ),
PREV),
( L + 4*ATR(14) ));

Stop2:=If( PREV > H,
If(( C + 3.5*ATR(14) ) <= PREV,
( C + 3.5*ATR(14) ),
PREV),
( C + 3.5*ATR(14) ));

StopShort:=If(Stop1<Stop2,Stop1,Stop2);

If( ((Year()<=2008) OR (Year()=2009 AND Month()<=04 AND DayOfMonth()<=31)),
If((BarsSince(L<Ref(StopLong,-1))>BarsSince(H>Ref(StopShort,-1)) ),StopLong,StopShort),C);
 
Last edited:
#3
correct this 'smart tail stop afl'

please correct this afl and also help how to use it...
_SECTION_BEGIN("smart trail stop");
SetTradeDelays (0,0,0,0);
SetOption("InitialEquity", 100000);
SetOption("AccountMargin", 10);
SetOption("CommissionMode", 1);
SetOption("CommissionAmount", 0.10); // .1% commission per entry / exit
SetOption("MaxOpenPositions", 12);
SetOption("AllowSameBarExit", True);
// FIXED FRACTIONAL POSITION SIZING
CapRisk = Param("CapitalRisk", 2, 1,5,0.5); // capital risk %
MaxCap = 10; // max capital % in one trade (10% capital = 2% stop)
StopPct = Param("StopPct", 20, 1,25,1)/100; // stop % - initial risk
FFRisk = Min(CapRisk/(StopPct*BuyPrice)*BuyPrice/10, MaxCap);
"FF risk = " +WriteVal(ffrisk, 1.2);
SetPositionSize(FFRisk * 10, spsPercentOfEquity);
// POS SCORE - BANG FOR BUCK ($10,000)
B4B = 10000/Ref(C,-1)*Ref(ATR(200),-1)/100;
"10k BFB = " +WriteVal(B4B, 1.2);
PositionScore = B4B;

// INDEX TREND FILTER - 2 x moving averages
SetForeign("$DJ"); // change to whatever index is of interest
IFa = Param("IF FMA", 30,0,100,5); IFb = Param("IF SMA", 100,0,300,5);
IFx = EMA(C,IFa);
IFy = MA(C,IFb);
IndexFilter = IFx > IFy;

//PlotForeign("XAO", "All Ords", colorBlack, styleLine);
//Plot(IndexFilter,"Index Filter", colorBlack, styleLine);
RestorePriceArrays();

// TREND FILTER - 2 x moving averages
j = Param("FMA", 30,0,50,5); k = Param("SMA", 100,0,100,5);
FMA = EMA(C,j); SMA = EMA(C,k); TF = FMA > SMA;
//Plot(FMA, "FMA", colorBlue, styleLine);
Plot(SMA, "SMA", colorOrange,styleLine);
// VOL FILTER - MONEY FLOW > $1 MILLION
VF = EMA(V*C,21) > 1000000;
Filter = VF; AddColumn(VF, "volFilter"); AddColumn(EMA(V*C,21), "V*C",
1.2); // leaves approx 225 from ASX 300

BDC1 = Param("#buydays", 20, 5,50,5);
BDC2 = Param("#buydays2", 70, 5,100,5);
Cond1 = H > Ref(HHV(H,BDC1),-1) + 0.01;
Cond2 = H > Ref(HHV(H,BDC2),-1) + 0.01;
Buy = Cond1 AND Cond2 AND IndexFilter AND VF AND TF;
BuyPrice = Max(O, Ref(HHV(H,BDC1),-1) + 0.01);

Sell = 0;

// REDUCE RISK - RAISE STOP X 2, RAISE TO BREAKEVEN, TIGHTEN AFTER 3 DOWN DAYS
MSD = Param("#DaysB4MoveStop1", 5, 0,50,1) -1; // needs -1 to calculate correctly
RSP = (100-Param("%raiseStop1By", 50, 0, 100, 5)) / 100;
MSD2 = Param("#DaysB4MoveStop2", 15, 0,50,1) -1;
RSP2 = (100-Param("%raiseStop2By", 50, 0, 100, 5)) / 100;
MSBE = Param("#DaysB4MoveStopBE", 10, 0,50,1) -1;
TSDM = 1 + Param("%raiseStop3DownDays", 25, 0,100,5) / 100 * StopPct;
WriteVal(tsdm);
IS = BuyPrice - (BuyPrice*StopPct);
NS = Ref(C,-1) - (Ref(C,-1)*StopPct); // initial stop
NS2 = Ref(C,-1) - (Ref(C,-1)*StopPct*RSP); // reduce risk
NS3 = Ref(C,-1) - (Ref(C,-1)*StopPct*RSP*RSP2); // reduce risk again

TS = 0; OpenPos = 0; j = 0; BES = Null; TSD = 0; LowDown = 0;
TSa = Null; Spa = Null; ja = Null; nsa = Null; ns2a = Null;
bpa = Null; besa = Null; ns3a = Null; TSDa = Null; Checka = Null;

for(i=1; i<BarCount; i++)
{
if(Buy AND OpenPos == 0 AND TS == 0) {
Buy = 1;
bpa = BuyPrice;
bes = BuyPrice * 1.005; // .5% to cover comissions, etc
besa = bes;
TS = IS;
OpenPos = 1;
j = 0;
TSD = L - TS;
TSDa = TSD;
}
else Buy = 0;
if(OpenPos == 1 AND TS > 0 AND L <= TS) {
Sell = 1;
SellPrice = Min(O, TS);
SPa = SellPrice;
TS = 0;
OpenPos = 0;
j = 0;
BES = 0;
TSD = 0;
TSDa = TSD;
LowDown = 0;
}
else Sell = 0;
// reduce risk after x days, first time
if(TS > 0 AND j <= MSD) {
TS = Max(TS, NS);
TSa = TS;
NSa = NS;
}
if(TS > 0 AND j > MSD) {
TS = Max(TS, NS2);
TSa = TS;
NS2a = NS2;
}
// reduce risk after x days, second time
if(TS > 0 AND j >= MSD2) {
TS = Max(TS, NS3);
TSa = TS;
NS3a = NS3;
}
// raise stop to BE after x days
if(OpenPos == 1) {
BES = Max(BES, BES[i-1]);
BESa = BES;
}
if(OpenPos == 1 AND j == MSBE) {
TS = Max(TS, BES);
TSa = TS;
BESa = BES;
}
// TSD getting smaller over last 3 days
if(TS > 0) {
TSD = L - TS;
TSDa = TSD;
if(TSD < TSD[i-1] AND TSD[i-1] < TSD[i-2] AND TSD[i-2] < TSD[i-3]) {

TS = Max(TS, TS * TSDM);
TSa = TS;
LowDown = 1;
Checka = LowDown;
}
}
j++; ja = j;
}

Plot(TSa, "Trail Stop", colorBlue, styleLine);
//Plot(bpa, "buyprice", colorGreen, styleLine);
//Plot(spa, "sellprice2", colorRed, styleLine);
//Plot(ja, "j", colorBlack, styleNoLine);
//Plot(NSa, "NS", colorBlack, styleLine);
//Plot(NS2a, "NS2", colorOrange, styleLine);
//Plot(NS3a, "NS3", colorBrown, styleLine);
//Plot(besa, "BES", colorBrightGreen, styleLine);
//Plot(TSDa, "TSD", colorRed, styleLine);
//Plot(Checka, "checkTSD", colorGreen, styleLine);


// GRAPH / INDICATOR PLOT
arrows = ParamToggle("Show Buy / Sell arrows?", "Yes please", 0);
if(arrows == 1) {
PlotShapes(Buy*shapeUpArrow,colorBrightGreen,0,Low );
PlotShapes(Sell*shapeDownArrow,colorRed,0,High);
}
bliss = ParamToggle("Show Buy / Sell Prices", "Oh yeah! ",0);
if(bliss == 1) {
PlotShapes( IIf(Buy, shapeSmallCircle, shapeNone),colorBrightGreen, 0,
BuyPrice, 0 );
PlotShapes( IIf( Sell, shapeSmallCircle, shapeNone),colorRed, 0
,SellPrice, 0 );
FirstVisibleBar = Status( "FirstVisibleBar" );
Lastvisiblebar = Status("LastVisibleBar");
for( b = Firstvisiblebar; b <= Lastvisiblebar AND b < BarCount; b++)
{
if( Buy ) PlotText("\n Buy\n
else if( Sell )
for( b = Firstvisiblebar; b <= Lastvisiblebar AND b < BarCount; b++)
{

else if( Sell )
PlotText( \n Sell\n + NumToStr(SellPrice,1.2),b,SellPrice,colorRed);


_SECTION_END();
thanks....
 
#4
_SECTION_BEGIN("Swing");
prev=AMA2(C,1,0);
d=IIf(C>Ref(Max(Max(H,Ref(H,-20)),Max(Ref(H,-10),Ref(H,-15))),-1),Min(Min(L,Ref(L,-20)),Min(Ref(L,-10),Ref(L,-15))),
IIf(C<Ref(Min(Min(L,Ref(L,-20)),Min(Ref(L,-10),Ref(L,-15))),-1),Max(Max(H,Ref(H,-20)),Max(Ref(H,-10),Ref(H,-15))),PREV));
a=Cross(Close,d);
b=Cross(d,Close);
state=IIf(BarsSince(a)<BarsSince(b),1,0);
s=state>Ref(state,-1);
ss=state<Ref(state,-1);
sss=state==Ref(state,-1);
col=IIf(state == 1 ,51,IIf(state ==0,4,1));
Plot(C,"",Col,64);
PlotShapes( shapeUpArrow * s ,6,0,L);
PlotShapes( shapeDownArrow *ss ,4,0,H);

Filter = s OR sss OR sss ;
AddColumn(C,"close",1.2);
AddColumn( IIf( s, 66,1 ), "buy", formatChar, 1, bkcolor =IIf (s,colorYellow, colorPink ));
AddColumn( IIf( Ss, 83,1 ), "sell", formatChar, 1, bkcolor =IIf (Ss,colorPink, colorYellow ));
AddColumn( IIf( sss, 87,1 ), "wait", formatChar, 1, bkcolor =IIf (sss,colorYellow, colorRed ));

_SECTION_BEGIN("");
_N(Title = "{{NAME}} - {{INTERVAL}} {{DATE}}: "+_DEFAULT_NAME()+" : {{OHLCX}} {{VALUES}}"
+"\n"+EncodeColor(colorYellow)+
WriteIf(s,"EXIT all Short positions\nif trading long positions, enter long Now-\nOR at the market price on tomorrow's Open with stop="+EncodeColor(4)+WriteVal(L+.75*ATR(5),1.4)+" ,","")+
WriteIf(ss,"exit all long positions today with a Market On Close (MOC) order\nOR at the market price on tomorrow's Open with stop="+EncodeColor(4)+WriteVal(Ref(H+.75*ATR(5), -1),1.4)+",","")+
WriteIf( sss ,"No trading signals today.","") );



_SECTION_BEGIN("swing1");
no=20;
res=HHV(H,no);
sup=LLV(L,no);
avd=IIf(C>Ref(res,-1),1,IIf(C<Ref(sup,-1),-1,0));
avn=ValueWhen(avd!=0,avd,1);
supres=IIf(avn==1,sup,res);

a=Cross(C,supres);
b=Cross(supres,C);

style = a * styleStaircase + b * styleStaircase;

PlotShapes(a,style, IIf(a,colorGreen,colorRed), 0, IIf(a,Low,High));
Plot(supres,"Swing",colorYellow,styleStaircase);

_SECTION_END();



_SECTION_BEGIN("trend");
uptrend=PDI(20)>MDI(10)AND Signal(29)<MACD(13);
downtrend=MDI(10)>PDI(20)AND Signal(29)>MACD(13);

//uptrend=PDI()>MDI()AND Signal()<MACD();
//downtrend=MDI()>PDI()AND Signal()>MACD();
Plot( 2, /* defines the height of the ribbon in percent of pane width */"ribbon",
IIf( uptrend, colorGreen, IIf( downtrend, colorRed, 0 )), /* choose color */
styleOwnScale|styleArea|styleNoLabel, -0.5, 100 );

_SECTION_END();

//d = Close > Ref( ChandelierHL(ATR(3),20), -1);
//e =Close < Ref( ChandelierHL(ATR(3),20), -1);
//f = Close < Ref( ChandelierHL(ATR(3),20), -1);
//g = Close > Ref( ChandelierHL(ATR(3),20), -1);

Buy = s AND a AND uptrend ;
Short = ss AND b AND downtrend ;
Sell = ss AND b AND downtrend ;
Cover = s AND a AND uptrend ;

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

Filter=Buy OR Sell;
Filter= Cover OR Short;

AddColumn( Buy, "Buy", 1);
AddColumn(Sell, "Sell", 1);
AddColumn(Close,"Close",1.2);
AddColumn(Volume,"Volume",1.0);

//_SECTION_BEGIN("Volume");
//Plot( Volume, _DEFAULT_NAME(), ParamColor("Color", colorLavender ), styleNoTitle | ParamStyle( "Style", styleHistogram | styleOwnScale | styleThick | styleNoLabel, maskHistogram ), 2 );
//_SECTION_END();
// Plot the Buy and Sell arrows.
PlotShapes( shapeUpArrow * Buy, colorGreen, 0, Low );
PlotShapes( shapeDownArrow * Sell, colorRed, 0, High );


dist = 3.5*ATR(35);
for( i = 0; i < BarCount; i++ )
{
if( Buy ) PlotText( "Buy\n@" + C[ i ], i, L[ i ]-dist, colorGreen );
if( Sell ) PlotText( "Sell\n@" + C[ i ], i, H[ i ]+dist, colorRed );
}

shape = Buy * shapeUpArrow + Sell * shapeDownArrow;
PlotShapes(shape, IIf(Buy,colorGreen,colorRed), 0, IIf(Buy,Low,High));
//Black Lable //
Plot(Close,"Close",colorBlack,styleCandle);
//White Candle//
Plot(C,"",colorWhite,styleCandle|styleNoLabel);

GraphXSpace = 5;


_SECTION_END();
 
#5
//please correct the following line you can see the price also on screen
dist = 3.5*ATR(35);
for( i = 0; i < BarCount; i++ )
{
if( s ) PlotText( "Buy\n@" + C[ i ], i, L[ i ]-dist, colorGreen );
if( ss ) PlotText( "Sell\n@" + C[ i ], i, H[ i ]+dist, colorRed );
}
 
#6
_SECTION_BEGIN("Swing");
prev=AMA2(C,1,0);
d=IIf(C>Ref(Max(Max(H,Ref(H,-20)),Max(Ref(H,-10),Ref(H,-15))),-1),Min(Min(L,Ref(L,-20)),Min(Ref(L,-10),Ref(L,-15))),
IIf(C<Ref(Min(Min(L,Ref(L,-20)),Min(Ref(L,-10),Ref(L,-15))),-1),Max(Max(H,Ref(H,-20)),Max(Ref(H,-10),Ref(H,-15))),PREV));
a=Cross(Close,d);
b=Cross(d,Close);
state=IIf(BarsSince(a)<BarsSince(b),1,0);
s=state>Ref(state,-1);
ss=state<Ref(state,-1);
sss=state==Ref(state,-1);
col=IIf(state == 1 ,51,IIf(state ==0,4,1));
Plot(C,"",Col,64);
PlotShapes( shapeUpArrow * s ,6,0,L);
PlotShapes( shapeDownArrow *ss ,4,0,H);

Filter = s OR sss OR sss ;
AddColumn(C,"close",1.2);
AddColumn( IIf( s, 66,1 ), "buy", formatChar, 1, bkcolor =IIf (s,colorYellow, colorPink ));
AddColumn( IIf( Ss, 83,1 ), "sell", formatChar, 1, bkcolor =IIf (Ss,colorPink, colorYellow ));
AddColumn( IIf( sss, 87,1 ), "wait", formatChar, 1, bkcolor =IIf (sss,colorYellow, colorRed ));

_SECTION_BEGIN("");
_N(Title = "{{NAME}} - {{INTERVAL}} {{DATE}}: "+_DEFAULT_NAME()+" : {{OHLCX}} {{VALUES}}"
+"\n"+EncodeColor(colorYellow)+
WriteIf(s,"EXIT all Short positions\nif trading long positions, enter long Now-\nOR at the market price on tomorrow's Open with stop="+EncodeColor(4)+WriteVal(L+.75*ATR(5),1.4)+" ,","")+
WriteIf(ss,"exit all long positions today with a Market On Close (MOC) order\nOR at the market price on tomorrow's Open with stop="+EncodeColor(4)+WriteVal(Ref(H+.75*ATR(5), -1),1.4)+",","")+
WriteIf( sss ,"No trading signals today.","") );



_SECTION_BEGIN("swing1");
no=20;
res=HHV(H,no);
sup=LLV(L,no);
avd=IIf(C>Ref(res,-1),1,IIf(C<Ref(sup,-1),-1,0));
avn=ValueWhen(avd!=0,avd,1);
supres=IIf(avn==1,sup,res);

a=Cross(C,supres);
b=Cross(supres,C);

style = a * styleStaircase + b * styleStaircase;

PlotShapes(a,style, IIf(a,colorGreen,colorRed), 0, IIf(a,Low,High));
Plot(supres,"Swing",colorYellow,styleStaircase);

_SECTION_END();



_SECTION_BEGIN("trend");
uptrend=PDI(20)>MDI(10)AND Signal(29)<MACD(13);
downtrend=MDI(10)>PDI(20)AND Signal(29)>MACD(13);

//uptrend=PDI()>MDI()AND Signal()<MACD();
//downtrend=MDI()>PDI()AND Signal()>MACD();
Plot( 2, /* defines the height of the ribbon in percent of pane width */"ribbon",
IIf( uptrend, colorGreen, IIf( downtrend, colorRed, 0 )), /* choose color */
styleOwnScale|styleArea|styleNoLabel, -0.5, 100 );

_SECTION_END();

//d = Close > Ref( ChandelierHL(ATR(3),20), -1);
//e =Close < Ref( ChandelierHL(ATR(3),20), -1);
//f = Close < Ref( ChandelierHL(ATR(3),20), -1);
//g = Close > Ref( ChandelierHL(ATR(3),20), -1);

Buy = s AND a AND uptrend ;
Short = ss AND b AND downtrend ;
Sell = ss AND b AND downtrend ;
Cover = s AND a AND uptrend ;

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

Filter=Buy OR Sell;
Filter= Cover OR Short;

AddColumn( Buy, "Buy", 1);
AddColumn(Sell, "Sell", 1);
AddColumn(Close,"Close",1.2);
AddColumn(Volume,"Volume",1.0);

//_SECTION_BEGIN("Volume");
//Plot( Volume, _DEFAULT_NAME(), ParamColor("Color", colorLavender ), styleNoTitle | ParamStyle( "Style", styleHistogram | styleOwnScale | styleThick | styleNoLabel, maskHistogram ), 2 );
//_SECTION_END();
// Plot the Buy and Sell arrows.
PlotShapes( shapeUpArrow * Buy, colorGreen, 0, Low );
PlotShapes( shapeDownArrow * Sell, colorRed, 0, High );


dist = 3.5*ATR(35);
for( i = 0; i < BarCount; i++ )
{
if( Buy ) PlotText( "Buy\n@" + C[ i ], i, L[ i ]-dist, colorGreen );
if( Sell ) PlotText( "Sell\n@" + C[ i ], i, H[ i ]+dist, colorRed );
}

shape = Buy * shapeUpArrow + Sell * shapeDownArrow;
PlotShapes(shape, IIf(Buy,colorGreen,colorRed), 0, IIf(Buy,Low,High));
//Black Lable //
Plot(Close,"Close",colorBlack,styleCandle);
//White Candle//
Plot(C,"",colorWhite,styleCandle|styleNoLabel);

GraphXSpace = 5;


_SECTION_END();



Can anyone code this for metastock ?
 
#7
I want to convert metastock code to amibroker AFL

This formula is based Metastock Pivot point. Any body code in to amibroker.

Thanks in advance

------------

TF:=Input("In the last 1=Hour 2=Day 3=Week 4=Month 5=Year",1,5,2);
NW:=If(TF=1,ROC(Minute(),1,$)<0,If(TF=2,ROC(Hour(),1,$)<0,If(TF=3,ROC(DayOfWeek(),1,$)<0,If(TF=4,ROC(DayOfMonth(),1,$)<0,ROC(Month(),1,$)<0))));
A1:=Cum(1);
A2:=LastValue(A1-BarsSince(NW>0));
WH:=ValueWhen(1,Nw,Ref(HighestSince(1,Nw,H),-1));
WL:=ValueWhen(1,Nw,Ref(LowestSince(1,Nw,L),-1));
WCL:=ValueWhen(1,Nw,Ref(C,-1));
BP:=LastValue((WH+WL+WCL)/3);
R#2:=If(A1<A2,BarsSince(A1>=A2),LastValue(((WH+WL+WCL)/3)-(2*((WH+WL+WCL)/3)-WH)+(2*((WH+WL+WCL)/3)-WL)));
R#1:=If(A1<A2,BarsSince(A1>=A2),LastValue(2*((WH+WL+WCL)/3)-WL));
R#3:=If(A1<A2,BarsSince(A1>=A2),LastValue(R#2+(WH-Wl)));
PP:=If(A1<A2,BarsSince(A1>=A2),LastValue((WH+WL+WCL)/3));
S#1:=If(A1<A2,BarsSince(A1>=A2),LastValue(2*((WH+WL+WCL)/3)-WH));
S#2:=If(A1<A2,BarsSince(A1>=A2),LastValue(((WH+WL+WCL)/3)-(((2*((WH+WL+WCL)/3)-WL)-(2*((WH+WL+WCL)/3)-WH)))));
S#3:=If(A1<A2,BarsSince(A1>=A2),LastValue(S#2-(WH-WL)));
R#3;
R#2;
R#1;
PP;
S#1;
S#2;
S#3;
 

Similar threads