Merry Christmas and Happy Holidays to all!
-----
/*
SignalSums_02. afl
Versions
_01 From
http://www.amibroke r.com/members/ library/formula. php?id=29
_02 (Progster) Parameterized filter requirements. Added ParamOptimize( ).
Put in LongShortBoth logic and N-bar stop for analysis purposes.
Replaced obsolete (per the docs) "ColumnX =" statements with AddColumn() statements.
Colored the long signals green, short signals red in the Exploration.
Added SummaryOrAll switch. Put parameters in front of output.
*/
/*
Against all odds (draft). Written by Thierry HUITEL o-l---}
based on Jim Varney's work-- CANDLESTOCHASTICS- -
and all the amibroker group :-)
This Exploration is a scan for 24 different buy or sell signals.
The odds are 1 of 6 to get a TWO with a dice. If you try 1000 times, the odds are more than 99%.
The aim of the exploration is to find days when many bullish or bearish signs are triggered at the same time.
If 5 indicators give a buy advice, it is more reliable than one.
I invite everybody to add your own systems to these ones, to improve the reliability.
And experimented technical analysts could give advices to avoid the trap of using several
different indicators all working off the same input data.
Vol Index: this column is the ratio of today's volume to the 14-day average volume.
This column should be sorted Descending. The best signals are occur when VolIndex is at least 2 or higher.
PCL[up]: Piercing Line, "up" signifies Bullish.
MDS[up]: Morning Doji Star
BLE[up]: Bullish Engulfing
HAM[up]: Hammer
BRE[dn]: Bearish Engulfing, "dn" signifies Bearish.
DCC[dn]: Dark Cloud Cover
EDS[dn]: Evening Doji Star
TDREI[up] & [dn]: Tom DeMark's Range Expansion Index
KUP[up] & [dn]: Keltner Bands -DIMITRIS TSOKAKIS
RSI[up] & [dn]: Relative Strength Index 14 periods
MFI[up] & [dn]: Money Flow Index
ST2[up] & [dn]: Stochastic Slow - Donald Dalley
DIV[up] & [dn]: % R divergence -DIMITRIS TSOKAKIS
KST[up] & [dn]: MARTIN PRING'S KST MOMENTUM SYSTEM -TJ
COP[up]: Coppock Curve TJ
SMH[up] & [dn]: smash day pattern. DIMA
CHK[up] & [dn]: Chaikin Money Flow. Thierry Huitel
A "1" in the column signifies TRUE, a "0" indicates no signal.
------------ --------- --------- --------- --------- --------- -*/
function ParamOptimize( ParamTitle, defaultVal, minv, maxv, step )
{
return Optimize( ParamTitle, Param( ParamTitle, defaultVal, minv, maxv, step ), minv, maxv, step );
}
"Commentaires sur " + name() +" pour le "+date();
/* Minimum Price and 14 day Avg Volume Values for Filter */
minPrice = 3; //change as needed
minVol = 50000; //change as needed
VolAvg = ma( v, 14 );
VolumeIdx = v / VolAvg;
AvgRange = sum( abs(O-C),15 )/15;
/* Candle Codes */
White = iif((C>O) AND ((C-O)>=0.8* (H-L)),1, 0) AND (C-O)>AvgRange;
Black = iif((C<O) AND ((O-C)>=0.8* (H-L)),1, 0) AND (O-C)>AvgRange;
Doji = iif(abs(O-C) <=0.1*(H- L),1,0);
/* Dark Cloud Cover [Bear] */
DCC = iif(ref(White, -1) AND Black AND C<=ref(((H+L) /2),-1)
AND O>ref(C,-1), 1,0);
/* Piercing Line [Bull] */
PL = iif(ref(Black, -1) AND White AND C>=ref(((H+L) /2),-1)
AND O<ref(C,-1), 1,0);
/* Evening Doji Star [Bear] */
EDS = iif(ref(White, -2) AND ref(Doji, -1) AND Black AND
C<=ref(((H+L) /2),-2), 1,0);
/* Morning Doji Star [Bull] */
MDS = iif(ref(Black, -2) AND ref(Doji, -1) AND White AND
C>=ref(((H+L) /2),-2), 1,0);
/* Hammer [Bull] */
HAM = iif( (H-L > 1.5*AvgRange) AND (C > (H+L)/2) AND (O > C) AND
(VolumeIdx > 2), 1, 0);
/* Bearish Engulfing */
BRE = iif(Black AND ref(White, -1) AND (C < ref(O, -1)) AND (O > ref(C, -1)), 1,0);
/* Bullish Engulfing */
BLE = iif(White AND ref(Black, -1) AND (C > ref(O,-1)) AND (O < ref(C,-1)), 1,0);
/* Stochastics 14-4 */
ss = ma(stochk(14) ,4);
StochBuy = iif(ss<=20, 1, 0);
StochSell = iif(ss>=80, 1, 0);
/* TDREI */
HighMom = H - Ref( H, -2 );
LowMom = L - Ref( L, -2 );
Cond1 = ( H >= Ref( L,-5) OR H >= Ref( L, -6 ) );
Cond2 = ( Ref( H, -2 ) >= Ref( C, -7 ) OR Ref( H, -2 ) >= Ref( C, -8 ) );
Cond3 = ( L <= Ref( H, -5 ) OR L <= Ref( H, -6) );
Cond4 = ( Ref( L, -2 ) <= Ref( C, -7 ) OR Ref( L, -2 ) <= Ref( C, -8 ) );
Cond = ( Cond1 OR Cond2 ) AND ( Cond3 OR Cond4 );
Num = IIf( Cond, HighMom + LowMom, 0 );
Den = Abs( HighMom ) + Abs( LowMom );
TDREI = 100 * Sum( Num, 5 )/Sum( Den, 5 ) ;
tdreiBuy = iif(TDREI<=- 95, 1, 0);
tdreiSell = iif(TDREI>=95, 1, 0);
/* KUP */
KUP=EMA((H+L+ C)/3,10)+ EMA(H-L,10) ;
KDOWN=EMA((H+ L+C)/3,10) -EMA(H-L, 10);
kupBuy = iif(CROSS(C, KDOWN), 1, 0);
kupSell = iif(CROSS(KUP, C), 1, 0);
/*RSI*/
vrsi= rsi(14);
rsiBuy = iif(CROSS(vrsi, 30), 1, 0);
rsiSell = iif(CROSS(70, vrsi), 1, 0);
/*MFI*/
mfiBuy = iif(CROSS(mfi( ),30), 1, 0);
mfiSell = iif(CROSS(70, mfi()), 1, 0);
/*STO2*/
lookback = 14;
buyrange = 30;
sellrange = 70;
stochKworkaround = STOCHK(14);
stochDworkaround = EMA( STOCHK(14), 5);
sto2Buy = iif(STOCHK(14) < buyrange AND CROSS(stochKworkaro und, stochDworkaround) , 1, 0);
sto2Sell = iif(STOCHK(14) > sellrange AND CROSS(stochDworkaro und, stochKworkaround) , 1, 0);
/* %R, ema 9 and divergences */
R=-100*((HHV( HIGH,14)- CLOSE))/( HHV(HIGH, 14)-LLV(LOW, 14));
DIVR=(R-REF( R,-1))*(C- REF(C,-1) );
DIVB=IIF((DIVR< 0) AND (R-ref(R,-1) )>0 and (REF(R,-1)<- 90),-100, 0);
DIVB1=IIF((DIVR< 0) AND (R-ref(R,-1) )>0 and (REF(R,-1)<- 90),-80,0) ;
DIVS=IIF((DIVR< 0) AND (R-ref(R,-1) )<0 and (REF(R,-1)>- 10),-20,0) ;
divBuy = iif(DIVB==-100, 1, 0);
divSell = iif(DIVS==-20, 1, 0);
/*KST*/
KST = (MA(ROC(CLOSE, 10),10) * 1) +
(MA(ROC(CLOSE, 15),10) * 2) +
(MA(ROC(CLOSE, 20),10) * 3) +
(MA(ROC(CLOSE, 30),15) * 4);
kstBuy = iif(CROSS(KST , MA(KST, 109)), 1, 0);
kstSell = iif(CROSS(MA( KST , 120), KST), 1, 0);
/*COP*/
copBuy = iif((EMA( ROC( MA( C, 22 ), 250 ), 150 ) / 100) < 0, 1, 0);
/*SMASH*/
numDays = 3; // Consider smash Day if closed above/below previous numDays highs/lows
closeInDayRangePct = 0.25; // Smash day close should be in the high/low %% of the day range
smashDayDown = close < LLV (ref (low, -1), numDays) AND close < open AND close < (low + closeInDayRangePct * (high - low));
smashDayUp = close > HHV (ref (high, -1), numDays) AND close > open AND close > (high - closeInDayRangePct * (high - low));
// Enter in the direction opposite to the smash day if the very next day price moves opposite the smash day.
smashBuy = iif(ref (smashDayDown, -1) AND high > ref (high, -1), 1, 0);
smashSell = iif(ref (smashDayUp, -1) AND low < ref (low, -1), 1, 0);
/*CHAIKIN MONEY FLOW*/
ICH = sum(((( C-L )-( H-C )) / ( H-L ))*V, 21 ) / sum(V,21);
LCH = llv( ICH, 255 );
top = (LCH/2);
chkBuy = cross (ICH, top);
chkSell = cross (0, ICH);
/*number of buy signals --- give weight to your favorite ones with a coefficient. */
somme= PL + MDS + HAM + BLE + tdreiBuy + kupBuy + rsiBuy + (2*mfibuy) + sto2Buy + (2*divBuy) + kstBuy + copBuy + (2*smashBuy) + chkBuy;
/*number of sell signals. */
somme2 = BRE + DCC + EDS + tdreiSell + kupSell + rsiSell + mfiSell + sto2Sell + divSell + divSell + kstSell + smashSell + chkSell;
/*Guru comment*/
"number of buy indicators triggered: " + writeval (somme) ;
"Aujourd'hui, les signaux haussiers suivants ont été déclenchés: ";
/* Exploration Columns for Sorting */
// NumColumns = 29;
SummaryOrAll = Param( "Explore Summary or All?", 1, 1, 2, 1 ) ;
if( Status("action" ) == 4 ){
// AddColumn( array, name, format = 1.2, textColor = colorDefault, bkgndColor = colorDefault, width = -1 )
AddColumn( V, "Volume", 1.0 ) ;
AddColumn( VolumeIdx, "Vol Idx", 1.0 ) ;
AddColumn( somme, "Long Count", 1.0, colorDefault, iif( somme > 0, colorGreen, colorDefault) ) ;
AddColumn( somme2, "Short Count", 1.0, colorDefault, iif( somme2 > 0, colorRed, colorDefault) ) ;
if(SummaryOrAll == 2){
AddColumn( PL, "PCL[up]", 1.0, colorDefault, iif( PL == 1, colorGreen, colorDefault) ) ;
AddColumn( MDS, "MDS[up]", 1.0, colorDefault, iif( MDS == 1, colorGreen, colorDefault) ) ;
AddColumn( BLE, "BLE[up]", 1.0, colorDefault, iif( BLE == 1, colorGreen, colorDefault) ) ;
AddColumn( HAM, "HAM[up]", 1.0, colorDefault, iif( HAM == 1, colorGreen, colorDefault) ) ;
AddColumn( BRE, "BRE[dn]", 1.0, colorDefault, iif( BRE == 1, colorRed, colorDefault) ) ;
AddColumn( DCC, "DCC[dn]", 1.0, colorDefault, iif( DCC == 1, colorRed, colorDefault) ) ;
AddColumn( EDS, "EDS[dn]", 1.0, colorDefault, iif( EDS == 1, colorRed, colorDefault) ) ;
AddColumn( tdreiBuy, "TDREI[up]", 1.0, colorDefault, iif( tdreiBuy == 1, colorGreen, colorDefault) ) ;
AddColumn( tdreiSell, "TDREI[dn]", 1.0, colorDefault, iif( tdreiSell == 1, colorRed, colorDefault) ) ;
AddColumn( kupBuy, "KUP[up]", 1.0, colorDefault, iif( kupBuy == 1, colorGreen, colorDefault) ) ;
AddColumn( kupSell, "KUP[dn]", 1.0, colorDefault, iif( kupSell == 1, colorRed, colorDefault) ) ;
AddColumn( rsiBuy, "RSI[up]", 1.0, colorDefault, iif( rsiBuy == 1, colorGreen, colorDefault) ) ;
AddColumn( rsiSell, "RSI[dn]", 1.0, colorDefault, iif( rsiSell == 1, colorRed, colorDefault) ) ;
AddColumn( mfiBuy, "MFI[up]", 1.0, colorDefault, iif( mfiBuy == 1, colorGreen, colorDefault) ) ;
AddColumn( mfiSell, "MFI[dn]", 1.0, colorDefault, iif( mfiSell == 1, colorRed, colorDefault) ) ;
AddColumn( sto2Buy, "ST2[up]", 1.0, colorDefault, iif( sto2Buy == 1, colorGreen, colorDefault) ) ;
AddColumn( sto2Sell, "ST2[dn]", 1.0, colorDefault, iif( sto2Sell == 1, colorRed, colorDefault) ) ;
AddColumn( divBuy, "DIV[up]", 1.0, colorDefault, iif( divBuy == 1, colorGreen, colorDefault) ) ;
AddColumn( divSell, "DIV[dn]", 1.0, colorDefault, iif( divSell == 1, colorRed, colorDefault) ) ;
AddColumn( kstBuy, "KST[up]", 1.0, colorDefault, iif( kstBuy == 1, colorGreen, colorDefault) ) ;
AddColumn( kstSell, "KST[dn]", 1.0, colorDefault, iif( kstSell == 1, colorRed, colorDefault) ) ;
AddColumn( copBuy, "COP[up]", 1.0, colorDefault, iif( copBuy == 1, colorGreen, colorDefault) ) ;
// AddColumn( copSell, "", 1.0 ) ; // DNE
AddColumn( smashBuy, "SMH[up]", 1.0, colorDefault, iif( smashBuy == 1, colorGreen, colorDefault) ) ;
AddColumn( smashSell, "SMH[dn]", 1.0, colorDefault, iif( smashSell == 1, colorRed, colorDefault) ) ;
AddColumn( chkBuy, "CHK[up]", 1.0, colorDefault, iif( chkBuy == 1, colorGreen, colorDefault) ) ;
AddColumn( chkSell, "CHK[dn]", 1.0, colorDefault, iif( chkSell == 1, colorRed, colorDefault) ) ;
} // end of: if(SummaryOrAll == 2)
} // end of: if( Status("action" ) == 4 )
printf( "Long Signal summary number is: " + NumToStr( somme, 1.0 ) + "\n" ) ;
printf( "Short Signal summary number is: " + NumToStr( somme2, 1.0 ) + "\n" ) ;
// ### Comment this out if you prefer your extra columns (parms) at the end rather than in front
// ### Also, comment this out if not running in AB 5.23 or later !
SetOption("ExtraCol umnsLocation" , 1 ); // put parameter columns up-front after optimization
/* Filter and Trading Choices */
LongShortBoth = Param( "LongShortBoth" , 1, 1, 3, 1 ) ;
SingleContract = ParamToggle( "SingleContract" , "No|Yes", 1 ) ;
// ShowBuysOrSells = ParamToggle( "ShowBuysOrSells" , "Buys|Sells" , 0 ) ;
ReqSigCount = ParamOptimize( "ReqSigCount" , 4, 1, 6, 1 ) ; // required signal count to pass filter
ReqVolRatio = ParamOptimize( "ReqVolRatio" , 2, .4, 2, .2 ) ; // required volume ratio to pass filter
BarsForStop = ParamOptimize( "BarsForStop" , 3, 1, 5, 1 ) ;
BuySigs = (somme >= ReqSigCount) and (VolumeIdx >= ReqVolRatio) ;
SellSigs = (somme2 >= ReqSigCount) and (VolumeIdx >= ReqVolRatio) ;
if (SingleContract) PositionSize = MarginDeposit = 1; // Trade a single contract.
// Set up Exploration Filter and Buys/Sells
if( LongShortBoth == 1 ){
Filter = BuySigs;
Buy = (somme >= ReqSigCount) and (VolumeIdx >= ReqVolRatio) ;
Sell = 0 ;
Short = 0 ;
Cover = 0 ;
}
else if( LongShortBoth == 2 ){
Filter = SellSigs ;
Buy = 0 ;
Sell = 0 ;
Short = (somme2 >= ReqSigCount) and (VolumeIdx >= ReqVolRatio) ;
Cover = 0 ;
}
else if( LongShortBoth == 3 ){
Filter = BuySigs or SellSigs;
Buy = (somme >= ReqSigCount) and (VolumeIdx >= ReqVolRatio) ;
Sell = 0 ;
Short = (somme2 >= ReqSigCount) and (VolumeIdx >= ReqVolRatio) ;
Cover = 0 ;
}
/* N-bar stop */
ApplyStop( stopTypeNBar, stopModeBars, BarsForStop );