Tuesday, August 14, 2012

Automatic Fibonacci Levels AFL


Automatic Fibonacci Retracement Levels plotted in Amibroker.
This helps a lot to those who are starting in understanding Fibonacci adjustments.

Another aspect is that sometime it gets difficult to choose levels from where
the retracement lines should be plotted.

In this AFL levels can be adjusted through Parameters.
Number of Bars can be chosen and the Fibonacci levels can be adjusted as per
individual preferences.

======================================================================



_SECTION_BEGIN("AA - Fibonacci Retracements and Goals");
///////////////////////////////////////////////////
// **************** Parameters ********************
//
Period = Param("Period", 1, 1, 50);
ShowExt = ParamToggle("Show Extension ?", "No|Yes");
///////////////////////////////////////////////////
// **************** Calculations *****************
//
Period = Period*(13*.618);

Hhb = LastValue(Ref(HHVBars(H, Period), -1))+1;
Llb = LastValue(Ref(LLVBars(L, Period), -1))+1;
Hv = LastValue(Ref(HHV(H, Period), -1));
Lv = LastValue(Ref(LLV(L, Period), -1));

Range = (Hv - Lv);
LText = "0 %, 21.4 %, 38.2 %, 50 %, 61.8 %, 78.6 %, 100 %, 1.618 %, 1.72 %, 200 %";
if(Hhb > Llb)
{
    Levels[0] = Lv;
    Levels[1]= (Range *.214)+Levels[0];
    Levels[2]= (Range *.382)+Levels[0];
    Levels[3]= (Range *.5)+Levels[0];
    Levels[4]= (Range *.618)+Levels[0];
    Levels[5]= (Range *.786)+Levels[0];
    Levels[6]= Hv;
    Levels[7]= (Range *.618)+Levels[6];
    Levels[8]= (Range *.72)+Levels[6];
    Levels[9]= (Range )+Levels[6];
    x0 = BarCount - 1 - Llb;
    x1 = BarCount - 1 - Hhb;
}
else
{
    Levels[0]= Hv;
    Levels[1]= Levels[0]- (Range *.214);
    Levels[2]= Levels[0]- (Range *.382);
    Levels[3]= Levels[0]- (Range *.5);
    Levels[4]= Levels[0]- (Range *.618);
    Levels[5]= Levels[0]- (Range *.786);
    Levels[6]= Lv;
    Levels[7]= Levels[6]- (Range *.618);
    Levels[8]= Levels[6]- (Range *.72);
    Levels[9]= Levels[6]- (Range );
    x0 = BarCount - 1 - Hhb;
    x1 = BarCount - 1 - Llb;
}

//////////////////////////////////////////////////////////////////////////////////////
/// **************************** Plotting Area ********************************
//
Plot(C, "", IIf(C > O, colorBrightGreen, colorRed), ParamStyle("Price Style", styleBar, maskPrice));
for(i=0; i<10; i++)
{
    if(i!=6)
        x = x0;
    else
        x = x1;
    if(i<7 OR ShowExt)
    {
        Plot(LineArray(x, Levels[i], BarCount, Levels[i]), "", i+2, styleDots);
        PlotText(StrExtract(LText, i), BarCount, Levels[i], i+2);
    }
}
Title = FullName()+" - "+Name()+"\nO:"+O+", H:"+H+", L:"+L+", C:"+C;
_SECTION_END();


======================================================================


8 comments:

  1. Which is the standard period to be entered in the parameter menu of amibroker? Is it 5 or 10?

    Satheesh mukundan.....

    ReplyDelete
  2. Look for a significant high or low made by the price in the near past . This is called the lookback period.

    Change the parameters accordingly to set to that High or Low . For every chart you will have to adjust to the levels . Significant high or low will have larger effect rather than short term high or low.

    ReplyDelete
  3. Sir.. if I am using 5 min chart, which isthe period tobe entered in the parameters option?
    Sunil...

    ReplyDelete
  4. @ Sunil ,

    I would suggest you to use Fibonacci levels in an Hourly timeframe even if you are doing Intraday Trading on a 5 Minute Chart.

    This is because in a 5 minute chart Fib. levels will not have that much effect to act as resistance or support for long.

    In an hourly chart suppose the previous High is at an eight period previous Bar than choose 8 periods through parameter.

    If a significant high is at 5 or 6 previous bars then choose the period accordingly. You may go as far back as you may like according to the swing high and swing low levels which you see that such levels are not being broken by the price frequently.

    No static level in parameter can be chosen for long as the dynamic nature of mkt. will force the price to make new significant high & lows.

    ReplyDelete
  5. Hi there, thanks for sharing. I am new to AFL / amibroker.
    How could I adapt this so that the reference would be a moving average on a different time frame than my chart? (so basically the script could plot the Fibo Retracements/Goals of a 100 hours MA on a 30mins charts for instance)

    Thanks!

    ReplyDelete
    Replies
    1. Hello,

      Fibonacci levels are basically plotted from Swing High & Swing Low levels as per the trend in place.
      The condition which you are requiring means you need dynamic Fibonacci levels. I am also not sure whether
      these conditions could be really met for plotting. If they can be fulfilled then also this will surely need lot of coding.
      You may either ask in Trading Forums where some coding experts can be
      helpful in prompting you in the right direction or take help with official Amibroker website support team.
      One forum I can point you to is www.traderji.com . There some Amibroker coding experts in the Amibroker section of the forum
      try to help other members.

      Delete
  6. Hi Trade Catcher, thank for the reply and the usggestion (I have actually created an account on traderji a few years back but havent been active there)!

    Given my limited coding experience in AFL, I think the 1st block defines which bars and levels are used for the calculations. Do you think that, instead of using the closing price of the stock, using the closing price of a MA(c,200) would work? (how could I do that by the way?).

    I am not too sure as per why you use a -1 and a +1 in the formula (1st block of code where you find the location adn value of the max & min values), is it possible for you to explicit this a little bit?
    What are x0 and x1? (confuses me a bit when reading the block of code on "plotting")

    I find your logic to add the fibo line very clear and elegant.

    Again, thanks for sharing your code, I think I am learning quite a bit just reading it at the moment (discovering useful functions and understanding the flow)

    ReplyDelete
    Replies
    1. Hi June,

      I would like to say that the above code is not created by me. This has been just shared by me after I found it through an internet source.
      I liked the code for Amibroker users thats why I thought of posting it for my blog readers.

      I am myself not into coding at all thats why I thought of suggesting you about the forum. There is another website called
      www.wisestocktrader.com which is dedicated to Amibroker formulas and coding etc. , I think you might get help there for
      your queries. I am really sorry that coding being Greek to me I could not really be a helping hand in that domain.

      Regards.

      Aryan.

      Delete

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.