Wednesday, January 8, 2014

Amibroker AFL for Trend Lines

Amibroker AFL for plotting Trend Lines automatically.

Trend Lines are one of the most useful tool in identifying areas of Supports & Resistance. It takes time to learn in plotting Trend lines in a more professional way.

This AFL code helps a lot in marking important trend lines. New users can benefit a lot in learning the process of marking and plotting trend lines with the help of this AFL.

Copy the Code from the below Code Box. You may refer to the image below the code box to look how the Trend lines plotted by this AFL look on the price chart.


_SECTION_BEGIN("Advanced Trend Lines");
function TD_Supply()
{
   return ( H > Ref(H, 1) AND H > Ref(H, -1) AND H > Ref(C, -2));
}
function TD_Demand()
{
   return ( L < Ref(L, 1) AND L < Ref(L, -1) AND L < Ref(C, -2));
}

function IsTD_Supply(n)
{
   n = (BarCount - 1) - n;
   return LastValue( Ref(H, -n) > Ref(H, -n+1) AND Ref(H, -n) > Ref(H, -n-1) AND Ref(H, -n) > Ref(C, -n-2));
}
function IsTD_Demand(n)
{
   n = (BarCount - 1) - n;
   return LastValue( Ref(L, -n) < Ref(L, -n+1) AND Ref(L, -n) < Ref(L, -n-1) AND Ref(L, -n) < Ref(C, -n-2));
}
function GetXSupport(Lo, Percentage, Back)
{
 return ((BarCount - 1) - LastValue(TroughBars(Lo, Percentage,Back)));
}
function GetYSupport(Lo, Percentage, Back)
{
 return (LastValue(Trough(Lo, Percentage, back)));
}

function GetXResistance(Hi, Percentage, Back)
{
 return ((BarCount - 1) -LastValue(PeakBars(Hi, Percentage, Back)));
}
function GetYResistance(Hi, Percentage, Back)
{
 return (LastValue(Peak(Hi, Percentage, Back)));
}
////////////////////////////////////////////////////////////////////////
//Parameters
Percentage    = Param("Percentage", 0.5, 0.01, 100. ,0.01);
Lines          = Param("Lines?", 5, 1, BarCount-2);
DrawR          = ParamList("Resistance Points", "Off|High to High|High to Low", 1);
DrawS          = ParamList("Support Points", "Off|Low to Low|Low to High", 1);
DrawAllLines    = ParamToggle("Draw All Lines?", "No|Yes", 1);
Method       = ParamToggle("Method", "TD Points|ZigZag",1);
ShowTDP       = ParamToggle("Show TD Pionts", "No|Yes");
AllOrDownR    = ParamToggle("Resistance Direction", "All|Down");
AllOrUpS       = ParamToggle("Support Direction", "All|Up");
////////////////////////////////////////////////////////////////////////
Main = C;
Con = ConS = ConR = 1;
if(DrawS=="Low to Low")
{
   Support1 = L;
   Support2 = L;
}
else
{
   Support1 = L;
   Support2 = H;
}
if(DrawR=="High to High")
{
   Resistance1 = H;
   Resistance2 = H;
}
else
{
   Resistance1 = H;
   Resistance2 = L;
}
////////////////////////////////////////////////////////////////////////
//Plotting Area
Plot(Main, "", IIf(C>O,colorGreen, colorRed), styleBar);
if(DrawAllLines)
for(i = 2; i<=Lines+1; i++)
{
   if(DrawS!="Off")
   {
      x0 = GetXSupport(Support1, Percentage, i);
      x1 = GetXSupport(Support2, Percentage, i-1);
      y0 = GetYSupport(Support1, Percentage, i);
      y1 = GetYSupport(Support2, Percentage, i-1);
      x = LineArray(x0, y0, x1, y1, 1);
      if(!Method)
         Con = (IsTD_Demand(x0) AND IsTD_Demand(x1));
      if(AllOrUpS) ConS = y0 < y1;
      if(Con AND ConS)
         Plot(x, "", colorLightBlue, styleLine|styleThick);
   }
   if(DrawR!="Off")
   {
      x0 = GetXResistance(Resistance1, Percentage, i);
      x1 = GetXResistance(Resistance2, Percentage, i-1);
      y0 = GetYResistance(Resistance1, Percentage, i);
      y1 = GetYResistance(Resistance2, Percentage, i-1);
      x = LineArray(x0, y0, x1, y1, 1);
      if(!Method)
         Con = (IsTD_Supply(x0) AND IsTD_Supply(x1));
      if(AllOrDownR) ConR = y0 > y1;
      if(Con AND ConR)
         Plot(x, "", colorRed , styleLine|styleThick);
   }
}
else
{
   if(DrawS!="Off")
   {
      x0 = GetXSupport(Support1, Percentage, Lines+1);
      x1 = GetXSupport(Support2, Percentage, Lines);
      y0 = GetYSupport(Support1, Percentage, Lines+1);
      y1 = GetYSupport(Support2, Percentage, Lines);
      x = LineArray(x0, y0, x1, y1, 1);
      if(!Method)
         Con = (IsTD_Demand(x0) AND IsTD_Demand(x1));
      if(AllOrUpS) ConS = y0 < y1;
      if(Con AND ConS)
         Plot(x, "", colorLightBlue, styleLine|styleThick);
   }
   if(DrawR!="Off")
   {
      x0 = GetXResistance(Resistance1, Percentage, Lines+1);
      x1 = GetXResistance(Resistance2, Percentage, Lines);
      y0 = GetYResistance(Resistance1, Percentage, Lines+1);
      y1 = GetYResistance(Resistance2, Percentage, Lines);
      x = LineArray(x0, y0, x1, y1, 1);
      if(!Method)
         Con = (IsTD_Supply(x0) AND IsTD_Supply(x1));
      if(AllOrDownR) ConR = y0 > y1;
      if(Con AND ConR)
         Plot(x, "", colorRed , styleLine|styleThick);
   }
}

if(ShowTDP)
{
   PlotShapes(TD_Supply()*shapeSmallCircle, colorRed, 0, H, H*.001);
   PlotShapes(TD_Demand()*shapeSmallCircle, colorGreen, 0, L, -L*.001);
}
Title =FullName()+" ({{NAME}})\n{{DATE}}\n"+"Open: "+O+", Hi: "+H+", Lo: "+L+", Close: "+C;

_SECTION_END();

There is a substantial risk of loss associated with trading in Stock Markets. Losses can and
will occur. No responsibility for loss occurred to any person acting or refraining to act as a result
of using the AFL written by their respective creators and published in this Blog for sharing of knowledge can be accepted by the Blog owner.



No comments:

Post a Comment

Required US Government Disclaimer & CTFC Rule 4.41

Futures trading contains substantial risk and is not suitable for every investor. An investor could potentially lose all or more than the initial investment. Risk capital is money that can be lost without jeopardizing ones financial security or lifestyle. Only consider risk capital that should be used for trading and only those with sufficient risk capital should consider trading. Past performance is not necessarily indicative of future results. CTFC RULE 4.41 – HYPOTHETICAL OR SIMULATED PERFORMANCE RESULTS HAVE CERTAIN LIMITATIONS. UNLIKE AN ACTUAL PERFORMANCE RECORD, SIMULATED RESULTS DO NOT REPRESENT ACTUAL TRADING. ALSO, SINCE THE TRADES HAVE NOT BEEN EXECUTED, THE RESULTS MAY HAVE UNDER-OR-OVER COMPENSATED FOR THE IMPACT, IF ANY, OF CERTAIN MARKET FACTORS SUCH AS LIQUIDITY. SIMULATED TRADING PROGRAMS IN GENERAL ARE ALSO SUBJECT TO THE FACT THAT THEY ARE DESIGNED WITH THE BENEFIT OF HINDSIGHT. NO REPRESENTATION IS BEING MADE THAT ANY ACCOUNT WILL OR IS LIKELY TO ACHIEVE PROFIT OR LOSSES SIMILAR TO THOSE SHOWN. All trades, patterns, charts, systems, etc., discussed in this website or advertisement are for illustrative purposes only and not construed as specific advisory recommendations. All ideas and materials presented herein are for information and educational purposes only. No system or trading methodology has ever been developed that can guarantee profits or prevent losses. The testimonials and examples used herein are exceptional results which do not apply to average people and are not intended to represent or guarantee that anyone will achieve the same or similar results. Trades placed on the reliance of Trend Methods systems are taken at your own risk for your own account. This is not an offer to buy or sell futures interests.