SHARE
TWEET

Autofib thinkscript

a guest Nov 14th, 2017 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #hint: <b>Fibonacci Retracements</b>\nFibonacci retracements use horizontal lines to indicate areas of support or resistance at the key Fibonacci levels before it continues in the original direction. These levels are created by drawing a trendline between two extreme points and then dividing the vertical distance by the key Fibonacci ratios of: 23.6%, 38.2%, 50%, 61.8%, 78.6%, and 100%.
  2.  
  3.  
  4. #hint Price: Price used in the alerts on crossing retracement lines. <b>(Default is Close)</b>
  5. #hint onExpansion: Determines if the retracement lines are projected past the current bar into the right side expansion <b>(Default is Yes)</b>
  6. #hint Extend_to_left: Determines if the retracement lines are extended to the left side of the chart. <b>(Default is No)</b>
  7.  
  8. #hint Coefficient0: Retracement Line 0: Retracement from the highest high to the lowest low.\n <b>(Default is 0%)</b>
  9. #hint Coefficient_1: Retracement Line 1: Retracement from the highest high to the lowest low.\n <b>(Default is 23.6%)</b>
  10. #hint Coefficient_2: Retracement Line 2: Retracement from the highest high to the lowest low.\n <b>(Default is 38.2%)</b>
  11. #hint Coefficient_3: Retracement Line 3: Retracement from the highest high to the lowest low.\n <b>(Default is 50%)</b>
  12. #hint Coefficient_4: Retracement Line 4: Retracement from the highest high to the lowest low.\n <b>(Default is 61.8%)</b>
  13. #hint Coefficient_5: Retracement Line 5: Retracement from the highest high to the lowest low.\n <b>(Default is 78.6%)</b>
  14. #hint Coefficient_6: Retracement Line 6: Retracement from the highest high to the lowest low.\n <b>(Default is 100%)</b>
  15.  
  16. #wizard input: Price
  17. #wizard text: Inputs: Price:
  18. #wizard input: onExpansion
  19. #wizard text: onExpansion:
  20. #wizard input: Extend_to_left
  21. #wizard text: Extend_to_left:
  22. #wizard input: Coefficient0
  23. #wizard text: Coefficient0:
  24. #wizard input: Coefficient_1
  25. #wizard text: Coefficient_1:
  26. #wizard input: Coefficient_2
  27. #wizard text: Coefficient_2:
  28. #wizard input: Coefficient_3
  29. #wizard text: Coefficient_3:
  30. #wizard input: Coefficient_4
  31. #wizard text: Coefficient_4:
  32. #wizard input: Coefficient_5
  33. #wizard text: Coefficient_5:
  34. #wizard input: Coefficient_6
  35. #wizard text: Coefficient_6:
  36.  
  37. input price = close;
  38. input high = high;
  39. input low = low;
  40. input onExpansion = Yes;
  41. input Extend_to_left = no;
  42. input Coefficient0 = 0.000;
  43. input coefficient_1 = .236;
  44. input Coefficient_2 = .382;
  45. input Coefficient_3 = .500;
  46. input Coefficient_4 = .618;
  47. Input Coefficient_5 = .786;
  48. input Coefficient_6 = 1.000;
  49.  
  50. def a = HighestAll(high);
  51. def b = LowestAll(low);
  52. def barnumber = barNumber();
  53. def c = if high == a then barnumber else double.nan;
  54. def d = if low == b then barnumber else double.nan;
  55. rec highnumber = compoundValue(1, if IsNaN(c) then highnumber[1] else c, c);
  56. def highnumberall = HighestAll(highnumber);
  57. rec lownumber = compoundValue(1, if IsNaN(d) then lownumber[1] else d, d);
  58. def lownumberall = LowestAll(lownumber);
  59.  
  60. def upward = highnumberall > lownumberall;
  61. def downward = highnumberall < lownumberall;
  62.  
  63. def x = AbsValue(lownumberall - highnumberall );
  64.  
  65. def slope = (a - b) / x;
  66. def slopelow = (b - a) / x;
  67.  
  68. def day = getDay();
  69. def month = getMonth();
  70. def year = getYear();
  71. def lastDay = getLastDay();
  72. def lastmonth = getLastMonth();
  73. def lastyear = getLastYear();
  74. def isToday = if(day == lastDay and month == lastmonth and year == lastyear, 1, 0);
  75. def istodaybarnumber = HighestAll(if isToday then barnumber else double.nan);
  76.  
  77. def line = b + (slope * (barnumber - lownumber));
  78. def linelow = a + (slopelow * (barnumber - highnumber));
  79.  
  80. def currentlinelow = if barnumber <= lownumberall then linelow else double.nan;
  81. def currentline = if barnumber <= highnumberall then line else double.nan;
  82.  
  83. Plot FibFan =  if  downward then currentlinelow else if upward then currentline else double.nan;
  84. FibFan.SetStyle(Curve.SHORT_DASH);
  85. FibFan.AssignValueColor(color.red);
  86. fibfan.hidebubble();
  87.  
  88. def range =  a - b;
  89.  
  90. Plot Retracement0 = if downward and !onexpansion and !extend_to_left and barnumber >= highnumberall and barnumber <= istodaybarnumber then highestall((b + (range *  coefficient0))) else if upward and !extend_to_left and !onexpansion and barnumber >= lownumberall and barnumber <= istodaybarnumber then highestall(a - (range * coefficient0)) else if downward and onexpansion and !extend_to_left and barnumber >= highnumberall then highestall((b + (range *  coefficient0))) else if upward and onexpansion and barnumber >= lownumberall and !extend_to_left then highestall(a - (range * coefficient0)) else if downward and !onexpansion and extend_to_left and barnumber <= istodaybarnumber then highestall((b + (range *  coefficient0))) else if upward and extend_to_left and !onexpansion and barnumber <= istodaybarnumber then highestall(a - (range * coefficient0)) else if downward and onexpansion and extend_to_left then highestall((b + (range *  coefficient0))) else if upward and onexpansion and extend_to_left then highestall(a - (range * coefficient0)) else double.nan;
  91. Retracement0.assignvaluecolor(color.red);
  92. retracement0.hidebubble();
  93. #AddChartBubble((barnumber == istodaybarnumber +10), retracement0, concat( "$", round(retracement0, 2)), color.red, yes);
  94. AddChartBubble((downward and barnumber == highnumberall), retracement0, concat( (coefficient0 * 100), "%"), color.red, yes);
  95. AddChartBubble((upward and barnumber == lownumberall), retracement0, concat( (coefficient0 * 100), "%"), color.red, yes);
  96.  
  97.  
  98. Plot Retracement1 =  if downward and !onexpansion and !extend_to_left and barnumber >= highnumberall and barnumber <= istodaybarnumber then highestall((b + (range *  coefficient_1))) else if upward and !extend_to_left and !onexpansion and barnumber >= lownumberall and barnumber <= istodaybarnumber then highestall(a - (range * coefficient_1)) else if downward and onexpansion and !extend_to_left and barnumber >= highnumberall then highestall((b + (range *  coefficient_1))) else if upward and onexpansion and barnumber >= lownumberall and !extend_to_left then highestall(a - (range * coefficient_1)) else if downward and !onexpansion and extend_to_left and barnumber <= istodaybarnumber then highestall((b + (range *  coefficient_1))) else if upward and extend_to_left and !onexpansion and barnumber <= istodaybarnumber then highestall(a - (range * coefficient_1)) else if downward and onexpansion and extend_to_left then highestall((b + (range *  coefficient_1))) else if upward and onexpansion and extend_to_left then highestall(a - (range * coefficient_1)) else double.nan;
  99. Retracement1.assignvaluecolor(color.red);
  100. retracement1.hidebubble();
  101. #AddChartBubble((barnumber == istodaybarnumber+10), retracement1, concat( "$", round(retracement1, 2)), color.red, yes);
  102. AddChartBubble((downward and barnumber == highnumberall), retracement1, concat( (coefficient_1 * 100), "%"), color.red, yes);
  103. AddChartBubble((upward and barnumber == lownumberall), retracement1, concat( (coefficient_1 * 100), "%"), color.red, yes);
  104.  
  105. Plot Retracement2 =if downward and !onexpansion and !extend_to_left and barnumber >= highnumberall and barnumber <= istodaybarnumber then highestall((b + (range *  coefficient_2))) else if upward and !extend_to_left and !onexpansion and barnumber >= lownumberall and barnumber <= istodaybarnumber then highestall(a - (range * coefficient_2)) else if downward and onexpansion and !extend_to_left and barnumber >= highnumberall then highestall((b + (range *  coefficient_2))) else if upward and onexpansion and barnumber >= lownumberall and !extend_to_left then highestall(a - (range * coefficient_2)) else if downward and !onexpansion and extend_to_left and barnumber <= istodaybarnumber then highestall((b + (range *  coefficient_2))) else if upward and extend_to_left and !onexpansion and barnumber <= istodaybarnumber then highestall(a - (range * coefficient_2)) else if downward and onexpansion and extend_to_left then highestall((b + (range *  coefficient_2))) else if upward and onexpansion and extend_to_left then highestall(a - (range * coefficient_2)) else double.nan;
  106. Retracement2.assignvaluecolor(color.red);
  107. retracement2.hidebubble();
  108. #AddChartBubble((barnumber == istodaybarnumber+10), retracement2, concat( "$", round(retracement2, 2)), color.red, yes);
  109. AddChartBubble((downward and barnumber == highnumberall), retracement2, concat( (coefficient_2 * 100), "%"), color.red, yes);
  110. AddChartBubble((upward and barnumber == lownumberall), retracement2, concat( (coefficient_2 * 100), "%"), color.red, yes);
  111.  
  112.  
  113. Plot Retracement3 = if downward and !onexpansion and !extend_to_left and barnumber >= highnumberall and barnumber <= istodaybarnumber then highestall((b + (range *  coefficient_3))) else if upward and !extend_to_left and !onexpansion and barnumber >= lownumberall and barnumber <= istodaybarnumber then highestall(a - (range * coefficient_3)) else if downward and onexpansion and !extend_to_left and barnumber >= highnumberall then highestall((b + (range *  coefficient_3))) else if upward and onexpansion and barnumber >= lownumberall and !extend_to_left then highestall(a - (range * coefficient_3)) else if downward and !onexpansion and extend_to_left and barnumber <= istodaybarnumber then highestall((b + (range *  coefficient_3))) else if upward and extend_to_left and !onexpansion and barnumber <= istodaybarnumber then highestall(a - (range * coefficient_3)) else if downward and onexpansion and extend_to_left then highestall((b + (range *  coefficient_3))) else if upward and onexpansion and extend_to_left then highestall(a - (range * coefficient_3)) else double.nan;
  114. Retracement3.assignvaluecolor(color.red);
  115. retracement3.hidebubble();
  116. #AddChartBubble((barnumber == istodaybarnumber+10), retracement3, concat( "$", round(retracement3, 2)), color.red, yes);
  117. AddChartBubble((upward and barnumber == lownumberall), retracement3, concat( (coefficient_3 * 100), "%"), color.red, yes);
  118. AddChartBubble((downward and barnumber == highnumberall), retracement3, concat( (coefficient_3 * 100), "%"), color.red, yes);
  119. AddChartBubble((upward and barnumber == lownumberall), retracement3, concat( (coefficient_3 * 100), "%"), color.red, yes);
  120.  
  121.  
  122. Plot Retracement4 = if downward and !onexpansion and !extend_to_left and barnumber >= highnumberall and barnumber <= istodaybarnumber then highestall((b + (range *  coefficient_4))) else if upward and !extend_to_left and !onexpansion and barnumber >= lownumberall and barnumber <= istodaybarnumber then highestall(a - (range * coefficient_4)) else if downward and onexpansion and !extend_to_left and barnumber >= highnumberall then highestall((b + (range *  coefficient_4))) else if upward and onexpansion and barnumber >= lownumberall and !extend_to_left then highestall(a - (range * coefficient_4)) else if downward and !onexpansion and extend_to_left and barnumber <= istodaybarnumber then highestall((b + (range *  coefficient_4))) else if upward and extend_to_left and !onexpansion and barnumber <= istodaybarnumber then highestall(a - (range * coefficient_4)) else if downward and onexpansion and extend_to_left then highestall((b + (range *  coefficient_4))) else if upward and onexpansion and extend_to_left then highestall(a - (range * coefficient_4)) else double.nan;
  123. Retracement4.assignvaluecolor(color.red);
  124. retracement4.hidebubble();
  125. #AddChartBubble((barnumber == istodaybarnumber+10), retracement4, concat( "$", round(retracement4, 2)), color.red, yes);
  126. AddChartBubble((downward and barnumber == highnumberall), retracement4, concat( (coefficient_4 * 100), "%"), color.red, yes);
  127. AddChartBubble((upward and barnumber == lownumberall), retracement4, concat( (coefficient_4 * 100), "%"), color.red, yes);
  128.  
  129. Plot Retracement5 = if downward and !onexpansion and !extend_to_left and barnumber >= highnumberall and barnumber <= istodaybarnumber then highestall((b + (range *  coefficient_5))) else if upward and !extend_to_left and !onexpansion and barnumber >= lownumberall and barnumber <= istodaybarnumber then highestall(a - (range * coefficient_5)) else if downward and onexpansion and !extend_to_left and barnumber >= highnumberall then highestall((b + (range *  coefficient_5))) else if upward and onexpansion and barnumber >= lownumberall and !extend_to_left then highestall(a - (range * coefficient_5)) else if downward and !onexpansion and extend_to_left and barnumber <= istodaybarnumber then highestall((b + (range *  coefficient_5))) else if upward and extend_to_left and !onexpansion and barnumber <= istodaybarnumber then highestall(a - (range * coefficient_5)) else if downward and onexpansion and extend_to_left then highestall((b + (range *  coefficient_5))) else if upward and onexpansion and extend_to_left then highestall(a - (range * coefficient_5)) else double.nan;
  130. Retracement5.assignvaluecolor(color.red);
  131. retracement5.hidebubble();
  132. #AddChartBubble((barnumber == istodaybarnumber+10), retracement5, concat( "$", round(retracement5, 2)), color.red, yes);
  133. AddChartBubble((downward and barnumber == highnumberall), retracement5, concat( (coefficient_5 * 100), "%"), color.red, yes);
  134. AddChartBubble((upward and barnumber == lownumberall), retracement5, concat( (coefficient_5 * 100), "%"), color.red, yes);
  135.  
  136.  
  137. Plot Retracement6 = if downward and !onexpansion and !extend_to_left and barnumber >= highnumberall and barnumber <= istodaybarnumber then highestall((b + (range *  coefficient_6))) else if upward and !extend_to_left and !onexpansion and barnumber >= lownumberall and barnumber <= istodaybarnumber then highestall(a - (range * coefficient_6)) else if downward and onexpansion and !extend_to_left and barnumber >= highnumberall then highestall((b + (range *  coefficient_6))) else if upward and onexpansion and barnumber >= lownumberall and !extend_to_left then highestall(a - (range * coefficient_6)) else if downward and !onexpansion and extend_to_left and barnumber <= istodaybarnumber then highestall((b + (range *  coefficient_6))) else if upward and extend_to_left and !onexpansion and barnumber <= istodaybarnumber then highestall(a - (range * coefficient_6)) else if downward and onexpansion and extend_to_left then highestall((b + (range *  coefficient_6))) else if upward and onexpansion and extend_to_left then highestall(a - (range * coefficient_6)) else double.nan;
  138. Retracement6.assignvaluecolor(color.red);
  139. retracement6.hidebubble();
  140. #AddChartBubble((barnumber == istodaybarnumber+10), retracement6, concat( "$", round(retracement6, 2)), color.red, yes);
  141.  
  142. AddChartBubble((downward and barnumber == highnumberall), retracement6, concat( (coefficient_6 * 100), "%"), color.red, yes);
  143. AddChartBubble((upward and barnumber == lownumberall), retracement6, concat( (coefficient_6 * 100), "%"), color.red, yes);
  144.  
  145.  
  146. alert((price crosses below Retracement0) , "Price crosses below Retracement Line 0");
  147. alert((price crosses above Retracement0) , "Price crosses above Retracement Line 0");
  148. alert((price crosses below Retracement1) , "Price crosses below Retracement Line 1");
  149. alert((price crosses above Retracement1) , "Price crosses above Retracement Line 1");
  150. alert((price crosses below Retracement2) , "Price crosses below Retracement Line 2");
  151. alert((price crosses above Retracement2) , "Price crosses above Retracement Line 2");
  152. alert((price crosses below Retracement3) , "Price crosses below Retracement Line 3");
  153. alert((price crosses above Retracement3) , "Price crosses above Retracement Line 3");
  154. alert((price crosses below Retracement4) , "Price crosses below Retracement Line 4");
  155. alert((price crosses above Retracement4) , "Price crosses above Retracement Line 4");
  156. alert((price crosses below Retracement5) , "Price crosses below Retracement Line 5");
  157. alert((price crosses above Retracement5) , "Price crosses above Retracement Line 5");
  158. alert((price crosses below Retracement6) , "Price crosses below Retracement Line 6");
  159. alert((price crosses above Retracement6) , "Price crosses above Retracement Line 6");
RAW Paste Data
Top