Learn How To Fish

Status
Not open for further replies.

VJAY

Well-Known Member

VJAY

Well-Known Member
Good post by Drvinod_k....

Some points of discipline to observe before any trader initiates a trade. Always measure a trade for the following criteria, and you will find that, you will end up taking a fewer but more fruitful trades : 1) Trend 2) Range 3) Entry 4) Exit 5) Stop loss.
1) Trend : Most trades, would not be initiated in the first place, as most novice traders are not sure about the trend.
2) Range : Baring the breakouts and breakdowns, the markets move in a range most of the time.
3) Entry : Most traders get sucked into a trade, after looking at sharp fluctuation of prices, which is a wrong practice. Entries need to be planned. ( Deliberate use of the words sucked and planned ).
4) Exit : Once entry has been made, the market decides the further course. Either the target is achieved or we get stopped out.
Even if the trade is in your favor, it is always better to come out a couple of points earlier than your expected target.
5) Stop loss : Stop loss is a discipline which protects your capital. I have heard ridiculous arguments form traders, saying that, "Stop loss always gets triggered."

Inculcate a habit to consider these points before initiating a trade and one can avoid most mishaps. Most mishaps, in the markets, develop into epics, simply because a stop loss is not employed.

Many people start as traders, then they are forced to become investors, and finally they become analysts.
 

josh1

Well-Known Member
Here I have developed AFL for Vijay's Pivots. They are being plotted 99% right. However, there are certain lacuna, to do things for which I require help.
1. H and L are some times not the highest or lowest point due to nature of definition of pivots. (If that is alright, we leave them as it is.)
2. In case of forced pivots ( crossing of mh or ml), next pivot marking may be one or two bars delayed.
3. I was not able to plotshapes.
4. I was not able to mark HH, HL, LH, LL. If that logic is determined, we will be able to mark VPH and VPL also.
5. Trend lines can also be drawn. (That can be done later).

Happy Singh, can you help? Half of the logic based on your "Inside Close" AFL.
Any other member who can contribute is also welcome.
C-like:
_SECTION_BEGIN("Vijay's Pivots");


GraphXSpace=10;
Bhclr = ParamColor("BHcolor",colorRed); Blclr = ParamColor("BLColor",colorLime); Brclr = ParamColor("Barcolour",colorPlum);

//If we have a possible pivot Low bar then first we need one higher High AND higher Low bar it may be  after any number of bars without going new lows.
//..when that HH-HL bar breaks upside, that possible pivot Low is Now real mPL. Vice versa for mPH.
//BaseHi and BaseLo will be determined for bars which are not inside any previous bar.
// BaseHi AND BaseLo will Hold High AND Low of Base bars respectively.
BaseHi        = Hx1 = H[0];        BaseLo        =     L[0];
BaseHi_1    = H[0];    BaseLo_1    = L[0];
BaseHi_2    = H[0];    BaseLo_2    = L[0];
trend = -1 ;                                         //Trend is up when 1 and down when 0.
x1 = 0; x2 = 0; x3 =0;                           // These will hold bar number of Base bars
Counter = 0;                                        // Since
pl1 = pl0 = ph1 = ph0 = 0;

for (i = 2; i < BarCount; i++)
{
        IamInside[i] = H[i] <= BaseHi[i-1] AND L[i] >= BaseLo[i-1];
        IamOutside= NOT IamInside;

        if (IamInside[i])
         {
            BaseHi[i]       = BaseHi[i-1];
            BaseLo[i]   = BaseLo[i-1];   
            BaseHi_1[i] = BaseHi_1[i-1];
            BaseLo_1[i] = BaseLo_1[i-1];
            BaseHi_2[i] = BaseHi_2[i-1];
            BaseLo_2[i] = BaseLo_2[i-1];
        }
        else
        {   
            x1 = x2; x2 = x3; x3 =i;
            BaseHi[i]   = H[i];
            BaseLo[i]   = L[i];
            BaseHi_1[i] = BaseHi[i-1];
            BaseLo_1[i] = BaseLo[i-1];
            BaseHi_2[i] = BaseHi_1[i-1];
            BaseLo_2[i] = BaseLo_1[i-1];
            pl = H[i] > BaseHi_1[i] AND BaseHi_1[i] > BaseHi_2[i] ;
            ph = L[i] < BaseLo_1[i] AND BaseLo_1[i] < BaseLo_2[i] ;
            if (pl AND trend !=1)
            {     pl0 = pl1; pl1 = x1;
                trend = 1;
            }
            if (ph AND trend !=0)
            {     ph0 = ph1; ph1 = x1;
                trend = 0;
            }
                //Code to identify forced pivots
            if (L[x3] < L[pl1] AND trend ==1)
            {     pl0 = pl1;
                IIf( H[x1]>H[x2], ph0= ph1, ph1=x2);
                trend = 0;
                x1 = x2;
            }
            if (H[x3]>H[ph1] AND trend ==0)
            {     pl0 = pl1;
                IIf(L[x1]<L[x2], pl0 = pl1, pl1=x2);
                trend = 1;
                x1=x2;
            }

//                IIf(H[pl1]>L[pl0],PlotText("hl",pl1,L[pl1]-5,colorWhite),PlotText("ll",pl1,L[pl1]-5,colorWhite));
                PlotText("L",pl1,L[pl1]-3,colorWhite);
//                IIf(H[ph1]>H[ph0],PlotText("hh",ph1,H[ph1]+3,colorWhite),PlotText("lh",ph1,H[ph1]+3,colorWhite));
                PlotText("H",ph1,H[ph1]+1,colorWhite);
        }       
}

Plot(BaseHi, "\nBH",Bhclr, styleStaircase|styleThick|styleNoTitle,0,0,0,-2);     
Plot(BaseLo, "   BL",Blclr, styleStaircase|styleThick|styleNoTitle,0,0,0,-2); 
PlotOHLC(BaseLo,BaseHi,BaseLo,BaseHi,"InsideCl",BrClr,styleCloud|styleNoLabel|styleNoTitle,0,0,0,-2);
//PlotShapes(ph*shapeSmallCircle,colorRed,0,H,15);
//PlotShapes(pl*shapeSmallCircle,colorGreen,0,L,-15);


_SECTION_END();



_SECTION_BEGIN("Price & Title");

Layer = Param("Layer", 0, -5, 5, 1) ;
//SetBarFillColor(colorHighliter);
//Plot(C, "Close", IIf(colorHighliter == ColorRGB(128, 0, 128), ColorRGB(255, 0, 255), IIf(colorHighliter == ColorRGB(128, 128, 0), ColorRGB(255, 255, 0), IIf(C > O, ColorRGB(0, 255, 0), IIf(C < O, ColorRGB(255, 0, 0), ColorRGB(255, 255, 255))))), styleCandle|styleNoTitle, Null, Null, 0,Layer);
   fvb=Status("firstvisiblebar");
   lvb=Status("lastvisiblebar");


Plot( C, "Close", ParamColor("Price",colorWhite), styleNoTitle | styleCandle ,0,0,0,Layer );
ToolTip = "Open = " + O + "\nHigh = " + H + "\nLow = " + L + "\nClose = " + C ;

_N(Title = StrFormat(EncodeColor( colorPaleGreen ) + "{{NAME}} - {{INTERVAL}} {{DATE}} " + " Open %g,"  + " Hi %g, "
+ " Lo %g, "  + " C %g  {{VALUES}}", O, H, L, C )+"  LH = " + NumToStr(Ref(H,-1), 1.4) + "  LL = " + NumToStr(Ref(L,-1), 1.4) + ",    trend = " + trend
+ "\n"  + "  BaseHi_1 = "  + NumToStr(BaseHi_1, 1.4) + ",  BaseHi_2 = " + NumToStr(BaseHi_2, 1.4) + ",    ph1 = " + NumToStr(ph1, 1.2) + ",    ph0 = " + NumToStr(ph0, 1.2)
+ "\n"  + "  BaseLo_1 = "  + NumToStr(BaseLo_1, 1.4) + ",  BaseLo_2 = " + NumToStr(BaseLo_2, 1.4) + ",    pl1 = " + NumToStr(pl1, 1.2) + ",    pl0 = " + NumToStr(ph0, 1.2)
+ "\n"  + "  x1 = "  + NumToStr(x1, 1.0) + ",  x2 = " + NumToStr(x2, 1.0) + ",    x3 = " + NumToStr(x3, 1.0)+ ",    i = " + NumToStr(i, 1.0)
//+"\n"  + "  FVB = "  + NumToStr(fvb, 1.2)  + "  LVB = "  + NumToStr(lvb, 1.2)
// + ",    i1 = " + NumToStr(i1, 1.4)  + ",    T1 = " + NumToStr(T1, 1.4)
);
_SECTION_END();


_SECTION_BEGIN("Dummy");

_SECTION_END();
 

KAL.YUG

Well-Known Member
Josh1

Check This AFL for plotting PH & PL
Hopefully this will help.

K

C-like:
_SECTION_BEGIN("Pivot 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.001,0.001,0.02,0.001);

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));


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

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",colorRed), styleLine);
Plot(Ref(Plfilter,2), "Downfilter",ParamColor("dnfilter Color",colorGreen), styleLine);

 
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();

_SECTION_BEGIN("Macd coloured price");
//============================================candle sticks=======================

a = Param("fast macd period",3,1,100,1);
b = Param("slow macd period",34,1,100,1);

Up_MACD_TRIX = MACD( a, b ) > Ref(MACD(a,b),-1);

Down_MACD_TRIX =  MACD( a, b ) < Ref(MACD(a,b),-1);

Colormacd = IIf(Up_MACD_TRIX,colorLime,IIf(Down_MACD_TRIX,colorRed,colorCustom12)); 


PlotOHLC( Open, High, Low, Close, "", Colormacd, styleBar ); 

_SECTION_END();
 

KAL.YUG

Well-Known Member
Trend lines AFL
(Pls also check Paramaters for custom adjustments)

Code:
// E.M.Pottasch, 9/20/2010
// using fractals for trendlines
// Version 6a:
// each trendline has 3 segments, see explanations
// between code. Segments are not combined because
// some trendlines partially overlap

SetBarsRequired(sbrAll,sbrAll);
xx = Cum(1);
nbar = Param("nbar",5,2,50,1);

// define fractals
PHigh = H > Ref(HHV(H,nbar),-1) AND Ref(HHV(H,nbar),nbar) <= H;
PHighPrice = ValueWhen(PHigh,H);
PLow = L < Ref(LLV(L,nbar),-1) AND Ref(LLV(L,nbar),nbar) >= L;
PLowPrice = ValueWhen(PLow,L);

// lower trendline, segment 1: between fractal points
startval_L = ValueWhen(PLow,L,1);
endval_L = ValueWhen(PLow,L,0);
startbar_L = ValueWhen(PLow,xx,1);
endbar_L = ValueWhen(PLow,xx,0);
aa_L = (endval_L-startval_L)/(endbar_L-startbar_L);
bb_L = startval_L;
trendline_L = aa_L * (xx - startbar_L) + bb_L;
// slope calculations for display purpose only
dtrendline_L = trendline_L - Ref(trendline_L,-1);

// lower extended trendline, segment 2: extend segment 1 to next fractals pivot
startval_L_extend = ValueWhen(PLow,L,2);
endval_L_extend = ValueWhen(PLow,L,1);
startbar_L_extend = ValueWhen(PLow,xx,2);
endbar_L_extend = ValueWhen(PLow,xx,1);
aa_L_extend = (endval_L_extend-startval_L_extend)/(endbar_L_extend-startbar_L_extend);
bb_L_extend = startval_L;
trendline_L_extend = aa_L_extend * (xx - startbar_L) + endval_L_extend;
// slope calculations for display purpose only
dtrendline_L_extend = trendline_L_extend - Ref(trendline_L_extend,-1);
dtrendline_L_extend = IIf(PLow,Ref(dtrendline_L,-1),dtrendline_L_extend);

// lower extended trendline, segment 3: extend segment 2 nbars past the fractal pivot
startval_L_extend2 = ValueWhen(PLow,L,3);
endval_L_extend2 = ValueWhen(PLow,L,2);
startbar_L_extend2 = ValueWhen(PLow,xx,3);
endbar_L_extend2 = ValueWhen(PLow,xx,2);
aa_L_extend2 = (endval_L_extend2-startval_L_extend2)/(endbar_L_extend2-startbar_L_extend2);
bb_L_extend2 = endval_L_extend2;
trendline_L_extend2 = aa_L_extend2 * (xx - endbar_L_extend2) + endval_L_extend2;
// slope calculations for display purpose only
dtrendline_L_extend2 = trendline_L_extend2 - Ref(trendline_L_extend2,-1);
dtrendline_L_extend2 = IIf(PLow,Ref(dtrendline_L_extend,-1),dtrendline_L_extend2);

// upper trendline, segment 1: between fractal points
startval_H = ValueWhen(PHigh,H,1);
endval_H = ValueWhen(PHigh,H,0);
startbar_H = ValueWhen(PHigh,xx,1);
endbar_H = ValueWhen(PHigh,xx,0);
aa_H = (endval_H-startval_H)/(endbar_H-startbar_H);
bb_H = startval_H;
trendline_H = aa_H * (xx - startbar_H) + bb_H;
// slope calculations for display purpose only
dtrendline_H = trendline_H - Ref(trendline_H,-1);

// upper extended trendline, segment 2: extend segment 1 to next fractals pivot
startval_H_extend = ValueWhen(PHigh,H,2);
endval_H_extend = ValueWhen(PHigh,H,1);
startbar_H_extend = ValueWhen(PHigh,xx,2);
endbar_H_extend = ValueWhen(PHigh,xx,1);
aa_H_extend = (endval_H_extend-startval_H_extend)/(endbar_H_extend-startbar_H_extend);
bb_H_extend = startval_H;
trendline_H_extend = aa_H_extend * (xx - startbar_H) + endval_H_extend;
// slope calculations for display purpose only
dtrendline_H_extend = trendline_H_extend - Ref(trendline_H_extend,-1);
dtrendline_H_extend = IIf(PHigh,Ref(dtrendline_H,-1),dtrendline_H_extend);

// upper extended trendline, segment 3: extend segment 2 nbars past the fractal pivot
startval_H_extend2 = ValueWhen(PHigh,H,3);
endval_H_extend2 = ValueWhen(PHigh,H,2);
startbar_H_extend2 = ValueWhen(PHigh,xx,3);
endbar_H_extend2 = ValueWhen(PHigh,xx,2);
aa_H_extend2 = (endval_H_extend2-startval_H_extend2)/(endbar_H_extend2-startbar_H_extend2);
bb_H_extend2 = endval_H_extend2;
trendline_H_extend2 = aa_H_extend2 * (xx - endbar_H_extend2) + endval_H_extend2;
// slope calculations for display purpose only
dtrendline_H_extend2 = trendline_H_extend2 - Ref(trendline_H_extend2,-1);
dtrendline_H_extend2 = IIf(PHigh,Ref(dtrendline_H_extend,-1),dtrendline_H_extend2);

tld = ParamToggle("All trendlines","show|hide",1);
if (tld)
{
    // omit uptrending upper trendlines and downtrending lower trendlines
    trendline_L = IIf(dtrendline_L > 0,trendline_L,Null);
    trendline_L_extend = IIf(dtrendline_L_extend > 0,trendline_L_extend,Null);
    trendline_L_extend2 = IIf(dtrendline_L_extend2 > 0,trendline_L_extend2,Null);
    trendline_H = IIf(dtrendline_H < 0,trendline_H,Null);
    trendline_H_extend = IIf(dtrendline_H_extend < 0,trendline_H_extend,Null);
    trendline_H_extend2 = IIf(dtrendline_H_extend2 < 0,trendline_H_extend2,Null);
}

trendline_L_extend2 = IIf(BarsSince(Plow) <= nbar,trendline_L_extend2,Null);
trendline_H_extend2 = IIf(BarsSince(PHigh) <= nbar,trendline_H_extend2,Null);

// chart
GraphXSpace = 5;
SetChartOptions(0, chartShowDates);
chartflag = ParamToggle("Heikin Ashi","show|hide",1);
if (chartFlag)
{
    Plot( C, "\nPrice",colorWhite, styleCandle );
}
else
{
    HaClose = (O+H+L+C)/4;
    HaOpen = AMA( Ref( HaClose, -1 ), 0.5 );
    HaHigh = Max( H, Max( HaClose, HaOpen ) );
    HaLow = Min( L, Min( HaClose, HaOpen ) );
    PlotOHLC( HaOpen, HaHigh, HaLow, HaClose, "Modified " + Name(), colorWhite, styleCandle );   
}

PlotShapes(shapeSmallCircle*PLow,colorGreen,0,L,-10);
PlotShapes(shapeSmallCircle*PHigh,colorRed,0,H,10);
// segment 1
Plot(trendline_L, "\nLower Trendline", colorBrightGreen,styleLine);
// segment 2, grey dots unconfirmed trend, green confirmed trend
Plot(IIf(BarsSince(Plow) <= nbar,trendline_L_extend,Null), "", colorLightGrey,styleNoLine | styleDots | styleThick);
Plot(IIf(BarsSince(Plow) > nbar,trendline_L_extend,Null), "",colorDarkGreen, styleNoLine | styleDots | styleThick);
// segment 3
Plot(IIf(BarsSince(Plow) <= nbar,trendline_L_extend2,Null), "",colorDarkGreen,styleNoLine | styleDots| styleThick);
// segment 1
Plot(trendline_H, "\nUpper Trendline", colorRed,styleLine);
// segment 2, grey dots unconfirmed trend, green confirmed trend
Plot(IIf(BarsSince(PHigh) <= nbar,trendline_H_extend,Null), "", colorLightGrey, styleNoLine | styleDots | styleThick);
Plot(IIf(BarsSince(PHigh) > nbar,trendline_H_extend,Null), "",colorOrange, styleNoLine | styleDots | styleThick);
// segment 3
Plot(IIf(BarsSince(PHigh) <= nbar,trendline_H_extend2,Null), "",colorOrange, styleNoLine | styleDots | styleThick);

Title = Name() +
" | " + EncodeColor( colorYellow ) + "nbar: " + nbar + EncodeColor( colorWhite ) +
" | " + EncodeColor( colorBrightGreen ) + "Lower Trendline: " + trendline_L + EncodeColor( colorWhite ) +
" | " + EncodeColor( colorRed ) + "Upper Trendline: " + trendline_H + EncodeColor( colorWhite );

signalFlag = ParamToggle("Possible Signals","show|hide",1);
if (signalFlag)
{
// ***** possible buy and short signal *****
Buy = (!IsEmpty(trendline_H_extend) AND Cross(C,trendline_H_extend) AND BarsSince(PHigh) > nbar) OR
(!IsEmpty(trendline_H_extend2) AND Cross(C,trendline_H_extend2) AND !PHigh) OR
(PHigh AND C > trendline_H_extend2 AND Ref(C,-1) < Ref(trendline_H_extend,-1) AND !IsEmpty(trendline_H_extend) AND !IsEmpty(trendline_H_extend2) );
BuyPrice = C;
Short = (!IsEmpty(trendline_L_extend) AND Cross(trendline_L_extend,C)  AND BarsSince(PLow) > nbar)  OR
(!IsEmpty(trendline_L_extend2) AND Cross(trendline_L_extend2,C) AND !PLow) OR
(PLow AND C < trendline_L_extend2 AND Ref(C,-1) > Ref(trendline_L_extend,-1) AND !IsEmpty(trendline_L_extend) AND !IsEmpty(trendline_L_extend2));
ShortPrice = C;
Sell = 0;
Cover = 0;

PlotShapes(IIf(Buy,shapeUpArrow,shapeNone),colorGreen,0,L,-15);
PlotShapes(IIf(Buy,shapeHollowUpArrow,shapeNone),colorWhite,0,L,-15);
PlotShapes(IIf(Buy,shapeHollowSmallCircle,shapeNone),colorWhite,0,BuyPrice,0);

PlotShapes(IIf(Short,shapeDownTriangle,shapeNone),colorYellow,0,H,IIf(Short AND Sell,-30,-15));
PlotShapes(IIf(Short,shapeHollowDownTriangle,shapeNone),colorWhite,0,H,IIf(Short AND Sell,-30,-15));
PlotShapes(IIf(Short,shapeHollowCircle,shapeNone),colorWhite,0,ShortPrice,0);
}
 
Status
Not open for further replies.

Similar threads