Advertisement
coondawg71

T3O

Sep 15th, 2014
471
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1. -- Indicator profile initialization routine
  2. -- Defines indicator profile properties and indicator parameters
  3. -- TODO: Add minimal and maximal value of numeric parameters and default color of the streams
  4. function Init()
  5. indicator:name("Tilson T3 Oscillator");
  6. indicator:description("Tilson T3 Oscillator");
  7. indicator:requiredSource(core.Tick);
  8. indicator:type(core.Oscillator);
  9.  
  10. indicator.parameters:addInteger("F1", "First T3 Period", "First T3 Period", 4,2,2000);
  11. indicator.parameters:addInteger("F2", "Second T3 Period", "Second T3 Period", 5,2,2000);
  12. indicator.parameters:addInteger("N", "Normalize Period", "Normalize Period", 10,0,2000);
  13. indicator.parameters:addDouble("VF", "Volume Factor", "Volume Factor", 0.7);
  14. indicator.parameters:addColor("T3O1_color", "Color of Short T3O", "Color of Short T3O", core.rgb(255, 0, 0));
  15. indicator.parameters:addColor("T3O2_color", "Color of Long T3O", "Color of Long T3O", core.rgb(0, 255, 0));
  16. end
  17.  
  18. -- Indicator instance initialization routine
  19. -- Processes indicator parameters and creates output streams
  20. -- TODO: Refine the first period calculation for each of the output streams.
  21. -- TODO: Calculate all constants, create instances all subsequent indicators and load all required libraries
  22. -- Parameters block
  23. local T3Frame1=nil;
  24. local T3Frame2=nil;
  25. local NormalizeFrame=nil;
  26. local T=nil
  27.  
  28. local first;
  29. local source = nil;
  30.  
  31. -- Streams block
  32. local T3O1 = nil;
  33. local T3O2 = nil;
  34. local Temp1= nil;
  35. local Temp2= nil;
  36.  
  37. -- Routine
  38. function Prepare()
  39. source = instance.source;
  40. first = source:first();
  41.  
  42. T = instance.parameters.VF;
  43. NormalizeFrame = instance.parameters.N;
  44. T3Frame1= instance.parameters.F1;
  45. T3Frame2= instance.parameters.F2;
  46.  
  47. Temp2 = instance:addInternalStream(0, 0);
  48. Temp1 = instance:addInternalStream(0, 0);
  49.  
  50. T31 = core.indicators:create("T3", source, T, T3Frame1);
  51. T32 = core.indicators:create("T3", source, T, T3Frame2);
  52.  
  53. local name = profile:id() .. "(" .. source:name() .. ", " .. T3Frame1 .. ", " .. T3Frame2 .. ", " .. NormalizeFrame .. ", " .. T .. ")";
  54. instance:name(name);
  55. T3O1 = instance:addStream("T3A", core.Line, name, "T3O Short", instance.parameters.T3O1_color, first);
  56. T3O2 = instance:addStream("T3B", core.Line, name, "T3O Long", instance.parameters.T3O2_color, first);
  57. end
  58.  
  59. -- Indicator calculation routine
  60. -- TODO: Add your code for calculation output values
  61. function Update(period,mode)
  62.  
  63. T31:update(mode);
  64. T32:update(mode);
  65.  
  66. if period >= 3*T3Frame1+ NormalizeFrame then
  67.  
  68. Normalize1(period,NormalizeFrame);
  69.  
  70. T3O1[period] = (Temp1[period]*100+100)/2;
  71.  
  72. end
  73.  
  74. if period >= 3*T3Frame2+ NormalizeFrame then
  75.  
  76. Normalize2(period,NormalizeFrame);
  77.  
  78. T3O2[period] = (Temp2[period]*100+100)/2;
  79.  
  80. end
  81.  
  82. end
  83.  
  84. function Normalize1(i, Frame)
  85.  
  86. local High,Low;
  87. local Under;
  88.  
  89. if Temp1[i-1] == nil then
  90. Temp1[i-1] = T31.DATA[i-1];
  91. end
  92.  
  93.  
  94. High = core.max (T31.DATA, core.range (i-Frame, i));
  95. Low = core.min (T31.DATA, core.range (i-Frame, i));
  96.  
  97. if High - Low ==0 then
  98. Under = 1 ;
  99. else
  100. Under = (High - Low);
  101. end
  102.  
  103.  
  104. Temp1[i]=0.5*2*((T31.DATA[i]-Low )/ Under - 0.5)+0.5 * Temp1[i-1];
  105.  
  106. if Temp1[i] > 0.9999 then
  107.  
  108. Temp1[i] = 0.9999;
  109. end
  110.  
  111. if Temp1[i] < -0.9999 then
  112.  
  113. Temp1[i] = -0.9999;
  114.  
  115. end
  116.  
  117. end
  118.  
  119.  
  120. function Normalize2(i, Frame)
  121.  
  122. local High,Low;
  123. local Under;
  124.  
  125. if Temp2[i-1] == nil then
  126. Temp2[i-1] = T32.DATA[i-1];
  127. end
  128.  
  129.  
  130. High = core.max (T32.DATA, core.range (i-Frame, i));
  131. Low = core.min (T32.DATA, core.range (i-Frame, i));
  132.  
  133. if High - Low ==0 then
  134. Under = 1 ;
  135. else
  136. Under = (High - Low);
  137. end
  138.  
  139.  
  140. Temp2[i]=0.5*2*((T32.DATA[i]-Low )/ Under - 0.5)+0.5 * Temp2[i-1];
  141.  
  142. if Temp2[i] > 0.9999 then
  143.  
  144. Temp2[i] = 0.9999;
  145. end
  146.  
  147. if Temp2[i] < -0.9999 then
  148.  
  149. Temp2[i] = -0.9999;
  150.  
  151. end
  152.  
  153. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement