saisri

HARMONIC PATTERN DETECTION

Feb 20th, 2013
1,652
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 56.24 KB | None | 0 0
  1. /* HARMONIC PATTERN DETECTION
  2.  
  3.  
  4. Automatic Detection of Harmonic Patterns - Gartley, Bat, Butterfly and Crab.
  5.  
  6. Zig Zag is not used in this AFL. It is based on fractals
  7.  
  8.  
  9. */
  10.  
  11.  
  12. _SECTION_BEGIN("Price");
  13. SetChartOptions(0,chartShowArrows|chartShowDates);
  14. _N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
  15. Plot( C, "Close", ParamColor("Color", colorWhite ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
  16. _SECTION_END();
  17.  
  18. _SECTION_BEGIN("Gartley");
  19.  
  20. GBmin = Param("Swing B Min.",0.55,0.3,1,0.01);
  21. GBmax = Param("Swing B Max.",0.72,0.4,1,0.01);
  22. GCmin = Param("Swing C Min.",0.38,0.3,1.27,0.01);
  23. GCmax = Param("Swing C Max.",1.0,0.4,1.27,0.01);
  24. GDmin = Param("Swing D Min.(XA)",0.55,0.3,1,0.01);
  25. GDmax = Param("Swing D Max.(XA)",1.0,0.4,1.0,0.01);
  26.  
  27. _SECTION_END();
  28.  
  29. _SECTION_BEGIN("Bat");
  30.  
  31. BatBmin = Param("Swing B Min.",0.38,0.3,1,0.01);
  32. BatBmax = Param("Swing B Max.",0.55,0.4,1,0.01);
  33. BatCmin = Param("Swing C Min.",0.38,0.3,1.62,0.01);
  34. BatCmax = Param("Swing C Max.",1.27,0.4,1.62,0.01);
  35. BatDmin = Param("Swing D Min.(XA)",0.5,0.3,1,0.01);
  36. BatDmax = Param("Swing D Max.(XA)",1.0,0.4,1.0,0.01);
  37.  
  38. _SECTION_END();
  39.  
  40. _SECTION_BEGIN("Butterfly");
  41.  
  42. BtBmin = Param("Swing B Min.",0.55,0.3,1,0.01);
  43. BtBmax = Param("Swing B Max.",0.9,0.4,1,0.01);
  44. BtCmin = Param("Swing C Min.",0.38,0.3,1.62,0.01);
  45. BtCmax = Param("Swing C Max.",1.27,0.4,1.62,0.01);
  46. BtDmin = Param("Swing D Min.(XA)",1,1,1.8,0.01);
  47. BtDmax = Param("Swing D Max.(XA)",1.8,1,1.8,0.01); // Max XA of Butterfly = (1.0 - 1.618)
  48.  
  49. _SECTION_END();
  50.  
  51. _SECTION_BEGIN("Crab");
  52.  
  53. CBmin = Param("Swing B Min.",0.38,0.3,1,0.01);
  54. CBmax = Param("Swing B Max.",0.65,0.4,1,0.01);
  55. CCmin = Param("Swing C Min.",0.38,0.3,1.62,0.01);
  56. CCmax = Param("Swing C Max.",1.270,0.4,1.62,0.01);
  57. CDmin = Param("Swing D Min.(XA)",1.25,1,1.8,0.01);
  58. CDmax = Param("Swing D Max.(XA)",1.8,1,2,0.01);
  59.  
  60. _SECTION_END();
  61.  
  62. _SECTION_BEGIN("AB=CD");
  63.  
  64. abcd_Cmin = Param("Swing C Min.",0.3, 0.3 , 1, 0.01);
  65. abcd_Cmax = Param("Swing C Max.",0.8, 0.8 , 1, 0.01);
  66. abcd_Dmin = Param("Swing D Min.",1.2, 1, 2.7, 0.01);
  67. abcd_Dmax = Param("Swing D Max.",3.7, 1, 4, 0.01);
  68.  
  69. _SECTION_END();
  70.  
  71. _SECTION_BEGIN("Patterns");
  72.  
  73. //strength = Param("Strength",5,2,15,1); // Best use: 3, 4, 5
  74. strength = Param("BARS of each LINE",5,2,15,1); // So luong bar cho moi duong XA, AB, BC,
  75. bu = ParamToggle("Bullish Pattern","Off|On",1); // So bar/lines se quyet dinh. mo^ hinh` duoc ve the' nao`
  76. be = ParamToggle("Bearish Pattern","Off|On",1);
  77.  
  78. bi = Cum(1)-1;
  79.  
  80. function GetTop(bars) // Lay' gia' tri cao nhat' = di?nh
  81. {
  82. Top = H == HHV(H,2*bars) AND Ref(HHV(H,bars),bars) < H;
  83. Top = Top AND LastValue(bi)-ValueWhen(Top,bi) > bars;
  84. return Top;
  85. }
  86.  
  87. function GetValley(bars) // La'y gia tri thap' nhat' = day'
  88. {
  89. Valley = L == LLV(L,2*bars) AND Ref(LLV(L,bars),bars) > L;
  90. Valley = Valley AND LastValue(bi)-ValueWhen(Valley,bi) > bars;
  91. return Valley;
  92. }
  93.  
  94.  
  95. // Build fractals array
  96.  
  97. P1 = GetTop(strength); // so' bar cho 1 duong` XA, AB, BC, CD
  98. V1 = GetValley(Strength);
  99.  
  100. P1 = IIf(P1,IIf(ValueWhen(P1,bi,2) < ValueWhen(V1,bi),P1,IIf(ValueWhen(P1,H,2) > H,False,P1)),P1);
  101. P1 = IIf(P1 AND ValueWhen(P1,bi,0) > bi,IIf(ValueWhen(P1,bi,0) < ValueWhen(V1,bi,0),IIf(ValueWhen(P1,H,0) >= H,False,P1),P1),P1);
  102. V1 = IIf(V1,IIf(ValueWhen(V1,bi,2) < ValueWhen(P1,bi),V1,IIf(ValueWhen(V1,L,2)<L,False,V1)),V1);
  103. V1 = IIf(V1 AND ValueWhen(V1,bi,0) > bi ,IIf(ValueWhen(V1,bi,0) < ValueWhen(P1,bi,0),IIf(ValueWhen(V1,L,0) <= L, False,V1),V1),V1);
  104.  
  105.  
  106. P1H1 = ValueWhen(P1,H);
  107. P1Bar1 = ValueWhen(P1,bi);
  108. P1H2 = ValueWhen(P1,H,2);
  109. P1Bar2 = ValueWhen(P1,bi,2);
  110. V1L1 = ValueWhen(V1,L);
  111. V1Bar1 = ValueWhen(V1,bi);
  112. V1L2 = ValueWhen(V1,L,2);
  113. V1Bar2 = ValueWhen(V1,bi,2);
  114.  
  115.  
  116. //============================================
  117. // BULLISH PATTERNS
  118. //============================================
  119. /*
  120. Mo hinh Bullish:
  121. A = P1H2
  122. B = V1L1
  123. C = P1H1
  124. X = V1L2
  125.  
  126. */
  127.  
  128. PTvalid = (P1Bar1 > V1Bar1 AND V1Bar1 > P1Bar2 AND P1bar2 > V1Bar2) AND P1; // Peaks and troughs are in order
  129.  
  130. myAX = P1H2-V1L2;
  131. myAB = P1H2-V1L1;
  132. myBC = P1H1-V1L1;
  133.  
  134. myAB_AX = myAB/ myAX;
  135. myBC_AB = myBC/ myAB;
  136.  
  137. BullGartley4 = PTvalid AND ( myAB_AX > GBmin ) AND ( myAB_AX < GBmax )
  138. AND ( myBC_AB > GCMin ) AND ( myBC_AB < GCMax );
  139.  
  140. BullBat4 = PTvalid AND ( myAB_AX > BatBmin ) AND ( myAB_AX < BatBmax )
  141. AND ( myBC_AB > BatCMin ) AND ( myBC_AB < BatCMax );
  142.  
  143. BullButterfly4 = PTvalid AND ( myAB_AX > BtBmin ) AND ( myAB_AX < BtBMax )
  144. AND ( myBC_AB > BtCmin ) AND ( myBC_AB < BtCmin );
  145.  
  146. BullCrab4 = PTvalid AND ( myAB_AX > CBmin ) AND ( myAB_AX < CBmax )
  147. AND ( myBC_AB > CCmin ) AND ( myBC_AB < CCmax );
  148.  
  149. BullABCD4 = PTvalid AND ( myBC_AB > abcd_Cmin) AND ( myBC_AB < abcd_Cmax );
  150.  
  151. strPattern = "";
  152.  
  153. //==================================================
  154. // BULLISH ABCD
  155. // Bullish pattern found. D retracement level is not evaluated
  156. //==================================================
  157. dHigh = HighestSince(BullABCD4,H); // Tinh' gia' tri min, max cua duong Ad. Duong Ad la duong con cua AD
  158. dLow = LowestSince(BullABCD4,L);
  159.  
  160. myC = ValueWhen(BullABCD4,P1H1);
  161. myB = ValueWhen(BullABCD4,V1L1);
  162. myA = ValueWhen(BullABCD4,P1H2);
  163. myX = ValueWhen(BullABCD4,V1L2);
  164. myCB = myC - myB;
  165.  
  166. my_d_min = myCB * abcd_DMin ; // Tinh' gia' tri cua duong Ad con. Khi gia' giam? tu` tre^n xuong' thi` max -> min
  167. my_d_max = myCB * abcd_DMax ;
  168. my_Cd_min = myC - my_d_min; // Khoang dich chuyen cua duong Ad con.
  169. my_Cd_max = myC - my_d_max;
  170.  
  171. BullABCD = IIf( ( dLow < my_Cd_min ) AND ( dLow > my_Cd_max )
  172. AND ( dHigh <= myC ) AND ( dLow == L),
  173. True, False
  174. );
  175.  
  176. BullABCD = BullABCD AND (dLow < myB);
  177.  
  178.  
  179. //==================================================
  180. // BULLISH GARTLEY
  181. //==================================================
  182. dHigh = HighestSince(BullGartley4,H); // Tinh' gia' tri min, max cua duong Ad. Duong Ad la duong con cua AD
  183. dLow = LowestSince(BullGartley4,L);
  184.  
  185. myC = ValueWhen(BullGartley4,P1H1);
  186. myB = ValueWhen(BullGartley4,V1L1);
  187. myA = ValueWhen(BullGartley4,P1H2);
  188. myX = ValueWhen(BullGartley4,V1L2);
  189. myAX = myA - myX;
  190.  
  191. my_d_min = myAX * GDmin; // Tinh' gia' tri cua duong Ad con. Khi gia' giam? tu` tre^n xuong' thi` max -> min
  192. my_d_max = myAX * GDMax;
  193. my_Ad_min = myA - my_d_min; // Khoang dich chuyen cua duong Ad con.
  194. my_Ad_max = myA - my_d_max;
  195.  
  196. BullGartley = IIf( ( dLow < my_Ad_min ) AND ( dLow > my_Ad_max )
  197. AND ( dHigh <= myC ) AND ( dLow == L),
  198. True, False
  199. );
  200. BullGartley = BullGartley AND (dLow < myB); // diem D thap' hon B
  201. strPattern = WriteIf(BullGartley,"BULLISH GARTLEY",strPattern);
  202.  
  203.  
  204.  
  205. //==================================================
  206. // BULLISH BAT
  207. //==================================================
  208. dHigh = HighestSince(BullBat4,H); // Tinh' gia' tri min, max cua duong Ad. Duong Ad la duong con cua AD
  209. dLow = LowestSince(BullBat4,L);
  210.  
  211. myC = ValueWhen(BullBat4,P1H1);
  212. myB = ValueWhen(BullBat4,V1L1);
  213. myA = ValueWhen(BullBat4,P1H2);
  214. myX = ValueWhen(BullBat4,V1L2);
  215. myAX = myA - myX;
  216.  
  217. my_d_min = myAX * BatDmin; // Tinh' gia' tri cua duong Ad con. Khi gia' giam? tu` tre^n xuong' thi` max -> min
  218. my_d_max = myAX * BatDmax ;
  219. my_Ad_min = myA - my_d_min; // Khoang dich chuyen cua duong Ad con.
  220. my_Ad_max = myA - my_d_max;
  221.  
  222. BullBat = IIf( ( dLow < my_Ad_min ) AND ( dLow > my_Ad_max )
  223. AND ( dHigh <= myC ) AND ( dLow == L),
  224. True, False
  225. );
  226. BullBat = BullBat AND (dLow < myB); // diem d thap hon diem B
  227. strPattern = WriteIf(BullBat,"BULLISH BAT",strPattern);
  228.  
  229.  
  230. //==================================================
  231. // BULLISH CRAB - CUA
  232. //==================================================
  233. dHigh = HighestSince(BullCrab4,H); // Tinh' gia' tri min, max cua duong Ad. Duong Ad la duong con cua AD
  234. dLow = LowestSince(BullCrab4,L);
  235.  
  236. myC = ValueWhen(BullCrab4,P1H1);
  237. myB = ValueWhen(BullCrab4,V1L1);
  238. myA = ValueWhen(BullCrab4,P1H2);
  239. myX = ValueWhen(BullCrab4,V1L2);
  240. myAX = myA - myX;
  241.  
  242. my_d_min = myAX * CDmin ; // Tinh' gia' tri cua duong Ad con. Khi gia' giam? tu` tre^n xuong' thi` max -> min
  243. my_d_max = myAX * CDmax ;
  244. my_Ad_min = myA - my_d_min; // Khoang dich chuyen cua duong Ad con.
  245. my_Ad_max = myA - my_d_max;
  246.  
  247. BullCrab = IIf( ( dLow < my_Ad_min ) AND ( dLow > my_Ad_max )
  248. AND ( dHigh <= myC ) AND ( dLow == L),
  249. True, False
  250. );
  251. BullCrab = BullCrab AND (dLow < myX); // diem D thap' hon X
  252. strPattern = WriteIf(BullCrab ,"BULLISH CRAB",strPattern);
  253.  
  254.  
  255. //==================================================
  256. // BULLISH BUTTTERFLY
  257. //==================================================
  258. dHigh = HighestSince(BullButterfly4,H); // Tinh' gia' tri min, max cua duong Ad. Duong Ad la duong con cua AD
  259. dLow = LowestSince(BullButterfly4,L);
  260.  
  261. myC = ValueWhen(BullButterfly4,P1H1);
  262. myB = ValueWhen(BullButterfly4,V1L1);
  263. myA = ValueWhen(BullButterfly4,P1H2);
  264. myX = ValueWhen(BullButterfly4,V1L2);
  265. myAX = myA - myX;
  266.  
  267. my_d_min = myAX * BtDmin ; // Tinh' gia' tri cua duong Ad con. Khi gia' giam? tu` tre^n xuong' thi` max -> min
  268. my_d_max = myAX * BtDmax ;
  269. my_Ad_min = myA - my_d_min; // Khoang dich chuyen cua duong Ad con.
  270. my_Ad_max = myA - my_d_max;
  271.  
  272. BullButterfly = IIf( ( dLow < my_Ad_min ) AND ( dLow > my_Ad_max )
  273. AND ( dHigh <= myC ) AND ( dLow == L),
  274. True, False
  275. );
  276. BullButterfly = BullButterfly AND (dLow < myX); // diem D thap' hon X
  277. strPattern = WriteIf(BullButterfly ,"BULLISH BUTTERFLY",strPattern);
  278.  
  279.  
  280.  
  281. //==========================================================
  282. // VE DUONG CHO MO HINH BULLISH ABCB
  283. //==========================================================
  284. BullHar4 = BullABCD4;
  285. BullHar = BullABCD;
  286.  
  287. Point4 = IIf(BullHar,ValueWhen(BullHar4,bi),Null);
  288. BullHar = IIf(BullHar, IIf(Point4 == ValueWhen(BullHar,point4,0) AND ValueWhen(BullHar,bi,0) > bi ,False,BullHar),BullHar);
  289.  
  290. A = ValueWhen(BullHar4,P1H2);
  291. Abar = ValueWhen(BullHar4,P1bar2);
  292. B = ValueWhen(BullHar4,V1L1);
  293. Bbar = ValueWhen(BullHar4,V1bar1);
  294. C1 = ValueWhen(BullHar4,P1H1);
  295. C1bar = ValueWhen(BullHar4,P1bar1);
  296. D = ValueWhen(BullHar,L);
  297. Dbar = ValueWhen(BullHar,bi);
  298.  
  299. BCdAB = (C1-B)/(A-B);
  300. BCdCD = (C1-D)/(C1-B);
  301.  
  302. PlotPattern = Dbar > C1bar;
  303.  
  304. if(LastValue(PlotPattern) AND bu)
  305. {
  306. ColorX = colorGreen;
  307. // Ve cac duong AB, BC, CD
  308. Plot(LineArray(LastValue(Abar),LastValue(A),LastValue(Bbar),LastValue(B)),"",ColorX ,styleThick);
  309. Plot(LineArray(LastValue(Bbar),LastValue(B),LastValue(C1bar),LastValue(C1)),"",ColorX ,styleThick);
  310. Plot(LineArray(LastValue(C1bar),LastValue(C1),LastValue(Dbar),LastValue(D)),"",ColorX ,styleThick);
  311. Plot(LineArray(LastValue(Abar),LastValue(A),LastValue(C1bar),LastValue(C1)),"",ColorX ,styleDashed);
  312. Plot(LineArray(LastValue(Bbar),LastValue(B),LastValue(Dbar),LastValue(D)),"",ColorX ,styleDashed);
  313.  
  314. // Ve cac gia tri Fibo
  315. PlotText(NumToStr(LastValue(BCdAB),1.2),(LastValue(C1bar)+LastValue(Abar))/2,(LastValue(C1)+LastValue(A))/2,ColorX );
  316. PlotText(NumToStr(LastValue(BCdCD),1.2),(LastValue(Bbar)+LastValue(Dbar))/2,(LastValue(B)+LastValue(D))/2,ColorX );
  317.  
  318. //---------- Viet cac diem X, A, B, C, D: by binhnd---------------------
  319. xlech = 0;
  320. ylech = 2;
  321. PlotText("A",LastValue(Abar) + xlech, LastValue(A) + ylech, ColorX );
  322. PlotText("B",LastValue(Bbar) + xlech, LastValue(B) - ylech, ColorX );
  323. PlotText("C",LastValue(C1bar) + xlech, LastValue(C1) + ylech, ColorX );
  324. PlotText("D",LastValue(Dbar) + xlech, LastValue(D) - ylech, ColorX );
  325.  
  326. //--------- Viet thuyet minh mo hinh: by binhnd--------------
  327. if (strPattern!="")
  328. {
  329. myStr = "Pattern: BULLISH AB=CD";
  330. toadoX = LastValue(Abar);
  331. toadoY = LastValue(D);
  332.  
  333. PlotText(myStr,toadoX,toadoY,ColorX );
  334. }
  335.  
  336. } // end of Ve duong` bullish abcd
  337.  
  338.  
  339.  
  340. //==========================================================
  341. // VE DUONG CHO MO HINH BULLISH BAT, GARTLEY, BUTTERFLY, CRAB
  342. //==========================================================
  343.  
  344.  
  345. BullHar4 = BullGartley4 OR BullButterfly4 OR BullBat4 OR BullCrab4 ;
  346. BullHar = BullGartley OR BullButterfly OR BullBat OR BullCrab;
  347.  
  348. Point4 = IIf(BullHar,ValueWhen(BullHar4,bi),Null);
  349. BullHar = IIf(BullHar, IIf(Point4 == ValueWhen(BullHar,point4,0) AND ValueWhen(BullHar,bi,0) > bi ,False,BullHar),BullHar);
  350.  
  351. X = ValueWhen(BullHar4,V1L2);
  352. Xbar = ValueWhen(BullHar4,V1Bar2);
  353. A = ValueWhen(BullHar4,P1H2);
  354. Abar = ValueWhen(BullHar4,P1bar2);
  355. B = ValueWhen(BullHar4,V1L1);
  356. Bbar = ValueWhen(BullHar4,V1bar1);
  357. C1 = ValueWhen(BullHar4,P1H1);
  358. C1bar = ValueWhen(BullHar4,P1bar1);
  359. D = ValueWhen(BullHar,L);
  360. Dbar = ValueWhen(BullHar,bi);
  361.  
  362. ABdXA = (A-B)/(A-X);
  363. BCdAB = (C1-B)/(A-B);
  364. ADdXA = (A-D)/(A-X);
  365. BCdCD = (C1-D)/(C1-B);
  366.  
  367. PlotPattern = Dbar > C1bar;
  368.  
  369. if(LastValue(PlotPattern) AND bu)
  370. {
  371. ColorX = colorBlue;
  372. // Ve cac duong XA, AB, BC, CD
  373. Plot( LineArray(LastValue(Xbar),LastValue(X),LastValue(Abar),LastValue(A)),"",ColorX,styleThick);
  374. Plot(LineArray(LastValue(Abar),LastValue(A),LastValue(Bbar),LastValue(B)),"",ColorX,styleThick);
  375. Plot(LineArray(LastValue(Bbar),LastValue(B),LastValue(C1bar),LastValue(C1)),"",ColorX,styleThick);
  376. Plot(LineArray(LastValue(C1bar),LastValue(C1),LastValue(Dbar),LastValue(D)),"",ColorX,styleThick);
  377. Plot(LineArray(LastValue(Xbar),LastValue(X),LastValue(Bbar),LastValue(B)),"",ColorX,styleDashed);
  378. Plot(LineArray(LastValue(Xbar),LastValue(X),LastValue(Abar),LastValue(A)),"",ColorX,styleThick);
  379. Plot(LineArray(LastValue(Abar),LastValue(A),LastValue(C1bar),LastValue(C1)),"",ColorX,styleDashed);
  380. Plot(LineArray(LastValue(Bbar),LastValue(B),LastValue(Dbar),LastValue(D)),"",ColorX,styleDashed);
  381. Plot(LineArray(LastValue(Xbar),LastValue(X),LastValue(Dbar),LastValue(D)),"",ColorX,styleDashed);
  382.  
  383. // Ve cac gia tri Fibo
  384. PlotText(NumToStr(LastValue(ABdXA),1.2),(LastValue(Bbar)+LastValue(Xbar))/2,(LastValue(B)+LastValue(X))/2,ColorX);
  385. PlotText(NumToStr(LastValue(BCdAB),1.2),(LastValue(C1bar)+LastValue(Abar))/2,(LastValue(C1)+LastValue(A))/2,ColorX);
  386. PlotText(NumToStr(LastValue(ADdXA),1.2) ,(LastValue(Dbar)+LastValue(Xbar))/2,(LastValue(D)+LastValue(X))/2,ColorX);
  387. PlotText(NumToStr(LastValue(BCdCD),1.2),(LastValue(Bbar)+LastValue(Dbar))/2,(LastValue(B)+LastValue(D))/2,ColorX);
  388.  
  389. //---------- Viet cac diem X, A, B, C, D: by binhnd---------------------
  390. xlech = 0;
  391. ylech = 2;
  392. PlotText("X",LastValue(Xbar) + xlech, LastValue(X) - ylech, ColorX);
  393. PlotText("A",LastValue(Abar) + xlech, LastValue(A) + ylech, ColorX);
  394. PlotText("B",LastValue(Bbar) + xlech, LastValue(B) - ylech, ColorX);
  395. PlotText("C",LastValue(C1bar) + xlech, LastValue(C1) + ylech, ColorX);
  396. PlotText("D",LastValue(Dbar) + xlech, LastValue(D) - ylech, ColorX);
  397.  
  398. //--------- Viet thuyet minh mo hinh: by binhnd--------------
  399. if (strPattern!="")
  400. {
  401. strPattern = "Pattern: " + strPattern;
  402. toadoX = (LastValue(Dbar)+LastValue(Xbar))/2;
  403. toadoY = (LastValue(D)+LastValue(X))/2;
  404.  
  405. PlotText(strPattern,toadoX,toadoY-2,ColorX);
  406. }
  407.  
  408. } // end of Ve duong cho cac mo hinh Crab, Butterfly, Bat
  409.  
  410.  
  411. //=============================================================
  412. // BEARISH PATTERNS
  413. //=============================================================
  414.  
  415. PTvalid = (V1Bar1 > P1Bar1 AND P1Bar1 > V1Bar2 AND V1Bar2 > P1Bar2) AND V1;
  416.  
  417. /*=====================
  418. X = P1H2 Trong mo hinh` bear: Die^m X cao hon diem A. MyAX = X-> A
  419. A = V1L2
  420. B = P1H1
  421. C = V1L1
  422.  
  423. =======================*/
  424. myAX = P1H2-V1L2;
  425. myAB = P1H1-V1L2;
  426. myBC = P1H1-V1L1;
  427.  
  428. myAB_AX = myAB/ myAX;
  429. myBC_AB = myBC/ myAB;
  430.  
  431. BearGartley4 = PTvalid AND ( myAB_AX > GBmin ) AND ( myAB_AX < GBmax )
  432. AND ( myBC_AB > GCMin ) AND ( myBC_AB < GCMax );
  433.  
  434. BearBat4 = PTvalid AND ( myAB_AX > BatBmin ) AND ( myAB_AX < BatBmax )
  435. AND ( myBC_AB > BatCMin ) AND ( myBC_AB < BatCMax );
  436.  
  437. BearButterfly4 = PTvalid AND ( myAB_AX > BtBmin ) AND ( myAB_AX < BtBMax )
  438. AND ( myBC_AB > BtCmin ) AND ( myBC_AB < BtCmin );
  439.  
  440. BearCrab4 = PTvalid AND ( myAB_AX > CBmin ) AND ( myAB_AX < CBmax )
  441. AND ( myBC_AB > CCmin ) AND ( myBC_AB < CCmax );
  442.  
  443. BearABCD4 = PTvalid AND ( myBC_AB > abcd_Cmin) AND ( myBC_AB < abcd_Cmax );
  444.  
  445. strPattern = "";
  446.  
  447.  
  448.  
  449. //==========================================================
  450. // BEARISH ABCD
  451. // Bearish pattern found. D retracement level is not evaluated
  452. //==========================================================
  453. dHigh = HighestSince(BearABCD4,H); // Tinh' gia' tri min, max cua duong Ad. Duong Ad la duong con cua AD
  454. dLow = LowestSince(BearABCD4,L);
  455.  
  456. myA = ValueWhen(BearABCD4,V1L2);
  457. myB = ValueWhen(BearABCD4,P1H1);
  458. myC = ValueWhen(BearABCD4,V1L1);
  459. myCB = myB - myC;
  460.  
  461. my_d_min = myCB * abcd_DMin ; // Tinh' gia' tri cua duong Ad con. Khi gia' giam? tu` tre^n xuong' thi` max -> min
  462. my_d_max = myCB * abcd_DMax ;
  463. my_Cd_min = myC + my_d_min; // Khoang dich chuyen cua duong Ad con.
  464. my_Cd_max = myC + my_d_max;
  465.  
  466. BearABCD = IIf( ( dHigh > my_Cd_min ) AND ( dHigh < my_Cd_max )
  467. AND ( dLow >= myC ) AND ( dHigh == H),
  468. True, False
  469. );
  470.  
  471. BearABCD = BearABCD AND (dHigh > myB);
  472.  
  473. //=============================================================
  474. // BEARISH GARTLEY
  475. //=============================================================
  476. dHigh = HighestSince(BearGartley4,H); // Tinh' gia' tri min, max cua duong Ad. Duong Ad la duong con cua AD
  477. dLow = LowestSince(BearGartley4,L);
  478.  
  479. myX = ValueWhen(BearGartley4,P1H2);
  480. myA = ValueWhen(BearGartley4,V1L2);
  481. myAX = myX - myA;
  482.  
  483. myB = ValueWhen(BearGartley4,P1H1);
  484. myC = ValueWhen(BearGartley4,V1L1);
  485.  
  486.  
  487. my_d_min = myAX * GDmin; // Tinh' gia' tri cua duong Ad con. Khi gia' giam? tu` tre^n xuong' thi` max -> min
  488. my_d_max = myAX * GDMax;
  489. my_Ad_min = myA + my_d_min; // Khoang dich chuyen cua duong Ad con.
  490. my_Ad_max = myA + my_d_max;
  491.  
  492. BearGartley = IIf( ( dHigh > my_Ad_min ) AND ( dHigh < my_Ad_max )
  493. AND ( dLow >= myC ) AND ( dHigh == H),
  494. True, False
  495. );
  496. BearGartley = BearGartley AND (dHigh > myB); // diem D cao hon B
  497. strPattern = WriteIf(BearGartley ,"BEARISH GARTLEY",strPattern);
  498.  
  499. //=============================================================
  500. // BEARISH BAT
  501. //=============================================================
  502. dHigh = HighestSince(BearBat4,H); // Tinh' gia' tri min, max cua duong Ad. Duong Ad la duong con cua AD
  503. dLow = LowestSince(BearBat4,L);
  504.  
  505. myX = ValueWhen(BearBat4,P1H2);
  506. myA = ValueWhen(BearBat4,V1L2);
  507. myAX = myX - myA;
  508.  
  509. myB = ValueWhen(BearBat4,P1H1);
  510. myC = ValueWhen(BearBat4,V1L1);
  511.  
  512.  
  513. my_d_min = myAX * BatDmin ; // Tinh' gia' tri cua duong Ad con. Khi gia' giam? tu` tre^n xuong' thi` max -> min
  514. my_d_max = myAX * BatDMax ;
  515. my_Ad_min = myA + my_d_min; // Khoang dich chuyen cua duong Ad con.
  516. my_Ad_max = myA + my_d_max;
  517.  
  518. BearBat = IIf( ( dHigh > my_Ad_min ) AND ( dHigh < my_Ad_max )
  519. AND ( dLow >= myC ) AND ( dHigh == H),
  520. True, False
  521. );
  522. BearBat = BearBat AND (dHigh > myB); // diem D cao hon B
  523. strPattern = WriteIf(BearBat ,"BEARISH BAT",strPattern);
  524.  
  525.  
  526. //=============================================================
  527. // BEARISH BUTTERFLY
  528. //=============================================================
  529. dHigh = HighestSince(BearButterfly4,H); // Tinh' gia' tri min, max cua duong Ad. Duong Ad la duong con cua AD
  530. dLow = LowestSince(BearButterfly4,L);
  531.  
  532. myX = ValueWhen(BearButterfly4,P1H2);
  533. myA = ValueWhen(BearButterfly4,V1L2);
  534. myAX = myX - myA;
  535.  
  536. myB = ValueWhen(BearButterfly4,P1H1);
  537. myC = ValueWhen(BearButterfly4,V1L1);
  538.  
  539.  
  540. my_d_min = myAX * BtDmin ; // Tinh' gia' tri cua duong Ad con. Khi gia' giam? tu` tre^n xuong' thi` max -> min
  541. my_d_max = myAX * BtDmax ;
  542. my_Ad_min = myA + my_d_min; // Khoang dich chuyen cua duong Ad con.
  543. my_Ad_max = myA + my_d_max;
  544.  
  545. BearButterfly = IIf( ( dHigh > my_Ad_min ) AND ( dHigh < my_Ad_max )
  546. AND ( dLow >= myC ) AND ( dHigh == H),
  547. True, False
  548. );
  549. BearButterfly = BearButterfly AND (dHigh > myX); // diem D cao hon X
  550. strPattern = WriteIf(BearButterfly ,"BEARISH BUTTERFLY",strPattern);
  551.  
  552.  
  553.  
  554. //=============================================================
  555. // BEARISH CRAB
  556. //=============================================================
  557. dHigh = HighestSince(BearCrab4,H); // Tinh' gia' tri min, max cua duong Ad. Duong Ad la duong con cua AD
  558. dLow = LowestSince(BearCrab4,L);
  559.  
  560. myX = ValueWhen(BearCrab4,P1H2);
  561. myA = ValueWhen(BearCrab4,V1L2);
  562. myAX = myX - myA;
  563.  
  564. myB = ValueWhen(BearCrab4,P1H1);
  565. myC = ValueWhen(BearCrab4,V1L1);
  566.  
  567.  
  568. my_d_min = myAX * CDmin ; // Tinh' gia' tri cua duong Ad con. Khi gia' giam? tu` tre^n xuong' thi` max -> min
  569. my_d_max = myAX * CDmax ;
  570. my_Ad_min = myA + my_d_min; // Khoang dich chuyen cua duong Ad con.
  571. my_Ad_max = myA + my_d_max;
  572.  
  573. BearCrab = IIf( ( dHigh > my_Ad_min ) AND ( dHigh < my_Ad_max )
  574. AND ( dLow >= myC ) AND ( dHigh == H),
  575. True, False
  576. );
  577. BearCrab = BearCrab AND (dHigh > myX); // diem D cao hon X
  578. strPattern = WriteIf(BearCrab ,"BEARISH CRAB",strPattern);
  579.  
  580.  
  581.  
  582. //==========================================================
  583. // VE DUONG CHO MO HINH BEARISH ABCD
  584. //==========================================================
  585.  
  586.  
  587. BearHar4 = BearABCD4;
  588. BearHar = BearABCD;
  589.  
  590. Point4 = IIf(BearHar,ValueWhen(BearHar4,bi),Null);
  591. BearHar = IIf(BearHar, IIf(Point4 == ValueWhen(BearHar,point4,0) AND ValueWhen(BearHar,bi,0) > bi ,False,BearHar),BearHar);
  592.  
  593. A = ValueWhen(BearHar4,V1L2);
  594. Abar = ValueWhen( BearHar4,V1bar2);
  595. B = ValueWhen(BearHar4,P1H1);
  596. Bbar = ValueWhen(BearHar4,P1bar1);
  597. C1 = ValueWhen(BearHar4,V1L1);
  598. C1bar = ValueWhen(BearHar4,V1bar1);
  599. D = ValueWhen(BearHar,H);
  600. Dbar = ValueWhen(BearHar,bi);
  601.  
  602. BCdAB = (B-C1)/(B-A);
  603. BCdCD = (D-C1)/(B-C1);
  604.  
  605. PlotPattern = Dbar > C1bar;
  606.  
  607. //--------- Ve duong ------------------
  608. if(LastValue(Plotpattern) AND be)
  609. {
  610. ColorX = colorYellow;
  611. // Ve duong AB, BC
  612. Plot(LineArray(LastValue(Abar),LastValue(A),LastValue(Bbar),LastValue(B)),"",ColorX ,styleThick);
  613. Plot(LineArray(LastValue(Bbar),LastValue(B),LastValue(C1bar),LastValue(C1)),"",ColorX ,styleThick);
  614. Plot(LineArray(LastValue(C1bar),LastValue(C1),LastValue(Dbar),LastValue(D)),"",ColorX ,styleThick);
  615. Plot(LineArray(LastValue(Abar),LastValue(A),LastValue(C1bar),LastValue(C1)),"",ColorX ,styleDashed);
  616. Plot(LineArray(LastValue(Bbar),LastValue(B),LastValue(Dbar),LastValue(D)),"",ColorX ,styleDashed);
  617.  
  618. // Viet cac gia tri Fibo tren duong AB, BC
  619. PlotText(NumToStr(LastValue(BCdAB),1.2),(LastValue(C1bar)+LastValue(Abar))/2,(LastValue(C1)+LastValue(A))/2,ColorX );
  620. PlotText(NumToStr(LastValue(BCdCD),1.2) ,(LastValue(Dbar)+LastValue(Bbar))/2,(LastValue(D)+LastValue(B))/2,ColorX );
  621.  
  622. //---------- Viet cac diem A, B, C, D: by binhnd---------------------
  623. xlech = -1;
  624. ylech = 1;
  625. PlotText("A",LastValue(Abar) + xlech, LastValue(A) - ylech, ColorX );
  626. PlotText("B",LastValue(Bbar) + xlech, LastValue(B) + ylech, ColorX );
  627. PlotText("C",LastValue(C1bar) + xlech, LastValue(C1) - ylech, ColorX );
  628. PlotText("D",LastValue(Dbar) + xlech, LastValue(D) + ylech, ColorX );
  629.  
  630. //--------- Viet thuyet minh mo hinh: by binhnd--------------
  631. if (strPattern!="")
  632. {
  633. myStr = "Pattern: BEARISH AB=CD";
  634. toadoaX = LastValue(Abar);
  635. toadoY = LastValue(D);
  636.  
  637. PlotText(myStr,toadoaX,toadoY+1,ColorX );
  638. }
  639.  
  640. } // end of VE DUONG CHO MO HINH BEARISH ABCD
  641.  
  642.  
  643. //==========================================================
  644. // VE DUONG CHO MO HINH BEARISH BAT, GARTLEY, BUTTERFLY, CRAB
  645. //==========================================================
  646.  
  647. BearHar4 = BearGartley4 OR BearButterfly4 OR BearBat4 OR BearCrab4 ;
  648. BearHar = BearGartley OR BearButterfly OR BearBat OR BearCrab ;
  649.  
  650. Point4 = IIf(BearHar,ValueWhen(BearHar4,bi),Null);
  651. BearHar = IIf(BearHar, IIf(Point4 == ValueWhen(BearHar,point4,0) AND ValueWhen(BearHar,bi,0) > bi ,False,BearHar),BearHar);
  652.  
  653. X = ValueWhen(BearHar4,P1H2);
  654. Xbar = ValueWhen(BearHar4,P1Bar2);
  655. A = ValueWhen(BearHar4,V1L2);
  656. Abar = ValueWhen( BearHar4,V1bar2);
  657. B = ValueWhen(BearHar4,P1H1);
  658. Bbar = ValueWhen(BearHar4,P1bar1);
  659. C1 = ValueWhen(BearHar4,V1L1);
  660. C1bar = ValueWhen(BearHar4,V1bar1);
  661. D = ValueWhen(BearHar,H);
  662. Dbar = ValueWhen(BearHar,bi);
  663.  
  664. ABdXA = (B-A)/(X-A);
  665. BCdAB = (B-C1)/(B-A);
  666. ADdXA = (D-A)/(X-A);
  667. BCdCD = (D-C1)/(B-C1);
  668.  
  669. PlotPattern = Dbar > C1bar;
  670.  
  671. //--------- Ve duong ------------------
  672. if(LastValue(Plotpattern) AND be)
  673. {
  674. ColorX = colorRed;
  675. // Ve duong XA, AB, BC
  676. Plot( LineArray(LastValue(Xbar),LastValue(X),LastValue(Abar),LastValue(A)),"",ColorX ,styleThick);
  677. Plot(LineArray(LastValue(Abar),LastValue(A),LastValue(Bbar),LastValue(B)),"",ColorX ,styleThick);
  678. Plot(LineArray(LastValue(Bbar),LastValue(B),LastValue(C1bar),LastValue(C1)),"",ColorX ,styleThick);
  679. Plot(LineArray(LastValue(C1bar),LastValue(C1),LastValue(Dbar),LastValue(D)),"",ColorX ,styleThick);
  680. Plot(LineArray(LastValue(Xbar),LastValue(X),LastValue(Bbar),LastValue(B)),"",ColorX ,styleDashed);
  681. Plot(LineArray(LastValue(Xbar),LastValue(X),LastValue(Abar),LastValue(A)),"",ColorX ,styleThick);
  682. Plot(LineArray(LastValue(Abar),LastValue(A),LastValue(C1bar),LastValue(C1)),"",ColorX ,styleDashed);
  683. Plot(LineArray(LastValue(Bbar),LastValue(B),LastValue(Dbar),LastValue(D)),"",ColorX ,styleDashed);
  684. Plot(LineArray(LastValue(Xbar),LastValue(X),LastValue(Dbar),LastValue(D)),"",ColorX ,styleDashed);
  685.  
  686. // Viet cac gia tri Fibo tren duong XA, AB, BC
  687. PlotText(NumToStr(LastValue(ABdXA),1.2),(LastValue(Bbar)+LastValue(Xbar))/2,(LastValue(B)+LastValue(X))/2,ColorX );
  688. PlotText(NumToStr(LastValue(BCdAB),1.2),(LastValue(C1bar)+LastValue(Abar))/2,(LastValue(C1)+LastValue(A))/2,ColorX );
  689. PlotText(NumToStr(LastValue(BCdCD),1.2) ,(LastValue(Dbar)+LastValue(Bbar))/2,(LastValue(D)+LastValue(B))/2,ColorX );
  690. PlotText(NumToStr(LastValue(ADdXA),1.2) ,(LastValue(Dbar)+LastValue(Xbar))/2,(LastValue(D)+LastValue(X))/2,ColorX );
  691.  
  692. //---------- Viet cac diem X, A, B, C, D: by binhnd---------------------
  693. xlech = -1;
  694. ylech = 1;
  695. PlotText("X",LastValue(Xbar) + xlech, LastValue(X) + ylech, ColorX );
  696. PlotText("A",LastValue(Abar) + xlech, LastValue(A) - ylech, ColorX );
  697. PlotText("B",LastValue(Bbar) + xlech, LastValue(B) + ylech, ColorX );
  698. PlotText("C",LastValue(C1bar) + xlech, LastValue(C1) - ylech, ColorX );
  699. PlotText("D",LastValue(Dbar) + xlech, LastValue(D) + ylech, ColorX );
  700.  
  701. //--------- Viet thuyet minh mo hinh: by binhnd--------------
  702. if (strPattern!="")
  703. {
  704. strPattern = "Pattern: " + strPattern;
  705. toadoaX = (LastValue(Dbar)+LastValue(Xbar))/2;
  706. toadoY = (LastValue(D)+LastValue(X))/2;
  707.  
  708. PlotText(strPattern,toadoaX,toadoY+1,ColorX );
  709. }
  710.  
  711. } // end of VE DUONG CHO MO HINH BEARISH BAT, GARTLEY, BUTTERFLY, CRAB
  712.  
  713.  
  714.  
  715.  
  716. //=================================
  717. // Show diem ho^~ tro. va` khang' cu. ko?
  718. //=================================
  719.  
  720. plotFractals = ParamToggle("Plot Fractals","Off|On",1);
  721. if(PlotFractals)
  722. {
  723. PlotShapes(shapeSmallCircle*P1,colorYellow,0,H,10);
  724. PlotShapes(shapeSmallCircle*V1,colorBlue,0,L,-10);
  725. }
  726.  
  727.  
  728.  
  729. //==============================================
  730. // DAT DIEU KIEN cho TIM KIEM BULL
  731. //==============================================
  732. dkBull = False;
  733. ListBull = ParamList("Type of Bullish", "None|AB=CD|Gartley|Butterfly|Bat|Crab|All Patterns", 6);
  734. if ( ListBull == "None" ) dkBull = True;
  735. if ( ListBull =="AB=CD" ) dkBull = BullABCD ;
  736. if ( ListBull =="Gartley" ) dkBull = BullGartley ;
  737. if ( ListBull =="Butterfly" ) dkBull = BullButterfly ;
  738. if ( ListBull =="Bat" ) dkBull = BullBat ;
  739. if ( ListBull =="Crab" ) dkBull = BullCrab ;
  740. if ( ListBull =="All Patterns") dkBull = (BullABCD) OR (BullGartley) OR (BullButterfly ) OR (BullBat ) OR (BullCrab);
  741.  
  742. //==============================================
  743. // DAT DIEU KIEN cho TIM KIEM BEAR
  744. //==============================================
  745. dkBear = False;
  746. ListBear = ParamList("Type of Bearish", "None|AB=CD|Gartley|Butterfly|Bat|Crab|All Patterns", 0);
  747. if ( ListBear == "None" ) dkBear = True;
  748. if ( ListBear =="AB=CD" ) dkBear = BearABCD ;
  749. if ( ListBear =="Gartley" ) dkBear = BearGartley ;
  750. if ( ListBear =="Butterfly" ) dkBear = BearButterfly ;
  751. if ( ListBear =="Bat" ) dkBear = BearBat ;
  752. if ( ListBear =="Crab" ) dkBear = BearCrab ;
  753. if ( ListBear =="All Patterns") dkBear = (BearABCD ) OR (BearGartley ) OR (BearButterfly ) OR (BearBat ) OR (BearCrab );
  754. //===============================
  755.  
  756. AddColumn(V,"Volume",1.0);
  757. Filter = (dkBull) AND (dkBear);
  758.  
  759. _SECTION_BEGIN("Kpl System");
  760. /* my entry is very simple(daily data for trading)
  761.  
  762. kpl system for entry only & exit as follow:
  763.  
  764. 1 st exit at x % from entry price only 1/3 quantity.(ie 1st profit target)
  765. 2 nd exit when exit Signal comes from kpl sys remaining 1/3 quantity.
  766. 3. scale-in to initial quantity if new kpl Buy Signal comes.
  767. re-do above scaling-out & scaling-in till filal exit.
  768. 4. final exit all quantity when Close below 21 Day EMA.
  769.  
  770. kpl system code bellow :
  771. */
  772. //AFL by Kamalesh Langote. Email:kpl@...
  773. no=Param( "Swing", 8, 1, 55 );
  774. tsl_col=ParamColor( "Color", colorLightGrey );
  775.  
  776. res=HHV(H,no);
  777. sup=LLV(L,no);
  778. avd=IIf(C>Ref(res,-1),1,IIf(C<Ref(sup,-1),-1,0));
  779. avn=ValueWhen(avd!=0,avd,1);
  780. tsl=IIf(avn==1,sup,res);
  781. Buy=Cover=Cross(C,tsl) ;
  782. Sell=Short=Cross(tsl,C) ;
  783.  
  784. Plot(tsl, _DEFAULT_NAME(), tsl_col, styleStaircase);
  785.  
  786. Buy=ExRem(Buy,Sell);
  787. Sell=ExRem(Sell,Buy);
  788. Short=ExRem(Short,Cover);
  789. Cover=ExRem(Cover,Short);
  790.  
  791. PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,Low,-15);
  792. PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorYellow, 0,High,-15);
  793. PlotShapes(IIf(Cover, shapeHollowCircle, shapeNone),colorWhite, 0,Close,0);
  794. PlotShapes(IIf(Short, shapeHollowCircle, shapeNone),colorYellow, 0,Close,0);
  795.  
  796.  
  797.  
  798. SetPositionSize(300,spsShares);
  799. ApplyStop(0,1,10,1);
  800. //-----------end--------------
  801. Long=Flip(Buy,Sell);
  802. Shrt=Flip(Sell,Buy);
  803.  
  804. BuyPrice=ValueWhen(Buy,C);
  805. SellPrice=ValueWhen(Sell,C);
  806.  
  807.  
  808. Edc=(
  809. WriteIf (Buy AND Ref(shrt,-1), " BUY@ "+C+" ","")+
  810. WriteIf (Sell AND Ref(Long,-1), " SEll@ "+C+" ","")+
  811. WriteIf(Sell , "Last Trade Profit Rs."+(C-BuyPrice)+"","")+
  812. WriteIf(Buy , "Last Trade Profit Rs."+(SellPrice-C)+"",""));
  813. _SECTION_END();
  814.  
  815. //============== TITLE ==============
  816. _SECTION_BEGIN("Title");
  817. if( Status("action") == actionIndicator )
  818. (
  819. Title = EncodeColor(colorGold)+ "KRISHNA Trading System" + EncodeColor(colorRose)+" (" + Name() + ") " + EncodeColor(colorGold)+ Interval(2) +
  820. " " + Date() +" " +" • Open "+WriteVal(O,1.2)+" • "+"Hi "+WriteVal(H,1.2)+" • "+"Lo "+WriteVal(L,1.2)+" • "+
  821. "Close "+WriteVal(C,1.2)+" ("+WriteVal(C-Ref(C,-1),1,0)+" "+WriteVal((C-Ref(C,-1))*100/Ref(C,-1),1.1)+ "%) • Vol= "+ WriteVal(V,1.0)
  822.  
  823.  
  824.  
  825. +"\n"+EncodeColor(colorGrey50)+"------------------------------------------------------------------------------------------------------------"
  826.  
  827. +"\n"+EncodeColor(colorGold)+
  828. WriteIf (Buy , "Signal: Go Long - Entry Price: "+WriteVal(C)+" - Traget: "+WriteVal((BuyPrice-tsl)+BuyPrice)
  829. +" - StopLoss:"+WriteVal(tsl)+" "
  830. ,"")+
  831.  
  832.  
  833. WriteIf (Sell , "Signal: Go Short - Entry Price: "+WriteVal(C)+" - Target: "+WriteVal((tsl-SellPrice)-SellPrice)+" - StopLoss:"+WriteVal(tsl)+" ","")+
  834. EncodeColor(ColorRGB(111,208,255))+
  835. WriteIf(Long AND NOT Buy, "Trade: Long - Entry Price: "+WriteVal((BuyPrice))+" - Profit: "+WriteVal((C-BuyPrice))+" "+EncodeColor(colorLime)+"Let your profit runs!","")+
  836. WriteIf(shrt AND NOT Sell, "Trade: Short - Entry Price: "+WriteVal((SellPrice))+" - Profit: "+WriteVal((SellPrice-C))+" - "+EncodeColor(colorLime)+"Let your profit runs!","")
  837.  
  838. +"\n"+EncodeColor(colorGrey50)+"------------------------------------------------------------------------------------------------------------"
  839. );
  840. Plot( C, "Close", ParamColor("Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
  841. dist = 3*ATR(15);
  842. dist1 = 3*ATR(15);
  843. for( i = 0; i < BarCount; i++ )
  844. {
  845. if( Buy[i] )
  846. {
  847. PlotText( "\nBuy:" + C[ i ] + "\nT= " + ((C[i]-tsl[i])+C[i]) + "\nSL= " + tsl[i], i, C[ i ]-dist[i], colorGreen, colorBlack );
  848. }
  849. if( Sell[i] )
  850. {
  851. PlotText( "Sell:" + C[ i ] + "\nT= " + (C[i]-(tsl[i]-C[i])) + "\nSL= " + tsl[i], i, C[ i ]+dist1[i], colorRed, colorBlack );
  852. }
  853. }
  854.  
  855. /*Plot Ribbon */
  856. Ribbon1=IIf( (C) >(tsl) ,colorWhite, IIf(( tsl )>( C ), colorOrange,colorYellow));
  857.  
  858. _SECTION_END();
  859.  
  860.  
  861. MAPeriod = Param("MA Period", 4, 1, 100);
  862. MAOpen = EMA(Open, MAPeriod);
  863. MAHigh = EMA(High, MAPeriod);
  864. MALow = EMA(Low, MAPeriod);
  865. MAClose = EMA(Close, MAPeriod);
  866.  
  867. HaClose = (MAOpen + MAHigh + MALow + MAClose) / 4;
  868. HaOpen = AMA(Ref(HaClose, -1), 0.5);
  869.  
  870. // for graph collapse
  871. //for(i = 0; i <= MAPeriod; i++) HaClose[i] = Null;
  872. /*
  873. // same
  874. // HaOpen = (Ref(HaOpen, -1) + Ref(HaClose, -1)) / 2;
  875. HaOpen[ 0 ] = HaClose[ 0 ];
  876. for(i = 1; i < BarCount; i++) {
  877. HaOpen[i] = (HaOpen[i - 1] + HaClose[i - 1]) / 2;
  878. }
  879. */
  880.  
  881. HaHigh = Max(MAHigh, Max(HaClose, HaOpen));
  882. HaLow = Min(MALow, Min(HaClose, HaOpen));
  883.  
  884.  
  885. //////////////////////////////////////////////////////////////////////////
  886. _SECTION_BEGIN("Peak");
  887.  
  888. PK = HaHigh > Ref(HaHigh,-1) AND Ref(HaHigh,1) < HaHigh;//Peak
  889. PKV0 = ValueWhen(PK,HaHigh,0);//PeakValue0
  890. PKV1 = ValueWhen(PK,HaHigh,1);//PeakValue1
  891. PKV2 = ValueWhen(PK,HaHigh,2);//PeakValue2
  892. MPK = PKV2 < PKV1 AND PKV1 > PKV0 ;//MajorPeak
  893. MPKV = ValueWhen(Ref(MPK,-1) == 0 AND MPK == 1, PKV1,1); //MajorPeakValue
  894. MPKD = ValueWhen(Ref(MPK,-1) == 0 AND MPK == 1, DateNum(),1); //MajorPeakDate
  895. SD = IIf(DateNum() < LastValue(MPKD,lastmode = True ), Null, LastValue(MPKV,Lastmode = True));//SelectedDate
  896. Plot(SD, "LastMinorResistance",colorRed,styleLine,styleThick);
  897.  
  898. //PLOT THE SECOND LAST MAJOR PEAK RESISTANCE LINE
  899. MPKV2 = ValueWhen(Ref(MPK,-1) == 0 AND MPK == 1, PKV1,2); //MajorPeakValue
  900. MPKD2 = ValueWhen(Ref(MPK,-1) == 0 AND MPK == 1, DateNum(),2); //MajorPeakDate
  901. SD2 = IIf(DateNum() < LastValue(MPKD2,lastmode = True ), Null, LastValue(MPKV2,Lastmode = True));//SelectedDate
  902. Plot(SD2, "LastMajorResistance",colorRed,styleLine);
  903. _SECTION_END();
  904.  
  905. _SECTION_BEGIN("Support");
  906. SP = Ref(HaLow,1) > HaLow AND HaLow < Ref(HaLow,-1);//Peak
  907. SPV0 = ValueWhen(SP,HaLow,0);//PeakValue0
  908. SPV1 = ValueWhen(SP,HaLow,1);//PeakValue1
  909. SPV2 = ValueWhen(SP,HaLow,2);//PeakValue2
  910. MSP = SPV2 > SPV1 AND SPV1 < SPV0 ;//MajorPeak
  911. MSPV = ValueWhen(Ref(MSP,-1) == 0 AND MSP == 1, SPV1,1);
  912. MSPD = ValueWhen(Ref(MSP,-1) == 0 AND MSP == 1, DateNum(),1);
  913. SD = IIf(DateNum() < LastValue(MSPD,lastmode = True ), Null, LastValue(MSPV,Lastmode = True));
  914. Plot(SD,"LastMinorSupport",colorGreen,styleLine,styleThick);
  915.  
  916.  
  917. MSPV2 = ValueWhen(Ref(MSP,-1) == 0 AND MSP == 1, SPV1,2);
  918. MSPD2 = ValueWhen(Ref(MSP,-1) == 0 AND MSP == 1, DateNum(),2);
  919. SD2 = IIf(DateNum() < LastValue(MSPD2,lastmode = True ), Null, LastValue(MSPV2,Lastmode = True));
  920. Plot(SD2,"LastMajorSupport",colorGreen,styleLine);
  921.  
  922. _SECTION_END();
  923.  
  924.  
  925. //ESSENTIAL TRADER TOOLS//
  926. // Compiled by K.Vidyasagar, [email protected] //
  927. //Previous Days HI LO //
  928.  
  929. DayH = TimeFrameGetPrice("H", inDaily, -1); DayHI = LastValue (DayH,1);// yesterdays high
  930. DayL = TimeFrameGetPrice("L", inDaily, -1); DayLI = LastValue (DayL,1); // yesterdays low
  931. DayC = TimeFrameGetPrice("C", inDaily, -1); // yesterdays close
  932. DayO = TimeFrameGetPrice("O", inDaily); // current day open
  933. WeekH= TimeFrameGetPrice("H", inWeekly, 1); WeekHI = LastValue (WeekH,1); // One Week before high
  934. WeekL= TimeFrameGetPrice("L", inWeekly, 1); WeekLI = LastValue (WeekL,1); // One Week before low
  935. MonthH= TimeFrameGetPrice("H", inMonthly, 1); MonthHI = LastValue (MonthH,1); // One Month before high
  936. MonthL= TimeFrameGetPrice("L", inMonthly, 1); MonthLI = LastValue (MonthL,1); // One Month before low
  937.  
  938. numbars = LastValue(Cum(Status("barvisible")));
  939. hts = -33.5;
  940.  
  941. YHL = ParamToggle("Yesterday HI LO","Hide|Show",1);
  942. if(YHL==1) {
  943. Plot(DayL,"YL",colorTurquoise,styleDashed|styleNoRescale|styleNoTitle);
  944. Plot(DayH,"YH",colorOrange,styleDashed|styleNoRescale|styleNoTitle);
  945. PlotText(" YstrdayH " , LastValue(BarIndex())-(numbars/Hts), DayHI, colorOrange);
  946. PlotText(" YstrdayL " , LastValue(BarIndex())-(numbars/Hts), DayLI, colorTurquoise);
  947. }
  948.  
  949. TDBHL = ParamToggle("Week And Month HI LO","Hide|Show",1);
  950. if(TDBHL==1) {
  951. Plot(WeekL,"WeekL",colorTurquoise,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  952. Plot(WeekH,"WeekH",colorOrange,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  953. Plot(MonthL,"MonthL",colorTurquoise,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  954. Plot(MonthH,"MonthH",colorOrange,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  955. PlotText(" WeekH " , LastValue(BarIndex())-(numbars/Hts), WeekHI, colorOrange);
  956. PlotText(" WeekL " , LastValue(BarIndex())-(numbars/Hts), WeekLI, colorTurquoise);
  957. PlotText(" MonthH " , LastValue(BarIndex())-(numbars/Hts), MonthHI, colorOrange);
  958. PlotText(" MonthL " , LastValue(BarIndex())-(numbars/Hts), MonthLI, colorTurquoise);
  959. }
  960.  
  961. // Pivot Levels //
  962. PP = (DayL + DayH + DayC)/3; PPI = LastValue (PP,1); // Pivot
  963. R1 = (PP * 2) - DayL; R1I = LastValue (R1,1); // Resistance 1
  964. S1 = (PP * 2) - DayH; S1I = LastValue (S1,1); // Support 1
  965. R2 = PP + R1 - S1; R2I = LastValue (R2,1); // Resistance 2
  966. S2 = PP - R1 + S1; S2I = LastValue (S2,1); // Support 2
  967. R3 = PP + R2 - S1; R3I = LastValue (R3,1); // Resistance 3
  968. S3 = PP - R2 + S1; S3I = LastValue (S3,1); // Support 3
  969.  
  970. ppl = ParamToggle("Pivot Levels","Hide|Show",0);
  971. if(ppl==1) {
  972. Plot(PP, "PP",colorYellow,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  973. Plot(R1, "R1",colorViolet,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  974. Plot(S1, "S1",colorViolet,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  975. Plot(R2, "R2",colorViolet,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  976. Plot(S2, "S2",colorViolet,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  977. Plot(R3, "R3",colorViolet,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  978. Plot(S3, "S3",colorViolet,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  979.  
  980. PlotText(" Pivot ", LastValue(BarIndex())-(numbars/Hts), PPI, colorYellow);
  981. PlotText(" R1 " , LastValue(BarIndex())-(numbars/Hts), R1I, colorViolet);
  982. PlotText(" S1 " , LastValue(BarIndex())-(numbars/Hts), S1I, colorViolet);
  983. PlotText(" R2 " , LastValue(BarIndex())-(numbars/Hts), R2I, colorViolet);
  984. PlotText(" S2 " , LastValue(BarIndex())-(numbars/Hts), S2I, colorViolet);
  985. PlotText(" R3 " , LastValue(BarIndex())-(numbars/Hts), R3I, colorViolet);
  986. PlotText(" S3 " , LastValue(BarIndex())-(numbars/Hts), S3I, colorViolet);
  987. }
  988. // Camerilla Levels //
  989.  
  990. rg = (DayH - DayL);
  991.  
  992. H5=DayC+1.1*rg; H5I = LastValue (H5,1);
  993. H4=DayC+1.1*rg/2; H4I = LastValue (H4,1);
  994. H3=DayC+1.1*rg/4; H3I = LastValue (H3,1);
  995. H2=DayC+1.1*rg/6; H2I = LastValue (H2,1);
  996. H1=DayC+1.1*rg/12; H1I = LastValue (H1,1);
  997. L1=DayC-1.1*rg/12; L1I = LastValue (L1,1);
  998. L2=DayC-1.1*rg/6; L2I = LastValue (L2,1);
  999. L3=DayC-1.1*rg/4; L3I = LastValue (L3,1);
  1000. L4=DayC-1.1*rg/2; L4I = LastValue (L4,1);
  1001. L5=DayC-1.1*rg; L5I = LastValue (L5,1);
  1002.  
  1003. pcl = ParamToggle("Camerilla Levels","Hide|Show",0);
  1004. if(pcl==1) {
  1005. Plot(H5,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  1006. Plot(H4,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  1007. Plot(H3,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  1008. Plot(H2,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  1009. Plot(H1,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  1010. Plot(L1,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  1011. Plot(L2,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  1012. Plot(L3,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  1013. Plot(L4,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  1014. Plot(L5,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  1015. PlotText(" H5 = " , LastValue(BarIndex())-(numbars/Hts), H5I +0.05, colorRose);
  1016. PlotText(" H4 = " , LastValue(BarIndex())-(numbars/Hts), H4I +0.05, colorRose);
  1017. PlotText(" H3 = " , LastValue(BarIndex())-(numbars/Hts), H3I +0.05, colorRose);
  1018. PlotText(" H2 = " , LastValue(BarIndex())-(numbars/Hts), H2I +0.05, colorRose);
  1019. PlotText(" H1 = " , LastValue(BarIndex())-(numbars/Hts), H1I +0.05, colorRose);
  1020. PlotText(" L1 = " , LastValue(BarIndex())-(numbars/Hts), L1I +0.05, colorRose);
  1021. PlotText(" L2 = " , LastValue(BarIndex())-(numbars/Hts), L2I +0.05, colorRose);
  1022. PlotText(" L3 = " , LastValue(BarIndex())-(numbars/Hts), L3I +0.05, colorRose);
  1023. PlotText(" L4 = " , LastValue(BarIndex())-(numbars/Hts), L4I +0.05, colorRose);
  1024. PlotText(" L5 = " , LastValue(BarIndex())-(numbars/Hts), L5I +0.05, colorRose);
  1025. }
  1026.  
  1027. // Current Days Hi Lo //
  1028. THL = ParamToggle("Todays Hi Lo","Hide|Show",1);
  1029. if(THL==1) {
  1030. isRth = TimeNum() >= 084500 & TimeNum() <= 085959;
  1031. isdRth = TimeNum() >= 084500 & TimeNum() <= 160000;
  1032. aRthL = IIf(isRth, L, 1000000);
  1033. aRthH = IIf(isdRth, H, Null);
  1034. aRthLd = IIf(isdRth, L, 1000000);
  1035. DayH = TimeFrameCompress( aRthH, inDaily, compressHigh );
  1036. DayH = TimeFrameExpand( DayH, inDaily, expandFirst );
  1037. DayL = TimeFrameCompress( aRthLd, inDaily, compressLow );
  1038. DayL = TimeFrameExpand( DayL, inDaily, expandFirst );
  1039. Bars = BarsSince(TimeNum() >= 94500 AND TimeNum() < 095959);//,BarIndex(),1); // AND DateNum()==LastValue(DateNum());
  1040. x0 = BarCount-LastValue(Bars);
  1041. x1 = BarCount-1;
  1042. DayHline=LineArray(x0,LastValue(DayH),x1,LastValue (DayH),0);
  1043. DayLline=LineArray(x0,LastValue(DayL),x1,LastValue (DayL),0);
  1044. DayHlineI = LastValue (DayHline,1);
  1045. DayLlineI = LastValue (DayLline,1);
  1046. Plot(DayHline,"DayH",colorGold,styleDashed|styleNoRescale|styleNoTitle);
  1047. Plot(DayLline,"DayL",colorBlue,styleDashed|styleNoRescale|styleNoTitle);
  1048. PlotText(" Day Hi " , LastValue(BarIndex())-(numbars/Hts), DayHlineI +0.05, colorYellow);
  1049. PlotText(" Day Lo " , LastValue(BarIndex())-(numbars/Hts), DayLlineI +0.05, colorYellow);
  1050. }
  1051. _SECTION_BEGIN("Title");
  1052.  
  1053. DDayO = TimeFrameGetPrice("O", inDaily);
  1054. DHiDay = TimeFrameGetPrice("H", inDaily);
  1055. DLoDay = TimeFrameGetPrice("L", inDaily);
  1056. prvC = TimeFrameGetPrice("C", inDaily, -1);//close
  1057. Title =EncodeColor(colorYellow)+ Date()+EncodeColor(colorPink)+" "+Interval(format=2)+EncodeColor(colorOrange)+" "+Name()+EncodeColor(colorBrightGreen)+" Open:"+WriteVal(O,1.2)+EncodeColor(colorBrightGreen)+" High: "+WriteVal(H,1.2)+EncodeColor(colorWhite)+" Low: "+WriteVal(L,1.2)+EncodeColor(colorYellow)+" Close "+
  1058. WriteVal(C,1.2) +
  1059. " ~ Prev Close : " + EncodeColor(colorYellow) + prvC +EncodeColor(colorGold)+
  1060. "\n Day-Open : " +DDayO + " Day-High : " +DHiDay + " Day-Low : "+ DLoDay ;
  1061.  
  1062. _SECTION_END();
  1063.  
  1064. _SECTION_BEGIN("Background");
  1065. tchoice=Param("Title Selection ",2,1,2,1);
  1066.  
  1067. Plot(C, "", IIf(O>=C, colorWhite, colorBrightGreen), ParamStyle("Price Style",styleCandle,maskPrice));
  1068.  
  1069. //////////////////////////////////////////////////////////////////
  1070.  
  1071. _SECTION_BEGIN("Background text");
  1072. SetChartBkColor(colorBlack);
  1073. strWeekday = StrMid("---sunday---Monday--TuesdayWednesday-Thursday--Friday--Saturday", SelectedValue(DayOfWeek())*9,9);
  1074. GraphXSpace=Param("GraphXSpace",0,-55,200,1);
  1075. C13=Param("fonts",30,10,30,1 );
  1076. C14=Param("left-right",2.1,1.0,5.0,0.1 );
  1077. C15=Param("up-down",12,1,20,1 );
  1078. Miny = Status("axisminy");
  1079. Maxy = Status("axismaxy");
  1080. lvb = Status("lastvisiblebar");
  1081. fvb = Status("firstvisiblebar");
  1082. pxwidth = Status("pxwidth");
  1083. pxheight = Status("pxheight");
  1084. GfxSetBkMode(transparent=1);
  1085. GfxSetOverlayMode(1);
  1086. GfxSelectFont("Candara", Status("pxheight")/C13 );
  1087. GfxSetTextAlign( 6 );
  1088. GfxSetTextColor( ColorRGB (217,217,213));
  1089. GfxTextOut( Name(), Status("pxwidth")/C14, Status("pxheight")/C15 );
  1090. GfxSelectFont("Tahoma", Status("pxheight")/C13*0.5 );
  1091. GfxSetTextColor( ColorRGB (103,103,103));
  1092. GfxTextOut( "By", Status("pxwidth")/C14, Status("pxheight")/C15*2.5 );
  1093. GfxSelectFont("Candara", Status("pxheight")/C13*0.5 );
  1094. GfxSetTextColor( ColorRGB (103,103,103));
  1095. GfxTextOut( " [email protected]", Status("pxwidth")/C14, Status("pxheight")/C15*4 );
  1096. GfxSelectFont("MS Sans Serif", 10, 500, False, False, 0);
  1097. _SECTION_END();
  1098.  
  1099.  
  1100. _SECTION_BEGIN("Parameters");
  1101. LB1= Param("Long LookBack Periods",42,1,60,1);
  1102. LB3= Param("Mid LookBack Periods",18,1,30,1);
  1103. LB2= Param("Short LookBack Periods",6,1,30,1);
  1104.  
  1105. _SECTION_END();
  1106.  
  1107. ppl = ParamToggle("","Off|On",0);
  1108. numbars = LastValue(Cum(Status("barvisible")));
  1109. fraction= IIf(StrRight(Name(),3) == "", 3.2, 3.2);
  1110. hts = -33.5;
  1111. _SECTION_BEGIN("Indicators");
  1112. RSI_Periods = Param("RSI Periods", 2, 1, 100, 1);
  1113. MA_Periods = Param("Periods for MAV", 50, 1, 200, 1);
  1114. Daily_Trend_MA_Pds = Param("Periods for Trend EMA", 6, 1, 100, 1);
  1115. MAV_Value = V/EMA(V, MA_Periods);
  1116. _SECTION_END();
  1117. // Check if previous day's close is above its 6-day EMA
  1118. DailyClose = TimeFrameCompress(Close, inDaily);
  1119. DailyEMA = EMA( DailyClose, Daily_Trend_MA_Pds);
  1120. DailyClose = TimeFrameExpand(DailyClose, inDaily, expandFirst);
  1121. DailyEma = TimeFrameExpand(DailyEMA, inDaily, expandFirst);
  1122. //Plot(DailyEma,"DailEma",colorCustom12,styleLine);
  1123.  
  1124. // Trend detection based on 6EMA for Daily
  1125. Daily_Trend_UP = DailyClose > DailyEMA;
  1126. Daily_Trend_DOWN = DailyClose < DailyEMA;
  1127. Trend_UP_Text = WriteIf(Daily_Trend_UP, "Daily Trend UP", "");
  1128. Trend_DOWN_Text = WriteIf(Daily_Trend_DOWN, "Daily Trend DOWN", "");
  1129. Trend_Neutral_Text = WriteIf(NOT Daily_Trend_DOWN AND NOT Daily_Trend_UP, "Neutral", "");
  1130. TrendCol = IIf(Daily_Trend_UP, colorGreen, IIf(Daily_Trend_DOWN, colorRed, colorLightGrey));
  1131. _SECTION_END();
  1132.  
  1133.  
  1134.  
  1135. _SECTION_BEGIN("Price");
  1136. pds = 20;
  1137. MAFAST = EMA( Close, 20 );
  1138. MASLOW = EMA( Close, 40 );
  1139.  
  1140. DonchianUpper = HHV( Ref( H, -1 ), pds ); // Highest high value of highs in last 20 periods
  1141. DonchianLower = LLV( Ref( L, -1 ), pds ); // Lowest low value of low in last 20 periods
  1142. DonchianMiddle = ( DonchianUpper + DonchianLower ) / 2;
  1143.  
  1144. UpTrend = C > ( LLV( L, 20 ) + 2 * ATR( 10 ) ) AND EMA( Close, 20 ) > EMA( Close, 40 );
  1145. DnTrend = C < ( HHV( H, 20 ) - 2 * ATR( 10 ) ) AND EMA( Close, 20 ) < EMA( Close, 40 );
  1146. Color = IIf( UpTrend, colorBlue, IIf( DnTrend, colorRed, colorYellow) );
  1147.  
  1148. // Plots a 20 period Donchian channel
  1149. Plot( C, "Price", Color, styleCandle | styleThick );
  1150. NewDay = IIf(Day() != Ref(Day(), -1) OR BarIndex() == LastValue(BarIndex()), 1, 0);
  1151. //Plot(NewDay, "", 47, 2 + 32768 + 4096, Minvalue = 0, Maxvalue = 1);
  1152. _SECTION_END();
  1153.  
  1154. _SECTION_BEGIN("PIVOT POINTS");
  1155.  
  1156. /* This code calculates the previous days high, low and close */
  1157. Hi1 = IIf(Day()!=Ref(Day(),-1),Ref(HighestSince(Day()!=Ref(Day(),-1),H,1),-1),0);
  1158. Hi = ValueWhen(Day()!=Ref(Day(),-1),Hi1,1);
  1159. Lo1 = IIf(Day()!=Ref(Day(),-1),Ref(LowestSince(Day()!=Ref(Day(),-1),L,1),-1),0);
  1160. Lo = ValueWhen(Day()!=Ref(Day(),-1),Lo1,1);
  1161. Cl1 = IIf(Day()!=Ref(Day(),-1),Ref(C,-1),0);
  1162. C1 = ValueWhen(Day()!=Ref(Day(),-1),Cl1,1);
  1163. wHi=TimeFrameGetPrice("H",inWeekly,-1);
  1164. wLo=TimeFrameGetPrice("L",inWeekly,-1);
  1165. wCl=TimeFrameGetPrice("C",inWeekly,-1);
  1166. mHi=TimeFrameGetPrice("H",inMonthly,-1);
  1167. mLo=TimeFrameGetPrice("L",inMonthly,-1);
  1168. mCl=TimeFrameGetPrice("C",inMonthly,-1);
  1169.  
  1170. //----------------------------------------------------------------------------------
  1171.  
  1172. /* This code calculates Daily Piovts */
  1173.  
  1174. rg = (Hi - Lo);
  1175. bp = (Hi + Lo + C1)/3; bpI = LastValue (bp,1);
  1176. r1 = (bp*2)-Lo; r1I = LastValue (r1,1);
  1177. s1 = (bp*2)-Hi; s1I = LastValue (s1,1);
  1178. r2 = bp + r1 - s1; r2I = LastValue (r2,1);
  1179. s2 = bp - r1 + s1; s2I = LastValue (s2,1);
  1180. r3 = bp + r2 - s1; r3I = LastValue (r3,1);
  1181. s3 = bp - r2 + s1; s3I = LastValue (s3,1);
  1182. r4 = bp + r2 - s2; r4I = LastValue (r4,1);
  1183. s4 = bp - r2 + s2; s4I = LastValue (s4,1);
  1184. wrg = (wHi - wLo);
  1185. wbp = (wHi + wLo + wCl)/3; wbpI = LastValue (wbp,1);
  1186. wr1 = (wbp*2)-wLo; wr1I = LastValue (wr1,1);
  1187. ws1 = (wbp*2)-wHi; ws1I = LastValue (ws1,1);
  1188. wr2 = wbp + wr1 - ws1; wr2I = LastValue (wr2,1);
  1189. ws2 = wbp - wr1 + ws1; ws2I = LastValue (ws2,1);
  1190. wr3 = wbp + wr2 - ws1; wr3I = LastValue (wr3,1);
  1191. ws3 = wbp - wr2 + ws1; ws3I = LastValue (ws3,1);
  1192. wr4 = wbp + wr2 - ws2; wr4I = LastValue (wr4,1);
  1193. ws4 = wbp - wr2 + ws2; ws4I = LastValue (ws4,1);
  1194. mrg = (mHi - mLo);
  1195. mobp = (mHi + mLo + mCl)/3; mobpI = LastValue (mobp,1);
  1196. mr1 = (mobp*2)-mLo; mr1I = LastValue (mr1,1);
  1197. ms1 = (mobp*2)-mHi; ms1I = LastValue (ms1,1);
  1198. mr2 = mobp + mr1 - ms1; mr2I = LastValue (mr2,1);
  1199. ms2 = mobp - mr1 + ms1; ms2I = LastValue (ms2,1);
  1200.  
  1201. if(ppl==1) {
  1202. Plot(bp,"",colorWhite,styleLine|styleLine|styleNoRescale);
  1203. Plot(s1,"",colorGreen,styleDashed|styleNoRescale);
  1204. Plot(s2,"",colorBrightGreen,styleDashed|styleNoRescale);
  1205. Plot(s3,"",colorBrightGreen,styleDashed|styleNoRescale);
  1206. Plot(s4,"",colorBrightGreen,styleDashed|styleNoRescale);
  1207. Plot(r1,"",colorRed,styleDashed|styleNoRescale);
  1208. Plot(r2,"",colorDarkRed,styleDashed|styleNoRescale);
  1209. Plot(r3,"",colorDarkRed,styleDashed|styleNoRescale);
  1210. Plot(r4,"",colorDarkRed,styleDashed|styleNoRescale);
  1211. Plot(wbp,"",colorTan,styleDashed|styleLine|styleNoRescale);
  1212. Plot(ws1,"",colorPaleGreen,styleDashed|styleNoRescale);
  1213. Plot(ws2,"",colorPaleGreen,styleDashed|styleNoRescale);
  1214. //Plot(ws3,"",colorPaleGreen,styleDashed|styleNoRescale);
  1215. //Plot(ws4,"",colorPaleGreen,styleDashed|styleNoRescale);
  1216. Plot(wr1,"",colorPaleBlue,styleDashed|styleNoRescale);
  1217. Plot(wr2,"",colorPaleBlue,styleDashed|styleNoRescale);
  1218. //Plot(wr3,"",colorPaleBlue,styleDashed|styleNoRescale);
  1219. //Plot(wr4,"",colorPaleBlue,styleDashed|styleNoRescale);
  1220. Plot(mobp,"",colorTan,styleDashed|styleLine|styleNoRescale);
  1221. Plot(ms1,"",colorPaleGreen,styleDashed|styleNoRescale);
  1222. Plot(mr1,"",colorPaleBlue,styleDashed|styleNoRescale);
  1223. PlotText(" Pivot = " + WriteVal(bp,fraction), LastValue(BarIndex())-(numbars/Hts), bpI +0.05, colorWhite);
  1224. PlotText(" Resistance 1 = " + WriteVal(r1,fraction), LastValue(BarIndex())-(numbars/Hts), r1I +0.05, colorRed);
  1225. PlotText(" Support 1 = " + WriteVal(s1,fraction), LastValue(BarIndex())-(numbars/Hts), s1I +0.05, colorGreen);
  1226. PlotText(" Resistance 2 = " + WriteVal(r2,fraction), LastValue(BarIndex())-(numbars/Hts), r2I +0.05, colorDarkRed);
  1227. PlotText(" Support 2 = " + WriteVal(s2,fraction), LastValue(BarIndex())-(numbars/Hts), s2I +0.05, colorBrightGreen);
  1228. PlotText(" Resistance 3 = " + WriteVal(r3,fraction), LastValue(BarIndex())-(numbars/Hts), r3I +0.05, colorDarkRed);
  1229. PlotText(" Support 3 = " + WriteVal(s3,fraction), LastValue(BarIndex())-(numbars/Hts), s3I +0.05, colorBrightGreen);
  1230. PlotText(" Resistance 4 = " + WriteVal(r4,fraction), LastValue(BarIndex())-(numbars/Hts), r4I +0.05, colorDarkRed);
  1231. PlotText(" Support 4 = " + WriteVal(s4,fraction), LastValue(BarIndex())-(numbars/Hts), s4I +0.05, colorBrightGreen);
  1232. PlotText(" W Pivot = " + WriteVal(wbp,fraction), LastValue(BarIndex())-(numbars/Hts), wbpI +0.05, colorTan);
  1233. PlotText(" W Resistance 1 = " + WriteVal(wr1,fraction), LastValue(BarIndex())-(numbars/Hts), wr1I +0.05, colorPaleBlue);
  1234. PlotText(" W Support 1 = " + WriteVal(ws1,fraction), LastValue(BarIndex())-(numbars/Hts), ws1I +0.05, colorPaleGreen);
  1235. PlotText(" W Resistance 2 = " + WriteVal(wr2,fraction), LastValue(BarIndex())-(numbars/Hts), wr2I +0.05, colorPaleBlue);
  1236. PlotText(" W Support 2 = " + WriteVal(ws2,fraction), LastValue(BarIndex())-(numbars/Hts), ws2I +0.05, colorPaleGreen);
  1237. //PlotText(" W Resistance 3 = " + WriteVal(wr3,fraction), LastValue(BarIndex())-(numbars/Hts), wr3I +0.05, colorPaleBlue);
  1238. //PlotText(" W Support 3 = " + WriteVal(ws3,fraction), LastValue(BarIndex())-(numbars/Hts), ws3I +0.05, colorPaleGreen);
  1239. //PlotText(" W Resistance 4 = " + WriteVal(wr4,fraction), LastValue(BarIndex())-(numbars/Hts), wr4I +0.05, colorPaleBlue);
  1240. //PlotText(" W Support 4 = " + WriteVal(ws4,fraction), LastValue(BarIndex())-(numbars/Hts), ws4I +0.05, colorPaleGreen);
  1241. PlotText(" M Pivot = " + WriteVal(mobp,fraction), LastValue(BarIndex())-(numbars/Hts), mobpI+0.05, colorTan);
  1242. PlotText(" M Resistance 1 = " + WriteVal(mr1,fraction), LastValue(BarIndex())-(numbars/Hts), mr1I+0.05, colorPaleBlue);
  1243. PlotText(" M Support 1 = " + WriteVal(ms1,fraction), LastValue(BarIndex())-(numbars/Hts), ms1I+0.05, colorPaleGreen);
  1244. }
  1245.  
  1246.  
  1247. _SECTION_END();
  1248.  
  1249.  
  1250. _SECTION_BEGIN("PIVOTS Hourly");
  1251. TimeFrameSet(inHourly);
  1252. HourlyH= LastValue(Ref(H,-1));
  1253. HourlyL= LastValue(Ref(L,-1));
  1254. HourlyC= LastValue(Ref(C,-1));
  1255. TimeFrameRestore();
  1256.  
  1257. //Daily
  1258. HPP = (HourlyL + HourlyH + HourlyC)/3;HPPI = LastValue (HPP,1);
  1259. HR1 = (2 * HPP) - HourlyL;HR1I = LastValue (HR1,1);
  1260. HS1 = (2 * HPP) - HourlyH;HS1I = LastValue (HS1,1);
  1261. HR2 = HPP + (HourlyH - HourlyL);HR2I = LastValue (HR2,1);
  1262. HS2 = HPP - (HourlyH - HourlyL);HS2I = LastValue (HS2,1);
  1263. HR3 = HourlyH + 2*(HPP - HourlyL);HR3I = LastValue (HR3,1);
  1264. HS3 = HourlyL - 2*(HourlyH - HPP);HS3I = LastValue (HS3,1);
  1265. SHOWDPIVOTS = ParamToggle("Daily Pivots", "No|Yes",0);
  1266. SHOW3 = ParamToggle("3rd Line", "No|Yes",0);
  1267.  
  1268. if (SHOWDPIVOTS & SHOW3)
  1269. {
  1270. Plot(HS3, "S3",ColorR);
  1271. Plot(HR3, "R3",ColorG);
  1272. PlotText(" H Resis 3 = " + WriteVal(HR3,fraction), LastValue(BarIndex())-(numbars/Hts), HR3I +0.05, colorGreen);
  1273. PlotText(" H Sup 3 = " + WriteVal(HS3,fraction), LastValue(BarIndex())-(numbars/Hts), HS3I +0.05, colorRed);
  1274.  
  1275. }
  1276.  
  1277.  
  1278.  
  1279. for (i=BarCount-2;i>(BarCount-13);i--)//set the last bars to the final PP value
  1280. {
  1281. HPP[i] = HPP[BarCount-1];
  1282. HR1[i] = HR1[BarCount-1];
  1283. HR2[i] = HR2[BarCount-1];
  1284. HS1[i] = HS1[BarCount-1];
  1285. HS2[i] = HS2[BarCount-1];
  1286.  
  1287. ColorG[i] = colorGreen;
  1288. ColorB[i] = colorBlue;
  1289. ColorR[i] = colorRed;
  1290. }
  1291.  
  1292. //
  1293. // Conceal all but the trailing portion of the line
  1294. //
  1295. for (i=0;i<BarCount-15;i++) //hide the line except most recent 15 bars
  1296. {
  1297. ColorR[i] = ColorG[i] = ColorB[i] = colorBlack;
  1298. }
  1299.  
  1300.  
  1301. if (SHOWDPIVOTS) {
  1302. Plot(HR2, "R2",ColorG);
  1303. Plot(HR1, "R1",ColorG);
  1304. Plot(HPP, "PP",ColorB);
  1305. Plot(HS1, "S1",ColorR);
  1306. Plot(HS2, "S2",ColorR);
  1307. PlotText(" H Pivot = " + WriteVal(HPP,fraction), LastValue(BarIndex())-(numbars/Hts), HPPI +0.05, colorBlue);
  1308. PlotText(" H Resis 1 = " + WriteVal(HR1,fraction), LastValue(BarIndex())-(numbars/Hts), HR1I +0.05, colorGreen);
  1309. PlotText(" H Sup 1 = " + WriteVal(HS1,fraction), LastValue(BarIndex())-(numbars/Hts), HS1I +0.05, colorRed);
  1310. PlotText(" H Resis 2 = " + WriteVal(HR2,fraction), LastValue(BarIndex())-(numbars/Hts), HR2I +0.05, colorGreen);
  1311. PlotText(" H Sup 2 = " + WriteVal(HS2,fraction), LastValue(BarIndex())-(numbars/Hts), HS2I +0.05, colorRed);
  1312.  
  1313. }
  1314.  
  1315. if (SHOWDPIVOTS & SHOW3)
  1316. {
  1317. Plot(S3, "S3",ColorR);
  1318. Plot(R3, "R3",ColorG);
  1319. PlotText(" H Resis 3 = " + WriteVal(HR3,fraction), LastValue(BarIndex())-(numbars/Hts), HR3I +0.05, colorGreen);
  1320. PlotText(" H Sup 3 = " + WriteVal(HS3,fraction), LastValue(BarIndex())-(numbars/Hts), HS3I +0.05, colorRed);
  1321.  
  1322. }
  1323.  
  1324.  
  1325. _SECTION_END();
  1326.  
  1327. _SECTION_BEGIN("Time Left");
  1328. function GetSecondNum()
  1329. {
  1330. Time = Now( 4 );
  1331. Seconds = int( Time % 100 );
  1332. Minutes = int( Time / 100 % 100 );
  1333. Hours = int( Time / 10000 % 100 );
  1334. SecondNum = int( Hours * 60 * 60 + Minutes * 60 + Seconds );
  1335. return SecondNum;
  1336. }
  1337. RequestTimedRefresh( 1 );
  1338. TimeFrame = Interval();
  1339. SecNumber = GetSecondNum();
  1340. Newperiod = SecNumber % TimeFrame == 0;
  1341. SecsLeft = SecNumber - int( SecNumber / TimeFrame ) * TimeFrame;
  1342. SecsToGo = TimeFrame - SecsLeft;
  1343.  
  1344. x=Param("xposn",0,0,1000,1);
  1345. y=Param("yposn",900,0,1000,1);
  1346.  
  1347. GfxSelectSolidBrush( ColorRGB( 230, 230, 230 ) );
  1348. GfxSelectPen( ColorRGB( 230, 230, 230 ), 2 );
  1349. if ( NewPeriod )
  1350. {
  1351. GfxSelectSolidBrush( colorYellow );
  1352. GfxSelectPen( colorYellow, 2 );
  1353. Say( "New time CANDLE start" );
  1354. }
  1355.  
  1356. cx = Param( "cxposn", 682, 0, 1250, 1 );
  1357. cy = Param( "cyposn", 15, 0, 500, 1 );
  1358. GfxSelectFont( "bold", 10, 98, False );
  1359. //gfxcup = SelectedValue(C > Ref(C,-1));
  1360. //gfxpcolor = IIf(gfxcup,colorBrightGreen,colorRed);
  1361. GfxSetTextColor(colorBrightGreen);
  1362. //GfxSetTextColor( colorWhite );
  1363. GfxTextOut( "Time left for candle close " + SecsToGo + " ", cx, cy );
  1364. _SECTION_END();
Advertisement
Add Comment
Please, Sign In to add comment