declare lower;
AssignBackgroundColor(CreateColor(8, 0, 15));
input length = 9;
input price = HL2;
input PriceColorOn = Yes;
input ArrowsOn = Yes;
input PredictionLineOn = Yes;
input BubblesOn = No;
input ShowTodayOnly = no;
input ShowExtraDays = 0;
input space = 0.333;
def Today = if !ShowTodayOnly then 1 else if GetDay() + ShowExtraDays >= GetLastDay() then 1 else 0;
def AvgPrice = Average(price, length);
def SumTime = fold i = 1 to length + 1 with x = 0 do x + i;
def AvgTime = SumTime / length;
def sx = fold j = 1 to length + 1 with y = 0 do y + ((j - AvgTime) * (GetValue(price, length - j, length + 1) - AvgPrice));
def sy = fold k = 1 to length + 1 with z = 0 do z + (Power(k - AvgTime, 2));
def m = sx / sy;
def b = AvgPrice - m * AvgTime;
def Prediction = (m * (length + 1)) + b;
plot FuturePrediction = if !PredictionLineOn then Double.NaN else Prediction;
FuturePrediction.AssignValueColor(if FuturePrediction > FuturePrediction[1] then Color.GREEN else Color.RED);
FuturePrediction.SetLineWeight(3);
FuturePrediction.HideBubble();
AssignPriceColor(if !PriceColorOn then Color.CURRENT else if Prediction > Prediction[1] then (CreateColor(000, 255, 255)) else if Prediction < Prediction[1] then (CreateColor(255, 160, 000)) else Color.GRAY);
AddLabel(PriceColorOn , "Price Color On", if Prediction > Prediction[1] then Color.GREEN else if Prediction < Prediction[1] then Color.RED else Color.GRAY);
AddLabel(ArrowsOn, "MagentaArrows=LeastSquares", if Prediction > Prediction[1] then Color.GREEN else if Prediction < Prediction[1] then Color.RED else Color.GRAY);
def spaceman = Average(high - low) * space;
def xover = Crosses(Prediction, Prediction[1], CrossingDirection.ANY);
plot UpArrow = if Today && xover && Prediction > Prediction[1] then low - spaceman else Double.NaN;
UpArrow.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
UpArrow.SetDefaultColor(Color.MAGENTA);
plot DnArrow = if Today && xover && Prediction < Prediction[1] then high + spaceman else Double.NaN;
DnArrow.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
DnArrow.SetDefaultColor(Color.MAGENTA);
rec LastArrow = if low > Prediction && LastArrow[1] <> 1 && Prediction < Prediction[1] then 1 else
if high < Prediction && LastArrow[1] <> -1 && Prediction > Prediction[1] then -1 else LastArrow[1];
plot firstup = if Today && LastArrow > 0 && LastArrow <> LastArrow[1] then low else Double.NaN;
firstup.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
firstup.SetLineWeight(5);
firstup.SetDefaultColor(Color.YELLOW);
plot firstdown = if Today && LastArrow < 0 && LastArrow <> LastArrow[1] then high else Double.NaN;
firstdown.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
firstdown.SetLineWeight(5);
firstdown.SetDefaultColor(Color.YELLOW);
input standardDeviation = 1.5;
input startDateYyyyMmDd = 20110105;
input startTime = 0030;
plot MiddleLR = InertiaAll(price, length, startDateYyyyMmDd, startTime, extendtoright = 1);
def dist = HighestAll(AbsValue(MiddleLR - price)) * standardDeviation / 2;
plot UpperLR = MiddleLR + dist;
plot LowerLR = MiddleLR - dist;
MiddleLR.SetDefaultColor(Color.WHITE);
MiddleLR.SetLineWeight(1);
MiddleLR.SetStyle(Curve.FIRM);
UpperLR.SetDefaultColor(Color.CYAN);
UpperLR.SetLineWeight(1);
UpperLR.SetStyle(Curve.FIRM);
LowerLR.SetDefaultColor(Color.CYAN);
LowerLR.SetLineWeight(1);
LowerLR.SetStyle(Curve.FIRM);
def exposure20pct = LastArrow < 0 && LastArrow <> LastArrow[1];
def exposure40pct = Prediction[3] > Prediction[4] && Prediction[2] > Prediction[3] && Prediction[1] > Prediction[2] && Prediction > Prediction[1] ;
def exposure60pct = Prediction[2] > Prediction[3] && Prediction[1] > Prediction[2] && Prediction > Prediction[1];
def exposure80pct = Prediction[1] > Prediction[2] && Prediction > Prediction[1];
def exposure100pct = xover && Prediction > Prediction[1];
def exposureneg20pct = LastArrow > 0 && LastArrow <> LastArrow[1];
def exposureneg40pct = Prediction[3] < Prediction[4] && Prediction[2] < Prediction[3] && Prediction[1] < Prediction[2] && Prediction < Prediction[1];
def exposureneg60pct = Prediction[2] < Prediction[3] && Prediction[1] < Prediction[2] && Prediction < Prediction[1];
def exposureneg80pct = Prediction[1] < Prediction[2] && Prediction < Prediction[1];
def exposureneg100pct = xover && Prediction < Prediction[1];
AddLabel(yes, Concat( "", Concat(" ",
if exposure20pct then "Long 20% " else
if exposure40pct then "Long 40% " else
if exposure60pct then "Long 60%" else
if exposure80pct then "Long 60%" else
if exposure100pct then "Long 100%" else
if exposureneg20pct then "Short 20% " else
if exposureneg40pct then "Short 40% " else
if exposureneg60pct then "Short 60%" else
if exposureneg80pct then "Short 60%" else
if exposureneg100pct then "Short 100%" else
"Go Flat")),
if exposure20pct then Color.CYAN else
if exposure40pct then Color.CYAN else
if exposure60pct then Color.CYAN else
if exposure80pct then Color.CYAN else
if exposure100pct then Color.CYAN else
if exposureneg20pct then Color.ORANGE else
if exposureneg40pct then Color.ORANGE else
if exposureneg60pct then Color.ORANGE else
if exposureneg80pct then Color.ORANGE else
if exposureneg100pct then Color.ORANGE else
Color.WHITE);
AddChartBubble(BubblesOn && xover && Prediction > Prediction[1] , low, Concat("Buy Baby", HL2), Color.CYAN, yes);
AddChartBubble(BubblesOn && xover && Prediction < Prediction[1] , high, Concat("Sell That Bad Boy", HL2), Color.ORANGE, yes);
AssignBackgroundColor(CreateColor(8, 0, 15));
input length2 = 14;
input price2 = HL2;
input PriceColorOn2 = Yes;
input ArrowsOn2 = Yes;
input PredictionLineOn2 = Yes;
input BubblesOn2 = No;
input ShowTodayOnly2 = no;
input ShowExtraDays2 = 0;
input space2 = 0.333;
def Today2 = if !ShowTodayOnly2 then 1 else if GetDay() + ShowExtraDays2 >= GetLastDay() then 1 else 0;
def AvgPrice2 = Average(price2, length2);
def SumTime2 = fold i2 = 1 to length2 + 1 with x2 = 0 do x2 + i2;
def AvgTime2 = SumTime2 / length2;
def sx2 = fold j2 = 1 to length2 + 1 with y2 = 0 do y2 + ((j2 - AvgTime2) * (GetValue(price2, length2 - j2, length2 + 1) - AvgPrice2));
def sy2 = fold k2 = 1 to length2 + 1 with z2 = 0 do z2 + (Power(k2 - AvgTime2, 2));
def m2 = sx2 / sy2;
def b2 = AvgPrice2 - m2 * AvgTime2;
def Prediction2 = (m2 * (length2 + 1)) + b2;
plot FuturePrediction2 = if !PredictionLineOn2 then Double.NaN else Prediction2;
FuturePrediction2.AssignValueColor(if FuturePrediction2 > FuturePrediction2[1] then Color.GREEN else Color.RED);
FuturePrediction2.SetLineWeight(3);
FuturePrediction2.HideBubble();
AssignPriceColor(if !PriceColorOn2 then Color.CURRENT else if Prediction2 > Prediction2[1] then (CreateColor(000, 255, 255)) else if Prediction2 < Prediction2[1] then (CreateColor(255, 160, 000)) else Color.GRAY);
AddLabel(PriceColorOn2 , "Price Color On", if Prediction2 > Prediction2[1] then Color.GREEN else if Prediction2 < Prediction2[1] then Color.RED else Color.GRAY);
AddLabel(ArrowsOn2, "MagentaArrows=LeastSquares", if Prediction2 > Prediction2[1] then Color.GREEN else if Prediction2 < Prediction2[1] then Color.RED else Color.GRAY);
def spaceman2 = Average(high - low) * space2;
def xover2 = Crosses(Prediction2, Prediction2[1], CrossingDirection.ANY);
plot UpArrow2 = if Today2 && xover2 && Prediction2 > Prediction2[1] then low - spaceman2 else Double.NaN;
UpArrow2.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
UpArrow2.SetDefaultColor(Color.MAGENTA);
plot DnArrow2 = if Today2 && xover2 && Prediction2 < Prediction2[1] then high + spaceman2 else Double.NaN;
DnArrow2.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
DnArrow2.SetDefaultColor(Color.MAGENTA);
rec LastArrow2 = if low > Prediction2 && LastArrow2[1] <> 1 && Prediction2 < Prediction2[1] then 1 else
if high < Prediction2 && LastArrow2[1] <> -1 && Prediction2 > Prediction2[1] then -1 else LastArrow2[1];
plot firstup2 = if Today2 && LastArrow2 > 0 && LastArrow2 <> LastArrow2[1] then low else Double.NaN;
firstup2.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
firstup2.SetLineWeight(5);
firstup2.SetDefaultColor(Color.YELLOW);
plot firstdown2 = if Today2 && LastArrow2 < 0 && LastArrow2 <> LastArrow2[1] then high else Double.NaN;
firstdown2.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
firstdown2.SetLineWeight(5);
firstdown2.SetDefaultColor(Color.YELLOW);
def exposure20pct2 = LastArrow2 < 0 && LastArrow2 <> LastArrow2[1];
def exposure40pct2 = Prediction2[3] > Prediction2[4] && Prediction2[2] > Prediction2[3] && Prediction2[1] > Prediction2[2] && Prediction2 > Prediction2[1] ;
def exposure60pct22 = Prediction2[2] > Prediction2[3] && Prediction2[1] > Prediction2[2] && Prediction2 > Prediction2[1];
def exposure80pct2 = Prediction2[1] > Prediction2[2] && Prediction2 > Prediction2[1];
def exposure100pct2 = xover2 && Prediction2 > Prediction2[1];
def exposureneg20pct2 = LastArrow2 > 0 && LastArrow2 <> LastArrow2[1];
def exposureneg40pct2 = Prediction2[3] < Prediction2[4] && Prediction2[2] < Prediction2[3] && Prediction2[1] < Prediction2[2] && Prediction2 < Prediction2[1];
def exposureneg60pct2 = Prediction2[2] < Prediction2[3] && Prediction2[1] < Prediction2[2] && Prediction2 < Prediction2[1];
def exposureneg80pct2 = Prediction2[1] < Prediction2[2] && Prediction2 < Prediction2[1];
def exposureneg100pct2 = xover2 && Prediction2 < Prediction2[1];
AddLabel(yes, Concat( "", Concat(" ",
if exposure20pct2 then "Long 20% " else
if exposure40pct2 then "Long 40% " else
if exposure80pct2 then "Long 80%" else
if exposure100pct2 then "Long 100%" else
if exposureneg20pct2 then "Short 20% " else
if exposureneg40pct2 then "Short 40% " else
if exposureneg60pct2 then "Short 60%" else
if exposureneg80pct2 then "Short 80%" else
if exposureneg100pct2 then "Short 100%" else
"Go Flat")),
if exposure20pct2 then Color.CYAN else
if exposure40pct2 then Color.CYAN else
if exposure80pct2 then Color.CYAN else
if exposure100pct2 then Color.CYAN else
if exposureneg20pct2 then Color.ORANGE else
if exposureneg40pct2 then Color.ORANGE else
if exposureneg60pct2 then Color.ORANGE else
if exposureneg80pct2 then Color.ORANGE else
if exposureneg100pct2 then Color.ORANGE else
Color.WHITE);
AddChartBubble(BubblesOn2 && xover2 && Prediction2 > Prediction2[1] , low, Concat("Buy Baby", HL2), Color.CYAN, yes);
AddChartBubble(BubblesOn2 && xover2 && Prediction2 < Prediction2[1] , high, Concat("Sell That Bad Boy", HL2), Color.ORANGE, yes);