Simple Coding Help - No Promise.

revendar

Well-Known Member
I have a couple of questions:

1. Im looping on the bars in 15 minute candles and I want to get the datetime of that candle. I want to add some conditions to filter buy signal if time <09:45:00.
I kind of achieved this by below code. But I guess better way is there.


Code:
for ( i = lb+5;i < BarCount;i++ )
      {
	 noCandles = 1;
	 tc[i] = C[i];
	 time[i] =  LastValue(Ref(TimeNum(),-(BarCount-i-1)));
         .......
         .......
         mx[i] = H[i-1]*1.005;;
      }
2. I'm creating some arrays(mx) from 15Minute time frame as shown above. I'm then time compressing to 5minute and want to trade based on my 5min price crossing 15min array.



Code:
mx=TimeFrameCompress(mx,in5Minute);
      fc=TimeFrameCompress(C,in5Minute);
      b2 =  Cross(fc,mx);
      Buy = b1+b2;
Currently it buys (in 15min) after the new candle is created. But I want to do that as soon as price crosses that mx. So I'm trying to achieve that by going to lower time frame.
 

Nehal_s143

Well-Known Member
try with below code on top of your afl

SetBarsRequired(501,0);
GraphXSpace = 9; //can change no. as required
SetChartOptions(0,chartShowArrows|chartShowDates);
GfxSetBkMode(0);
GfxSetOverlayMode(0);
SetTradeDelays(1,1,1,1);


some body can help me to fix this error please , sometime i cant see the arrow show buy and sell when price too high or too low
it look like this , please fix it , thanks


code
Code:
_SECTION_BEGIN("H K CHART");
//---- TREND TRADE
SetChartOptions(1,chartShowArrows|chartShowDates);
GraphXSpace=5;
HaClose = (O+H+L+C)/4;
HaOpen = AMA( Ref( HaClose, -1 ), 0.8);
HaHigh = Max( H, Max( HaClose, HaOpen ) );
HaLow = Min( L, Min( HaClose, HaOpen ) );
xDiff = (HaHigh - Halow) * IIf(StrFind(Name(),"JPY"),100,10000);
barcolor = IIf(HaClose >= HaOpen,colorWhite,colorWhite);
if( ParamToggle("Plot Heikin-Ashi", "No,Yes", 1 ) )
PlotOHLC( HaOpen, HaHigh, HaLow, HaClose, "Heikin Ashi " + Name(), colorWhite, styleCandle );
else
Plot( C, "Regular candles " + Name(), colorWhite, styleCandle );
j=Haclose;

//PRICE LINE

PriceLineColor = PriceLineColor= colorCustom4;
PriceLevel = C;

Daysback = 200;
FirstBar = BarCount - DaysBack;
YY = IIf(BarIndex() >= Firstbar,EndValue(PriceLevel),Null);

Plot(YY,"Current Price",PriceLineColor,ParamStyle("LineStyle",style Line|styleDashed|styleNoTitle|styleThick,maskAll)) ;


side = 1;

dist = 0;

for( i = 0; i < BarCount; i++ )
{
if(i+side== BarCount) PlotText( "\n " + PriceLevel[ i ], i, YY[ i ]-dist[i], colorCustom2 );
}


_SECTION_END();



_SECTION_BEGIN("Trend Lines");
p1 = Param("TL 1 Periods", 13, 1, 50, 1);
p2 = Param("TL 2 Periods", 3, 1, 25, 1);
TL1 = LinearReg(C, p1);
TL2 = EMA(TL1, p2);
Col1 = IIf(TL1 > TL2, ParamColor("TL Up Colour", colorGreen), ParamColor("TL Dn Colour", colorRed));
Plot(TL1, "TriggerLine 1", Col1, styleLine|styleThick|styleNoLabel);
Plot(TL2, "TriggerLine 2", Col1, styleLine|styleThick|styleNoLabel);
_SECTION_END();



//================================================En d Chart Configuration===================================== ==========================================


//====================================Start of Linear Regression Code============================================== ====================================

P = ParamField("Price field",-1);

Length = 150;

Daysback = Param("Period for Liner Regression Line",Length,1,240,1);
shift = Param("Look back period",0,0,240,1);

//=============================== Math Formula ================================================== ================================================== ====

x = Cum(1);
lastx = LastValue( x ) - shift;
aa = LastValue( Ref(LinRegIntercept( p, Daysback), -shift) );
bb = LastValue( Ref(LinRegSlope( p, Daysback ), -shift) );
y = Aa + bb * ( x - (Lastx - DaysBack +1 ) );

//==================Plot the Linear Regression Line ================================================== ==================================================

LRColor = ParamColor("LR Color", colorCycle );
LRStyle = ParamStyle("LR Style");

LRLine = IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y, Null );

LRStyle = ParamStyle("LR Style");
Angle = Param("Angle", 0.05, 0, 1.5, 0.01);// A slope higher than 0.05 radians will turn green, less than -0.05 will turn red and anything in between will be white.

LRLine = IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y, Null );

Pi = 3.14159265 * atan(1); // Pi
SlopeAngle = atan(bb)*(180/Pi);

LineUp = SlopeAngle > Angle;
LineDn = SlopeAngle < - Angle;

if(LineUp)
{
Plot(LRLine, "Lin. Reg. Line Up", IIf(LineUp, colorBrightGreen, colorWhite), LRStyle);
}
else
{
Plot(LRLine, "Lin. Reg. Line Down", IIf(LineDn, colorDarkRed, colorWhite), LRStyle);
}




//========================== Plot 1st SD Channel ================================================== ================================================== ==

SDP = Param("Standard Deviation", 1.5, 0, 6, 0.1);
SD = SDP/2;

width = LastValue( Ref(SD*StDev(p, Daysback),-shift) ); //Set width of inside chanels here.
SDU = IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y+width , Null ) ;
SDL = IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y-width , Null ) ;

SDColor = ParamColor("SD Color", colorCycle );
SDStyle = ParamStyle("SD Style");

Plot( SDU , "Upper Lin Reg", colorRed,SDStyle ); //Inside Regression Lines
Plot( SDL , "Lower Lin Reg", colorLime,SDStyle ); //Inside Regression Lines

//========================== Plot 2d SD Channel ================================================== ================================================== ====

SDP2 = Param("2d Standard Deviation", 2.0, 0, 6, 0.1);
SD2 = SDP2/2;

width2 = LastValue( Ref(SD2*StDev(p, Daysback),-shift) ); //Set width of outside chanels here.
SDU2 = IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y+width2 , Null ) ;
SDL2 = IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y-width2 , Null ) ;

SDColor2 = ParamColor("2 SD Color", colorCycle );
SDStyle2 = ParamStyle("2 SD Style");

Plot( SDU2 , "Upper Lin Reg", colorRed,SDStyle2 ); //OutSide Regression Lines
Plot( SDL2 , "Lower Lin Reg", colorLime,SDStyle2 ); //OutSide Regression Lines

Trend = IIf(LRLine > Ref(LRLine,-1),colorGreen,colorRed);//Changes LR line to green if sloping up and red if sloping down.

Plot( LRLine , "LinReg", Trend, LRSTYLE );

//============================ End Indicator Code =========


/* **********************************

Code to automatically identify pivots

********************************** */

// -- what will be our lookback range for the hh and ll?
farback=Param("How Far back to go",300,50,5000,10);
nBars = Param("Number of bars", 13, 1, 40);

// -- Title.

Title = Name() ;

// -- Plot basic candle chart

PlotOHLC(Open, High, Low, Close,

"BIdx = " + BarIndex() +

"\n" + "O = " + O + "\n"+"H = "+ H + "\n"+"L = " + L

+ "\n"+"C ",

colorBlack, styleBar);

GraphXSpace=7;

// -- Create 0-initialized arrays the size of barcount

aHPivs = H - H;

aLPivs = L - L;

// -- More for future use, not necessary for basic plotting

aHPivHighs = H - H;

aLPivLows = L - L;

aHPivIdxs = H - H;

aLPivIdxs = L - L;

nHPivs = 0;

nLPivs = 0;

lastHPIdx = 0;

lastLPIdx = 0;

lastHPH = 0;

lastLPL = 0;

curPivBarIdx = 0;

// -- looking back from the current bar, how many bars

// back were the hhv and llv values of the previous

// n bars, etc.?

aHHVBars = HHVBars(H, nBars);

aLLVBars = LLVBars(C, nBars);

aHHV = HHV(H, nBars);

aLLV = LLV(L, nBars);

// -- Would like to set this up so pivots are calculated back from

// last visible bar to make it easy to "go back" and see the pivots

// this code would find. However, the first instance of

// _Trace output will show a value of 0

aVisBars = Status("barvisible");

nLastVisBar = LastValue(Highest(IIf(aVisBars, BarIndex(), 0)));

_TRACE("Last visible bar: " + nLastVisBar);

// -- Initialize value of curTrend

curBar = (BarCount-1);

curTrend = "";

if (aLLVBars[curBar] <

aHHVBars[curBar]) {

curTrend = "D";

}

else {

curTrend = "U";

}

// -- Loop through bars. Search for

// entirely array-based approach

// in future version

for (i=0; i<farback; i++) {

curBar = (BarCount - 1) - i;

// -- Have we identified a pivot? If trend is down...

if (aLLVBars[curBar] < aHHVBars[curBar]) {

// ... and had been up, this is a trend change

if (curTrend == "U") {

curTrend = "D";

// -- Capture pivot information

curPivBarIdx = curBar - aLLVBars[curBar];

aLPivs[curPivBarIdx] = 1;

aLPivLows[nLPivs] = L[curPivBarIdx];

aLPivIdxs[nLPivs] = curPivBarIdx;

nLPivs++;

}

// -- or current trend is up

} else {

if (curTrend == "D") {

curTrend = "U";

curPivBarIdx = curBar - aHHVBars[curBar];

aHPivs[curPivBarIdx] = 1;

aHPivHighs[nHPivs] = H[curPivBarIdx];

aHPivIdxs[nHPivs] = curPivBarIdx;

nHPivs++;

}

// -- If curTrend is up...else...

}

// -- loop through bars

}

// -- Basic attempt to add a pivot this logic may have missed

// -- OK, now I want to look at last two pivots. If the most

// recent low pivot is after the last high, I could

// still have a high pivot that I didn't catch

// -- Start at last bar

curBar = (BarCount-1);

candIdx = 0;

candPrc = 0;

lastLPIdx = aLPivIdxs[0];

lastLPL = aLPivLows[0];

lastHPIdx = aHPivIdxs[0];

lastHPH = aHPivHighs[0];

if (lastLPIdx > lastHPIdx) {

// -- Bar and price info for candidate pivot

candIdx = curBar - aHHVBars[curBar];

candPrc = aHHV[curBar];

if (

lastHPH < candPrc AND

candIdx > lastLPIdx AND

candIdx < curBar) {


// -- OK, we'll add this as a pivot...

aHPivs[candIdx] = 1;

// ...and then rearrange elements in the

// pivot information arrays

for (j=0; j<nHPivs; j++) {

aHPivHighs[nHPivs-j] = aHPivHighs[nHPivs-

(j+1)];

aHPivIdxs[nHPivs-j] = aHPivIdxs[nHPivs-(j+1)];

}

aHPivHighs[0] = candPrc ;

aHPivIdxs[0] = candIdx;

nHPivs++;

}

} else {


// -- Bar and price info for candidate pivot

candIdx = curBar - aLLVBars[curBar];

candPrc = aLLV[curBar];

if (

lastLPL > candPrc AND

candIdx > lastHPIdx AND

candIdx < curBar) {


// -- OK, we'll add this as a pivot...

aLPivs[candIdx] = 1;

// ...and then rearrange elements in the

// pivot information arrays

for (j=0; j<nLPivs; j++) {

aLPivLows[nLPivs-j] = aLPivLows[nLPivs-(j+1)];

aLPivIdxs[nLPivs-j] = aLPivIdxs[nLPivs-(j+1)];

}

aLPivLows[0] = candPrc;

aLPivIdxs[0] = candIdx;

nLPivs++;

}

}

// -- Dump inventory of high pivots for debugging

/*

for (k=0; k<nHPivs; k++) {

_TRACE("High pivot no. " + k

+ " at barindex: " + aHPivIdxs[k] + ", "

+ WriteVal(ValueWhen(BarIndex()==aHPivIdxs[k],

DateTime(), 1), formatDateTime)

+ ", " + aHPivHighs[k]);

}

*/

// -- OK, let's plot the pivots using arrows

PlotShapes(

IIf(aHPivs==1, shapeHollowDownArrow, shapeNone), colorRed, 0,

High, Offset=-15);

PlotShapes(

IIf(aLPivs==1, shapeHollowUpArrow , shapeNone), colorBrightGreen, 0,

Low, Offset=-15);
_SECTION_END();


_SECTION_BEGIN("Magnified Market Price");
FS=Param("Font Size",21,11,100,1);
GfxSelectFont("Times New Roman", FS, 700, True );
GfxSetBkMode( colorWhite );
GfxSetTextColor( ParamColor("Color",colorBrightGreen) );
Hor=Param("Horizontal Position",1,1,1200,1);
Ver=Param("Vertical Position",7,1,830,1);
GfxTextOut(""+C, Hor , Ver );
YC=TimeFrameGetPrice("C",inDaily,-1);
DD=Prec(C-YC,2);
xx=Prec((DD/YC)*100,2);
GfxSelectFont("Times New Roman", 12, 700, True );
GfxSetBkMode( colorWhite );
GfxSetTextColor(ParamColor("Color",colorGreen) );
GfxTextOut(""+DD+" ("+xx+"%)", Hor , Ver+30 );
GraphXSpace=-5;
_SECTION_END();
 

Nehal_s143

Well-Known Member
try this code

Filter = Buy OR Sell;
AddColumn( Close, "Close " );
AddColumn( Volume, "Volume " );

Please add exploration to this afl. Thanks in advance.

HTML:
_SECTION_BEGIN("ZeroLag MACD"); 
/*Xero Lag MACD(p,q,r)*/
//based on ZeroLag EMA - see Technical Analysis of Stocks and Commodities, April 2000
p = Param("P",12,3,36,2);
q = Param("Q",26,3,52,2);
r = Param("R",9,3,15,1);

EMA1= EMA(Close,p);
EMA2= EMA(EMA1,p);
Difference= EMA1 - EMA2;
ZeroLagEMAp= EMA1 + Difference;
//---------------------------------------
EMA1= EMA(Close,q);
EMA2= EMA(EMA1,q);
Difference= EMA1 - EMA2;
ZeroLagEMAq= EMA1 + Difference;
//---------------------------------------
ZeroLagMACD=ZeroLagEMAp - ZeroLagEMAq;
//---------------------------------------
// Signal line
EMA1= EMA(ZeroLagMACD,r);
EMA2= EMA(EMA1,r);
Difference= EMA1 - EMA2;
ZeroLagTRIG= EMA1 + Difference;

Plot(zerolagMACD,"",colorRed,styleThick+styleDots);
Plot(zerolagtrig,"",colorBlue,styleDashed);

a = round (zerolagMACD*100)/100;
a1 = Ref(a,-1);
diff1 = round((a - a1)*10)/10;
diff2 = round((a1 - Ref(a,-2))*10)/10;

Title =
EncodeColor(colorYellow)+"MACD              : "+ WriteIf(a > 0,  EncodeColor(colorGreen)+a,EncodeColor(colorRed)+a)
+ WriteIf(diff1 > 0, EncodeColor(colorGreen)+"  Up by "+diff1,EncodeColor(colorRed)+"  Dn by "+diff1)
+"\n"
+EncodeColor(colorYellow)+ "One bar ago   : "+ WriteIf(a1 > 0, EncodeColor(colorGreen)+a1,EncodeColor(colorRed)+a1)
+ WriteIf(diff2 > 0, EncodeColor(colorGreen)+"  Up by "+diff2,EncodeColor(colorRed)+"  Dn by "+diff2)
;
 

//===========================end zeroLagMACD
_SECTION_END();
 
Friends,

I'm new to AFL coding and I would like to make a system that identifies the candlestick patterns and set the buy n sell rules based on the patterns. I have scanned many of the websites and got the AFL that identifies the pattern, but I need it in a different manner. For ex. Lets say we have a Bullish Engulfing pattern formed i.e. this pattern involves two candles. All the current candlestick pattern indicators/ codes that I have found is, the Bullish engulfing pattern identified on the current candle, but I need as follows:
1. When the pattern is formed and two candles are involved, then I want to highlight the pattern say drawing a circle/ rectangle around the candles (in this case bullish engulfing involves two candles, so I want the rectangle to be around both the candles that forms the pattern.

2. Put an buy/ sell arrow near that pattern

3. Set the buy/ sell rules based on the pattern

Can we achieve my requirement using AFL? If yes, then please guide me.

I'm not sure whether this is the right thread to place this query, but since it s dealing with AFL, I'm posting it here.

Thanks
 

Nehal_s143

Well-Known Member
Dear Seniors,

I'm looking for two AFL... MTF ICHIMOKU & MTF SUPERTREND. Could you please help me with the same

Best Regards
Annhik

try this, also google first before putting request for popular afls


Code:
/* Done      by    Rajandran R */
/* Author of www.marketcalls.in  */

_SECTION_BEGIN("SuperTrend 5min");

_N(Title = "MultiTimeframe Signal DashBoard");


TimeFrameSet(in5Minute);
SetBarsRequired(100000,0);
GraphXSpace = 15;
SetChartBkColor(ParamColor("bkcolor",ColorRGB(0,0, 0)));
GfxSetBkMode(0); 
GfxSetOverlayMode(1);
SetBarFillColor(IIf(C>O,ParamColor("Candle UP Color", colorGreen),IIf(C<=O,ParamColor("Candle Down Color", colorRed),colorLightGrey)));
//Plot(C,"\nPrice",IIf(C>O,ParamColor("Wick UP Color", colorDarkGreen),IIf(C<=O,ParamColor("Wick Down Color", colorDarkRed),colorLightGrey)),64,0,0,0,0);

SetTradeDelays(1,1,1,1);
PositionSize=100000;


symbol = Name();
Factor=Optimize("Factor",4,2,10,1);
Pd=Optimize("ATR Period",10,3,20,1);

Up=(H+L)/2+(Factor*ATR(Pd));
Dn=(H+L)/2-(Factor*ATR(Pd));
iATR=ATR(Pd);
TrendUp=TrendDown=Null;
trend[0]=1;
changeOfTrend=0;
flag=flagh=0;

for (i = 1; i <BarCount; i++) {
      TrendUp[i] = Null;
      TrendDown[i] = Null;
     
      trend[i]=1;
   
      
      if (Close[i]>Up[i-1]) {
         trend[i]=1;
         if (trend[i-1] == -1) changeOfTrend = 1;
         
      }
      else if (Close[i]<Dn[i-1]) {
         trend[i]=-1;
         if (trend[i-1] == 1) changeOfTrend = 1;
      }
      else if (trend[i-1]==1) {
         trend[i]=1;
         changeOfTrend = 0;       
      }
      else if (trend[i-1]==-1) {
         trend[i]=-1;
         changeOfTrend = 0;
      }

      if (trend[i]<0 && trend[i-1]>0) {
         flag=1;
      }
      else {
         flag=0;
      }
      
      if (trend[i]>0 && trend[i-1]<0) {
         flagh=1;
      }
      else {
         flagh=0;
      }
      
      if (trend[i]>0 && Dn[i]<Dn[i-1]){
         Dn[i]=Dn[i-1];
		}
      
      if (trend[i]<0 && Up[i]>Up[i-1])
        { Up[i]=Up[i-1];
		}
      
      if (flag==1)
       {  Up[i]=(H[i]+L[i])/2+(Factor*iATR[i]);;
        } 
      if (flagh==1)
        { Dn[i]=(H[i]+L[i])/2-(Factor*iATR[i]);;
         }
      if (trend[i]==1) {
         TrendUp[i]=Dn[i];
         if (changeOfTrend == 1) {
            TrendUp[i-1] = TrendDown[i-1];
            changeOfTrend = 0;
         }
      }
      else if (trend[i]==-1) {
         TrendDown[i]=Up[i];
         if (changeOfTrend == 1) {
            TrendDown[i-1] = TrendUp[i-1];
            changeOfTrend = 0;
         }
      }
   } 

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

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


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




TrendSL=IIf(trend==1,TrendUp,TrendDown);

for(i=BarCount-1;i>1;i--)
{
if(Buy[i] == 1)
{
entry = C[i];
sig = "BUY";
sl = TrendSL[i];
tar1 = entry + (entry * .0050);
tar2 = entry + (entry * .0092);
tar3 = entry + (entry * .0179);
 
bars = i;
i = 0;
}
if(Sell[i] == 1)
{
sig = "SELL";
entry = C[i];
sl = TrendSL[i];
tar1 = entry - (entry * .0050);
tar2 = entry - (entry * .0112);
tar3 = entry - (entry * .0212);
 
 
bars = i;
i = 0;
}
}
Offset = 20;
Clr = IIf(sig == "BUY", colorLime, colorRed);
ssl = IIf(bars == BarCount-1, TrendSL[BarCount-1], Ref(TrendSL, -1));
sl = ssl[BarCount-1];
 
 
messageboard = ParamToggle("Message Board","Show|Hide",1);
if (messageboard == 1 )
{
GfxSelectFont( "Tahoma", 13, 100 );
GfxSetBkMode( 1 );
GfxSetTextColor( colorWhite );
 
if ( sig =="BUY")
{
GfxSelectSolidBrush( colorBlue ); // this is the box background color
}
else
{
GfxSelectSolidBrush( colorRed ); // this is the box background color
}
pxHeight = Status( "pxchartheight" ) ;
xx = Status( "pxchartwidth");
Left = 1100;
width = 310;
x = 5;
x2 = 290;
 
y = pxHeight;
 
GfxSelectPen( colorGreen, 1); // broader color
GfxRoundRect( x, y - 98, x2, y , 7, 7 ) ;
GfxTextOut( ( "Marketcalls - Supertrend 5min"),13,y-100);
GfxTextOut( (" "),27,y-100);
GfxTextOut( ("Last " + sig + " Signal came " + (BarCount-bars-1) * Interval()/60 + " mins ago"), 13, y-80) ; // The text format location
GfxTextOut( ("" + WriteIf(sig =="BUY",sig + " @ ",sig + " @") + " : " + entry), 13, y-60);
GfxTextOut( ("Trailing SL : " + Ref(TrendSL,-1) + " (" + WriteVal(IIf(sig == "SELL",entry-sl,sl-entry), 2.2) + ")"), 13, y-40);
/*GfxTextOut( ("TGT:1 : " + tar1), 13, y -80);
GfxTextOut( ("TGT:2 : " + tar2), 13,y-60);
GfxTextOut( ("TGT:3 : " + tar3), 13,y-40);*/
GfxTextOut( ("Current P/L : " + WriteVal(IIf(sig == "BUY",(C-entry),(entry-C)),2.2)), 13, y-22);;

}

TimeFrameRestore();

_SECTION_END();


_SECTION_BEGIN("SuperTrend 15min");

TimeFrameSet(in5Minute*3);
SetBarsRequired(100000,0);
GraphXSpace = 15;
SetChartBkColor(ParamColor("bkcolor",ColorRGB(0,0, 0)));
GfxSetBkMode(0); 
GfxSetOverlayMode(1);
SetBarFillColor(IIf(C>O,ParamColor("Candle UP Color", colorGreen),IIf(C<=O,ParamColor("Candle Down Color", colorRed),colorLightGrey)));
//Plot(C,"\nPrice",IIf(C>O,ParamColor("Wick UP Color", colorDarkGreen),IIf(C<=O,ParamColor("Wick Down Color", colorDarkRed),colorLightGrey)),64,0,0,0,0);

SetTradeDelays(1,1,1,1);
PositionSize=100000;


symbol = Name();
Factor=Optimize("Factor",4,2,10,1);
Pd=Optimize("ATR Period",10,3,20,1);


Up=(H+L)/2+(Factor*ATR(Pd));
Dn=(H+L)/2-(Factor*ATR(Pd));
iATR=ATR(Pd);
TrendUp=TrendDown=Null;
trend[0]=1;
changeOfTrend=0;
flag=flagh=0;

for (i = 1; i <BarCount; i++) {
      TrendUp[i] = Null;
      TrendDown[i] = Null;
     
      trend[i]=1;
   
      
      if (Close[i]>Up[i-1]) {
         trend[i]=1;
         if (trend[i-1] == -1) changeOfTrend = 1;
         
      }
      else if (Close[i]<Dn[i-1]) {
         trend[i]=-1;
         if (trend[i-1] == 1) changeOfTrend = 1;
      }
      else if (trend[i-1]==1) {
         trend[i]=1;
         changeOfTrend = 0;       
      }
      else if (trend[i-1]==-1) {
         trend[i]=-1;
         changeOfTrend = 0;
      }

      if (trend[i]<0 && trend[i-1]>0) {
         flag=1;
      }
      else {
         flag=0;
      }
      
      if (trend[i]>0 && trend[i-1]<0) {
         flagh=1;
      }
      else {
         flagh=0;
      }
      
      if (trend[i]>0 && Dn[i]<Dn[i-1]){
         Dn[i]=Dn[i-1];
		}
      
      if (trend[i]<0 && Up[i]>Up[i-1])
        { Up[i]=Up[i-1];
		}
      
      if (flag==1)
       {  Up[i]=(H[i]+L[i])/2+(Factor*iATR[i]);;
        } 
      if (flagh==1)
        { Dn[i]=(H[i]+L[i])/2-(Factor*iATR[i]);;
         }
      if (trend[i]==1) {
         TrendUp[i]=Dn[i];
         if (changeOfTrend == 1) {
            TrendUp[i-1] = TrendDown[i-1];
            changeOfTrend = 0;
         }
      }
      else if (trend[i]==-1) {
         TrendDown[i]=Up[i];
         if (changeOfTrend == 1) {
            TrendDown[i-1] = TrendUp[i-1];
            changeOfTrend = 0;
         }
      }
   } 

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

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


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




TrendSL=IIf(trend==1,TrendUp,TrendDown);

for(i=BarCount-1;i>1;i--)
{
if(Buy[i] == 1)
{
entry = C[i];
sig = "BUY";
sl = TrendSL[i];
tar1 = entry + (entry * .0050);
tar2 = entry + (entry * .0092);
tar3 = entry + (entry * .0179);
 
bars = i;
i = 0;
}
if(Sell[i] == 1)
{
sig = "SELL";
entry = C[i];
sl = TrendSL[i];
tar1 = entry - (entry * .0050);
tar2 = entry - (entry * .0112);
tar3 = entry - (entry * .0212);
 
 
bars = i;
i = 0;
}
}
Offset = 20;
Clr = IIf(sig == "BUY", colorLime, colorRed);
ssl = IIf(bars == BarCount-1, TrendSL[BarCount-1], Ref(TrendSL, -1));
sl = ssl[BarCount-1];
 
 
messageboard = ParamToggle("Message Board","Show|Hide",1);
if (messageboard == 1 )
{
GfxSelectFont( "Tahoma", 13, 100 );
GfxSetBkMode( 1 );
GfxSetTextColor( colorWhite );
 
if ( sig =="BUY")
{
GfxSelectSolidBrush( colorBlue ); // this is the box background color
}
else
{
GfxSelectSolidBrush( colorRed ); // this is the box background color
}
pxHeight = Status( "pxchartheight" ) ;
xx = Status( "pxchartwidth");
Left = 1100;
width = 310;
x = 305;
x2 = 590;
 
y = pxHeight;
 
GfxSelectPen( colorGreen, 1); // broader color
GfxRoundRect( x, y - 98, x2, y , 7, 7 ) ;
GfxTextOut( ( "Marketcalls - Supertrend 15min"),313,y-100);
GfxTextOut( (" "),327,y-100);
GfxTextOut( ("Last " + sig + " Signal came " + (BarCount-bars-1) * Interval()/60 + " mins ago"), 313, y-80) ; // The text format location
GfxTextOut( ("" + WriteIf(sig =="BUY",sig + " @ ",sig + " @") + " : " + entry), 313, y-60);
GfxTextOut( ("Trailing SL : " + Ref(TrendSL,-1) + " (" + WriteVal(IIf(sig == "SELL",entry-sl,sl-entry), 2.2) + ")"), 313, y-40);
/*GfxTextOut( ("TGT:1 : " + tar1), 13, y -80);
GfxTextOut( ("TGT:2 : " + tar2), 13,y-60);
GfxTextOut( ("TGT:3 : " + tar3), 13,y-40);*/
GfxTextOut( ("Current P/L : " + WriteVal(IIf(sig == "BUY",(C-entry),(entry-C)),2.2)), 313, y-22);;

}
TimeFrameRestore();

_SECTION_END();



_SECTION_BEGIN("SuperTrend Hourly");

TimeFrameSet(inHourly);
SetBarsRequired(100000,0);
GraphXSpace = 15;
SetChartBkColor(ParamColor("bkcolor",ColorRGB(0,0, 0)));
GfxSetBkMode(0); 
GfxSetOverlayMode(1);
SetBarFillColor(IIf(C>O,ParamColor("Candle UP Color", colorGreen),IIf(C<=O,ParamColor("Candle Down Color", colorRed),colorLightGrey)));
//Plot(C,"\nPrice",IIf(C>O,ParamColor("Wick UP Color", colorDarkGreen),IIf(C<=O,ParamColor("Wick Down Color", colorDarkRed),colorLightGrey)),64,0,0,0,0);

SetTradeDelays(1,1,1,1);
PositionSize=100000;


symbol = Name();
Factor=Optimize("Factor",4,2,10,1);
Pd=Optimize("ATR Period",10,3,20,1);

Up=(H+L)/2+(Factor*ATR(Pd));
Dn=(H+L)/2-(Factor*ATR(Pd));
iATR=ATR(Pd);
TrendUp=TrendDown=Null;
trend[0]=1;
changeOfTrend=0;
flag=flagh=0;

for (i = 1; i <BarCount; i++) {
      TrendUp[i] = Null;
      TrendDown[i] = Null;
     
      trend[i]=1;
   
      
      if (Close[i]>Up[i-1]) {
         trend[i]=1;
         if (trend[i-1] == -1) changeOfTrend = 1;
         
      }
      else if (Close[i]<Dn[i-1]) {
         trend[i]=-1;
         if (trend[i-1] == 1) changeOfTrend = 1;
      }
      else if (trend[i-1]==1) {
         trend[i]=1;
         changeOfTrend = 0;       
      }
      else if (trend[i-1]==-1) {
         trend[i]=-1;
         changeOfTrend = 0;
      }

      if (trend[i]<0 && trend[i-1]>0) {
         flag=1;
      }
      else {
         flag=0;
      }
      
      if (trend[i]>0 && trend[i-1]<0) {
         flagh=1;
      }
      else {
         flagh=0;
      }
      
      if (trend[i]>0 && Dn[i]<Dn[i-1]){
         Dn[i]=Dn[i-1];
		}
      
      if (trend[i]<0 && Up[i]>Up[i-1])
        { Up[i]=Up[i-1];
		}
      
      if (flag==1)
       {  Up[i]=(H[i]+L[i])/2+(Factor*iATR[i]);;
        } 
      if (flagh==1)
        { Dn[i]=(H[i]+L[i])/2-(Factor*iATR[i]);;
         }
      if (trend[i]==1) {
         TrendUp[i]=Dn[i];
         if (changeOfTrend == 1) {
            TrendUp[i-1] = TrendDown[i-1];
            changeOfTrend = 0;
         }
      }
      else if (trend[i]==-1) {
         TrendDown[i]=Up[i];
         if (changeOfTrend == 1) {
            TrendDown[i-1] = TrendUp[i-1];
            changeOfTrend = 0;
         }
      }
   } 

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

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


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




TrendSL=IIf(trend==1,TrendUp,TrendDown);

for(i=BarCount-1;i>1;i--)
{
if(Buy[i] == 1)
{
entry = C[i];
sig = "BUY";
sl = TrendSL[i];
tar1 = entry + (entry * .0050);
tar2 = entry + (entry * .0092);
tar3 = entry + (entry * .0179);
 
bars = i;
i = 0;
}
if(Sell[i] == 1)
{
sig = "SELL";
entry = C[i];
sl = TrendSL[i];
tar1 = entry - (entry * .0050);
tar2 = entry - (entry * .0112);
tar3 = entry - (entry * .0212);
 
 
bars = i;
i = 0;
}
}
Offset = 20;
Clr = IIf(sig == "BUY", colorLime, colorRed);
ssl = IIf(bars == BarCount-1, TrendSL[BarCount-1], Ref(TrendSL, -1));
sl = ssl[BarCount-1];
 
 
messageboard = ParamToggle("Message Board","Show|Hide",1);
if (messageboard == 1 )
{
GfxSelectFont( "Tahoma", 13, 100 );
GfxSetBkMode( 1 );
GfxSetTextColor( colorWhite );
 
if ( sig =="BUY")
{
GfxSelectSolidBrush( colorBlue ); // this is the box background color
}
else
{
GfxSelectSolidBrush( colorRed ); // this is the box background color
}
pxHeight = Status( "pxchartheight" ) ;
xx = Status( "pxchartwidth");
Left = 1100;
width = 310;
x = 605;
x2 = 890;
 
y = pxHeight;
 
GfxSelectPen( colorGreen, 1); // broader color
GfxRoundRect( x, y - 98, x2, y , 7, 7 ) ;
GfxTextOut( ( "Marketcalls - Supertrend Hourly"),613,y-100);
GfxTextOut( (" "),327,y-100);
GfxTextOut( ("Last " + sig + " Signal came " + (BarCount-bars-1) * Interval()/60 + " mins ago"), 613, y-80) ; // The text format location
GfxTextOut( ("" + WriteIf(sig =="BUY",sig + " @ ",sig + " @") + " : " + entry), 613, y-60);
GfxTextOut( ("Trailing SL : " + Ref(TrendSL,-1) + " (" + WriteVal(IIf(sig == "SELL",entry-sl,sl-entry), 2.2) + ")"), 613, y-40);
/*GfxTextOut( ("TGT:1 : " + tar1), 13, y -80);
GfxTextOut( ("TGT:2 : " + tar2), 13,y-60);
GfxTextOut( ("TGT:3 : " + tar3), 13,y-40);*/
GfxTextOut( ("Current P/L : " + WriteVal(IIf(sig == "BUY",(C-entry),(entry-C)),2.2)), 613, y-22);;

}
TimeFrameRestore();

_SECTION_END();
 

vikas2131

Well-Known Member
Would somebody be kind enough to convert the below mentioned mt4 code to amibroker..the orignal name of indicator is Dynamic Gains

#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 LightBlue
#property indicator_color2 Orange
#property indicator_color3 LightBlue
#property indicator_color4 Orange

extern int MA_Period = 3;
extern int MA_Method = 2;
extern int UseSignal = 1;
extern int AlertMode = 1;
extern int WarningMode = 1;
extern int UpArrowCode = 233;
extern int DownArrowCode = 234;

double g_ibuf_96[];
double g_ibuf_100[];
double g_ibuf_104[];
double g_ibuf_108[];
double g_ibuf_112[];
double g_ibuf_116[];
double g_ibuf_120[];
int gi_124 = 0;
bool gi_128 = FALSE;
bool gi_132 = FALSE;

int init() {
string ls_unused_4;
IndicatorBuffers(7);
SetIndexStyle(0, DRAW_LINE);
SetIndexStyle(1, DRAW_LINE);
SetIndexStyle(2, DRAW_ARROW);
SetIndexArrow(2, UpArrowCode);
SetIndexStyle(3, DRAW_ARROW);
SetIndexArrow(3, DownArrowCode);
IndicatorDigits(MarketInfo(Symbol(), MODE_DIGITS));
if (MA_Period < 2) MA_Period = 10;
int li_0 = MA_Period - 1;
IndicatorShortName("Dynamic Gains (" + MA_Period + ")");
SetIndexLabel(0, "UpTrendEnv");
SetIndexLabel(1, "DnTrendEnv");
SetIndexLabel(2, "UpSignal");
SetIndexLabel(3, "DnSignal");
SetIndexDrawBegin(0, li_0);
SetIndexDrawBegin(1, li_0);
SetIndexDrawBegin(2, li_0);
SetIndexDrawBegin(3, li_0);
SetIndexBuffer(0, g_ibuf_96);
SetIndexBuffer(1, g_ibuf_100);
SetIndexBuffer(2, g_ibuf_104);
SetIndexBuffer(3, g_ibuf_108);
SetIndexBuffer(4, g_ibuf_112);
SetIndexBuffer(5, g_ibuf_116);
SetIndexBuffer(6, g_ibuf_120);
return (0);
}

int start() {
string ls_8;
if (Bars <= MA_Period) return (0);
gi_124 = IndicatorCounted();
if (gi_124 < 0) return (-1);
if (gi_124 > 0) gi_124--;
int li_0 = Bars - gi_124;
for (int li_4 = li_0; li_4 >= 0; li_4--) {
g_ibuf_112[li_4] = iMA(NULL, 0, MA_Period, 0, MA_Method, PRICE_HIGH, li_4);
g_ibuf_116[li_4] = iMA(NULL, 0, MA_Period, 0, MA_Method, PRICE_LOW, li_4);
g_ibuf_120[li_4] = g_ibuf_120[li_4 + 1];
if (Close[li_4] > g_ibuf_112[li_4 + 1]) g_ibuf_120[li_4] = 1;
if (Close[li_4] < g_ibuf_116[li_4 + 1]) g_ibuf_120[li_4] = -1;
if (g_ibuf_120[li_4] > 0.0) {
if (g_ibuf_116[li_4] < g_ibuf_116[li_4 + 1]) g_ibuf_116[li_4] = g_ibuf_116[li_4 + 1];
g_ibuf_96[li_4] = g_ibuf_116[li_4];
if (UseSignal > 0) {
if (g_ibuf_120[li_4 + 1] < 0.0) {
g_ibuf_104[li_4] = g_ibuf_116[li_4];
if (WarningMode > 0 && li_4 == 0) PlaySound("alert2.wav");
} else g_ibuf_104[li_4] = EMPTY_VALUE;
}
g_ibuf_100[li_4] = EMPTY_VALUE;
g_ibuf_108[li_4] = EMPTY_VALUE;
} else {
if (g_ibuf_112[li_4] > g_ibuf_112[li_4 + 1]) g_ibuf_112[li_4] = g_ibuf_112[li_4 + 1];
g_ibuf_100[li_4] = g_ibuf_112[li_4];
if (UseSignal > 0) {
if (g_ibuf_120[li_4 + 1] > 0.0) {
g_ibuf_108[li_4] = g_ibuf_112[li_4];
if (WarningMode > 0 && li_4 == 0) PlaySound("alert2.wav");
} else g_ibuf_108[li_4] = EMPTY_VALUE;
}
g_ibuf_96[li_4] = EMPTY_VALUE;
g_ibuf_104[li_4] = EMPTY_VALUE;
}
}
if (g_ibuf_120[2] < 0.0 && g_ibuf_120[1] > 0.0 && Volume[0] > 1.0 && !gi_128) {
ls_8 = " " + Symbol() + " M" + Period() + ": - CONFIRMED BUY ENTRY - ";
if (AlertMode > 0) Alert(ls_8);
gi_128 = TRUE;
gi_132 = FALSE;
}
if (g_ibuf_120[2] > 0.0 && g_ibuf_120[1] < 0.0 && Volume[0] > 1.0 && !gi_132) {
ls_8 = " " + Symbol() + " M" + Period() + ": - CONFIRMED SELL ENTRY - ";
if (AlertMode > 0) Alert(ls_8);
gi_132 = TRUE;
gi_128 = FALSE;
}
return (0);
}
 

Similar threads