Monday, January 13, 2014

EOD Based Quadra Trading System - Amibroker AFL

Quadra Trading System  - Amibroker AFL

Credit goes to the creator of the AFL Code. No changes have been made by the Blog owner to the AFL code. The code has been obtained through online resource and is presented on as it is basis.

Please copy the code from the Code Box below. You may refer to the image posted below to look how the chart looks after applying the AFL.


/****************************/
/*                          */
/*  Quadra Trading System   */
/*       Version 1.30       */
/*     (11 August 2012)     */
/*                          */
/****************************/



SetChartOptions(0, chartShowDates | chartWrapTitle);


/**********************/
/*                    */
/* Define Wilder's MA */
/*                    */
/**********************/

function WilderMA(Field, Period)
 {
  X[0] = Field[0];
  
  for(i = 1; i < BarCount; i++)
   {
    X[i] = Field[i]/Period + X[i - 1] * (Period - 1)/Period;
   }
  return X;
 }

/**********************/
/*                    */
/*     Define EMA     */
/*                    */
/**********************/

function MyEMA(Field, Period)
 {
  Y[0] = Field[0];
  for(i = 1; i < BarCount; i++)
   {
    Y[i] = (Field[i] - Y[i - 1]) * 2/(Period + 1) + Y[i - 1];
   }
  return Y;
 }


/********************/
/*                  */
/*   Wilder's MAs   */
/*                  */
/********************/

A1 = WilderMA(C, 5);
A2 = WilderMA(C, 8);
A3 = WilderMA(C, 13);
A4 = MyEMA(C, 50);

/********************/
/*                  */
/*   Candle Color   */
/*                  */
/********************/


Green = C > O;
Red = C < O;

for(i = 1; i<BarCount; i++)
 {
  if(C[i] == O[i])
   {
    if(Green[i - 1] AND C[i] >= C[i - 1]) Green[i] = 1;
    if(Red[i - 1] AND C[i] <= C[i - 1]) Red[i] = 1;
   }
 }



/********************/
/*                  */
/*    COMPUTE SL    */
/*                  */
/********************/


PrevHi = H;
PrevLo = L;
SLBuy = Null;
SLShort = Null;
GUp = GapUp();
GDn = GapDown();

for(i = 2; i < BarCount; i++)
 {
  PrevLo[i] = PrevLo[i - 1];

  if(L[i - 2] >= L[i - 1] AND L[i - 1] <= L[i])
   {
    PrevLo[i] = L[i - 1];
   }

  PrevHi[i] = PrevHi[i - 1];
  if(H[i - 2] <= H[i - 1] AND H[i - 1] >= H[i])
   {
    PrevHi[i] = H[i - 1];
   }

 }

/********************/
/*                  */
/*     BUY/SELL     */
/*                  */
/********************/


Buy = (A1 > A2 AND A2 > A3);
Sell =(A1 < A2 AND A2 < A3);

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

Bought = Flip(Buy, Sell);
Sold = Flip(Sell, Buy);

for(i = 1; i <BarCount; i++)
 {
  SLShort[i] = Min(PrevHi[i], PrevHi[i - 1]) + 0.1;
  if(GDn[i]) SLShort[i] = L[i - 1] + 0.1;
  if(Sell[i]) break;
 }
for(i = 1; i <BarCount; i++)
 {
  SLBuy[i] = Max(PrevLo[i], PrevLo[i - 1]) - 0.1;
  if(GUp[i]) SLBuy[i] = H[i - 1] - 0.1;
  if(Buy[i]) break;
 }

for(i = 1; i < BarCount; i++)
 {
  if(Buy[i]) SLBuy[i] = PrevLo[i - 1] - 0.1;
  if(Bought[i]) SLBuy[i] = Max(SLBuy[i - 1], PrevLo[i] - 0.1);
  if(GUp[i]) SLBuy[i] = H[i - 1] - 0.1;

  if(Sell[i]) SLShort[i] = PrevHi[i - 1] + 0.1;
  if(Sold[i]) SLShort[i] = Min(PrevHi[i - 1], PrevHi[i]) + 0.1;
  if(GDn[i]) SLShort[i] = L[i - 1] + 0.1;
 }
for(i = 1; i<BarCount; i++)
 {
  if(SLShort[i] > SLShort[i - 1]) SLShort[i] = SLShort[i - 1];
 }



Marker1 = Buy * shapeUpArrow + Sell * shapeDownArrow;
MarkerColor = IIf(Buy, colorBrightGreen, colorYellow);
Marker1Dist = IIf(Buy, 0.995 * L, 1.005 * H);
EMA_Position = IIf(A4 > A3 AND A4 > A2 AND A4 > A1, 1, IIf(A4 < A3 AND A4 < A2 AND A4 < A1, -1, 0));


/***********************/
/*                     */
/*   SHOW/HIDE Option  */
/*                     */
/***********************/

Arrows = ParamToggle("Show Arrows", "NO|YES");
ShowTSL= ParamToggle("Show TSL Line", "NO|YES");

_N(Title = EncodeColor(colorAqua) + "QUADRA EOD TRADING SYSTEM by Savant Garde; AFL Version 1.30 by Anant Navale\n\n" 
          + EncodeColor(colorPink)  + StrFormat("{{NAME}}({{INTERVAL}}), {{DATE}} : {{OHLCX}}, Vol=%1.0f\n{{VALUES}}\n", V)
    + "EMA50 location: " + EncodeColor(colorBrightGreen) + WriteIf(EMA_Position > 0, "Above,     ", "")
    + EncodeColor(colorLightGrey) + WriteIf(EMA_Position == 0, "Inside,     ", "")
    + EncodeColor(colorRed) + WriteIf(EMA_Position < 0, "Below,     ", "")
    + EncodeColor(colorBrightGreen) + WriteIf(Green, " Green Candle", "") 
    + EncodeColor(colorRed) + WriteIf(Red, " RedCandle\n\n", "\n\n")
    + EncodeColor(colorWhite) + WriteIf(Buy, "Buy Above " + (H + 0.1), "") 
    + WriteIf(Sell, "Sell Below " + (L - 0.1), "")
    + WriteIf(Buy, "   StopLoss = " + SLBuy, "") 
    + WriteIf(Sell, "   StopLoss = " + SLShort, ""));

if(Status("action") == actionIndicator)
 {
  Plot(A4, "EMA50", colorLightGrey, styleLine | styleThick);
  Plot(A3, "Wilder MA13", colorRed, styleLine);
  Plot(A2, "Wilder MA8", colorLightOrange, styleLine);
  Plot(A1, "Wilder MA5", colorBrightGreen, styleLine);

  if(ShowTSL)
   {
    Plot(SLBuy, "SL for Buy", colorLightBlue, styleDots);
    Plot(SLShort, "SL for Short", colorGold, styleDots);
   }

  Plot(C, "", IIf(Green, colorGreen, IIf(Red, colorRed, colorGrey50)), styleCandle);

  if(Arrows)
   {
    PlotShapes(Marker1, MarkerColor, 0, MArker1Dist);
   }
 }

if(Status("action") == actionExplore)
 {
  Filter = Buy | Sell;
     
  SetOption("NoDefaultColumns", True);
  AddTextColumn(Name(), "SYMBOL");
  AddColumn(DateTime(), "DATE", formatDateTime);
  AddColumn(IIf(Buy, 66, 83), "TRIGGER", formatChar, colorWhite, IIf(Buy, colorGreen, colorRed));
  AddColumn(EMA_Position, "EMA Position",1.0);
  AddColumn(IIf(Buy, H + 0.1, L - 0.1), "TRIG PRICE", 1.2);
  AddColumn(IIf(Buy, SLBuy, SLShort), "Stop Loss", 1.2);
  AddColumn(C, "Last Close", 1.2);
 }

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.