Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Legal_Thieves_Trending_Stocks_Indicator
- #Inputs
- input length = 5;
- input numDevDn = -2.0;
- input numDevUp = 2.0;
- input timeFrame = {default DAY, WEEK, MONTH};
- input price = CLOSE;
- input displace = 0;
- input averageType = AverageType.Simple;
- input ThermoLookBackBars = 50;
- input PlotType = {default AdaptiveMovingAverages, Standard};
- input trailType = {default modified, unmodified};
- input ATRPeriod = 5;
- input ATRFactor = 3.5;
- input firstTrade = {default long, short};
- input FastLengthShort = 5;
- input SlowLengthShort = 15;
- input EffRatioShort = 10;
- input FastLengthLong = 10;
- input SlowLengthLong = 25;
- input EffRatioLong = 5;
- input length2 = 3;
- ##############################################
- #StockPrice
- def StockPrice = (close);
- ###########################
- def SMA = Average(price[-displace], length2);
- ##############################################
- #VWAP
- def cap = getAggregationPeriod();
- def errorInAggregation =
- timeFrame == timeFrame.DAY and cap >= AggregationPeriod.WEEK or
- timeFrame == timeFrame.WEEK and cap >= AggregationPeriod.MONTH;
- assert(!errorInAggregation, "timeFrame should be not less than current chart aggregation period");
- def yyyyMmDd = getYyyyMmDd();
- def periodIndx;
- switch (timeFrame) {
- case DAY:
- periodIndx = yyyyMmDd;
- case WEEK:
- periodIndx = Floor((daysFromDate(first(yyyyMmDd)) + getDayOfWeek(first(yyyyMmDd))) / 7);
- case MONTH:
- periodIndx = roundDown(yyyyMmDd / 100, 0);
- }
- def isPeriodRolled = compoundValue(1, periodIndx != periodIndx[1], yes);
- def volumeSum;
- def volumeVwapSum;
- def volumeVwap2Sum;
- if (isPeriodRolled) {
- volumeSum = volume;
- volumeVwapSum = volume * vwap;
- volumeVwap2Sum = volume * Sqr(vwap);
- } else {
- volumeSum = compoundValue(1, volumeSum[1] + volume, volume);
- volumeVwapSum = compoundValue(1, volumeVwapSum[1] + volume * vwap, volume * vwap);
- volumeVwap2Sum = compoundValue(1, volumeVwap2Sum[1] + volume * Sqr(vwap), volume * Sqr(vwap));
- }
- def pricee = volumeVwapSum / volumeSum;
- def deviation = Sqrt(Max(volumeVwap2Sum / volumeSum - Sqr(price), 0));
- def VWAP = pricee;
- #######################################
- #BollingerBands
- def sDev = stdev(data = price[-displace], length = length);
- def MidLine = MovingAverage(averageType, data = price[-displace], length = length);
- #######################################
- #StrongBull
- def StrongBull = StockPrice > VWAP and VWAP > MidLine and StockPrice > MidLine;
- #SmallBull
- def SmallBull = StockPrice > VWAP and VWAP < MidLine and StockPrice > MidLine;
- #StrongBear
- def StrongBear = StockPrice < VWAP and VWAP < MidLine and StockPrice < MidLine;
- #SmallBear
- def SmallBear = StockPrice < VWAP and VWAP > MidLine and StockPrice < MidLine;
- def Nuetral = !StrongBull and !SmallBull and !StrongBear and !SmallBear;
- ######################################
- def HighLowScore = 1000 * ((high - high[1]) / (high[1]) +
- (low - low[1]) / low[1]);
- def HiLo = Min(high - low, 1.5 * Average(high - low, ATRPeriod));
- def HRef = if low <= high[1]
- then high - close[1]
- else (high - close[1]) - 0.5 * (low - high[1]);
- def LRef = if high >= low[1]
- then close[1] - low
- else (close[1] - low) - 0.5 * (low[1] - high);
- def ATRMod = ExpAverage(Max(HiLo, Max(HRef, LRef)), 2 * ATRPeriod - 1);
- def loss;
- switch (trailType) {
- case modified:
- loss = ATRFactor * ATRMod;
- case unmodified:
- loss = ATRFactor * Average(TrueRange(high, close, low), ATRPeriod);
- }
- rec state = {default init, long, short};
- rec trail;
- switch (state[1]) {
- case init:
- if (!IsNaN(loss)) {
- switch (firstTrade) {
- case long:
- state = state.long;
- trail = close - loss;
- case short:
- state = state.short;
- trail = close + loss;
- }
- } else {
- state = state.init;
- trail = Double.NaN;
- }
- case long:
- if (close > trail[1]) {
- state = state.long;
- trail = Max(trail[1], close - loss);
- }
- else {
- state = state.short;
- trail = close + loss;
- }
- case short:
- if (close < trail[1]) {
- state = state.short;
- trail = Min(trail[1], close + loss);
- }
- else {
- state = state.long;
- trail = close - loss;
- }
- }
- def BUYRSI = 50;
- def SELLRSI = 50;
- def NetChgAvg = MovingAverage(averageType, price - price[1], length);
- def TotChgAvg = MovingAverage(averageType, AbsValue(price - price[1]), length);
- def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;
- def RSI = 50 * (ChgRatio + 1);
- def BuySignal = Crosses(state == state.long, 0, CrossingDirection.ABOVE) and RSI < BUYRSI;
- def SellSignal = Crosses(state == state.short, 0, CrossingDirection.ABOVE) and RSI > SELLRSI;
- plot TrailingStop = trail;
- TrailingStop.Hide();
- #End ATR Trailing Stop Code
- def A = Highest(high[1], ThermoLookBackBars);
- def B = Lowest(low[1], ThermoLookBackBars);
- def FiftyTwoWeekHigh = A;
- def FiftyTwoWeekLow = B;
- def FiftyTwoWeekScore = 10 * (((high
- - FiftyTwoWeekHigh) / FiftyTwoWeekHigh) +
- ((low - FiftyTwoWeekLow) / FiftyTwoWeekLow));
- def ThermoScore = ExpAverage(HighLowScore + FiftyTwoWeekScore, ThermoLookBackBars);
- def AMA = MovAvgAdaptive(ThermoScore, FastLengthShort, SlowLengthShort, EffRatioShort);
- def AMA2 = MovAvgAdaptive(ThermoScore, FastLengthLong, SlowLengthLong, EffRatioLong);
- plot Line1;
- Line1.Hide();
- plot Line2;
- Line2.Hide();
- switch (PlotType) {
- case AdaptiveMovingAverages:
- Line1 = AMA;
- Line2 = AMA2;
- case Standard:
- Line1 = ThermoScore;
- Line2 = ThermoScore;
- }
- def InvisibleLine = close * 0;
- plot Line3 = InvisibleLine;
- Line3.Hide();
- def SMAUP = SMA > SMA[1];
- def SMADN = SMA < SMA[1];
- def NOTSMA = !SMADN or !SMAUP;
- def VWAPdiffup = VWAP[1] / VWAP;
- def VWAPdiffdn = VWAP / VWAP[1];
- def differenceup = VWAPdiffup < 0.9999;
- def differencedn = VWAPdiffdn < 0.9999;
- def notdifference = !differenceup or !differencedn;
- def Buy = Line1 > 0 and Line2 < 0 and state == state.long;
- def StrongBuy = Line1 > 0 and Line2 >= 0 and state == state.long;
- def Sell = Line1 < 0 and Line2 > 0 and state == state.short;
- def StrongSell = Line1 < 0 and Line2 <= 0 and state == state.short;
- def Consoli = !Buy and !StrongBuy and !Sell and !StrongSell;
- #PlotGraph
- AssignPriceColor(if StrongBull and Buy and SMAUP and differenceup then Color.CYAN
- else if StrongBull and StrongBuy and SMAUP and differenceup then Color.CYAN
- else if StrongBull and Consoli and SMAUP and differenceup then Color.CYAN
- else if SmallBull and Buy and SMAUP and differenceup then Color.CYAN
- else if Smallbull and StrongBuy and SMAUP and differenceup then Color.CYAN
- else if SmallBull and Consoli and SMAUP and differenceup then Color.CYAN
- else if StrongBear and StrongSell and SMADN and differencedn then Color.PINK
- else if StrongBear and Sell and SMADN and differencedn then Color.PINK
- else if StrongBear and Consoli and SMADN and differencedn then Color.PINK
- else if SmallBear and StrongSell and SMADN and differencedn then Color.Pink
- else if SmallBear and Sell and SMADN and differencedn then Color.Pink
- else if SmallBear and Consoli and SMADN and differencedn then Color.Pink
- else if Nuetral then Color.Yellow
- else if notdifference then color.yellow
- else if StrongBull or SmallBull and StrongSell or Sell then Color.Yellow
- else if NOTSMA then color.yellow
- else if StrongBear or SmallBear and StrongBuy or Buy then Color.Yellow else Color.gray);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement