May 2017 - aflbank

The Bank Of Amibroker AFL Code

Post Top Ad

Wednesday, May 31, 2017

Color MA with Histogram - Amibroker AFL Code

May 31, 2017 0

Click Image To Enlarge. Please Rate And Comment.

Color MA with Histogram

_SECTION_BEGIN("");
A1 = MA(C,5);
B1 = MA(C,17);
D1 = A1-B1;

Colorm=IIf(D1>Ref(D1,-1),colorBrightGreen,colorDarkGreen);

Plot( D1, "Short", Colorm, styleHistogram|styleNoLabel );



PlotOHLC( 0, D1, 0 , 0 , "517",Colorm, styleCloud | styleNoLabel);



//////////////////////////////////////////////////////////////////////////////////////
A2 = MA(C,5);
B2 = MA(C,35);
D2 = A2-B2;


Colorm=IIf(D2>Ref(D2,-1),colorRed,colorDarkRed);
Plot( D2, "MED", Colorm, styleHistogram|styleNoLabel );



PlotOHLC( 0, D2, 0 , 0 , "535",Colorm, styleCloud | styleNoLabel);



//////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
A3 = MA(C,10);
B3 = MA(C,70);
D3 = A3-B3;


Colorm=IIf(D3>Ref(D3,-1),colorPaleBlue,colorBlue);
Plot( D3, "Long", Colorm, styleHistogram|styleNoLabel );



PlotOHLC( 0, D3, 0 , 0 , "1070",Colorm, styleCloud | styleNoLabel);



//////////////////////////////////////////////////////////////////////////////////////
_SECTION_END();
Read More

Very Accurate Zig Zag Trend Line - Amibroker AFL Code

May 31, 2017 0

Click Image To Enlarge. Please Rate And Comment.

Very Accurate Zig Zag Trend Line

swingDays = 0;
swingVol = 0;
ordVol = 0;
upswingSignal = 0;
downswingSignal = 0;
accumulatedVolume = 0;
accumulatedDays = 0; 
arrayHasSignal = 0;
zigPercentage = Param("ZigZag Percent", 10, 2, 50);
trendZig = Zig(C, zigPercentage); 
action = Status("action");
midPointY = 0;
midPointValue = 0;
peakVolumeExtremeDetectionDays = Param("Peak Vol. Days", 6, 1, 20); //3 days before and 3 days after a peak
daysBeforeAndAfterForPeakVolume = round(peakVolumeExtremeDetectionDays/2);
peakVolume = 0;
colorOrdVolume = ParamColor("Ord Vol. Info.", colorGrey50);
colorZig = ParamColor("ZigZag", colorGold);
colorPeakUp = ParamColor("Support Info.", colorGreen);
colorPeakDown = ParamColor("Resistance Info.", colorRed);
scalingFactor = 0.1;


function volumeInMillions(inVolume)
{
 volInM = inVolume/1000000;
 return NumToStr(volInM, 1.2, False) + " m";
}

function getPeakVolume(daysToCheck, nowDay)
{
 returnPeakVolume = V[nowDay];
 dayNumberBefore = (nowDay) - daysToCheck;
 dayNumberAfter = (nowDay) + daysToCheck;
 //find Max swing Volume
 if( dayNumberBefore > 0 AND dayNumberAfter < BarCount )
 {
  returnPeakVolume = V[dayNumberBefore];
  //_TRACE("Start returnPeakVolume = " + returnPeakVolume);
  for( j = dayNumberBefore; j < dayNumberAfter; j++ )
  {
   if(returnPeakVolume < V[j])
   {
    returnPeakVolume = V[j];
   } 
   //_TRACE("returnPeakVolume = " + returnPeakVolume);
  }
 }
 else if( dayNumberBefore > 0 AND dayNumberAfter >= BarCount )
 {
  returnPeakVolume = V[dayNumberBefore];
  //_TRACE("Start returnPeakVolume = " + returnPeakVolume); 
  for( j = dayNumberBefore; j < BarCount; j++ )
  {
   if(returnPeakVolume < V[j])
   {
    returnPeakVolume = V[j];
   }
   //_TRACE("returnPeakVolume = " + returnPeakVolume);   
  }
 }
 else 
 {
  peakVolume = V[i-1];
 }
 
 return returnPeakVolume;
}

for( i = 3; i < BarCount; i++)
{
//initialize parameters
 arrayHasSignal[i] = 0;

//Then we check which way the price goes

 swingVol = swingVol + V[i-1];//Don't add today since price may have changed direction
 swingDays = swingDays + 1;

 if( (trendZig[i] < trendZig[i-1]) AND (trendZig[i-1] > trendZig[i-2]) AND i > 10 )//Changes from up swing to down swing
 {
  /*_TRACE("Changes from up swing to down swing, i = " + i);
  _TRACE("trendZig[i-2] = " + trendZig[i-2]);
  _TRACE("trendZig[i-1] = " + trendZig[i-1]);
  _TRACE("trendZig[i] = " + trendZig[i]);*/
  downswingSignal[i-1] = 0;
  upswingSignal[i-1] = 1;
  ordVol[i-1] = swingVol/swingDays; 
  accumulatedVolume[i-1] = swingVol;
  accumulatedDays[i-1] = swingDays; 
  arrayHasSignal[i-1] = 1;
  if(action == actionIndicator)
  {
   midPointValue = i - round(swingDays/2) - 1;
   midPointY = trendZig[midPointValue];
   peakVolume = getPeakVolume(daysBeforeAndAfterForPeakVolume, i - 1);
   PlotText("(" + volumeInMillions(ordVol[i-1]) + ")", midPointValue, midPointY, colorOrdVolume);
   PlotText(NumToStr(H[i-1], 1.1, False) + " (" + volumeInMillions(peakVolume) + ")", i-4, trendZig[i-1] * 1.02, colorPeakUp);
  }
  swingVol = 0;
  swingDays = 0;
 }

 if( (trendZig[i] > trendZig[i-1]) AND (trendZig[i-1] < trendZig[i-2]) AND i > 10 )//Changes from down swing to up swing
 {
  downswingSignal[i-1] = 1;
  upswingSignal[i-1] = 0;
  ordVol[i-1] = swingVol/swingDays;
  accumulatedVolume[i-1] = swingVol;
  accumulatedDays[i-1] = swingDays; 
  arrayHasSignal[i-1] = 1;
  if(action == actionIndicator)
  {
   midPointValue = i - round(swingDays/2) - 1;
   midPointY = trendZig[midPointValue];
   peakVolume = getPeakVolume(daysBeforeAndAfterForPeakVolume, i - 1);
   PlotText("(" + volumeInMillions(ordVol[i-1]) + ")", midPointValue, midPointY, colorOrdVolume);
   PlotText(NumToStr(L[i-1], 1.1, False) + " (" + volumeInMillions(peakVolume) + ")", i-4, trendZig[i-1] * 0.95, colorPeakDown);
  } 
  swingVol = 0;
  swingDays = 0;
 }
 if( i == BarCount - 1)//add last signal too
 {
  swingVol = swingVol + V[i];//Remember to add today also
  swingDays = swingDays + 1;

  if(trendZig[i] < trendZig[i-1])//is down swing
  {
   downswingSignal[i] = 1;
   upswingSignal[i] = 0;
   ordVol[i] = swingVol/swingDays; 
   accumulatedVolume[i] = swingVol;
   accumulatedDays[i] = swingDays; 
   arrayHasSignal[i] = 1;
  }

  if(trendZig[i] > trendZig[i-1])//is up swing
  {
   downswingSignal[i] = 0;
   upswingSignal[i] = 1;
   ordVol[i] = swingVol/swingDays;
   accumulatedVolume[i] = swingVol;
   accumulatedDays[i] = swingDays; 
   arrayHasSignal[i] = 1;
  }
  
  if(action == actionIndicator)
  {
   midPointValue = i - round(swingDays/2) - 1;
   midPointY = trendZig[midPointValue]; 
   PlotText("(" + volumeInMillions(ordVol[i]) + ")", midPointValue, midPointY, colorOrdVolume);
  }

 }
}
if(action == actionExplore)
{
 //Filter = 1;
 Filter = arrayHasSignal;
 //AddColumn(trendZig , "trendZig ", 1.2);
 AddColumn(C, "Swing Up", 1.2, colorDefault, IIf(upswingSignal, colorPeakUp, colorDefault));
 AddColumn(C, "Swing Down", 1.2, colorDefault, IIf(downswingSignal, colorPeakDown, colorDefault));
 AddColumn(ordVol, "Ord Vol.", 1.0);
 AddColumn(accumulatedDays, "Swing Days", 1.0);
 AddColumn(accumulatedVolume, "Tot. Swing Vol.", 1.0);
}
else if(action == actionIndicator)
{
 Plot(trendZig, "Ord Vol ZigZag", colorZig);
 //Scale the axis so we can read the numbers
 Plot(trendZig + (trendZig * scalingFactor), "", colorRed, styleNoDraw);
 Plot(trendZig - (trendZig * scalingFactor), "", colorBlue, styleNoDraw);
}
Read More

Line Chart With Perfect Trendline - Amibroker AFL Code

May 31, 2017 0

Click Image To Enlarge. Please Rate And Comment.

Line Chart With Perfect Trendline

_SECTION_BEGIN("");
priceTitle=EncodeColor(colorYellow)+ StrFormat("    {{NAME}}  --  {{INTERVAL}}" ) + "\n"+ EncodeColor(colorCustom11)+
"Date =  " + Date() ;
ToolTip=StrFormat("  Close = %g (%.1f%%)",C,SelectedValue( ROC( C, 1 ) ));
Title ="DHIRAJ" + priceTitle + "\n" + EncodeColor(colorWhite) + ToolTip; 


breakoutime = 100500;
afterbreakout0 = Cross(TimeNum(),100500);
afterbreakout1 = TimeNum()>=100500;
NewDay = Day()!= Ref(Day(), -1);
highestoftheday = HighestSince(newday,H,1);
Lowestoftheday =LowestSince(newday,L,1);
ORBHigh = ValueWhen(afterbreakout0,highestoftheday,1);
ORBLow = ValueWhen(afterbreakout0,lowestoftheday,1);
buycandidate =Cross(C,orblow) AND afterbreakout1;
sellcandidate = Cross(orbhigh,C) AND afterbreakout1 ;

BuyCond2 = Cross(C, WMA((L+C+H)/3,9)+0.01);/*((MidMA, LongMA);*/
SellCond4=Cross( WMA((L+C+H)/3,9)+0.01,C); 
Buy1 = BuyCond2;
Sell1 = SellCond4 ;
entryprice=WMA((L+C+H)/3,9)+0.01;
ENTRYSELL=WMA((L+C+H)/3,9)-0.01;

Buy= Cross(C,orbhigh) AND afterbreakout1;
Sell = Cross(orblow,C) AND afterbreakout1;
color = IIf(Buy,colorGreen,IIf(Sell,colorRed,IIf(buycandidate,colorBlue,IIf(sellcandidate,colorPink,0))));


Plot(C,"",colorYellow,styleBar);
PlotShapes( shapeUpArrow * Buy, colorGreen,0,L,-12);
PlotShapes( shapeDownArrow * Sell, colorRed,0,H,-12);
//Plot(afterbreakout0,"",colorBlue,styleHistogram|styleOwnScale);

StyleOR=styleNoLine|styleDots+styleThick;

Plot(ORBHigh,"RESISTENCE",colorGreen,StyleOR);
Plot(ORBLow,"SUPPORT",colorRed,StyleOR);
Filter = Buy OR Sell OR sellcandidate  OR buycandidate OR Buy1 OR Sell1;


//Filter = Buy OR Sell OR sellcandidate  OR buycandidate;
AddColumn(C,"CMP",0,colorBlue);
AddColumn(IIf(Buy OR sellcandidate,ORBHigh,ORBLow),"INTRA ",0,colorDefault,color);
AddColumn(IIf(Buy1,entryprice,ENTRYSELL),"DELIVERY ",0,colorDefault,IIf(Buy1,colorGreen, colorRed));
_SECTION_END();

SetBarsRequired(100000,0);
GraphXSpace = 3;


k = Param("K", 0.01, 0, 0.2, 0.001);

Trend[0] = 1;  
Revers[0] = C[0] - C[0]*k;
PE[0] = C[0];

for(i = 1; i < BarCount; i++)
{
 if(Trend[i-1] == 1)     //Up Trend
 {
  if(C[i] > PE[i-1])    // High 
  {
   Trend[i] = 1;
   PE[i] = C[i];
   Revers[i] = C[i] - C[i]*k;
  }
  else
  {
   if(C[i] < Revers[i-1])  
   {
    Trend[i] = -1;
    PE[i] = C[i];
    Revers[i] = C[i] + C[i]*k;
   }
   else       
   {
    Trend[i] = 1;
    PE[i] = PE[i-1];
    Revers[i] = Revers[i-1];
   }
  }
 }
//======================================
 else         //Down Trend
 {
  if(C[i] < PE[i-1])    // Low
  {
   Trend[i] = -1;
   PE[i] = C[i];
   Revers[i] = C[i] + C[i]*k;
  }
  else
  {
   if(C[i] > Revers[i-1])   
   {
    Trend[i] = 1;
    PE[i] = C[i];
    Revers[i] = C[i] - C[i]*k;
   }
   else       
   {
    Trend[i] = -1;
    PE[i] = PE[i-1];
    Revers[i] = Revers[i-1];
   }
  }
 }
}

Plot(Revers, "NRTR WATR", IIf(Trend == 1, 27, 4), 4);
_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
//_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}}Open %g, Hi %g, Lo %g, Close %g (%.1f%%) Vol " +WriteVal( V, 1.0 ) +" {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 )) ));
//Plot( C, "", ParamColor("Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
Buy=Cross(C,Revers);
Sell=Cross(Revers,C);
Filter=Buy OR Sell;
PlotShapes(Buy*shapeHollowUpTriangle,colorPink, 0, Low, Offset =-35);
PlotShapes(Buy*shapeDigit8,colorAqua, 0, Low, Offset =-50);
PlotShapes(Sell*shapeHollowDownTriangle,colorCustom12, 0, High, Offset =-10);
PlotShapes(Sell*shapeDigit8,colorRed, 0, High, Offset =25);
AddColumn( IIf(Buy,1,IIf(Sell,-1,0)) ,"12",1.0,colorBlack,IIf(Sell,colorGreen,IIf(Sell,colorRed,colorBlack)));


_SECTION_END();

//source http://www.traderji.com/amibroker/24404-help-amibroker-forumla-afl-saintjis-going-60min-flow-practice-2.html

// Written by Abhay (aad on www.traderji.com)

// Previous version 1.0 on 28.07.2008.
// Current Version 1.1 on 31.07.2008.

///////////////////////////////////////////////////////////
///// Major changes done from previous version 1.0 : //////

// The method of pivot definition is changed. 
// Now it does not use zig() or related functions anywhere.

// Stoploss line error, that it moves in both directions after a position is taken,
// is removed. Now stoploss lines are unidirectional.

// To protect profit on long positions, SELL is triggered either when trailing buy stop
// is hit OR when previous lower pivot is cracked. Fresh long position will be initiated 
// if uptrend continues after previous high pivot is cracked.   

///////////////////////////////////////////////////////////


// This is simple YET powerful pivot based trading system.
// Used only for intraday. Not suitable for EOD data.
// Designed for continuous trading. i.e. short=sell and cover=buy
// When long, watch crack of close in green line ONLY. Ignore red line.
// When short, watch crack of close in red line ONLY. Ignore green line.
// You will have to tweak the parameters as per your trading style.

// Warning :  I have tried to develop this method from Saint's Pivot Based System (www.traderji.com) 
//   However, it is very well possible that its performance is not as per actual system
//   suggested by Saint due to programming errors and inadequate knowledge level at my end.
// Warning :  Traders are advised to trade on their own, knowing very well that it is their money.










_SECTION_BEGIN("Simple Pivot based Trading System");

// User defined parameters.

GraphXSpace = 10; //defines how much extra space should be added above and below graph line (in percent).

dist = 0.25*ATR(10); 
Capital=Param("Total capital",100000,10000,1000000,1000);
drawdown=Param("Max. loss per trade as % of Capital", 1.0,0.5,25.0,0.1);
room=Param("Room for S/L as % of Pivot value",0.0001,0,0.0001,0.02,0.0001);

// Now calculate pivots. Method courtesy : Kenneth (www.traderji.com)

PH= ValueWhen(
(Ref(H,-2) > Ref(H, -4)) AND
(Ref(H,-2) > Ref(H, -3)) AND
(Ref(H,-2) > Ref(H, -1)) AND
(Ref(H,-2) > H), Ref(H,-2));

PL= ValueWhen(
(Ref(L,-2) <= Ref(L, -4)) AND
(Ref(L,-2) <= Ref(L, -3)) AND
(Ref(L,-2) <= Ref(L, -1)) AND
(Ref(L,-2) <= L), Ref(L,-2));

// filter lines

phfilter=PH+(room*PH);
plfilter=PL-(room*PL);

//Uncomment following code if you wish to see the pivot lines.
//Plot(Ref(PH,2), "UpPivot", ParamColor("UpPivot Color",colorRed), styleDashed);
//Plot(Ref(PL,2), "DownPivot",ParamColor("DownPivot Color",colorGreen), styleDashed);

Plot(Ref(Phfilter,2), "Upfilter", ParamColor("upfilter Color",colorWhite), styleLine);
Plot(Ref(Plfilter,2), "Downfilter",ParamColor("dnfilter Color",colorWhite), styleLine);


//Plot(Ref(UpFractal,2), "Up Fractal", ParamColor("Up Fractal Color",colorRed),8);
//Plot(Ref(DownFractal,2), "Down Fractal",ParamColor("Down Fractal Color",colorGreen),8);



 
/*for (a=4;a<BarCount;a++)
{
if ((H[a-2] >= H[a-4]) AND
(H[a-2] >= H[a-3]) AND
(H[a-2] >= H[a-1]) AND
(H[a-2] >= H[a]))
PlotText("PH", a-2, H[a-2], colorGreen);

if ((L[a-2] <= L[a-4]) AND
(L[a-2] <= L[a-3]) AND
(L[a-2] <= L[a-1]) AND
(L[a-2] <= L[a]))
PlotText("PL", a-2, L[a-2]-dist[a-2], colorRed);
}*/

//Condition for buy : Enter when Close crosses latest pivot high.
Buy = C> (PH+(room*PH));

initialStopBuy=Ref(PL,2)-(Ref(PL,2)*room/100);
trailStopBuy=IIf(C>PH,Ref(initialStopBuy,-1),initialStopBuy);

newStopBuy=trailStopBuy;

BuyLimitCapital=int(Capital/C);
SLbuy=round(C-initialStopBuy);
BuyLimitSL=int((Capital*drawdown)/(100*SLbuy));

//Condition for sell : Exit when previous pivot low is cracked.
Sell = C< (PL-(room*PL));

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

//Short = Sell;
//Cover = Buy;

//Short=ExRem(Short,Cover);
//Cover=ExRem(Cover,Short);

shape = Buy * shapeUpArrow + Sell * shapeDownArrow;
PlotShapes( shape, IIf( Buy, colorGreen, colorRed ),0, IIf( Buy, Low, High ) );


_SECTION_END();
Read More

Post Top Ad