Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.43 KB | None | 0 0
  1. #Legal_Thieves_Trending_Stocks_Indicator
  2.  
  3.  
  4. #Inputs
  5. input length = 5;
  6. input numDevDn = -2.0;
  7. input numDevUp = 2.0;
  8. input timeFrame = {default DAY, WEEK, MONTH};
  9. input price = CLOSE;
  10. input displace = 0;
  11. input averageType = AverageType.Simple;
  12. input ThermoLookBackBars = 50;
  13. input PlotType = {default AdaptiveMovingAverages, Standard};
  14. input trailType = {default modified, unmodified};
  15. input ATRPeriod = 5;
  16. input ATRFactor = 3.5;
  17. input firstTrade = {default long, short};
  18. input FastLengthShort = 5;
  19. input SlowLengthShort = 15;
  20. input EffRatioShort = 10;
  21. input FastLengthLong = 10;
  22. input SlowLengthLong = 25;
  23. input EffRatioLong = 5;
  24. input length2 = 3;
  25.  
  26. ##############################################
  27.  
  28.  
  29. #StockPrice
  30. def StockPrice = (close);
  31.  
  32.  
  33. ###########################
  34.  
  35.  
  36. def SMA = Average(price[-displace], length2);
  37.  
  38.  
  39. ##############################################
  40.  
  41.  
  42. #VWAP
  43. def cap = getAggregationPeriod();
  44. def errorInAggregation =
  45. timeFrame == timeFrame.DAY and cap >= AggregationPeriod.WEEK or
  46. timeFrame == timeFrame.WEEK and cap >= AggregationPeriod.MONTH;
  47. assert(!errorInAggregation, "timeFrame should be not less than current chart aggregation period");
  48. def yyyyMmDd = getYyyyMmDd();
  49. def periodIndx;
  50. switch (timeFrame) {
  51. case DAY:
  52. periodIndx = yyyyMmDd;
  53. case WEEK:
  54. periodIndx = Floor((daysFromDate(first(yyyyMmDd)) + getDayOfWeek(first(yyyyMmDd))) / 7);
  55. case MONTH:
  56. periodIndx = roundDown(yyyyMmDd / 100, 0);
  57. }
  58. def isPeriodRolled = compoundValue(1, periodIndx != periodIndx[1], yes);
  59. def volumeSum;
  60. def volumeVwapSum;
  61. def volumeVwap2Sum;
  62.  
  63. if (isPeriodRolled) {
  64. volumeSum = volume;
  65. volumeVwapSum = volume * vwap;
  66. volumeVwap2Sum = volume * Sqr(vwap);
  67. } else {
  68. volumeSum = compoundValue(1, volumeSum[1] + volume, volume);
  69. volumeVwapSum = compoundValue(1, volumeVwapSum[1] + volume * vwap, volume * vwap);
  70. volumeVwap2Sum = compoundValue(1, volumeVwap2Sum[1] + volume * Sqr(vwap), volume * Sqr(vwap));
  71. }
  72. def pricee = volumeVwapSum / volumeSum;
  73. def deviation = Sqrt(Max(volumeVwap2Sum / volumeSum - Sqr(price), 0));
  74. def VWAP = pricee;
  75.  
  76.  
  77. #######################################
  78.  
  79.  
  80. #BollingerBands
  81. def sDev = stdev(data = price[-displace], length = length);
  82. def MidLine = MovingAverage(averageType, data = price[-displace], length = length);
  83.  
  84.  
  85. #######################################
  86.  
  87.  
  88. #StrongBull
  89. def StrongBull = StockPrice > VWAP and VWAP > MidLine and StockPrice > MidLine;
  90.  
  91.  
  92. #SmallBull
  93. def SmallBull = StockPrice > VWAP and VWAP < MidLine and StockPrice > MidLine;
  94.  
  95.  
  96. #StrongBear
  97. def StrongBear = StockPrice < VWAP and VWAP < MidLine and StockPrice < MidLine;
  98.  
  99.  
  100. #SmallBear
  101. def SmallBear = StockPrice < VWAP and VWAP > MidLine and StockPrice < MidLine;
  102.  
  103. def Nuetral = !StrongBull and !SmallBull and !StrongBear and !SmallBear;
  104.  
  105. ######################################
  106.  
  107.  
  108. def HighLowScore = 1000 * ((high - high[1]) / (high[1]) +
  109. (low - low[1]) / low[1]);
  110.  
  111. def HiLo = Min(high - low, 1.5 * Average(high - low, ATRPeriod));
  112. def HRef = if low <= high[1]
  113. then high - close[1]
  114. else (high - close[1]) - 0.5 * (low - high[1]);
  115. def LRef = if high >= low[1]
  116. then close[1] - low
  117. else (close[1] - low) - 0.5 * (low[1] - high);
  118. def ATRMod = ExpAverage(Max(HiLo, Max(HRef, LRef)), 2 * ATRPeriod - 1);
  119.  
  120. def loss;
  121. switch (trailType) {
  122. case modified:
  123. loss = ATRFactor * ATRMod;
  124. case unmodified:
  125. loss = ATRFactor * Average(TrueRange(high, close, low), ATRPeriod);
  126. }
  127.  
  128. rec state = {default init, long, short};
  129. rec trail;
  130. switch (state[1]) {
  131. case init:
  132. if (!IsNaN(loss)) {
  133. switch (firstTrade) {
  134. case long:
  135. state = state.long;
  136. trail = close - loss;
  137. case short:
  138. state = state.short;
  139. trail = close + loss;
  140. }
  141. } else {
  142. state = state.init;
  143. trail = Double.NaN;
  144. }
  145. case long:
  146. if (close > trail[1]) {
  147. state = state.long;
  148. trail = Max(trail[1], close - loss);
  149. }
  150. else {
  151. state = state.short;
  152. trail = close + loss;
  153. }
  154. case short:
  155. if (close < trail[1]) {
  156. state = state.short;
  157. trail = Min(trail[1], close + loss);
  158. }
  159. else {
  160. state = state.long;
  161. trail = close - loss;
  162. }
  163. }
  164. def BUYRSI = 50;
  165.  
  166. def SELLRSI = 50;
  167. def NetChgAvg = MovingAverage(averageType, price - price[1], length);
  168. def TotChgAvg = MovingAverage(averageType, AbsValue(price - price[1]), length);
  169. def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;
  170. def RSI = 50 * (ChgRatio + 1);
  171.  
  172.  
  173. def BuySignal = Crosses(state == state.long, 0, CrossingDirection.ABOVE) and RSI < BUYRSI;
  174. def SellSignal = Crosses(state == state.short, 0, CrossingDirection.ABOVE) and RSI > SELLRSI;
  175.  
  176. plot TrailingStop = trail;
  177. TrailingStop.Hide();
  178. #End ATR Trailing Stop Code
  179.  
  180. def A = Highest(high[1], ThermoLookBackBars);
  181. def B = Lowest(low[1], ThermoLookBackBars);
  182.  
  183. def FiftyTwoWeekHigh = A;
  184.  
  185. def FiftyTwoWeekLow = B;
  186.  
  187. def FiftyTwoWeekScore = 10 * (((high
  188. - FiftyTwoWeekHigh) / FiftyTwoWeekHigh) +
  189. ((low - FiftyTwoWeekLow) / FiftyTwoWeekLow));
  190.  
  191. def ThermoScore = ExpAverage(HighLowScore + FiftyTwoWeekScore, ThermoLookBackBars);
  192.  
  193. def AMA = MovAvgAdaptive(ThermoScore, FastLengthShort, SlowLengthShort, EffRatioShort);
  194. def AMA2 = MovAvgAdaptive(ThermoScore, FastLengthLong, SlowLengthLong, EffRatioLong);
  195.  
  196. plot Line1;
  197. Line1.Hide();
  198. plot Line2;
  199. Line2.Hide();
  200.  
  201. switch (PlotType) {
  202. case AdaptiveMovingAverages:
  203. Line1 = AMA;
  204. Line2 = AMA2;
  205. case Standard:
  206. Line1 = ThermoScore;
  207. Line2 = ThermoScore;
  208. }
  209.  
  210. def InvisibleLine = close * 0;
  211. plot Line3 = InvisibleLine;
  212. Line3.Hide();
  213.  
  214.  
  215. def SMAUP = SMA > SMA[1];
  216.  
  217. def SMADN = SMA < SMA[1];
  218.  
  219. def NOTSMA = !SMADN or !SMAUP;
  220.  
  221.  
  222. def VWAPdiffup = VWAP[1] / VWAP;
  223. def VWAPdiffdn = VWAP / VWAP[1];
  224. def differenceup = VWAPdiffup < 0.9999;
  225. def differencedn = VWAPdiffdn < 0.9999;
  226. def notdifference = !differenceup or !differencedn;
  227.  
  228.  
  229. def Buy = Line1 > 0 and Line2 < 0 and state == state.long;
  230. def StrongBuy = Line1 > 0 and Line2 >= 0 and state == state.long;
  231. def Sell = Line1 < 0 and Line2 > 0 and state == state.short;
  232. def StrongSell = Line1 < 0 and Line2 <= 0 and state == state.short;
  233. def Consoli = !Buy and !StrongBuy and !Sell and !StrongSell;
  234.  
  235.  
  236.  
  237.  
  238.  
  239. #PlotGraph
  240. AssignPriceColor(if StrongBull and Buy and SMAUP and differenceup then Color.CYAN
  241. else if StrongBull and StrongBuy and SMAUP and differenceup then Color.CYAN
  242. else if StrongBull and Consoli and SMAUP and differenceup then Color.CYAN
  243. else if SmallBull and Buy and SMAUP and differenceup then Color.CYAN
  244. else if Smallbull and StrongBuy and SMAUP and differenceup then Color.CYAN
  245. else if SmallBull and Consoli and SMAUP and differenceup then Color.CYAN
  246. else if StrongBear and StrongSell and SMADN and differencedn then Color.PINK
  247. else if StrongBear and Sell and SMADN and differencedn then Color.PINK
  248. else if StrongBear and Consoli and SMADN and differencedn then Color.PINK
  249. else if SmallBear and StrongSell and SMADN and differencedn then Color.Pink
  250. else if SmallBear and Sell and SMADN and differencedn then Color.Pink
  251. else if SmallBear and Consoli and SMADN and differencedn then Color.Pink
  252. else if Nuetral then Color.Yellow
  253. else if notdifference then color.yellow
  254. else if StrongBull or SmallBull and StrongSell or Sell then Color.Yellow
  255. else if NOTSMA then color.yellow
  256. 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