# k-nearest neighbors algorithm

Discussion in 'MetaTrader' started by amibrokerfans, Jul 30, 2014.

1. ### amibrokerfansWell-Known Member

Joined:
May 11, 2012
Messages:
706
802
Trophy Points:
93
Location:
kolkata

a 'prediction' is a prediction.
so you never cant rely on this type of indicators for trading!

BUT
on other hand, what we do for trading?
we do 'prediction'. we try to imagine what will be market next move and go for order.
i remember once a pro trader told that - i flip a coin, if its head i go for long, if tail i go short. and rest all about money management.

this indicator can help us to make mind set whether i go long or short. WITH OTHER confirmations.price level does not follow it much, it can be use for trend.

this indicator neutral net type. so lower time frame , much noise not accepted. atleast 1 hour min.

and "best fit" also needed on 'Npast' parameter.

but then again

a 'prediction' is a prediction.

below chart i posted 27th july 4.00 p.m

and here is recent scenario..

Last edited: Aug 1, 2014
2. ### mayank87New Member

Joined:
Jul 14, 2009
Messages:
5
0
Trophy Points:
1
Hi

// ********************************************
// INDICATOR IVERGENCE
// DATE :07-30-04
// PROGRAMMER :M.Lauronen
// ********************************************

// --------------------------------------------
// Recommended AmiBroker indicator settings:
// Level 0
// Percent
// Middle
// Scaling: Custom 0 - 100
// --------------------------------------------

//period = Param("% change",0.4,0.1,25,0.1);
//period = 5;
uplimit=2;
uplimit=IIf(Interval()==in15Minute,4,IIf(Interval()==inHourly,8,
IIf(Interval()==inDaily,12,uplimit)));
i=uplimit;
globalTitile=" ";
divergenceCount=0;
highCount=0.0;

for(;i>=0.2{
numChg = i ;
i=i-0.1;
// --------------------------------------------
// Divergence for LONG
// --------------------------------------------

trendMom = RSI(5);

periods = 5;
Ksmooth = 3;
trendStoch=StochK( periods , Ksmooth);

bull=High - EMA(Close,13);
bear=Low - EMA(Close,13);
bb=bull+bear;

Hist=MACD(12,26)-Signal(12,26,9);

FI = (Close - Ref(Close, -1)) * V;

trendZig = Zig(L, numChg);

f = trendZig > Ref(trendZig, -1) AND Ref(trendZig, -1) < Ref(trendZig, -2);

p1 = ValueWhen(f, Ref(trendZig, -1), 1);
p2 = ValueWhen(f, Ref(trendZig, -1), 2);
r1 = ValueWhen(f, Ref(trendMom,-1), 1);
r2 = ValueWhen(f, Ref(trendMom,-1), 2);
stockUp1= ValueWhen(f, Ref(trendStoch,-1), 1);
stockUp2= ValueWhen(f, Ref(trendStoch,-1), 2);
bbUP1= ValueWhen(f, Ref(bb,-1), 1);
bbUP2= ValueWhen(f, Ref(bb,-1), 2);
HistUP1= ValueWhen(f, Ref(Hist,-1), 1);
HistUP2= ValueWhen(f, Ref(Hist,-1), 2);
FIUP1= ValueWhen(f, Ref(FI,-1), 1);
FIUP2= ValueWhen(f, Ref(FI,-1), 2);

f = r1 > r2 AND p1 < p2;
fStoch=stockUp1 > stockUp2 AND p1 < p2;
fbb=bbUP1> bbUP2 AND p1 < p2;
HistUP=HistUP1> HistUP2 AND p1 < p2;
FIUP= FIUP1 > FIUP2 AND p1 < p2;

sigRSI = f AND NOT Ref(f, -1) AND trendMom > Ref(trendMom, -1);
sigStockUp = fStoch AND NOT Ref(fStoch, -1) AND trendStoch > Ref(trendStoch, -1);
sigbbUp = fbb AND NOT Ref(fbb, -1) AND bb> Ref(bb, -1);
HistSigUP = HistUP AND NOT Ref(HistUP , -1) AND Hist> Ref(Hist, -1);
FISigUP = FIUP AND NOT Ref(FIUP, -1) AND FI> Ref(FI, -1);

//_N(str = "(" + period + ")");
///Plot(trendMom, "RSI" + str, colorWhite);
Plot(trendZig, "zig", colorBlack,styleOwnScale);
Plot(sigRSI* 100, "RsiSigUp", colorGreen, styleHistogram + styleThick + styleNoLabel);
Plot(sigStockUp* 100, "StockSigUp", colorGreen, styleHistogram + styleThick + styleNoLabel);
Plot(sigbbUp* 100, "bbUp", colorGreen, styleHistogram + styleThick + styleNoLabel);
Plot(HistSigUP * 100, "MACDUp", colorGreen, styleHistogram + styleThick + styleNoLabel);
Plot(FISigUP * 100, "FIUP", colorGreen, styleHistogram + styleThick + styleNoLabel);

// --------------------------------------------
// Divergence for SHORT
// --------------------------------------------

trendZig2 = Zig(H, numChg);

f = trendZig2 < Ref(trendZig2, -1) AND Ref(trendZig2, -1) > Ref(trendZig2, -2);

p1 = ValueWhen(f, Ref(trendZig2, -1), 1);
p2 = ValueWhen(f, Ref(trendZig2, -1), 2);
r1 = ValueWhen(f, Ref(trendMom,-1), 1);
r2 = ValueWhen(f, Ref(trendMom,-1), 2);
stockDown1= ValueWhen(f, Ref(trendStoch,-1), 1);
stockDown2= ValueWhen(f, Ref(trendStoch,-1), 2);
bbDown1= ValueWhen(f, Ref(bb,-1), 1);
bbDown2= ValueWhen(f, Ref(bb,-1), 2);
HistDown1= ValueWhen(f, Ref(Hist,-1), 1);
HistDown2= ValueWhen(f, Ref(Hist,-1), 2);
FIDown1= ValueWhen(f, Ref(FI,-1), 1);
FIDown2= ValueWhen(f, Ref(FI,-1), 2);

f = r1 < r2 AND p1 > p2;
fStoch=stockDown1 < stockDown2 AND p1 > p2;
fbb=bbDown1 < bbDown2 AND p1 > p2;
HistDown=HistDown1< HistDown2 AND p1 > p2;
FIDown= FIDown1 < FIDown2 AND p1 > p2;

sigRSIDown = f AND NOT Ref(f, -1) AND trendMom < Ref(trendMom, -1);
sigStock = fStoch AND NOT Ref(fStoch, -1) AND trendStoch < Ref(trendStoch, -1);
sigbbDown = fbb AND NOT Ref(fbb, -1) AND bb < Ref(bb, -1);
HistSigDown = HistDown AND NOT Ref(HistDown , -1) AND Hist< Ref(Hist, -1);
FISigDown = FIDown AND NOT Ref(FIDown, -1) AND FI< Ref(FI, -1);

//_N(str = "(" + period + ")");
Plot(sigRSIDown* 100, "rsiSigDown", colorRed, styleHistogram + styleThick + styleNoLabel);
Plot(sigStock* 100, "StockSigDown", colorRed, styleHistogram + styleThick + styleNoLabel);
Plot(sigbbDown* 100, "bbDown", colorRed, styleHistogram + styleThick + styleNoLabel);
Plot(HistSigDown * 100, "MAcdDown", colorRed, styleHistogram + styleThick + styleNoLabel);
Plot(FISigDown* 100, "FIDown", colorRed, styleHistogram + styleThick + styleNoLabel);

divergenceCount+=IIf(sigRSI,1,0) + IIf(FISigUP,1,0)+
IIf(sigStockUp ,1,0)+ IIf(sigbbUp ,1,0)+ IIf(HistSigUp ,1,0);

divergenceCount+=IIf(sigRSIDown ,1,0) + IIf(sigStock ,1,0)+
IIf(sigbbDown ,1,0)+ IIf(HistSigDown ,1,0)+ IIf(FISigDown,1,0);

BuyCondTmp=sigRSI OR sigStockUp OR sigbbUp OR HistSigUp OR FISigUP;
ShortCondTmp=sigRSIDown OR sigStock OR sigbbDown OR HistSigDown OR FISigDown;

ShortCond= ShortCond OR ShortCondTmp;
ShortCond=ShortCond AND (highCount>10);

WriteIf(sigRSI,"rsiup ","")+WriteIf(sigStockUp,"stochup ","")+WriteIf(sigbbUp,"bbup ","")
+WriteIf(HistSigUP ,"macdup ","")+ WriteIf(FISigUP,"FIUP ","")

+WriteIf(sigRSIDown ,"rsiDown ","")+WriteIf(sigStock ,"stochDown ","")+WriteIf(sigbbDown,"bbDown ","")
+WriteIf(HistSigDown ,"macdDown ","")+ WriteIf(FISigDown,"FIDown ","")

} //for

Title=globalTitile;

ExitAllPositionsTime = 151500;
//change = Param("% change",0.1,0.1,25,0.1);
z = Zig(Close, 0.8 );

Sell = IIf(z < Ref(z, -1), 1, 0) ;// OR TimeNum() > ExitAllPositionsTime;
Cover = IIf(z > Ref(z, -1), 1, 0) ;//OR TimeNum() > ExitAllPositionsTime;

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

Joined:
May 7, 2015
Messages:
1,226