[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [amibroker] TTM Squeeze [trend indicator and pivots added for Forex]



PureBytes Links

Trading Reference Links

Alex

This is an effort for Carter's Squeeze I attempted a while ago.
See if it suits what you need.

//****************
//****************
// www.tradethemarkets.com with John Carter.

// the squeeze.

SetChartBkColor(colorBlack);
SetChartOptions(1,0,1);
// first the momentum indicator

_SECTION_BEGIN("Momo");
mPeriod = Param("mPeriod", 12, 5, 30,1);
momo = ROC(C,mPeriod);
momo = MA( momo, mPeriod);
Colorup = IIf( momo < Ref(Momo,-1) AND Ref (momo,-1) < Ref(momo,-2), 
ColorRGB(10,10,198), ColorRGB(13,26,255));
Colordown = IIf( momo > Ref(momo,-1) AND Ref(momo,-1) > Ref( momo,-2), 
ColorRGB(180,0,0), colorRed);
momocolor = IIf(momo > 0, Colorup,IIf(momo < 0, Colordown,Null));
_SECTION_END();

_SECTION_BEGIN("Channels");// define bb and keltners

bPeriod = Param("bPeriod", 20, 10,60, 1);
btop = BBandTop(C,bperiod,2);
bbot = BBandBot(C,bperiod,2);

// keltners

P = ParamField("Price field",-1);
kPeriod = Param("kPeriod", 20, 2, 100, 1 );
kWidth = Param("kWidth", 1.5, 0, 10, 0.05 );
//Color = ParamColor("Color", colorCycle );
//Style = ParamStyle("Style");

CenterLine = MA( P, kPeriod );
KTop   = CenterLine + kWidth * ATR( kPeriod );
KBot = CenterLine - kWidth * ATR( kPeriod );

//Plot( KTop, "KBTop" + _PARAM_VALUES(), Color, Style );
//Plot( KBot, "KBBot" + _PARAM_VALUES(), Color, Style );
//Plot( 0, "", colorLime, styleLine);
_SECTION_END();

Squeeze = btop < Ktop AND bbot > KBot;
Colorsqueeze = IIf(squeeze, colorRed, colorLime);

Plot ( 0 , "", Colorsqueeze, styleDots | styleThick | styleNoLine );
Plot( momo,"The Squeeze Indicator",momocolor,styleHistogram | styleThick );


Title = EncodeColor(colorWhite)  + Name() + "  {{INTERVAL}}   {{DATE}}" ;

//*********
// END OF SQUEEZE
//*********

I don't use this too much.

Here is what I knocked together for his Trend Indicator and Scalper buy 
and Sells. Not the Gospel truth perhaps, but as close as I could get for 
my needs. This code is used for intraday Forex: adjust as needed for 
your market:

//*************
// CARTER TREND INDICATOR, SCALPER SIGNALS AND PIVOTS
//*************

// John Carter Trend Indicator
// includes pivots.


SetChartBkColor(  colorBlack  );
SetChartOptions(1,0,1);


//Carter scalper signals

_SECTION_BEGIN("Price");
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 ) );
_SECTION_END();


// define the conditions

_SECTION_BEGIN("J Carter Scalper Sells");
IbarH= H > Ref(H,-1) AND H > Ref(H,1);
TBarH = Ref(IbarH,-1);
TBarHLow = ValueWhen(TBarH,L);
firstof3LC = C > Ref(C,1) AND Ref(C,1) > Ref(C,2);
pivotH1 = IbarH AND firstof3LC;
LowCross = Cross(Ref(TBarHLow,1),Ref(C,2)) OR
Cross(Ref(TBarHLow,1),Ref(C,3)) OR
Cross(Ref(TBarHLow,1),Ref(C,4)) OR
Cross(Ref(TBarHLow,1),Ref(C,5));
pivotH2 = IbarH AND LowCross;
S_Sell = PivotH1 OR PivotH2;
_SECTION_END();

_SECTION_BEGIN("J Carter Scalper Buys");
IBarL = L < Ref(L,-1) AND L < Ref(L,1);
TBarL = Ref(IBarL,-1);
TBarLHigh= ValueWhen(TBarL,H);
firstof3HC = C < Ref(C,1) AND Ref(C,1) < Ref(C,2);
pivotL1 = IBarL AND firstof3HC;
HighCross = Cross(Ref(C,2),Ref(TBarLHigh,1 )) OR
Cross(Ref(C,3),Ref(TBarLHigh,1 )) OR
Cross(Ref(C,4),Ref(TBarLHigh,1 )) OR
Cross(Ref(C,5),Ref(TBarLHigh,1 ));
//Cross(Ref(C,2),Ref(TBarLHigh,1 )) OR
pivotL2 = IBarL AND HighCross;
S_Buy = PivotL1 OR PivotL2;

_SECTION_END();

ScalperBuy = ExRem(S_Buy,S_Sell);
ScalperSell = ExRem(S_Sell,S_Buy);

barcolor = IIf(S_Sell OR S_Buy, colorWhite,IIf(HaClose >= HaOpen, 
colorLime, colorRed));
// bar types
bars = Param("bar types", 128,64,128,64);
//SetChartOptions(1,chartShowArrows|chartShowDates);
_N(Title = EncodeColor(colorWhite) + 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, "Close", barcolor, styleNoTitle | bars | styleThick | 
GetPriceStyle() );
ToolTip=StrFormat("Open: %g\nHigh: %g\nLow: %g\nClose: %g 
(%.1f%%)\nVolume: "+NumToStr( V, 1 ), O, H, L, C, SelectedValue( ROC( C, 
1 )));

//PlotShapes( shapeDownArrow * iBar,colorWhite,0,H,-12);
//PlotShapes( shapeHollowCircle * Ref(PivotHigh,-1), colorWhite,0,H, 25);
//PlotShapes( shapeHollowCircle * Ref(PivotLow,-1), colorPink,0,L, -25);

//PlotShapes( shapeUpArrow * Cond1, colorGreen, 0, L, -12);
//Plot(TBarL,"",colorWhite,1);
//Plot(TBarH,"",colorPink,1);
PlotShapes(shapeHollowDownTriangle * Scalpersell,colorRed,0,H, -30);
PlotShapes(shapeHollowUpTriangle * Scalperbuy,colorLime,0,L, -30);
//PlotShapes( shapeCircle , colorRed,0,H,30);
//PlotShapes(shapeCircle * ThreeLows,colorWhite,0,L, -15);
//PlotShapes( shapeUpArrow * RBBullish, colorWhite, 0, L, -12);
//PlotShapes( shapeDownArrow * RBBearish, colorWhite, 0, H , -20);

// Pivots

numbars = LastValue(Cum(Status("barvisible")));

_SECTION_BEGIN("Values");
HighestinRange = HHV(H,numbars);
LowestinRange = LLV(L,numbars);
Pips = Param("Pips", 30, 5, 100, 5);
Factor = IIf( StrRight(Name(),3) == "JPY", 0.01, 0.0001);
Buffer = pips * factor;
TopRange = LastValue( HHV(H,Numbars) + Buffer);
BottomRange = LastValue( LLV(L,Numbars) - Buffer);

_SECTION_END();

//Plot(C,"close", colorWhite, styleBar);
//Plot(HighestInRange,"",colorLime,1);
//Plot(HighestinRange + Buffer,"",colorSkyblue,1);
//Plot ( TopRange,"", colorRed, 1);
//Plot ( LowestinRange,"",colorLime,1);
//Plot( LastValue(LowestinRange),"",colorYellow,1);
//Plot ( LowestinRange - Buffer,"",colorSkyblue);
//Plot ( BottomRange,"", colorRed);
//Title = EncodeColor(colorWhite) + Name() + "   numbars == " + 
WriteVal(numbars, 3.0);


_SECTION_BEGIN("Pivots Daily");

TimeFrameSet(inDaily);

dH1 = Ref(H,-1);
dL1 = Ref(L,-1);
dC1 = Ref(C,-1);

dPP =(dH1 + dL1 + dC1)/3;

dR3 = dH1 + 2*(dPP - dL1);
dR2 = dPP + (dH1 - dL1);
dR1 = (2*dPP) - dL1;
dPP = dPP;
dS1 = (2*dPP) - dH1;
dS2 = dPP - (dH1 - dL1);
dS3 = dL1 - 2*(dH1-dPP);
dM4 = (dR1 + dR2)/2;
dM3 = (dR1 + dPP)/2;
dM2 = (dPP + dS1)/2;
dM1 = (dS1 + dS2)/2;

TimeFrameRestore();

R3d = TimeFrameExpand(dR3,inDaily,expandFirst);
R2d = TimeFrameExpand(dR2,inDaily,expandFirst);
M4d = TimeFrameExpand(dM4,inDaily,expandFirst);
R1d = TimeFrameExpand(dR1,inDaily,expandFirst);
M3d = TimeFrameExpand(dM3,inDaily,expandFirst);
PPd = TimeFrameExpand(dPP,inDaily,expandFirst);
M2d = TimeFrameExpand(dM2,inDaily,expandFirst);
S1d = TimeFrameExpand(dS1,inDaily,expandFirst);
M1d = TimeFrameExpand(dM1,inDaily,expandFirst);
S2d = TimeFrameExpand(dS2,inDaily,expandFirst);
S3d = TimeFrameExpand(dS3,inDaily,expandFirst);

_SECTION_END();

_SECTION_BEGIN("Weekly Pivot");
TimeFrameSet(inWeekly);
wH1 = Ref(H,-1);
wL1 = Ref(L,-1);
wC1 = Ref(C,-1);
wPP =(wH1 + wL1 + wC1)/3;
wR3 = wH1 + 2*(wPP - wL1);
wR2 = wPP + (wH1 - wL1);
wR1 = (2*wPP) - wL1;
wPP = wPP;
wS1 = (2*wPP) - wH1;
wS2 = wPP - (wH1 - wL1);
wS3 = wL1 - 2*(wH1-wPP);
wM4 = (wR1 + wR2)/2;
wM3 = (wR1 + wPP)/2;
wM2 = (wPP + wS1)/2;
wM1 = (wS1 + wS2)/2;

TimeFrameRestore();

R3w = TimeFrameExpand(wR3,inWeekly,expandFirst);
R2w = TimeFrameExpand(wR2,inWeekly,expandFirst);
M4w = TimeFrameExpand(wM4,inWeekly,expandFirst);
R1w = TimeFrameExpand(wR1,inWeekly,expandFirst);
M3w = TimeFrameExpand(wM3,inWeekly,expandFirst);
PPw = TimeFrameExpand(wPP,inWeekly,expandFirst);
M2w = TimeFrameExpand(wM2,inWeekly,expandFirst);
S1w = TimeFrameExpand(wS1,inWeekly,expandFirst);
M1w = TimeFrameExpand(wM1,inWeekly,expandFirst);
S2w = TimeFrameExpand(wS2,inWeekly,expandFirst);
S3w = TimeFrameExpand(wS3,inWeekly,expandFirst);

_SECTION_END();

_SECTION_BEGIN("Monthly Pivots");

TimeFrameSet(inMonthly);

mH1 = Ref(H,-1);
mL1 = Ref(L,-1);
mC1 = Ref(C,-1);
mPP =(mH1 + mL1 + mC1)/3;
mR3 = mH1 + 2*(mPP - mL1);
mR2 = mPP + (mH1 - mL1);
mR1 = (2*mPP) - mL1;
mPP = mPP;
mS1 = (2*mPP) - mH1;
mS2 = mPP - (mH1 - mL1);
mS3 = mL1 - 2*(mH1-mPP);
mM4 = (mR1 + mR2)/2;
mM3 = (mR1 + mPP)/2;
mM2 = (mPP + mS1)/2;
mM1 = (mS1 + mS2)/2;

TimeFrameRestore();

R3m = TimeFrameExpand(mR3,inMonthly,expandFirst);
R2m = TimeFrameExpand(mR2,inMonthly,expandFirst);
M4m = TimeFrameExpand(mM4,inMonthly,expandFirst);
R1m = TimeFrameExpand(mR1,inMonthly,expandFirst);
M3m = TimeFrameExpand(mM3,inMonthly,expandFirst);
PPm = TimeFrameExpand(mPP,inMonthly,expandFirst);
M2m = TimeFrameExpand(mM2,inMonthly,expandFirst);
S1m = TimeFrameExpand(mS1,inMonthly,expandFirst);
M1m = TimeFrameExpand(mM1,inMonthly,expandFirst);
S2m = TimeFrameExpand(mS2,inMonthly,expandFirst);
S3m = TimeFrameExpand(mS3,inMonthly,expandFirst);
_SECTION_END();

//colors

PivotweeklyColor = ParamColor("Pivot Weekly Color", colorTurquoise);
PivotDailyColor = ParamColor("Pivot Daily Color", colorYellow);
PivotMonthlyColor = ParamColor("Pivot Monthly Color",ColorRGB(255,32,255));
MColor = ParamColor("M Color", colorLightGrey);
RSColorsDaily = ParamColor("R & S colors daily",colorYellow);
SupportColor = ParamColor("Support Color", colorGreen);
LineStyle = ParamStyle("Line Style",styleStaircase,maskAll) + styleNoLabel;
MStyle = ParamStyle("M Line Style", styleStaircase | styleDashed, 
maskAll) + styleNoLabel;

// Assign Pivots to Lastvalue;

// Daily
R3d = LastValue(R3d,1);
R2d = LastValue(R2d,1);
M4d = LastValue(M4d,1);
R1d = LastValue(R1d,1);
M3d = LastValue(M3d,1);
ppd = LastValue(PPd,1);
M2d = LastValue(M2d,1);
S1d = LastValue(S1d,1);
M1d = LastValue(M1d,1);
S2d = LastValue(S2d,1);
S3d = LastValue(S3d,1);
// weekly
R3w = LastValue(R3w,1);
R2w = LastValue(R2w,1);
R1w = LastValue(R1w,1);
ppw = LastValue(PPw,1);
S1w = LastValue(S1w,1);
S2w = LastValue(S2w,1);
S3w = LastValue(S3w,1);
//Monthly
R3m = LastValue(R3m,1);
R2m = LastValue(R2m,1);
R1m = LastValue(R1m,1);
ppm = LastValue(PPm,1);
S1m = LastValue(S1m,1);
S2m = LastValue(S2m,1);
S3m = LastValue(S3m,1);

// Daily plots
Plot( IIf(R3d < TopRange AND R3d > BottomRange, R3d, Null), "", 
PivotDailyColor, styleDashed);
Plot( IIf(R2d < TopRange AND R2d > BottomRange, R2d, Null), "", 
PivotDailyColor, styleDashed);
Plot( IIf(M4d < TopRange AND M4d > BottomRange, M4d, Null), 
"",MColor,styleDashed | styleNoLabel);
Plot( IIf(R1d < TopRange AND R1d > BottomRange, R1d, Null), "", 
RScolorsdaily,  styleDashed);
Plot( IIf(M3d < TopRange AND M3d > BottomRange, M3d, Null), "",Mcolor, 
styleDashed | styleNoLabel);
Plot( IIf(PPd < TopRange AND PPd > BottomRange, PPd, Null), 
"",PivotDailyColor, Linestyle | styleThick);
Plot( IIf(M2d < TopRange AND M2d > BottomRange, M2d, Null), "", MColor, 
styleDashed | styleNoLabel);
Plot( IIf(S1d < TopRange AND S1d > BottomRange, S1d, Null), "", 
RScolorsdaily, styleDashed);
Plot( IIf(M1d < TopRange AND M1d > BottomRange, M1d, Null), "", MColor, 
styleDashed | styleNoLabel);
Plot( IIf(S2d < TopRange AND S2d > BottomRange, S2d, Null), "", 
RScolorsdaily, styleDashed);
Plot( IIf(S3d < TopRange AND S3d > BottomRange, S3d, Null), "", 
RScolorsdaily, styleDashed);

// Weekly Plots
Plot( IIf(R3w < TopRange AND R3w > BottomRange, R3w, Null), 
"",PivotweeklyColor, styleDashed | styleNoLabel);
Plot( IIf(R2w < TopRange AND R2w > BottomRange, R2w, Null), "", 
Pivotweeklycolor,styleDashed | styleNoLabel);
Plot( IIf(R1w < TopRange AND R1w > BottomRange, R1w, Null), "", 
Pivotweeklycolor,styleDashed | styleNoLabel);
Plot( IIf(ppw < TopRange AND ppw > BottomRange, ppw, Null), 
"",PivotweeklyColor, Linestyle | styleThick);
Plot( IIf(S1w < TopRange AND S1w > BottomRange, S1w, Null), "", 
Pivotweeklycolor, styleDashed | styleNoLabel);
Plot( IIf(S2w < TopRange AND S2w > BottomRange, S2w, Null), "", 
Pivotweeklycolor, styleDashed | styleNoLabel);
Plot( IIf(S3w < TopRange AND S3w > BottomRange, S3w, Null), "", 
Pivotweeklycolor, styleDashed | styleNoLabel);


// Monthly Plots
Plot( IIf(R3m < TopRange AND R3m > BottomRange, R3m, Null), "", 
pivotMonthlyColor, styleDashed);
Plot( IIf(R2m < TopRange AND R2m > BottomRange, R2m, Null), "", 
pivotMonthlyColor, styleDashed);
Plot( IIf(R1m < TopRange AND R1m > BottomRange, R1m, Null), "", 
pivotMonthlyColor, styleDashed);
Plot( IIf(ppm < TopRange AND ppm > BottomRange, ppm, Null), "", 
pivotmonthlycolor, Linestyle | styleThick);
Plot( IIf(S1m < TopRange AND S1m > BottomRange, S1m, Null), "", 
PivotmonthlyColor, styleDashed);
Plot( IIf(S2m < TopRange AND S2m > BottomRange, S2m, Null), "", 
PivotmonthlyColor, styleDashed);
Plot( IIf(S3m < TopRange AND S3m > BottomRange, S3m, Null), "", 
PivotmonthlyColor, styleDashed);

// PlotValues
fraction= IIf(StrRight(Name(),3) == "JPY", 3.2, 3.4);

PlotText("Daily Pivot  " + WriteVal(PPd,fraction), 
LastValue(BarIndex())- numbars/2, PPd  , PivotDailyColor);
PlotText("Daily R1  " + WriteVal(R1d,fraction), LastValue(BarIndex())- 
numbars/2, R1d  , PivotDailyColor);
PlotText("Daily S1  " + WriteVal(S1d,fraction), LastValue(BarIndex())- 
numbars/2, S1d  , PivotDailyColor);
PlotText("Daily R2  " + WriteVal(R2d,fraction), LastValue(BarIndex())- 
numbars/2, R2d  , PivotDailyColor);
PlotText("Daily R3  " + WriteVal(R3d,fraction), LastValue(BarIndex())- 
numbars/2, R3d  , PivotDailyColor);
PlotText("Daily S2  " + WriteVal(S2d,fraction), LastValue(BarIndex())- 
numbars/2, S2d  , PivotDailyColor);
PlotText("Daily S3  " + WriteVal(S3d,fraction), LastValue(BarIndex())- 
numbars/2, S3d  , PivotDailyColor);
PlotText("Weekly Pivot   " + WriteVal(PPw,fraction), 
LastValue(BarIndex()) - 3*numbars/4, PPw, PivotWeeklyColor);
PlotText("Weekly R1   " + WriteVal(R1w,fraction), LastValue(BarIndex()) 
- 3*numbars/4, R1w, PivotWeeklyColor);
PlotText("Weekly S1   " + WriteVal(S1w,fraction), LastValue(BarIndex()) 
- 3*numbars/4, S1w, PivotWeeklyColor);
PlotText("Weekly R2   " + WriteVal(R2w,fraction), LastValue(BarIndex()) 
- 3*numbars/4, R2w, PivotWeeklyColor);
PlotText("Weekly S2   " + WriteVal(S2w,fraction), LastValue(BarIndex()) 
- 3*numbars/4, S2w, PivotWeeklyColor);
PlotText("Monthly PP   ", LastValue(BarIndex()) - (numbars-5),PPm, 
PivotMonthlyColor);
PlotText("Monthly R1   ", LastValue(BarIndex()) - (numbars-5),R1m, 
PivotMonthlyColor);
PlotText("Monthly S1   ", LastValue(BarIndex()) - (numbars-5),S1m, 
PivotMonthlyColor);
PlotText("Monthly S2   ", LastValue(BarIndex()) - (numbars-5),S2m, 
PivotMonthlyColor);
PlotText("Monthly S3   ", LastValue(BarIndex()) - (numbars-5),S3m, 
PivotMonthlyColor);

PlotText(WriteVal(PPm,fraction), LastValue(BarIndex()) + 4, PPm, 
PivotMonthlyColor);



//Plot( LastValue( S1m, 1),"\n S1 Monthly", PivotMonthlycolor, styleDashed);

//*************
// END OF CARTER TREND INDICATOR ETC
//*************

Hope this helps
Modify to suit/improve/add etc .

ChrisB


Arnie wrote:
> 
> 
> Hi,
> 
> I was wondering whether anyone had the TTM Squeeze indicator. I have 
> searched through the archives without really finding anything.
> 
> Here is the link to Trade the Markets 
> <http://www.tradethemarkets.com/products/item5.cfm> where it is described.
> 
> Thanks,
> Alex Z
> 





Please note that this group is for discussion between users only.

To get support from AmiBroker please send an e-mail directly to 
SUPPORT {at} amibroker.com

For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/

For other support material please check also:
http://www.amibroker.com/support.html
 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/amibroker/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http://groups.yahoo.com/group/amibroker/join
    (Yahoo! ID required)

<*> To change settings via email:
    mailto:amibroker-digest@xxxxxxxxxxxxxxx 
    mailto:amibroker-fullfeatured@xxxxxxxxxxxxxxx

<*> To unsubscribe from this group, send an email to:
    amibroker-unsubscribe@xxxxxxxxxxxxxxx

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 
// www.tradethemarkets.com with John Carter.

// the squeeze.

SetChartBkColor(colorBlack);
SetChartOptions(1,0,1); 
// first the momentum indicator

_SECTION_BEGIN("Momo");
mPeriod = Param("mPeriod", 12, 5, 30,1);
momo = ROC(C,mPeriod);
momo = MA( momo, mPeriod);
Colorup = IIf( momo < Ref(Momo,-1) AND Ref (momo,-1) < Ref(momo,-2), ColorRGB(10,10,198), ColorRGB(13,26,255));
Colordown = IIf( momo > Ref(momo,-1) AND Ref(momo,-1) > Ref( momo,-2), ColorRGB(180,0,0), colorRed);
momocolor = IIf(momo > 0, Colorup,IIf(momo < 0, Colordown,Null));
_SECTION_END();

_SECTION_BEGIN("Channels");// define bb and keltners

bPeriod = Param("bPeriod", 20, 10,60, 1);
btop = BBandTop(C,bperiod,2);
bbot = BBandBot(C,bperiod,2);

// keltners

P = ParamField("Price field",-1);
kPeriod = Param("kPeriod", 20, 2, 100, 1 );
kWidth = Param("kWidth", 1.5, 0, 10, 0.05 );
//Color = ParamColor("Color", colorCycle );
//Style = ParamStyle("Style");

CenterLine = MA( P, kPeriod );
KTop   = CenterLine + kWidth * ATR( kPeriod );
KBot = CenterLine - kWidth * ATR( kPeriod );

//Plot( KTop, "KBTop" + _PARAM_VALUES(), Color, Style ); 
//Plot( KBot, "KBBot" + _PARAM_VALUES(), Color, Style ); 
//Plot( 0, "", colorLime, styleLine);
_SECTION_END();

Squeeze = btop < Ktop AND bbot > KBot;
Colorsqueeze = IIf(squeeze, colorRed, colorLime);

Plot ( 0 , "", Colorsqueeze, styleDots | styleThick | styleNoLine );
Plot( momo,"The Squeeze Indicator",momocolor,styleHistogram | styleThick );


Title = EncodeColor(colorWhite)  + Name() + "  {{INTERVAL}}   {{DATE}}" ; 
// John Carter Trend Indicator
// includes pivots.



SetChartBkColor(  colorBlack  );
SetChartOptions(1,0,1);


//Carter scalper signals

_SECTION_BEGIN("Price");
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 ) );
_SECTION_END();


// define the conditions

_SECTION_BEGIN("J Carter Scalper Sells");
IbarH= H > Ref(H,-1) AND H > Ref(H,1);
TBarH = Ref(IbarH,-1);
TBarHLow = ValueWhen(TBarH,L);
firstof3LC = C > Ref(C,1) AND Ref(C,1) > Ref(C,2);
pivotH1 = IbarH AND firstof3LC;
LowCross = Cross(Ref(TBarHLow,1),Ref(C,2)) OR
Cross(Ref(TBarHLow,1),Ref(C,3)) OR
Cross(Ref(TBarHLow,1),Ref(C,4)) OR
Cross(Ref(TBarHLow,1),Ref(C,5));
pivotH2 = IbarH AND LowCross;
S_Sell = PivotH1 OR PivotH2;
_SECTION_END();

_SECTION_BEGIN("J Carter Scalper Buys");
IBarL = L < Ref(L,-1) AND L < Ref(L,1);
TBarL = Ref(IBarL,-1);
TBarLHigh= ValueWhen(TBarL,H);
firstof3HC = C < Ref(C,1) AND Ref(C,1) < Ref(C,2);
pivotL1 = IBarL AND firstof3HC;
HighCross = Cross(Ref(C,2),Ref(TBarLHigh,1 )) OR
Cross(Ref(C,3),Ref(TBarLHigh,1 )) OR
Cross(Ref(C,4),Ref(TBarLHigh,1 )) OR
Cross(Ref(C,5),Ref(TBarLHigh,1 ));
//Cross(Ref(C,2),Ref(TBarLHigh,1 )) OR
pivotL2 = IBarL AND HighCross;
S_Buy = PivotL1 OR PivotL2;

_SECTION_END();

ScalperBuy = ExRem(S_Buy,S_Sell);
ScalperSell = ExRem(S_Sell,S_Buy);

barcolor = IIf(S_Sell OR S_Buy, colorWhite,IIf(HaClose >= HaOpen, colorLime, colorRed));
// bar types
bars = Param("bar types", 128,64,128,64);
//SetChartOptions(1,chartShowArrows|chartShowDates);
_N(Title = EncodeColor(colorWhite) + 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, "Close", barcolor, styleNoTitle | bars | styleThick | GetPriceStyle() );
ToolTip=StrFormat("Open: %g\nHigh: %g\nLow: %g\nClose: %g (%.1f%%)\nVolume: "+NumToStr( V, 1 ), O, H, L, C, SelectedValue( ROC( C, 1 )));

//PlotShapes( shapeDownArrow * iBar,colorWhite,0,H,-12);
//PlotShapes( shapeHollowCircle * Ref(PivotHigh,-1), colorWhite,0,H, 25);
//PlotShapes( shapeHollowCircle * Ref(PivotLow,-1), colorPink,0,L, -25);

//PlotShapes( shapeUpArrow * Cond1, colorGreen, 0, L, -12);
//Plot(TBarL,"",colorWhite,1);
//Plot(TBarH,"",colorPink,1);
PlotShapes(shapeHollowDownTriangle * Scalpersell,colorRed,0,H, -30);
PlotShapes(shapeHollowUpTriangle * Scalperbuy,colorLime,0,L, -30);
//PlotShapes( shapeCircle , colorRed,0,H,30);
//PlotShapes(shapeCircle * ThreeLows,colorWhite,0,L, -15);
//PlotShapes( shapeUpArrow * RBBullish, colorWhite, 0, L, -12);
//PlotShapes( shapeDownArrow * RBBearish, colorWhite, 0, H , -20);

// Pivots

numbars = LastValue(Cum(Status("barvisible")));

_SECTION_BEGIN("Values");
HighestinRange = HHV(H,numbars);
LowestinRange = LLV(L,numbars);
Pips = Param("Pips", 30, 5, 100, 5);
Factor = IIf( StrRight(Name(),3) == "JPY", 0.01, 0.0001);
Buffer = pips * factor;
TopRange = LastValue( HHV(H,Numbars) + Buffer);
BottomRange = LastValue( LLV(L,Numbars) - Buffer);

_SECTION_END();

//Plot(C,"close", colorWhite, styleBar);
//Plot(HighestInRange,"",colorLime,1);
//Plot(HighestinRange + Buffer,"",colorSkyblue,1);
//Plot ( TopRange,"", colorRed, 1);
//Plot ( LowestinRange,"",colorLime,1);
//Plot( LastValue(LowestinRange),"",colorYellow,1);
//Plot ( LowestinRange - Buffer,"",colorSkyblue);
//Plot ( BottomRange,"", colorRed);
//Title = EncodeColor(colorWhite) + Name() + "   numbars == " + WriteVal(numbars, 3.0);


_SECTION_BEGIN("Pivots Daily");

TimeFrameSet(inDaily);

dH1 = Ref(H,-1);
dL1 = Ref(L,-1);
dC1 = Ref(C,-1);

dPP =(dH1 + dL1 + dC1)/3;

dR3 = dH1 + 2*(dPP - dL1);
dR2 = dPP + (dH1 - dL1);
dR1 = (2*dPP) - dL1;
dPP = dPP;
dS1 = (2*dPP) - dH1;
dS2 = dPP - (dH1 - dL1);
dS3 = dL1 - 2*(dH1-dPP);
dM4 = (dR1 + dR2)/2;
dM3 = (dR1 + dPP)/2;
dM2 = (dPP + dS1)/2;
dM1 = (dS1 + dS2)/2;

TimeFrameRestore();

R3d = TimeFrameExpand(dR3,inDaily,expandFirst);
R2d = TimeFrameExpand(dR2,inDaily,expandFirst);
M4d = TimeFrameExpand(dM4,inDaily,expandFirst);
R1d = TimeFrameExpand(dR1,inDaily,expandFirst);
M3d = TimeFrameExpand(dM3,inDaily,expandFirst);
PPd = TimeFrameExpand(dPP,inDaily,expandFirst);
M2d = TimeFrameExpand(dM2,inDaily,expandFirst);
S1d = TimeFrameExpand(dS1,inDaily,expandFirst);
M1d = TimeFrameExpand(dM1,inDaily,expandFirst);
S2d = TimeFrameExpand(dS2,inDaily,expandFirst);
S3d = TimeFrameExpand(dS3,inDaily,expandFirst);

_SECTION_END();

_SECTION_BEGIN("Weekly Pivot");
TimeFrameSet(inWeekly);
wH1 = Ref(H,-1);
wL1 = Ref(L,-1);
wC1 = Ref(C,-1);
wPP =(wH1 + wL1 + wC1)/3;
wR3 = wH1 + 2*(wPP - wL1);
wR2 = wPP + (wH1 - wL1);
wR1 = (2*wPP) - wL1;
wPP = wPP;
wS1 = (2*wPP) - wH1;
wS2 = wPP - (wH1 - wL1);
wS3 = wL1 - 2*(wH1-wPP);
wM4 = (wR1 + wR2)/2;
wM3 = (wR1 + wPP)/2;
wM2 = (wPP + wS1)/2;
wM1 = (wS1 + wS2)/2;

TimeFrameRestore();

R3w = TimeFrameExpand(wR3,inWeekly,expandFirst);
R2w = TimeFrameExpand(wR2,inWeekly,expandFirst);
M4w = TimeFrameExpand(wM4,inWeekly,expandFirst);
R1w = TimeFrameExpand(wR1,inWeekly,expandFirst);
M3w = TimeFrameExpand(wM3,inWeekly,expandFirst);
PPw = TimeFrameExpand(wPP,inWeekly,expandFirst);
M2w = TimeFrameExpand(wM2,inWeekly,expandFirst);
S1w = TimeFrameExpand(wS1,inWeekly,expandFirst);
M1w = TimeFrameExpand(wM1,inWeekly,expandFirst);
S2w = TimeFrameExpand(wS2,inWeekly,expandFirst);
S3w = TimeFrameExpand(wS3,inWeekly,expandFirst);

_SECTION_END();

_SECTION_BEGIN("Monthly Pivots");

TimeFrameSet(inMonthly);

mH1 = Ref(H,-1);
mL1 = Ref(L,-1);
mC1 = Ref(C,-1);
mPP =(mH1 + mL1 + mC1)/3;
mR3 = mH1 + 2*(mPP - mL1);
mR2 = mPP + (mH1 - mL1);
mR1 = (2*mPP) - mL1;
mPP = mPP;
mS1 = (2*mPP) - mH1;
mS2 = mPP - (mH1 - mL1);
mS3 = mL1 - 2*(mH1-mPP);
mM4 = (mR1 + mR2)/2;
mM3 = (mR1 + mPP)/2;
mM2 = (mPP + mS1)/2;
mM1 = (mS1 + mS2)/2;

TimeFrameRestore();

R3m = TimeFrameExpand(mR3,inMonthly,expandFirst);
R2m = TimeFrameExpand(mR2,inMonthly,expandFirst);
M4m = TimeFrameExpand(mM4,inMonthly,expandFirst);
R1m = TimeFrameExpand(mR1,inMonthly,expandFirst);
M3m = TimeFrameExpand(mM3,inMonthly,expandFirst);
PPm = TimeFrameExpand(mPP,inMonthly,expandFirst);
M2m = TimeFrameExpand(mM2,inMonthly,expandFirst);
S1m = TimeFrameExpand(mS1,inMonthly,expandFirst);
M1m = TimeFrameExpand(mM1,inMonthly,expandFirst);
S2m = TimeFrameExpand(mS2,inMonthly,expandFirst);
S3m = TimeFrameExpand(mS3,inMonthly,expandFirst);
_SECTION_END();

//colors

PivotweeklyColor = ParamColor("Pivot Weekly Color", colorTurquoise);
PivotDailyColor = ParamColor("Pivot Daily Color", colorYellow);
PivotMonthlyColor = ParamColor("Pivot Monthly Color",ColorRGB(255,32,255));
MColor = ParamColor("M Color", colorLightGrey);
RSColorsDaily = ParamColor("R & S colors daily",colorYellow);
SupportColor = ParamColor("Support Color", colorGreen);
LineStyle = ParamStyle("Line Style",styleStaircase,maskAll) + styleNoLabel;
MStyle = ParamStyle("M Line Style", styleStaircase | styleDashed, maskAll) + styleNoLabel;

// Assign Pivots to Lastvalue;

// Daily
R3d = LastValue(R3d,1);
R2d = LastValue(R2d,1);
M4d = LastValue(M4d,1);
R1d = LastValue(R1d,1);
M3d = LastValue(M3d,1);
ppd = LastValue(PPd,1);
M2d = LastValue(M2d,1);
S1d = LastValue(S1d,1);
M1d = LastValue(M1d,1);
S2d = LastValue(S2d,1);
S3d = LastValue(S3d,1);
// weekly
R3w = LastValue(R3w,1);
R2w = LastValue(R2w,1);
R1w = LastValue(R1w,1);
ppw = LastValue(PPw,1);
S1w = LastValue(S1w,1);
S2w = LastValue(S2w,1);
S3w = LastValue(S3w,1);
//Monthly
R3m = LastValue(R3m,1);
R2m = LastValue(R2m,1);
R1m = LastValue(R1m,1);
ppm = LastValue(PPm,1);
S1m = LastValue(S1m,1);
S2m = LastValue(S2m,1);
S3m = LastValue(S3m,1);

// Daily plots
Plot( IIf(R3d < TopRange AND R3d > BottomRange, R3d, Null), "", PivotDailyColor, styleDashed);
Plot( IIf(R2d < TopRange AND R2d > BottomRange, R2d, Null), "", PivotDailyColor, styleDashed);
Plot( IIf(M4d < TopRange AND M4d > BottomRange, M4d, Null), "",MColor,styleDashed | styleNoLabel);
Plot( IIf(R1d < TopRange AND R1d > BottomRange, R1d, Null), "", RScolorsdaily,  styleDashed);
Plot( IIf(M3d < TopRange AND M3d > BottomRange, M3d, Null), "",Mcolor, styleDashed | styleNoLabel);
Plot( IIf(PPd < TopRange AND PPd > BottomRange, PPd, Null), "",PivotDailyColor, Linestyle | styleThick);
Plot( IIf(M2d < TopRange AND M2d > BottomRange, M2d, Null), "", MColor, styleDashed | styleNoLabel);
Plot( IIf(S1d < TopRange AND S1d > BottomRange, S1d, Null), "", RScolorsdaily, styleDashed);
Plot( IIf(M1d < TopRange AND M1d > BottomRange, M1d, Null), "", MColor, styleDashed | styleNoLabel);
Plot( IIf(S2d < TopRange AND S2d > BottomRange, S2d, Null), "", RScolorsdaily, styleDashed);
Plot( IIf(S3d < TopRange AND S3d > BottomRange, S3d, Null), "", RScolorsdaily, styleDashed);

// Weekly Plots
Plot( IIf(R3w < TopRange AND R3w > BottomRange, R3w, Null), "",PivotweeklyColor, styleDashed | styleNoLabel);
Plot( IIf(R2w < TopRange AND R2w > BottomRange, R2w, Null), "", Pivotweeklycolor,styleDashed | styleNoLabel);
Plot( IIf(R1w < TopRange AND R1w > BottomRange, R1w, Null), "", Pivotweeklycolor,styleDashed | styleNoLabel);
Plot( IIf(ppw < TopRange AND ppw > BottomRange, ppw, Null), "",PivotweeklyColor, Linestyle | styleThick);
Plot( IIf(S1w < TopRange AND S1w > BottomRange, S1w, Null), "", Pivotweeklycolor, styleDashed | styleNoLabel);
Plot( IIf(S2w < TopRange AND S2w > BottomRange, S2w, Null), "", Pivotweeklycolor, styleDashed | styleNoLabel);
Plot( IIf(S3w < TopRange AND S3w > BottomRange, S3w, Null), "", Pivotweeklycolor, styleDashed | styleNoLabel);


// Monthly Plots
Plot( IIf(R3m < TopRange AND R3m > BottomRange, R3m, Null), "", pivotMonthlyColor, styleDashed);
Plot( IIf(R2m < TopRange AND R2m > BottomRange, R2m, Null), "", pivotMonthlyColor, styleDashed);
Plot( IIf(R1m < TopRange AND R1m > BottomRange, R1m, Null), "", pivotMonthlyColor, styleDashed);
Plot( IIf(ppm < TopRange AND ppm > BottomRange, ppm, Null), "", pivotmonthlycolor, Linestyle | styleThick);
Plot( IIf(S1m < TopRange AND S1m > BottomRange, S1m, Null), "", PivotmonthlyColor, styleDashed); 
Plot( IIf(S2m < TopRange AND S2m > BottomRange, S2m, Null), "", PivotmonthlyColor, styleDashed);
Plot( IIf(S3m < TopRange AND S3m > BottomRange, S3m, Null), "", PivotmonthlyColor, styleDashed);

// PlotValues
fraction= IIf(StrRight(Name(),3) == "JPY", 3.2, 3.4);

PlotText("Daily Pivot  " + WriteVal(PPd,fraction), LastValue(BarIndex())- numbars/2, PPd  , PivotDailyColor);
PlotText("Daily R1  " + WriteVal(R1d,fraction), LastValue(BarIndex())- numbars/2, R1d  , PivotDailyColor);
PlotText("Daily S1  " + WriteVal(S1d,fraction), LastValue(BarIndex())- numbars/2, S1d  , PivotDailyColor);
PlotText("Daily R2  " + WriteVal(R2d,fraction), LastValue(BarIndex())- numbars/2, R2d  , PivotDailyColor);
PlotText("Daily R3  " + WriteVal(R3d,fraction), LastValue(BarIndex())- numbars/2, R3d  , PivotDailyColor);
PlotText("Daily S2  " + WriteVal(S2d,fraction), LastValue(BarIndex())- numbars/2, S2d  , PivotDailyColor);
PlotText("Daily S3  " + WriteVal(S3d,fraction), LastValue(BarIndex())- numbars/2, S3d  , PivotDailyColor);
PlotText("Weekly Pivot   " + WriteVal(PPw,fraction), LastValue(BarIndex()) - 3*numbars/4, PPw, PivotWeeklyColor);
PlotText("Weekly R1   " + WriteVal(R1w,fraction), LastValue(BarIndex()) - 3*numbars/4, R1w, PivotWeeklyColor);
PlotText("Weekly S1   " + WriteVal(S1w,fraction), LastValue(BarIndex()) - 3*numbars/4, S1w, PivotWeeklyColor);
PlotText("Weekly R2   " + WriteVal(R2w,fraction), LastValue(BarIndex()) - 3*numbars/4, R2w, PivotWeeklyColor);
PlotText("Weekly S2   " + WriteVal(S2w,fraction), LastValue(BarIndex()) - 3*numbars/4, S2w, PivotWeeklyColor);
PlotText("Monthly PP   ", LastValue(BarIndex()) - (numbars-5),PPm, PivotMonthlyColor);
PlotText("Monthly R1   ", LastValue(BarIndex()) - (numbars-5),R1m, PivotMonthlyColor);
PlotText("Monthly S1   ", LastValue(BarIndex()) - (numbars-5),S1m, PivotMonthlyColor);
PlotText("Monthly S2   ", LastValue(BarIndex()) - (numbars-5),S2m, PivotMonthlyColor);
PlotText("Monthly S3   ", LastValue(BarIndex()) - (numbars-5),S3m, PivotMonthlyColor);

PlotText(WriteVal(PPm,fraction), LastValue(BarIndex()) + 4, PPm, PivotMonthlyColor);


 
//Plot( LastValue( S1m, 1),"\n S1 Monthly", PivotMonthlycolor, styleDashed);