_SECTION_BEGIN("Bollinger Bands");
P = ParamField("Price field",-1);
Periods = Param("Periods", 20, 2, 300, 1 );
Width = Param("Width", 2, 0, 10, 0.05 );
Color = ParamColor("Color", colorCycle );
Style = ParamStyle("Style");
Plot( BBandTop( P, Periods, Width ), "BBTop" + _PARAM_VALUES(), Color, Style );
Plot( BBandBot( P, Periods, Width ), "BBBot" + _PARAM_VALUES(), Color, Style );
a = BBandTop( C,20);
b = BBandBot( C, 20 );
buy=High>a;
sell=Low<b;
Colors = IIf(buy, colorYellow, IIf(sell, colorRed, colorPaleGreen));
Plot(Close, "Price", Colors, styleCandle | styleThick);
_SECTION_END();
_SECTION_BEGIN("rsionobv");
r = RSIa(OBV(),21);
Buy= r>50;
Sell= r<50;
Buy=ExRem(Buy,Sell);
Sell=ExRem(Sell,Buy);
cover=r>50;
short=r<50;
Cover=ExRem(Cover,Short);
Short=ExRem(Short,Cover);
Color =IIf(Buy,colorBlue,IIf(Sell,colorred,colorPaleGreen));
Plot(Close, "Price", Color, styleCandle | styleThick);
shape = Buy * shapeUpArrow + Sell * shapeDownArrow;
PlotShapes( shape, IIf( Buy, colorGreen, colorRed ), 0, IIf( Buy, Low, High ) );
GraphXSpace = 5;
_SECTION_END();
_SECTION_BEGIN("vwapmultiday");
// TerryH - [email protected] 3/9/2012
// Move AvgPrice computation before the loop so it's only done ONCE in an array.
// Replace SetBarsRequired with only computing visible bars...back to the start of the oldest time period even if it's "off screen".
// These 2 changes make it run in 1/12th the time. No doubt database dependent on how much time is saved.// VWAP code that also plots 3 standard deviations.
// I think more savings is possible, but will take a harder look to find.
// NOTE: the code is SLOOOOWWWW...can someone help speed it up?
// I tried my best, but can't really do much with the two for-loops...
//
// LarryJR - [email protected]
SetBarsRequired(-2,-2);
_TRACE( "!CLEAR!" ); //For trouble-shooting
//PlotOHLC( O, H, L, C, "Price", colorDefault, styleCandle );
//Just do this once as an array. Saves 40% the compute time from ~ 1 second to ~ .6 seconds
AvgPrice = ( O + H + L + C ) / 4;
// Store true/false based on a new calendar day...
// Added Weekly, Monthly breaks.
// Also for futures, need to add break at new trading Day, which is NOT midnight.
period = ParamList( "What period?", "Daily|Weekly|Monthly", 0 );
switch ( period )
{
case "Daily":
newPeriod = Day() != Ref( Day(), -1 );
break;
case "Weekly":
newPeriod = DayOfWeek() == 0 AND Ref( DayOfWeek(), -1 ) != 0;
break;
case "Monthly":
newPeriod = Month() != Ref( Month(), -1 );
break;
}
//--Setup-----------
bi = BarIndex(); // Just shorthand
StartBar = StrToNum("2"); //1st bar == 0. Set to 1 so Ref(xxx, -1) doesn't give an error
EndBar = BarCount -1;
ExtraBarsRequired = 0;
//CalcRangeAll = ParamToggle( "Calc ALL or VISIBLE?", "VISIBLE|ALL", 0 ); //Option to compute all bars in case of Backtesting or Explore if you add this kind of code.
if ( Status( "action" ) < 3 ) // Limit to visible range unless we're doing Backtest, Explore, Scan, Optimize.
// In other words, for viewing/commentary we only compute what we can see.
// Saves add'l 85% the compute time from ~ .6 second to ~ .09 seconds.
{
//This code finds only the VISIBLE chart area, which may or may not be the most recent day. This let's me look at any day for evaulation purposes.
FirstVisibleBar = FirstVisibleValue( bi );
_TRACE( "1st Visible: " + FirstVisibleBar );
LastVisibleBar = LastVisibleValue( bi ); //Testing shows this returns BarIndex (0 based) values. So, if you want to use in a loop the last value IS the last bar.
VisibleBars = LastVisibleBar - FirstVisibleBar; //Used in trouble-shooting.
_TRACE( "1st Visible: " + FirstVisibleBar + " Last Visible: " + LastVisibleBar + " #bars: " + VisibleBars );
StartBar = FirstVisibleBar; //We get variable results when there are not 2 periods of data available so we won't show them at all.
EndBar = LastVisibleBar;
flag = StrToNum( "0" ); //Force a numeric value
for ( k = FirstVisibleBar; k > 0; k-- )
{
if ( newPeriod[k] )
{
flag++;
if ( flag == 2 ) //Need to lookback 2 periods if possible
{
StartBar = k; //Set starting location
k = 0; //End the loop
}
}
}
if ( flag < 2 ) //We have less than 2 periods of history to the left, so we must start displaying no sooner than the 1st newPeriod that is on-screen
{
for ( k = 1; k < EndBar; k++ )
{
if ( newPeriod[k] )
{
StartBar = k;
k = EndBar;
}
}
}
}
_TRACE("1st Required: " + StartBar + " Last Required: " + EndBar + " Extra Bars: " + ExtraBarsRequired + " Bar in Database: " + BarCount );
// Initialize loop variables
SumPriceVolume = 0;
totVolume = 0;
VWAP = 0;
stddev = 0;
newPeriodindex = EndBar; //Move way out until we find a "real one"
Variance = 0;
//Initialize the plotted variables for early data we cannot compute without getting variable results.
VWAP = C[1];
stddev_1_pos = stddev_1_neg = stddev_2_pos = stddev_2_neg = stddev_3_pos = stddev_3_neg = C[1];
// we must use a loop here because we need to save the vwap for each bar to calc the variance later
for ( i = startBar; i <= EndBar; i++ )
{
// only want to reset our values at the start of a new period
if ( newPeriod[i] )
{
SumPriceVolume = 0;
totVolume = 0;
newPeriodindex = i; // this is the index at the start of a new period
Variance = 0;
}
// Sum of Volume*price for each bar
sumPriceVolume += AvgPrice[i] * ( Volume[i] );
// running total of volume each bar
totVolume += ( Volume[i] );
if ( totVolume[i] > 0 )
{
VWAP[i] = Sumpricevolume / totVolume ;
VWAPtemp = VWAP[i];
}
// now the hard part...calculate the variance...
// a separate calc from the start of each day - note it requires the vwap from above
// also note, we calculate starting at the first bar in the new day to today to the curent bar
Variance = 0;
for ( j = newPeriodindex; j < i; j++ )
{
Variance += ( Volume[j] / totVolume ) * ( Avgprice[j] - VWAPtemp ) * ( Avgprice[j] - VWAPtemp );
}
sqrtVariance = sqrt( Variance );
stddev_1_pos[i] = VWAPtemp + sqrtVariance;
stddev_1_neg[i] = VWAPtemp - sqrtVariance;
stddev_2_pos[i] = VWAPtemp + sqrtVariance * 2;
stddev_2_neg[i] = VWAPtemp - sqrtVariance * 2;
stddev_3_pos[i] = VWAPtemp + sqrtVariance * 3;
stddev_3_neg[i] = VWAPtemp - sqrtVariance * 3;
}
//Prior period VWAP centerline
PPC = ValueWhen( newPeriod == True, Ref(VWAP, -1), 1);
Plot ( PPC, "PPC", colorBlue, styleDots|styleNoLine|styleNoRescale );
Plot ( VWAP, "VWAP", colorPink, styleLine );
Plot ( stddev_1_pos, "VWAP_std+1", ColorGrey40, styleDashed );
Plot ( stddev_1_neg, "VWAP_std-1", ColorGrey40, styleDashed );
Plot ( stddev_2_pos, "VWAP_std+2", ColorGrey40, styleDashed );
Plot ( stddev_2_neg, "VWAP_std-2", ColorGrey40, styleDashed );
Plot ( stddev_3_pos, "VWAP_std+3", ColorGrey40, styleDashed );
Plot ( stddev_3_neg, "VWAP_std-3", ColorGrey40, styledashed );
_SECTION_END();
_SECTION_BEGIN("pivotstext");
GraphXSpace = 5 ;
SetChartOptions(0,chartShowArrows|chartShowDates);
Plot(C,"Close",colorBlack, styleCandle);
ppl = ParamToggle("Plot Pivot Levels","Off|On",1);
numbars = LastValue(Cum(Status("barvisible")));
fraction= IIf(StrRight(Name(),3) == "", 3.2, 3.2);
hts = -33.5;
/* This code calculates the previous days high, low and close */
Hi1 = IIf(Day()!=Ref(Day(),-1),Ref(HighestSince(Day()!=Ref(Day(),-1),H,1),-1),0);
Hi = ValueWhen(Day()!=Ref(Day(),-1),Hi1,1);
Lo1 = IIf(Day()!=Ref(Day(),-1),Ref(LowestSince(Day()!=Ref(Day(),-1),L,1),-1),0);
Lo = ValueWhen(Day()!=Ref(Day(),-1),Lo1,1);
Cl1 = IIf(Day()!=Ref(Day(),-1),Ref(C,-1),0);
C1 = ValueWhen(Day()!=Ref(Day(),-1),Cl1,1);
//----------------------------------------------------------------------------------
/* This code calculates Daily Piovts */
rg = (Hi - Lo);
bp = (Hi + Lo + C1)/3; bpI = LastValue (bp,1);
r1 = (bp*2)-Lo; r1I = LastValue (r1,1);
s1 = (bp*2)-Hi; s1I = LastValue (s1,1);
r2 = bp + r1 - s1; r2I = LastValue (r2,1);
s2 = bp - r1 + s1; s2I = LastValue (s2,1);
r3 = bp + r2 - s1; r3I = LastValue (r3,1);
s3 = bp - r2 + s1; s3I = LastValue (s3,1);
r4 = bp + r2 - s2; r4I = LastValue (r4,1);
s4 = bp - r2 + s2; s4I = LastValue (s4,1);
if(ppl==1) {
PlotText(" Pivot = " + WriteVal(bp,fraction), LastValue(BarIndex())+(numbars/Hts), bpI +0.05,colorwhite );
PlotText(" r1 = " + WriteVal(r1,fraction), LastValue(BarIndex())+(numbars/Hts), r1I +0.05, colorwhite);
PlotText(" s1 = " + WriteVal(s1,fraction), LastValue(BarIndex())+(numbars/Hts), s1I +0.05, colorwhite);
PlotText(" r2 = " + WriteVal(r2,fraction), LastValue(BarIndex())+(numbars/Hts), r2I +0.05, colorwhite);
PlotText(" s2 = " + WriteVal(s2,fraction), LastValue(BarIndex())+(numbars/Hts), s2I +0.05, colorwhite);
PlotText(" r3 = " + WriteVal(r3,fraction), LastValue(BarIndex())+(numbars/Hts), r3I +0.05, colorwhite);
PlotText(" s3 = " + WriteVal(s3,fraction), LastValue(BarIndex())+(numbars/Hts), s3I +0.05, colorwhite);
PlotText(" r4 = " + WriteVal(r4,fraction), LastValue(BarIndex())+(numbars/Hts), r4I +0.05, colorwhite);
PlotText(" s4 = " + WriteVal(s4,fraction), LastValue(BarIndex())+(numbars/Hts), s4I +0.05, colorwhite);
}
//--end---------------------------