Advertisement
saisri

HARMONIC PATTERN DETECTION

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