please code morpheus-signal-trader formula in amibroker

rvlv

Active Member
#2
actually the above is a replica of half trend indicator in mt4
can somebody code it in afl please?
here is mt4 code
=========================
//+----------------------------------------------------------------------+
//| HalfTrend.mq4 |
///+----------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 6
#property indicator_color1 DodgerBlue // up[]
#property indicator_width1 2
#property indicator_color2 Red // down[]
#property indicator_width2 2
#property indicator_color3 DodgerBlue // atrlo[]
#property indicator_width3 1
#property indicator_color4 Red // atrhi[]
#property indicator_width4 1
#property indicator_color5 DodgerBlue // arrup[]
#property indicator_width5 1
#property indicator_color6 Red // arrdwn[]
#property indicator_width6 1


extern int Amplitude = 2;
extern bool ShowBars = true;
extern bool ShowArrows = true;
extern bool alertsOn = true;
extern bool alertsOnCurrent = false;
extern bool alertsMessage = true;
extern bool alertsSound = true;
extern bool alertsEmail = false;

bool nexttrend;
double minhighprice,maxlowprice;
double up[],down[],atrlo[],atrhi[],trend[];
double arrup[],arrdwn[];
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int init()
{
IndicatorBuffers(7); // +1 buffer - trend[]

SetIndexBuffer(0,up);
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(1,down);
SetIndexStyle(1,DRAW_LINE);
SetIndexBuffer(2,atrlo);
SetIndexBuffer(3,atrhi);
SetIndexBuffer(6,trend);
SetIndexBuffer(4,arrup);
SetIndexBuffer(5,arrdwn);
SetIndexEmptyValue(0,0.0);
SetIndexEmptyValue(1,0.0);
SetIndexEmptyValue(6,0.0);

if(ShowBars)
{
SetIndexStyle(2,DRAW_HISTOGRAM, STYLE_SOLID);
SetIndexStyle(3,DRAW_HISTOGRAM, STYLE_SOLID);
}
else
{
SetIndexStyle(2,DRAW_NONE);
SetIndexStyle(3,DRAW_NONE);
}
if(ShowArrows)
{
SetIndexStyle(4,DRAW_ARROW,STYLE_SOLID); SetIndexArrow(4,233);
SetIndexStyle(5,DRAW_ARROW,STYLE_SOLID); SetIndexArrow(5,234);
}
else
{
SetIndexStyle(4,DRAW_NONE);
SetIndexStyle(5,DRAW_NONE);
}


nexttrend=0;
minhighprice= High[Bars-1];
maxlowprice = Low[Bars-1];
return (0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
class CFix { } ExtFix;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start()
{
double atr,lowprice_i,highprice_i,lowma,highma;
int workbar=0;
int counted_bars=IndicatorCounted();
if(counted_bars<0) return(-1);
if(counted_bars>0) counted_bars--;
int limit = MathMin(Bars-counted_bars,Bars-1);

for(int i=Bars-1; i>=0; i--)
{
lowprice_i=iLow(Symbol(),Period(),iLowest(Symbol(),Period(),MODE_LOW,Amplitude,i));
highprice_i=iHigh(Symbol(),Period(),iHighest(Symbol(),Period(),MODE_HIGH,Amplitude,i));
lowma=NormalizeDouble(iMA(NULL,0,Amplitude,0,MODE_SMA,PRICE_LOW,i),Digits());
highma=NormalizeDouble(iMA(NULL,0,Amplitude,0,MODE_SMA,PRICE_HIGH,i),Digits());
trend=trend[i+1];
atr=iATR(Symbol(),0,100,i)/2;

arrup = EMPTY_VALUE;
arrdwn = EMPTY_VALUE;
if(nexttrend==1)
{
maxlowprice=MathMax(lowprice_i,maxlowprice);

if(highma<maxlowprice && Close<Low[i+1])
{
trend=1.0;
nexttrend=0;
minhighprice=highprice_i;
}
}
if(nexttrend==0)
{
minhighprice=MathMin(highprice_i,minhighprice);

if(lowma>minhighprice && Close>High[i+1])
{
trend=0.0;
nexttrend=1;
maxlowprice=lowprice_i;
}
}
if(trend==0.0)
{
if(trend[i+1]!=0.0)
{
up=down[i+1];
up[i+1]=up;
arrup = up - 2*atr;
}
else
{
up=MathMax(maxlowprice,up[i+1]);
}
atrhi = up - atr;
atrlo = up;
down=0.0;
}
else
{
if(trend[i+1]!=1.0)
{
down=up[i+1];
down[i+1]=down;
arrdwn = down + 2*atr;
}
else
{
down=MathMin(minhighprice,down[i+1]);
}
atrhi = down + atr;
atrlo = down;
up=0.0;
}
}
manageAlerts();
return (0);
}
//+------------------------------------------------------------------+
//+-------------------------------------------------------------------
//|
//+-------------------------------------------------------------------
//
//
//
//
//

void manageAlerts()
{
if (alertsOn)
{
if (alertsOnCurrent)
int whichBar = 0;
else whichBar = 1;
if (arrup[whichBar] != EMPTY_VALUE) doAlert(whichBar,"up");
if (arrdwn[whichBar] != EMPTY_VALUE) doAlert(whichBar,"down");
}
}

//
//
//
//
//

void doAlert(int forBar, string doWhat)
{
static string previousAlert="nothing";
static datetime previousTime;
string message;

if (previousAlert != doWhat || previousTime != Time[forBar]) {
previousAlert = doWhat;
previousTime = Time[forBar];

//
//
//
//
//

message = StringConcatenate(Symbol()," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," HalfTrend signal ",doWhat);
if (alertsMessage) Alert(message);
if (alertsEmail) SendMail(StringConcatenate(Symbol(),"HalfTrend "),message);
if (alertsSound) PlaySound("alert2.wav");
}
}
 

rvlv

Active Member
#4
Rajendran of market calls.com coded the half trend
//Amibroker - Halftrend AFL Code

_SECTION_BEGIN("Half Trend Strategy");

SetPositionSize(1*RoundLotSize,spsShares);

amplitude = Param("Amplitude",2,1,10,1);
channelDeviation = Param("Channel Deviation",2,1,10,1);
showChannels = ParamToggle("Show ATR Channels","SHOW|HIDE",1);

main = Null;
trend = 0;
upLine = Null;
downLine = Null;
upperBand = Null;
lowerBand = Null;

atrHigh = Null;
atrLow = Null;


atr2 = ATR(100)/2;
dev = channelDeviation * atr2;

currentHigh = Ref(High, -abs(HHVBars(High,amplitude)));
currentLow = Ref(low, -abs(LLVBars(Low,amplitude)));

upperBand = MA(High,amplitude);
lowerBand = MA(Low,amplitude);


for(i=1;i<BarCount;i++)
{

// update trend direction and main Support Resistance values
if (trend[i-1] > 0) {
main = max(main[i-1], currentLow);
if (upperBand < main && Close < Low[i-1]) {
trend = -1;
main = min(main[i-1], currentHigh);
}
else trend = trend[i-1] + 1;
}
else if (trend[i-1] < 0) {
main = min(main[i-1], currentHigh);
if (lowerBand > main && Close > High[i-1]) {
trend = 1;
main = max(main[i-1], currentLow);
}
else trend = trend[i-1] - 1;
}
else {
// initialize the first, left-most value
if (Close > Close[i-1]) {
trend = 1;
main = currentLow;
}
else {
trend = -1;
main = currentHigh;
}
}

// update Support Resistance sections
if (trend > 0) {
upLine = main;
downLine = null;

atrHigh = upLine + dev;
atrLow = upLine - dev;
if (showChannels) { // make sure reversals become visible
upLine[i-1] = main[i-1];
if (trend[i-1] > 0)
downLine[i-1] = null;
}
}
else /*(trend < 0)*/{
upLine = null;
downLine = main;

atrHigh = downLine + dev;
atrLow = downLine - dev;
if (showChannels) { // make sure reversals become visible
if (trend[i-1] < 0)
upLine[i-1] = Null;
downLine[i-1] = main[i-1];
}
}



}//end of for loop





Plot(upLine,"UpLine",colorbrightgreen,styleThick);
Plot(downLine,"DownLine",colorRed,styleThick);

Plot(atrHigh,"ATR High",colorgreen,styleThick|styleDashed);
Plot(atrLow,"ATR Low",colorRed,styleThick|styleDashed);



Buy = trend == 1 ;
Sell = trend == -1 ;



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

Short = Sell;
Cover = Buy;


Buy = Ref(Buy,-1);
Sell = Ref(Sell,-1);

Short = Ref(Short,-1);
Cover = Ref(Cover,-1);

BuyPrice = ValueWhen(Buy,Open);
SellPrice = ValueWhen(Sell,Open);
ShortPrice = ValueWhen(Short,Open);
CoverPrice = ValueWhen(Cover,Open);


buycontinue = Flip(Buy,Sell);
shortcontinue = Flip(Short,Cover);




for( i = 0; i < BarCount; i++ )
{
if( Buy ) PlotText("Buy",i,L,colorWhite,colorLime,-70);
if( Short ) PlotText("Short",i,H,colorWhite,colorRed,70);
//if( Sell ) PlotText( "Selln@" + C[ i ], i, H[ i ]+dist, colorRed, colorYellow );
}


/* Plot Buy and Sell Signal Arrows */
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);




for(i=BarCount-1;i>1;i--)

{

if(Buy == 1)

{

entry = open;

sig = 1; //flag contains 1 when buy is valid

tar1 = entry + (entry * .0050);

tar2 = entry + (entry * .0092);

tar3 = entry + (entry * .0179);



bars = i;

i = 0;

}

if(Short == 1)

{

sig = -1; //flag contains -1 wjhen short is valid

entry = open;


tar1 = entry - (entry * .0050);

tar2 = entry - (entry * .0112);

tar3 = entry - (entry * .0212);





bars = i; // i - holds the value of bar index, computing the barindex value when the signal happened

i = 0;

}

}//for loop



Offset = 20;

Clr = IIf(sig == 1, colorLime, colorRed);




//plot target lines

Plot(LineArray(bars-Offset, tar1, BarCount-1, tar1,1), "", Clr, styleLine|styleDots, Null, Null, Offset);

Plot(LineArray(bars-Offset, tar2, BarCount-1, tar2,1), "", Clr, styleLine|styleDots, Null, Null, Offset);

Plot(LineArray(bars-Offset, tar3, BarCount-1, tar3,1), "", Clr, styleLine|styleDots, Null, Null, Offset);






_SECTION_END();


_SECTION_BEGIN("Signal Dashboard");

messageboard = ParamToggle("Message Board","Show|Hide",1);
upcolor = ParamColor("Up Color",colorBlue);
dncolor = ParamColor("Down Color",colorRed);



if (messageboard == 1 )

{

GfxSelectFont( "Tahoma", 13, 100 );

GfxSetBkMode( 1 );

GfxSetTextColor( colorWhite );


//Dashboard color changes dynamically according to the signals continuation
color = IIf(buycontinue, upcolor, IIf(shortcontinue, dncolor, Null));



GfxSelectSolidBrush(SelectedValue(color));


pxHeight = Status( "pxchartheight" ) ;

xx = Status( "pxchartwidth");

Left = 1100;
width = 310;

x = 5;
x2 = 360;
y = pxHeight;

sigstatus = WriteIf(buycontinue,"Buy Signal @ ","Sell Signal @");



GfxSelectPen( colorGreen, 1); // broader color
GfxRoundRect( x, y - 98, x2, y , 7, 7 ) ;
GfxTextOut( ( "Marketcalls - Halftrend"),13,y-90);
GfxTextOut( (" "),27,y-100);

if(SelectedValue(Buycontinue))
{
GfxTextOut( ("Last " + sigstatus + " Signal came " + BarsSince(Buy) * Interval()/60 + " mins ago"), 13, y-70) ; // The text format location
GfxTextOut( ("" + sigstatus + " : " + ValueWhen(Buy,BuyPrice)), 13, y-50);
GfxTextOut( ("Profit/Loss : " + NumToStr(Close-ValueWhen(Buy,BuyPrice),1.2)), 13, y-30);
}

if(SelectedValue(Shortcontinue))
{
GfxTextOut( ("Last " + sigstatus + " Signal came " + BarsSince(short) * Interval()/60 + " mins ago"), 13, y-70) ; // The text format location
GfxTextOut( ("" + sigstatus + " : " + ValueWhen(Short,ShortPrice)), 13, y-50);
GfxTextOut( ("Current P/L : " + NumToStr(ValueWhen(Short,ShortPrice)-Close,1.2)), 13, y-30);
}


//GfxTextOut( ("Trailing SL : " + Ref(TrendSL,-1) + " (" + WriteVal(IIf(sig == -1,entry-sl,sl-entry), 2.2) + ")"), 13, y-40);





}


_SECTION_END();

_SECTION_BEGIN("Top Dashboard");

X0 = 10;
Y0 = 20;

procedure DrawData (Text, x1, y1, x2, y2, colorFrom, colorTo)
{
GfxSetOverlayMode(0);
GfxSelectFont("Verdana", 8.5, 700);
GfxSetBkMode(1);
GfxGradientRect(x1, y1, x2, y2, colorFrom, colorTo);
GfxDrawText(Text, x1, y1, x2, y2, 32|1|4|16);
}


DrawData (Name(), X0, Y0, X0+150, Y0+20, colorGrey40, colorblack);
DrawData (Date(), X0+155, Y0, X0+320, Y0+20, colorGrey40, colorblack);
DrawData ("Open : " + Open, X0+325, Y0, X0+450, Y0+20, colorGrey40, colorblack);
DrawData ("Close : " + Close, X0+455, Y0, X0+580, Y0+20, colorGrey40, colorblack);
DrawData ("High : " + High, X0+585, Y0, X0+710, Y0+20, colorGrey40, colorblack);
DrawData ("Low : " + Low, X0+715, Y0, X0+840, Y0+20, colorGrey40, colorblack);


_SECTION_END();




_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", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
_SECTION_END();