Reverse MACD-Amibroker

#1
please help in converting the process of metastock code into amibroker Afl.
metastock code is:
In the Tools menu, select Indicator Builder.
Click New to open the Indicator Editor for a new indicator.
Type in the name MACD price window all in one.
Click in the larger window and paste or type in the following formula:
t1:= Input("fast periods", 5, 24, 12);
t2:= Input("slow periods", 10, 52, 26);
pick:= Input("plot: <1=MACDeq 2=MACDeq0 3=HLC 4=BBands>",1,4,1);

a1:= 2/(t1+1);
a2:= 2/(t2+1);
CMACDeq:=(Ref(Mov(C,t1,E)*a1,-1)-Ref(Mov(C,t2,E)*a2,-1))/(a1-a2);
HMACDeq:=(Ref(Mov(H,t1,E)*a1,-1)-Ref(Mov(H,t2,E)*a2,-1))/(a1-a2);
LMACDeq:=(Ref(Mov(L,t1,E)*a1,-1)-Ref(Mov(L,t2,E)*a2,-1))/(a1-a2);
MACDeq0:= Ref(Mov(C,t2,E)*(1-a2) - Mov(C,t1,E)*(1-a1),-1)/(a1-a2);

z:= LastValue(If(pick > 2, 0, Cum(1)+1));
plot1:= If(pick=3, HMACDeq, BBandTop(CMACDeq,10,S,1));
plot2:= If(pick=2, MACDeq0, CMACDeq);
plot3:= If(pick=3, LMACDeq, BBandBot(CMACDeq,10,S,1));

plot1 + Ref(0,+z);
plot2;
plot3 + Ref(0,+z);


Click OK to close the Indicator Editor.
Click New to open the Indicator Editor for the second indicator.
Type the name MACD indicator window all in one.
Click in the larger window and paste or type in the following formula:
t1:= Input("fast periods", 5, 24, 12);
t2:= Input("slow periods", 10, 52, 26);
t3:= Input("signal periods", 3, 18, 9);
pick:= Input("plot: <1=MACD 2=MACD_HLC 3=MACD_MTF>",1,4,1);

MACDH:=Mov(H,t1,E)-Mov(H,t2,E);
MACDL:=Mov(L,t1,E)-Mov(L,t2,E);
MACDC:=OscP(12,26,E,$);
MTFw:=OscP(12*5,26*5,E,$);
MTFM:=OscP(12*21,26*21,E,$);

y:= LastValue(If(pick <>2, 0, Cum(1)+1));
z:= LastValue(If(pick > 1, 0, Cum(1)+1));
plot1:= MACDC;
plot2:= If(pick=2, MACDH, MTFw);
plot3:= If(pick=2, MACDL, MTFm);
plot4:= Mov(MACDC,t3,E);

plot1;
plot2 + Ref(0,+z);
plot3 + Ref(0,+z);
plot4 + Ref(0,+y);


Click OK to close the Indicator Editor.
Click OK to close the Indicator Builder.
:thumb:
 

KelvinHand

Well-Known Member
#2
please help in converting the process of metastock code into amibroker Afl.
metastock code is:
In the Tools menu, select “Indicator Builder.”
Click New to open the Indicator Editor for a new indicator.
Type in the name “MACD price window all in one.”
Click in the larger window and paste or type in the following formula:
t1:= Input("fast periods", 5, 24, 12);
t2:= Input("slow periods", 10, 52, 26);
pick:= Input("plot: <1=MACDeq 2=MACDeq0 3=HLC 4=BBands>",1,4,1);

a1:= 2/(t1+1);
a2:= 2/(t2+1);
CMACDeq:=(Ref(Mov(C,t1,E)*a1,-1)-Ref(Mov(C,t2,E)*a2,-1))/(a1-a2);
HMACDeq:=(Ref(Mov(H,t1,E)*a1,-1)-Ref(Mov(H,t2,E)*a2,-1))/(a1-a2);
LMACDeq:=(Ref(Mov(L,t1,E)*a1,-1)-Ref(Mov(L,t2,E)*a2,-1))/(a1-a2);
MACDeq0:= Ref(Mov(C,t2,E)*(1-a2) - Mov(C,t1,E)*(1-a1),-1)/(a1-a2);

z:= LastValue(If(pick > 2, 0, Cum(1)+1));
plot1:= If(pick=3, HMACDeq, BBandTop(CMACDeq,10,S,1));
plot2:= If(pick=2, MACDeq0, CMACDeq);
plot3:= If(pick=3, LMACDeq, BBandBot(CMACDeq,10,S,1));

plot1 + Ref(0,+z);
plot2;
plot3 + Ref(0,+z);


Click OK to close the Indicator Editor.
Click New to open the Indicator Editor for the second indicator.
Type the name “MACD indicator window all in one.”
Click in the larger window and paste or type in the following formula:
t1:= Input("fast periods", 5, 24, 12);
t2:= Input("slow periods", 10, 52, 26);
t3:= Input("signal periods", 3, 18, 9);
pick:= Input("plot: <1=MACD 2=MACD_HLC 3=MACD_MTF>",1,4,1);

MACDH:=Mov(H,t1,E)-Mov(H,t2,E);
MACDL:=Mov(L,t1,E)-Mov(L,t2,E);
MACDC:=OscP(12,26,E,$);
MTFw:=OscP(12*5,26*5,E,$);
MTFM:=OscP(12*21,26*21,E,$);

y:= LastValue(If(pick <>2, 0, Cum(1)+1));
z:= LastValue(If(pick > 1, 0, Cum(1)+1));
plot1:= MACDC;
plot2:= If(pick=2, MACDH, MTFw);
plot3:= If(pick=2, MACDL, MTFm);
plot4:= Mov(MACDC,t3,E);

plot1;
plot2 + Ref(0,+z);
plot3 + Ref(0,+z);
plot4 + Ref(0,+y);


Click OK to close the Indicator Editor.
Click OK to close the Indicator Builder.
:thumb:
Addition information from this site:
http://www.traders.com/Documentation/FEEDbk_docs/2012/01/TradersTips.html#item16


Not difficult to convert from metastock to amibroker.
Almost no diff in the functions.
 
Last edited:
#3
dear KelvinHand i shall be highly obliged if you can convert this metastock code to amibroker afl, bcas i don't know much of programming
thanks in advance
 

johnnypareek

Well-Known Member
#4
dear KelvinHand i shall be highly obliged if you can convert this metastock code to amibroker afl, bcas i don't know much of programming
thanks in advance
will it serve??

HTML:
_SECTION_BEGIN("MACD");
GraphXSpace = 8;
r1 = Param( "Fast avg", 12, 2, 200, 1 );
r2 = Param( "Slow avg", 26, 2, 200, 1 );
r3 = Param( "Signal avg", 9, 2, 200, 1 );

mm = MACD(r1, r2);
ms = Signal(r1, r2, r3);

f1 = ((r3+1)*MM-2*MS)/(r3-1);
f2 = EMA(C,r1)*(r1-1)/(r1+1);
f3 = EMA(C,r2)*(r2-1)/(r2+1);
f4 = 2/(r1+1) - 2/(r2+1);

dcv = (f1-f2+f3)/ f4;

Plot(dcv,"",4);
PlotOHLC(O,H,L,C,"",1,styleCandle);
Title = Name() + "  -  " + FullName() + "  -  " + Date() + "  -  Close = " +
WriteVal(C,0.3) + "  -  DCV = " + WriteVal(dcv,0.3);
_SECTION_END();
forget past guys. help

johnny
 

johnnypareek

Well-Known Member
#5
or may be this

HTML:
// General-purpose MC part 
HowManyMCSteps = 20000; // adjust that to change the number of MC tests 

PositionScore = 100 * mtRandomA(); 
// that is single-line that causes random picking of signals 

Step = Optimize("Step", 1, 1, HowManyMCSteps , 1 ); 
// this is dummy variable, not used below 

// The trading system itself 
// ( you may enter your own system below instead of one from the article ) 

NumPos = 8; // maximum number of open positions 
SetOption("MaxOpenPositions", NumPos ); 
SetPositionSize( GetOption("InitialEquity") / NumPos, spsValue ); 
// as in the article - no compounding of profits 
// SetPositionSize( 100 / NumPos, spsPercentOfEquity ); 
// uncomment this for compounding profits 

// signals 
s = Signal( 12, 26, 9 ); 
m = MACD( 12, 26 ); 
Buy = Cross( s, m ); 
Sell = Cross( m, s ); 

SetTradeDelays( 1, 1, 1, 1 ); // trade with one bar delay on open price 
BuyPrice = Open; 
SellPrice = Open;
 

KelvinHand

Well-Known Member
#6
Hi johnny,
I already stated very easy, just convert the metastock.
1. := to =
2. input to param, exchange last and 1st variable
3. mov(,,E) to EMA(), mov(,,S) to MA(), mov(,,W) to WMA() ...etc
4. if() to iif(), in iif () = to ==, in iif() <> to !=
5. Ref(0,+z); to Ref(0,z);
6. Plot(plot2, "plot2" ... ) ... etc
7. OscP() remove the E & $

TradeStation version give you the Buy/Sell Strategy
 
Last edited:
#7
dear Kelvinhand i have been able to convert some part of code but not complete,please can u convert the rest of it.(marked by //)

_SECTION_BEGIN(" REVERSING MACD");
t1= Param("fast periods", 5, 24, 12);
t2= Param("slow periods", 10, 52, 26);
pick= ParamList("plot","MACDeq,MACDeq0,HLC,BBands",1);

a1= 2/(t1+1);
a2= 2/(t2+1);
CMACDeq=(Ref(EMA(C,t1)*a1,-1)-Ref(EMA(C,t2)*a2,-1))/(a1-a2);
HMACDeq=(Ref(EMA(H,t1)*a1,-1)-Ref(EMA(H,t2)*a2,-1))/(a1-a2);
LMACDeq=(Ref(EMA(L,t1)*a1,-1)-Ref(EMA(L,t2)*a2,-1))/(a1-a2);
MACDeq0= Ref(EMA(C,t2)*(1-a2) - EMA(C,t1)*(1-a1),-1)/(a1-a2);

//z= LastValue(if(pick > 2, 0, Cum(1)+1));
//plot1= if(pick=3, HMACDeq, BBandTop(CMACDeq,10,S,1));
//plot2= if(pick=2, MACDeq0, CMACDeq);
//plot3= if(pick=3, LMACDeq, BBandBot(CMACDeq,10,S,1));

//plot1 + Ref(0,z);
//plot2;
//plot3 + Ref(0,z);

pick2= ParamList("plot2","MACD,MACD_HLC,MACD_MTF",1);

MACDH=EMA(H,t1)-EMA(H,t2);
MACDL=EMA(L,t1)-EMA(L,t2);
MACDC=OscP(12,26);
MTFw=OscP(12*5,26*5);
MTFM=OscP(12*21,26*21);

//y= LastValue(if(pick <>2, 0, Cum(1)+1));
//z= LastValue(if(pick > 1, 0, Cum(1)+1));
plot1= MACDC;
//plot2= if(pick=2, MACDH, MTFw);
//plot3= if(pick=2, MACDL, MTFm);
//plot4= EMA(MACDC,t3);


Plot(Plot1,"MACDC",colorBlue,styleLine);//plot1;
//plot2 + Ref(0,z);
//plot3 + Ref(0,z);
//plot4 + Ref(0,y);
_SECTION_END();
 

KelvinHand

Well-Known Member
#8
dear Kelvinhand i have been able to convert some part of code but not complete,please can u convert the rest of it.(marked by //)


_SECTION_BEGIN("REVERSING_MACD_PRICE_WINDOW");

Post#6 Rule 2 - not complete
t1= Param("fast periods", 5, 24, 12);
t2= Param("slow periods", 10, 52, 26);

Post #6 Rule 2
pick= Param("plot: <1=MACDeq 2=MACDeq0 3=HLC 4=BBands>",1,1,4);

//pick= ParamList("plot","MACDeq,MACDeq0,HLC,BBands",1);

a1= 2/(t1+1);
a2= 2/(t2+1);
CMACDeq=(Ref(EMA(C,t1)*a1,-1)-Ref(EMA(C,t2)*a2,-1))/(a1-a2);
HMACDeq=(Ref(EMA(H,t1)*a1,-1)-Ref(EMA(H,t2)*a2,-1))/(a1-a2);
LMACDeq=(Ref(EMA(L,t1)*a1,-1)-Ref(EMA(L,t2)*a2,-1))/(a1-a2);
MACDeq0= Ref(EMA(C,t2)*(1-a2) - EMA(C,t1)*(1-a1),-1)/(a1-a2);

Post #6 Rule 4
//z= LastValue(if(pick > 2, 0, Cum(1)+1));
//plot1= if(pick=3, HMACDeq, BBandTop(CMACDeq,10,S,1));
//plot2= if(pick=2, MACDeq0, CMACDeq);
//plot3= if(pick=3, LMACDeq, BBandBot(CMACDeq,10,S,1));


//---- Post #6 Rule 6 --------------------------------------------
//plot1 + Ref(0,z);
//plot2;
//plot3 + Ref(0,z);
_SECTION_END();

Metastock Indicator Window code deleted, not suitable for amibroker
There are two script files - 1 script for Price Window, the other script for Indicator Window
 
Last edited:

KelvinHand

Well-Known Member
#9
Found that the Mestastock programming seen to be wrong, after i check the rest of code given in the website
http://www.traders.com/Documentation/FEEDbk_docs/2012/01/TradersTips.html#item16
on
tradestation, esignal, ... etc.

Totally not tally.
So consider metastock is a bug version.


Here is the indication window version for you:


Code:
_SECTION_BEGIN("REVERSING_MACD_INDICATOR_WINDOW");
t1= Param("fast periods", 12, 5,  24);
t2= Param("slow periods", 26, 10, 52);
t3= Param("signal periods", 9,3, 18);

pick= ParamList("plot", "MACD|MACD_HLC|MACD_MTF");

MACDH=EMA(H,t1)-EMA(H,t2);
MACDL=EMA(L,t1)-EMA(L,t2);
MACDC=EMA(C, t1)- EMA(C,t2);
MTFw= EMA(C,5*t1) - EMA(C,5*t2);
MTFM= EMA(C,21*t1)- EMA(C,21*t2);

Sig= EMA(MACDC,t3);

Plot(MACDC, "MACD",   colorBlue);
Plot(Sig,   "Signal", colorRed);
Plot(0,     "",      colorGrey40);
     

switch (pick)
{
  case "MACD_HLC":
     Plot(MACDH, "MACDH",   colorGreen);
     Plot(MACDL, "MACHL",   colorDarkYellow);
     break; 
     
  case "MACD_MTF":
     Plot(MTFw, "MTFw",   colorGreen);
     Plot(MTFm, "MTFm",   colorDarkYellow);
     break;

  default /*MACD*/:
      diff = MACDc- sig;
     Plot(diff, "Diff",   colorTeal, styleHistogram);
     break;
 
}
_SECTION_END();
 
Last edited:

Similar threads