Advertisement
retesere20

--recyclet--8.61 avp before Last_Pivot_ var changes

Sep 12th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 58.51 KB | None | 0 0
  1.  
  2.  
  3. //**********************************************************************************************//
  4. #region Pivot1 ATR
  5.  
  6. pivot1_atr_allow= Pivot1_TrailingReversal = 1 or Pivot1_Exit______________ONOFF = 1;
  7.  
  8. if pivot1_atr_allow then begin
  9.  
  10. EntryFilter_MA1_ORB = avp_core_avg_types( close, ORB_MAPeriod, ORB_MAType, pivot1_atr_allow );
  11.  
  12. if Pivot1_High_ATR_LeftFilter_Length > 0 then
  13. Pivot1_High_ATR_LeftFilter_val = tt_Average( TrueRange, Pivot1_High_ATR_LeftFilter_Length, 0, pivot1_atr_allow and Pivot1_High_ATR_LeftFilter_Length > 0);
  14.  
  15. if Pivot1_Low_ATR_LeftFilter_Length > 0 then
  16. Pivot1_Low_ATR_LeftFilter_val = tt_Average( TrueRange, Pivot1_Low_ATR_LeftFilter_Length, 0, pivot1_atr_allow and Pivot1_Low_ATR_LeftFilter_Length > 0);
  17.  
  18. if Pivot1_High_ATR_RightFilter_Length > 0 then
  19. Pivot1_High_ATR_RightFilter_val = tt_Average( TrueRange, Pivot1_High_ATR_RightFilter_Length,0, pivot1_atr_allow and Pivot1_High_ATR_RightFilter_Length > 0);
  20.  
  21. if Pivot1_Low_ATR_RightFilter_Length > 0 then
  22. Pivot1_Low_ATR_RightFilter_val = tt_Average( TrueRange, Pivot1_Low_ATR_RightFilter_Length, 0, pivot1_atr_allow and Pivot1_Low_ATR_RightFilter_Length > 0);
  23. end;
  24.  
  25.  
  26. #endregion
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33. //**********************************************************************************************//
  34. #region Pivot 1
  35.  
  36.  
  37. // vars:
  38. // ix(0),
  39. // Last_Pivot1_High(0),
  40. // Last_Pivot1_Low(0),
  41. // Last_Pivot1_HighBar(0),
  42. // Last_Pivot1_LowBar(0),
  43. // the_Pivot1_High(0),
  44. // the_Pivot1_Low(0),
  45. // Pivot1_HighRev(0),
  46. // Pivot1_LowRev(0),
  47. // MR_High(0),
  48. // MR_Low(0),
  49. // intrabarpersist the_Pivot1_High_CheckRight(false),
  50. // intrabarpersist the_Pivot1_Low_CheckRight(false),
  51. // intrabarpersist Valid_Pivot1_High(0),
  52. // intrabarpersist Valid_Pivot1_Low(0),
  53. // LeftAmt(0);
  54.  
  55. Pivot1_XR_allow_temp = Pivot1_Exit______________ONOFF = 1 or Pivot1_TrailingReversal = 1;
  56.  
  57. if Pivot1_XR_allow_temp then begin
  58.  
  59. If BarComplete then begin
  60.  
  61. // Must wait till close of bar to confirm pivots
  62. if PivotHighVSBar(
  63. iff(Pivot1_XR_allow_temp and BarComplete, 1, 0),
  64. high,
  65. iff(Pivot1_XR_allow_temp and BarComplete, Pivot1_High_Left_Strength, 0),
  66. iff(Pivot1_XR_allow_temp and BarComplete, Pivot1_High_Right_Strength, 0),
  67. iff(Pivot1_XR_allow_temp and BarComplete, Pivot1_High_Right_Strength+1, 1)
  68. ) <> -1 then begin
  69. //if AVPPivotHighVSBar( high, Pivot1_High_Left_Strength, Pivot1_High_Right_Strength, MaxBarsBack ) <> -1 and BarComplete then
  70.  
  71. Last_Pivot1_High = high[Pivot1_High_Right_Strength];
  72. Last_Pivot1_HighBar = barnumber[Pivot1_High_Right_Strength];
  73.  
  74. // Filter amounts
  75. if Pivot1_Filter_Option = 1 then
  76. Pivot1_High_ATR_LeftFilter_Amt = Last_Pivot1_High * Pivot1_High_PctLeftFilter * 0.01
  77. else
  78. Pivot1_High_ATR_LeftFilter_Amt = Pivot1_High_ATR_LeftFilter_val * Pivot1_High_ATR_LeftFilter_Period;
  79.  
  80. // Get left percent
  81. if TradeOnlySameDay then begin
  82. // if there is no valid low, use: low of day
  83. value1= iff (Valid_Pivot1_LowBar < NewDayBarNum, LowOfDay, Valid_Pivot1_Low ) ;
  84. end
  85. else begin
  86. // if there is no valid low, use: low of chart
  87. value1= iff (Valid_Pivot1_LowBar = 0, LowOfChart, Valid_Pivot1_Low );
  88. end;
  89. LeftAmt = Last_Pivot1_High - value1;
  90.  
  91. // The pivot1 is valid only, if the pivot is :
  92. // - x% away from the opposite (left) pivot
  93. // - higher than the last valid swing pivot high
  94. if LeftAmt > Pivot1_High_ATR_LeftFilter_Amt or Last_Pivot1_High > the_Pivot1_High then begin
  95. the_Pivot1_High = Last_Pivot1_High;
  96. the_Pivot1_HighBar = Last_Pivot1_HighBar;
  97. the_Pivot1_High_CheckRight = true;
  98. end;
  99. end;
  100.  
  101. if PivotLowVSBar(
  102. iff(Pivot1_XR_allow_temp and BarComplete, 1, 0),
  103. low,
  104. iff(Pivot1_XR_allow_temp and BarComplete, Pivot1_Low_Left_Strength, 0),
  105. iff(Pivot1_XR_allow_temp and BarComplete, Pivot1_Low_Right_Strength, 0),
  106. iff(Pivot1_XR_allow_temp and BarComplete, Pivot1_Low_Right_Strength+1, 1)
  107. ) <> -1 then begin
  108. //if AVPPivotLowVSBar( low, Pivot1_Low_Left_Strength, Pivot1_Low_Right_Strength, MaxBarsBack ) <> -1 and BarComplete then
  109.  
  110. Last_Pivot1_Low = low[Pivot1_Low_Right_Strength];
  111. Last_Pivot1_LowBar = barnumber[Pivot1_Low_Right_Strength];
  112.  
  113. // Filter amounts
  114. Pivot1_Low_ATR_LeftFilter_Amt = iff( Pivot1_Filter_Option = 1, Last_Pivot1_Low * Pivot1_Low_PctLeftFilter * 0.01, Pivot1_Low_ATR_LeftFilter_val * Pivot1_Low_ATR_LeftFilter_Period );
  115.  
  116. // Get left percent
  117. if TradeOnlySameDay then begin
  118. // if there is no valid high, use: high of day
  119. value1 = iff ( Valid_Pivot1_HighBar < NewDayBarNum, HighOfDay, Valid_Pivot1_High);
  120. end
  121. else begin
  122. // if there is no valid high, use: high of chart
  123. value1 = iff ( Valid_Pivot1_HighBar = 0, HighOfChart, Valid_Pivot1_High);
  124. end;
  125. LeftAmt = value1- Last_Pivot1_Low;
  126.  
  127. // The pivot1 is valid only, if the pivot is x% away from the opposite pivot
  128. // Exception if this is lowest than the last valid swing pivot low
  129. if LeftAmt > Pivot1_Low_ATR_LeftFilter_Amt or Last_Pivot1_Low < the_Pivot1_Low then begin
  130. the_Pivot1_Low = Last_Pivot1_Low;
  131. the_Pivot1_LowBar = Last_Pivot1_LowBar;
  132. the_Pivot1_Low_CheckRight = true;
  133. end;
  134. end;
  135.  
  136. end;
  137.  
  138.  
  139. // Filter amounts
  140. if Pivot1_Filter_Option = 1 then begin
  141. Pivot1_High_ATR_RightFilter_Amt = the_Pivot1_High * Pivot1_High_PctRightFilter * 0.01;
  142. Pivot1_Low_ATR_RightFilter_Amt = the_Pivot1_Low * Pivot1_Low_PctRightFilter * 0.01;
  143. end
  144. else begin
  145. Pivot1_High_ATR_RightFilter_Amt = Pivot1_High_ATR_RightFilter_val * Pivot1_High_ATR_RightFilter_Period;
  146. Pivot1_Low_ATR_RightFilter_Amt = Pivot1_Low_ATR_RightFilter_val * Pivot1_Low_ATR_RightFilter_Period;
  147. end;
  148.  
  149. // Stop checking for %Filter Right if price breaks the pivot high
  150. if the_Pivot1_High_CheckRight and high > the_Pivot1_High then
  151. the_Pivot1_High_CheckRight = false;
  152.  
  153. if the_Pivot1_Low_CheckRight and low < the_Pivot1_Low then
  154. the_Pivot1_Low_CheckRight = false;
  155.  
  156. // Check %Filter Right
  157. if the_Pivot1_High_CheckRight and close <= the_Pivot1_High - Pivot1_High_ATR_RightFilter_Amt then begin
  158. Valid_Pivot1_High = the_Pivot1_High;
  159. Valid_Pivot1_HighBar = the_Pivot1_HighBar;
  160. the_Pivot1_High_CheckRight = false;
  161. end;
  162.  
  163. if the_Pivot1_Low_CheckRight and close >= the_Pivot1_Low + Pivot1_Low_ATR_RightFilter_Amt then begin
  164. Valid_Pivot1_Low = the_Pivot1_Low;
  165. Valid_Pivot1_LowBar = the_Pivot1_LowBar;
  166. the_Pivot1_Low_CheckRight = false;
  167. end;
  168.  
  169.  
  170.  
  171. // If the last valid swing pivot high today was broken calculate the highest price that occurred after the break
  172. if Pivot1_HighBreakBar = Valid_Pivot1_HighBar and Valid_Pivot1_HighBar >= NewDayBarNum then begin
  173. MR_High = Valid_Pivot1_High;
  174. for ix = barnumber[0] - Valid_Pivot1_HighBar downto 1 begin
  175. MR_High = maxlist(high[ix],MR_High);
  176. end;
  177. MR_High = maxlist(PrevHigh,MR_High);
  178. end;
  179.  
  180. // If the last valid swing pivot low today was broken calculate the lowest price that occurred after the break
  181. if Pivot1_LowBreakBar = Valid_Pivot1_LowBar and Valid_Pivot1_LowBar >= NewDayBarNum then begin
  182. MR_Low = Valid_Pivot1_Low;
  183. for ix = barnumber[0] - Valid_Pivot1_LowBar downto 1 begin
  184. MR_Low = minlist(Low[ix],MR_Low);
  185. end;
  186. MR_Low = minlist(PrevLow,MR_Low);
  187. end;
  188.  
  189. // If no valid pivot high today then use HighOfDay
  190. // Only for reversals
  191. if TradeOnlySameDay and Valid_Pivot1_HighBar < NewDayBarNum and MarketPosition=-1 then
  192. Pivot1_HighRev = HighOfDay + CalculateEntrySlack(HighOfDay, Entry_Slack_ATR)
  193. // Update 9/22/16
  194. // If the last valid swing pivot high was broken calculate the highest price that occurred after the break to reverse a short
  195. else if Pivot1_HighBreakBar = Valid_Pivot1_HighBar and MarketPosition=-1 then
  196. Pivot1_HighRev=MR_High + CalculateEntrySlack(MR_High, Entry_Slack_ATR)
  197. else if (not TradeOnlySameDay) or Valid_Pivot1_HighBar >= NewDayBarNum then
  198. Pivot1_HighRev = Valid_Pivot1_High + CalculateEntrySlack(Valid_Pivot1_High, Entry_Slack_ATR)
  199. else
  200. Pivot1_HighRev = 0;
  201.  
  202. // If no valid pivot low today then use LowOfDay
  203. // Only for reversals
  204. if TradeOnlySameDay and Valid_Pivot1_LowBar < NewDayBarNum and MarketPosition=1 then
  205. Pivot1_LowRev = LowOfDay - CalculateEntrySlack(LowOfDay, Entry_Slack_ATR)
  206. // Update 9/22/16
  207. // If the last valid swing pivot low was broken calculate the lowest price that occurred after the break to reverse a long
  208. else if Pivot1_LowBreakBar = Valid_Pivot1_LowBar and MarketPosition=1 then
  209. Pivot1_LowRev = MR_Low - CalculateEntrySlack(MR_Low, Entry_Slack_ATR)
  210. else if (not TradeOnlySameDay) or Valid_Pivot1_LowBar >= NewDayBarNum then
  211. Pivot1_LowRev = Valid_Pivot1_Low - CalculateEntrySlack(Valid_Pivot1_Low, Entry_Slack_ATR)
  212. else
  213. Pivot1_LowRev = 0;
  214. end;
  215.  
  216. #endregion
  217.  
  218.  
  219.  
  220.  
  221. //**********************************************************************************************//
  222. #region Pivot 2
  223.  
  224. // vars:
  225. // Last_Pivot2_High(0),
  226. // Last_Pivot2_Low(0),
  227. // Last_Pivot2_HighBar(0),
  228. // Last_Pivot2_LowBar(0),
  229. // Exit_Pivot2_High(0),
  230. // Exit_Pivot2_Low(0),
  231. // Pivot2_HighRev(0),
  232. // Pivot2_LowRev(0),
  233. // MR_High2(0),
  234. // MR_Low2(0),
  235. // intrabarpersist Exit_Pivot2_High_CheckRight(false),
  236. // intrabarpersist Exit_Pivot2_Low_CheckRight(false),
  237. // intrabarpersist Valid_Pivot2_High(0),
  238. // intrabarpersist Valid_Pivot2_Low(0),
  239. // intrabarpersist Pivot2_High_RightConfirmBar(0),
  240. // intrabarpersist Pivot2_Low_RightConfirmBar(0),
  241. // NewValid_Pivot2_High(false),
  242. // NewValid_Pivot2_Low(false),
  243. // intrabarpersist Pivot2_High_EntryPr(0),
  244. // intrabarpersist Pivot2_Low_EntryPr(0),
  245. // LeftPct(0);
  246.  
  247. Pivot2_ER_allow_temp = Pivot2_Entry_____________ONOFF =1 and Pivot2_TrailingReversal=1;
  248.  
  249. if Pivot2_ER_allow_temp then begin
  250.  
  251. NewValid_Pivot2_High = false;
  252. NewValid_Pivot2_Low = false;
  253.  
  254. // Must wait till close of bar to confirm pivots
  255. //if PivotHighVSBar( 1, high, Pivot2_High_Left_Strength, Pivot2_High_Right_Strength, Pivot2_High_Right_Strength + 1 ) <> -1 and
  256.  
  257. if ( BarComplete ) then begin
  258.  
  259. if AVPPivotHighVSBar(
  260. high,
  261. iff(Pivot2_ER_allow_temp and BarComplete, Pivot2_High_Left_Strength, 0),
  262. iff(Pivot2_ER_allow_temp and BarComplete, Pivot2_High_Right_Strength, 0),
  263. iff(Pivot2_ER_allow_temp and BarComplete, MaxBarsBack, 1)
  264. ) <> -1 then begin
  265. Last_Pivot2_High = high[Pivot2_High_Right_Strength];
  266. Last_Pivot2_HighBar = barnumber[Pivot2_High_Right_Strength];
  267.  
  268.  
  269. // Get left percent
  270. // There may not be a valid pivot low, in that case use low of day for DT or low of chart for swing trade
  271. if Last_Pivot2_High<>0 then begin
  272. if TradeOnlySameDay then begin
  273. LeftPct = iff( Valid_Pivot2_LowBar < NewDayBarNum,
  274. (Last_Pivot2_High - LowOfDay) / Last_Pivot2_High,
  275. (Last_Pivot2_High - Valid_Pivot2_Low) / Last_Pivot2_High
  276. );
  277. end
  278. else begin
  279. LeftPct = iff( Valid_Pivot2_LowBar = 0,
  280. (Last_Pivot2_High - LowOfChart) / Last_Pivot2_High,
  281. (Last_Pivot2_High - Valid_Pivot2_Low) / Last_Pivot2_High
  282. );
  283. end;
  284. end;
  285.  
  286. // The pivot is valid only, if the pivot is x% away from the opposite pivot
  287. // OR if this is higher than the last valid swing pivot high
  288. if LeftPct > Pivot2_High_PctLeftFilter * 0.01 or Last_Pivot2_High > Exit_Pivot2_High then begin
  289.  
  290. Exit_Pivot2_High = Last_Pivot2_High;
  291. Exit_Pivot2_HighBar = Last_Pivot2_HighBar;
  292.  
  293. Valid_Pivot2_High = Exit_Pivot2_High;
  294. Valid_Pivot2_HighBar = Exit_Pivot2_HighBar;
  295. //Exit_Pivot2_High_CheckRight = true;
  296. NewValid_Pivot2_High = true;
  297. end;
  298. end;
  299.  
  300.  
  301.  
  302. //if PivotLowVSBar( 1, low, Pivot2_Low_Left_Strength, Pivot2_Low_Right_Strength, Pivot2_Low_Right_Strength + 1 ) <> -1 and
  303. if AVPPivotLowVSBar(
  304. low,
  305. iff(Pivot2_ER_allow_temp and BarComplete, Pivot2_Low_Left_Strength, 0),
  306. iff(Pivot2_ER_allow_temp and BarComplete, Pivot2_Low_Right_Strength, 0),
  307. iff(Pivot2_ER_allow_temp and BarComplete, MaxBarsBack, 1)
  308. ) <> -1 then begin
  309. Last_Pivot2_Low = low[Pivot2_Low_Right_Strength];
  310. Last_Pivot2_LowBar = barnumber[Pivot2_Low_Right_Strength];
  311.  
  312. // Get left percent
  313. // There may not be a valid pivot high, in that case use high of day for DT or high of chart for swing trade
  314. if Last_Pivot2_Low<>0 then begin
  315. if TradeOnlySameDay then begin
  316. LeftPct = iff( Valid_Pivot2_HighBar < NewDayBarNum,
  317. (HighOfDay - Last_Pivot2_Low) / Last_Pivot2_Low,
  318. (Valid_Pivot2_High - Last_Pivot2_Low) / Last_Pivot2_Low
  319. );
  320. end
  321. else begin
  322. LeftPct = iff( Valid_Pivot2_HighBar = 0,
  323. (HighOfChart - Last_Pivot2_Low) / Last_Pivot2_Low,
  324. (Valid_Pivot2_High - Last_Pivot2_Low) / Last_Pivot2_Low
  325. );
  326. end;
  327. end;
  328.  
  329. // The pivot is valid only, if the pivot is:
  330. // x% away from the opposite pivot
  331. // lowest than the last valid swing pivot low
  332. if Last_Pivot2_Low<>0 and (Last_Pivot2_High - Last_Pivot2_Low) / Last_Pivot2_Low > Pivot2_Low_PctLeftFilter * 0.01 or Last_Pivot2_Low < Exit_Pivot2_Low then begin
  333. Exit_Pivot2_Low = Last_Pivot2_Low;
  334. Exit_Pivot2_LowBar = Last_Pivot2_LowBar;
  335.  
  336. Valid_Pivot2_Low = Exit_Pivot2_Low;
  337. Valid_Pivot2_LowBar = Exit_Pivot2_LowBar;
  338. //Exit_Pivot2_Low_CheckRight = true;
  339. NewValid_Pivot2_Low = true;
  340.  
  341. ////_PRINT("NewValid_Pivot2_Low");
  342. end;
  343. end;
  344. end;
  345.  
  346. // Reset Pivot 2
  347. if IsFirstBarOfDay and PrevBarStatus=2 and TradeOnlySameDay then begin
  348. Pivot2_High_EntryOK = false;
  349. Pivot2_Low_EntryOK = false;
  350. end;
  351.  
  352. // Pivot 2 does not check right. Instead if Pivot2_High_PctRightFilter is non-zero then this becomes the
  353. // entry price if it is below the calculated entry.
  354. if NewValid_Pivot2_High then begin
  355. Pivot2_High_EntryOK = true;
  356. value1= tt_lowest(low,Pivot2_Low_Right_Strength, Pivot2_Entry_____________ONOFF=1 or Pivot2_TrailingReversal=1);
  357. Pivot2_High_EntryPr = value1 - CalculateEntrySlack(value1, Entry_Slack_ATR);
  358. if Pivot2_High_PctRightFilter > 0 and Valid_Pivot2_High * (1-Pivot2_High_PctRightFilter * 0.01) < Pivot2_High_EntryPr then
  359. Pivot2_High_EntryPr = Round(Valid_Pivot2_High * (1-Pivot2_High_PctRightFilter * 0.01),2);
  360. end;
  361.  
  362. if NewValid_Pivot2_Low then begin
  363. Pivot2_Low_EntryOK = true;
  364. Value1 = tt_highest(high,Pivot2_High_Right_Strength, Pivot2_Entry_____________ONOFF=1 or Pivot2_TrailingReversal=1);
  365. Pivot2_Low_EntryPr = Value1 + CalculateEntrySlack( Value1 , Entry_Slack_ATR);
  366. if Pivot2_Low_PctRightFilter > 0 and Valid_Pivot2_Low * (1+Pivot2_Low_PctRightFilter * 0.01) > Pivot2_Low_EntryPr then
  367. Pivot2_Low_EntryPr = Round(Valid_Pivot2_Low * (1+Pivot2_Low_PctRightFilter * 0.01),2);
  368. end;
  369.  
  370.  
  371. // If the last valid swing pivot high today was broken calculate the highest price that occurred after the break
  372. if Pivot2_HighBreakBar = Valid_Pivot2_HighBar and Valid_Pivot2_HighBar >= NewDayBarNum then begin
  373. MR_High2 = Valid_Pivot2_High;
  374. for ix = barnumber[0] - Valid_Pivot2_HighBar downto 1 begin
  375. MR_High2 = maxlist(high[ix],MR_High2);
  376. end;
  377. MR_High2 = maxlist(PrevHigh,MR_High2);
  378. end;
  379.  
  380. // If the last valid swing Pivot low today was broken calculate the lowest price that occurred after the break
  381. if Pivot2_LowBreakBar = Valid_Pivot2_LowBar and Valid_Pivot2_LowBar >= NewDayBarNum then begin
  382. MR_Low2 = Valid_Pivot2_Low;
  383. for ix = barnumber[0] - Valid_Pivot2_LowBar downto 1 begin
  384. MR_Low2 = minlist(Low[ix],MR_Low2);
  385. end;
  386. MR_Low2 = minlist(PrevLow,MR_Low2);
  387. end;
  388.  
  389. // If no valid pivot high today then use HighOfDay
  390. if TradeOnlySameDay and Valid_Pivot2_HighBar < NewDayBarNum and MarketPosition=-1 then
  391. Pivot2_HighRev = HighOfDay + CalculateEntrySlack(HighOfDay, Entry_Slack_ATR)
  392. // Update 9/22/16
  393. // If the last valid swing pivot high was broken calculate the highest price that occurred after the break to reverse a short
  394. else if Pivot2_HighBreakBar = Valid_Pivot2_HighBar and MarketPosition=-1 then
  395. Pivot2_HighRev=MR_High2 + CalculateEntrySlack(MR_High2, Entry_Slack_ATR)
  396. else if (not TradeOnlySameDay) or Valid_Pivot2_HighBar >= NewDayBarNum then
  397. Pivot2_HighRev = Valid_Pivot2_High + CalculateEntrySlack(Valid_Pivot2_High, Entry_Slack_ATR)
  398. else
  399. Pivot2_HighRev = 0;
  400.  
  401. // If no valid pivot low today then use LowOfDay
  402. if TradeOnlySameDay and Valid_Pivot2_LowBar < NewDayBarNum and MarketPosition=1 then
  403. Pivot2_LowRev = LowOfDay - CalculateEntrySlack(LowOfDay, Entry_Slack_ATR)
  404. // Update 9/22/16
  405. // If the last valid swing pivot low was broken calculate the lowest price that occurred after the break to reverse a long
  406. else if Pivot2_LowBreakBar = Valid_Pivot2_LowBar and MarketPosition=1 then
  407. Pivot2_LowRev=MR_Low2 - CalculateEntrySlack(MR_Low2, Entry_Slack_ATR)
  408. else if (not TradeOnlySameDay) or Valid_Pivot2_LowBar >= NewDayBarNum then
  409. Pivot2_LowRev = Valid_Pivot2_Low - CalculateEntrySlack(Valid_Pivot2_Low, Entry_Slack_ATR)
  410. else
  411. Pivot2_LowRev = 0;
  412.  
  413. end;
  414.  
  415.  
  416. #endregion
  417.  
  418.  
  419.  
  420.  
  421.  
  422.  
  423. //**********************************************************************************************//
  424. #region Pivot 3
  425.  
  426. // vars:
  427. // Pivot3_HAColor(0),
  428. // Pivot3_HASignalUp(false),
  429. // Pivot3_HASignalDown(false),
  430.  
  431. // Last_Pivot3_High(0), Last_Pivot3_HighOffset(0), Last_Pivot3_HighBar(0),
  432. // Last_Pivot3_Low(0), Last_Pivot3_LowOffset(0), Last_Pivot3_LowBar(0),
  433.  
  434. // Pivot3_High_LeftFilter_Amt(0), Pivot3_Low_LeftFilter_Amt(0),
  435. // Pivot3_High_RightFilter_Amt(0), Pivot3_Low_RightFilter_Amt(0),
  436.  
  437. // Exit_Pivot3_High(0), Exit_Pivot3_HighOffset(0), Exit_Pivot3_High_CheckRight(false), //Exit_Pivot3_HighBar(0),
  438. // Exit_Pivot3_Low(0), Exit_Pivot3_LowOffset(0), Exit_Pivot3_Low_CheckRight(false), //Exit_Pivot3_LowBar(0),
  439.  
  440. // Valid_Pivot3_High(0),
  441. // //Valid_Pivot3_HighBar(0),
  442. // Valid_Pivot3_Low(0),
  443.  
  444. // Pivot3_HighRev(0),
  445. // Pivot3_LowRev(0),
  446.  
  447. // Pivot3_HAGreen_High(0),
  448. // Pivot3_HAGreen_HighBar(0),
  449. // Pivot3_HARed_Low(0),
  450. // Pivot3_HARed_LowBar(0),
  451.  
  452. // LeftBarsOK(false),
  453. // RightBarsOK(false)
  454. // ;
  455.  
  456. if Pivot3_Entry_____________ONOFF = 1 then begin
  457.  
  458. Value1 = AVP_HeikinAshi3(
  459. iff(Pivot3_Entry_____________ONOFF = 1, Pivot3_HA_CompBars, 0),
  460. iff(Pivot3_Entry_____________ONOFF = 1, Pivot3_HA_SmoothHA, 0),
  461. iff(Pivot3_Entry_____________ONOFF = 1, Pivot3_HA_SmoothAvgType,0),
  462. iff(Pivot3_Entry_____________ONOFF = 1, Pivot3_HA_SmoothLength, 1),
  463. Pivot3_debug_Show_HA_VISUALS,
  464. Pivot3_HAColor,
  465. Pivot3_HASignalUp,
  466. Pivot3_HASignalDown
  467. );
  468.  
  469. //reset on new day
  470. Pivot3_HA_reset = (TradeOnlySameDay and IsFirstTickOfDay);
  471.  
  472. _HA_Change_up = (Pivot3_HASignalUp and BarComplete);
  473. _HA_Change_down = (Pivot3_HASignalDown and BarComplete);
  474.  
  475.  
  476. // Update Red Low
  477. // This will include the confirmation candle
  478. if (Pivot3_HAColor=red or _HA_Change_up) and (low < Pivot3_HARed_Low or (Pivot3_HA_reset or Pivot3_HARed_Low = 0) ) then begin
  479. Pivot3_HARed_Low = low;
  480. Pivot3_HARed_LowBar = barnumber;
  481. end;
  482.  
  483. // Update Green High
  484. // This will include the confirmation candle
  485. if (Pivot3_HAColor=green or _HA_Change_down) and (high > Pivot3_HAGreen_High or (Pivot3_HA_reset or Pivot3_HAGreen_High = 0) ) then begin
  486. Pivot3_HAGreen_High = high;
  487. Pivot3_HAGreen_HighBar = barnumber;
  488. end;
  489.  
  490.  
  491.  
  492. // Pivot High: Change of trend down
  493. if BarComplete then begin
  494.  
  495. If (Pivot3_HASignalDown[maxlist(0,Pivot3_High_Right_Strength-1)] ) then begin
  496. // Lookback for high
  497. //value1=Extremes( high, Pivot3_High_Look_Back, 1, Last_Pivot3_High, Last_Pivot3_HighOffset ) ;
  498. //Last_Pivot3_HighBar = barnumber[Last_Pivot3_HighOffset];
  499.  
  500. if Pivot3_HAGreen_High > 0 then begin
  501.  
  502. Last_Pivot3_High = Pivot3_HAGreen_High;
  503. Last_Pivot3_HighBar = Pivot3_HAGreen_HighBar;
  504.  
  505. // Check left bars
  506. LeftBarsOK = true;
  507. for ix = currentbar-Pivot3_HAGreen_HighBar+1 to currentbar-Pivot3_HAGreen_HighBar + Pivot3_High_Left_Strength begin
  508. if Pivot3_HAColor[ix] <> green then
  509. LeftBarsOK = false;
  510. end;
  511.  
  512. // Check right bars
  513. RightBarsOK = currentbar-Pivot3_HAGreen_HighBar >= Pivot3_High_Right_Strength;
  514.  
  515. // Filter amounts
  516. Pivot3_High_LeftFilter_Amt = Last_Pivot3_High * Pivot3_High_PctLeftFilter * 0.01;
  517.  
  518. // Get left percent
  519. // There may not be a valid pivot low, in that case use: low of day (for DT) or low of chart (for swing trade)
  520. if TradeOnlySameDay then begin
  521. value1 = iff(Valid_Pivot3_LowBar <= NewDayBarNum, LowOfDay, Valid_Pivot3_Low);
  522. end
  523. else begin
  524. value1 = iff(Valid_Pivot3_LowBar = 0, LowOfChart, Valid_Pivot3_Low);
  525. end;
  526. LeftAmt = Last_Pivot3_High - value1;
  527.  
  528. // The pivot is valid only, if the pivot is x% away from the opposite pivot
  529. // Exception if this is higher than the last valid swing pivot high
  530. if LeftBarsOK and RightBarsOK and (LeftAmt > Pivot3_High_LeftFilter_Amt or Last_Pivot3_High > Exit_Pivot3_High) then begin
  531. Exit_Pivot3_High = Last_Pivot3_High;
  532. Exit_Pivot3_HighBar = Last_Pivot3_HighBar;
  533. Exit_Pivot3_High_CheckRight = true;
  534. end;
  535. end;
  536.  
  537. Pivot3_HARed_Low = low;
  538. Pivot3_HARed_LowBar = barnumber;
  539. end;
  540. end;
  541.  
  542. // Pivot Low: Change of trend up
  543. if BarComplete then begin
  544. If (Pivot3_HASignalUp[maxlist(0,Pivot3_Low_Right_Strength-1)] ) then begin
  545.  
  546. // Lookback for low
  547. //value1=Extremes( low, Pivot3_Low_Look_Back, -1, Last_Pivot3_Low, Last_Pivot3_LowOffset ) ;
  548. //Last_Pivot3_LowBar = barnumber[Last_Pivot3_LowOffset];
  549.  
  550. if Pivot3_HARed_Low > 0 then begin
  551.  
  552. Last_Pivot3_Low = Pivot3_HARed_Low;
  553. Last_Pivot3_LowBar = Pivot3_HARed_LowBar;
  554.  
  555. // Check left bars
  556. LeftBarsOK = true;
  557. for ix = currentbar-Pivot3_HARed_LowBar+1 to currentbar-Pivot3_HARed_LowBar + Pivot3_Low_Left_Strength begin
  558. if Pivot3_HAColor[ix] <> red then
  559. LeftBarsOK = false;
  560. end;
  561.  
  562. // Check right bars
  563. RightBarsOK = currentbar-Pivot3_HARed_LowBar >= Pivot3_Low_Right_Strength;
  564.  
  565. // Filter amounts
  566. Pivot3_Low_LeftFilter_Amt = Last_Pivot3_Low * Pivot3_Low_PctLeftFilter * 0.01;
  567.  
  568. // Get left percent
  569. // There may not be a valid pivot high, in that case use: high of day (for DT) or high of chart (for swing trade)
  570. if TradeOnlySameDay then begin
  571. value1 = iff(Valid_Pivot3_HighBar < NewDayBarNum, HighOfDay, Valid_Pivot3_High);
  572. end
  573. else begin
  574. value1 = iff(Valid_Pivot3_HighBar = 0, HighOfChart, Valid_Pivot3_High);
  575. end;
  576. LeftAmt = value1 - Last_Pivot3_Low;
  577.  
  578. // The pivot is valid only, if the pivot is x% away from the opposite pivot
  579. // Exception if this is Lower than the last valid swing pivot Low
  580. if LeftBarsOK and RightBarsOK and (LeftAmt > Pivot3_Low_LeftFilter_Amt or Last_Pivot3_Low < Exit_Pivot3_Low) then begin
  581. Exit_Pivot3_Low = Last_Pivot3_Low;
  582. Exit_Pivot3_LowBar = Last_Pivot3_LowBar;
  583. Exit_Pivot3_Low_CheckRight = true;
  584. end;
  585. end;
  586.  
  587. Pivot3_HAGreen_High = high;
  588. Pivot3_HAGreen_HighBar = barnumber;
  589. end;
  590. end;
  591.  
  592.  
  593.  
  594. // Filter amounts
  595. Pivot3_High_RightFilter_Amt = Exit_Pivot3_High * Pivot3_High_PctRightFilter * 0.01;
  596. Pivot3_Low_RightFilter_Amt = Exit_Pivot3_Low * Pivot3_Low_PctRightFilter * 0.01;
  597.  
  598. // Stop checking for %Filter Right if price breaks the pivot high
  599. if Exit_Pivot3_High_CheckRight and high > Exit_Pivot3_High then
  600. Exit_Pivot3_High_CheckRight = false;
  601.  
  602. if Exit_Pivot3_Low_CheckRight and low < Exit_Pivot3_Low then
  603. Exit_Pivot3_Low_CheckRight = false;
  604.  
  605. // Check %Filter Right
  606. if Exit_Pivot3_High_CheckRight and close <= Exit_Pivot3_High - Pivot3_High_RightFilter_Amt then begin
  607. Valid_Pivot3_High = Exit_Pivot3_High;
  608. Valid_Pivot3_HighBar = Exit_Pivot3_HighBar;
  609. Exit_Pivot3_High_CheckRight = false;
  610. end;
  611.  
  612. if Exit_Pivot3_Low_CheckRight and close >= Exit_Pivot3_Low + Pivot3_Low_RightFilter_Amt then begin
  613. Valid_Pivot3_Low = Exit_Pivot3_Low;
  614. Valid_Pivot3_LowBar = Exit_Pivot3_LowBar;
  615. Exit_Pivot3_Low_CheckRight = false;
  616. end;
  617.  
  618. // If the last valid swing pivot high today was broken calculate the highest price that occurred after the break
  619. if Pivot3_HighBreakBar = Valid_Pivot3_HighBar and Valid_Pivot3_HighBar >= NewDayBarNum then begin
  620. MR_High = Valid_Pivot3_High;
  621. for ix = barnumber[0] - Valid_Pivot3_HighBar downto 1 begin
  622. MR_High = maxlist(high[ix],MR_High);
  623. end;
  624. MR_High = maxlist(PrevHigh,MR_High);
  625. end;
  626.  
  627. // If the last valid swing pivot low today was broken calculate the lowest price that occurred after the break
  628. if Pivot3_LowBreakBar = Valid_Pivot3_LowBar and Valid_Pivot3_LowBar > NewDayBarNum then begin
  629. MR_Low = Valid_Pivot3_Low;
  630. for ix = barnumber[0] - Valid_Pivot3_LowBar downto 1 begin
  631. MR_Low = minlist(Low[ix],MR_Low);
  632. end;
  633. MR_Low = minlist(PrevLow,MR_Low);
  634. end;
  635.  
  636. // If no valid pivot high today then use HighOfDay
  637. // Only for reversals
  638. if TradeOnlySameDay and Valid_Pivot3_HighBar < NewDayBarNum and MarketPosition=-1 then
  639. Pivot3_HighRev = HighOfDay + CalculateEntrySlack(HighOfDay, Entry_Slack_ATR)
  640. // Update 9/22/16
  641. // If the last valid swing pivot high was broken calculate the highest price that occurred after the break to reverse a short
  642. else if Pivot3_HighBreakBar = Valid_Pivot3_HighBar and MarketPosition=-1 then
  643. Pivot3_HighRev=MR_High + CalculateEntrySlack(MR_High, Entry_Slack_ATR)
  644. else if (not TradeOnlySameDay) or Valid_Pivot3_HighBar >= NewDayBarNum then
  645. Pivot3_HighRev = Valid_Pivot3_High + CalculateEntrySlack(Valid_Pivot3_High, Entry_Slack_ATR)
  646. else
  647. Pivot3_HighRev = 0;
  648.  
  649.  
  650. // If no valid pivot low today then use LowOfDay
  651. // Only for reversals
  652. if TradeOnlySameDay and Valid_Pivot3_LowBar <= NewDayBarNum and MarketPosition=1 then
  653. Pivot3_LowRev = LowOfDay - CalculateEntrySlack(LowOfDay, Entry_Slack_ATR)
  654. // Update 9/22/16
  655. // If the last valid swing pivot low was broken calculate the lowest price that occurred after the break to reverse a long
  656. else if MR_Low > 0 and Pivot3_LowBreakBar = Valid_Pivot3_LowBar and MarketPosition=1 then
  657. Pivot3_LowRev=MR_Low - CalculateEntrySlack(MR_Low, Entry_Slack_ATR)
  658. else if (not TradeOnlySameDay) or Valid_Pivot3_LowBar > NewDayBarNum then
  659. Pivot3_LowRev = Valid_Pivot3_Low - CalculateEntrySlack(Valid_Pivot3_Low, Entry_Slack_ATR)
  660. else
  661. Pivot3_LowRev = 0;
  662.  
  663.  
  664. end;
  665.  
  666.  
  667. #endregion
  668.  
  669.  
  670.  
  671.  
  672.  
  673.  
  674. //**********************************************************************************************//
  675. #region HA Pivots
  676.  
  677. // vars:
  678. // HAColor(0),
  679. // HASignalUp(false),
  680. // HASignalDown(false),
  681. // HARed_Low(0),
  682. // HARed_LowBar(0),
  683. // HAGreen_High(0),
  684. // HAGreen_HighBar(0),
  685.  
  686. // Valid_HAPiv_High(0), Valid_HAPiv_HighBar(0), Valid_HAPiv_Low(0), Valid_HAPiv_LowBar(0),
  687.  
  688. // HAPiv_HighBreakBar(0),
  689. // HAPiv_LowBreakBar(0),
  690. // HAPiv_HighRev(0),
  691. // HAPiv_LowRev(0);
  692.  
  693. if HA_Entry_____________ONOFF <> 0 then begin
  694.  
  695. value1 = AVP_HeikinAshi3(
  696. iff(HA_Entry_____________ONOFF = 0, 0, HA_CompBars),
  697. iff(HA_Entry_____________ONOFF = 0, 0, HA_SmoothHA),
  698. iff(HA_Entry_____________ONOFF = 0, 0, HA_SmoothAvgType),
  699. iff(HA_Entry_____________ONOFF = 0, 1, HA_SmoothLength),
  700. false,
  701. HAColor,
  702. HASignalUp,
  703. HASignalDown
  704. );
  705.  
  706. _HA_Change_up = (HASignalUp and BarComplete);
  707. _HA_Change_down= (HASignalDown and BarComplete);
  708.  
  709. // Update Red Low
  710. // This will include the confirmation candle
  711. if (HAColor=red or _HA_Change_up) and
  712. (low < HARed_Low or HARed_Low = 0) then begin
  713.  
  714. HARed_Low = low;
  715. HARed_LowBar = barnumber;
  716. end;
  717.  
  718. // Update Green High
  719. // This will include the confirmation candle
  720. if (HAColor=green or _HA_Change_down ) and
  721. (high > HAGreen_High or HAGreen_High = 0) then begin
  722.  
  723. HAGreen_High = high;
  724. HAGreen_HighBar = barnumber;
  725. end;
  726.  
  727. // Pivot High: Change of trend down
  728. if _HA_Change_down then begin
  729.  
  730. // Lookback for high
  731. if HAGreen_High > 0 then begin
  732.  
  733. Valid_HAPiv_High = HAGreen_High;
  734. Valid_HAPiv_HighBar = HAGreen_HighBar;
  735. end;
  736.  
  737. HARed_Low = low;
  738. HARed_LowBar = barnumber;
  739. end;
  740.  
  741. // Pivot Low: Change of trend up
  742. if _HA_Change_up then begin
  743.  
  744. // Lookback for low
  745. if HARed_Low > 0 then begin
  746.  
  747. Valid_HAPiv_Low = HARed_Low;
  748. Valid_HAPiv_LowBar = HARed_LowBar;
  749. end;
  750.  
  751. HAGreen_High = high;
  752. HAGreen_HighBar = barnumber;
  753. end;
  754.  
  755. // If the last valid swing pivot high today was broken calculate the highest price that occurred after the break
  756. if HAPiv_HighBreakBar = Valid_HAPiv_HighBar and Valid_HAPiv_HighBar >= NewDayBarNum then begin
  757.  
  758. MR_High = Valid_HAPiv_High;
  759. for ix = barnumber[0] - Valid_HAPiv_HighBar downto 1 begin
  760. MR_High = maxlist(high[ix],MR_High);
  761. end;
  762. MR_High = maxlist(PrevHigh,MR_High);
  763. end;
  764.  
  765. // If the last valid swing pivot low today was broken calculate the lowest price that occurred after the break
  766. if HAPiv_LowBreakBar = Valid_HAPiv_LowBar and Valid_HAPiv_LowBar >= NewDayBarNum then begin
  767.  
  768. MR_Low = Valid_HAPiv_Low;
  769. for ix = barnumber[0] - Valid_HAPiv_LowBar downto 1 begin
  770. MR_Low = minlist(Low[ix],MR_Low);
  771. end;
  772. MR_Low = minlist(PrevLow,MR_Low);
  773. end;
  774.  
  775. // If no valid pivot high today then use HighOfDay
  776. // Only for reversals
  777. if TradeOnlySameDay and Valid_HAPiv_HighBar < NewDayBarNum and MarketPosition=-1 then
  778. HAPiv_HighRev = HighOfDay + CalculateEntrySlack(HighOfDay, Entry_Slack_ATR)
  779. // Update 9/22/16
  780. // If the last valid swing pivot high was broken calculate the highest price that occurred after the break to reverse a short
  781. else if MR_High > 0 and HAPiv_HighBreakBar = Valid_HAPiv_HighBar and MarketPosition=-1 then
  782. HAPiv_HighRev=MR_High + CalculateEntrySlack(MR_High, Entry_Slack_ATR)
  783. else if (not TradeOnlySameDay) or Valid_HAPiv_HighBar >= NewDayBarNum then
  784. HAPiv_HighRev = Valid_HAPiv_High + CalculateEntrySlack(Valid_HAPiv_High, Entry_Slack_ATR)
  785. else
  786. HAPiv_HighRev = 0;
  787.  
  788. // If no valid pivot low today then use LowOfDay
  789. // Only for reversals
  790. if TradeOnlySameDay and Valid_HAPiv_LowBar < NewDayBarNum and MarketPosition=1 then
  791. HAPiv_LowRev = LowOfDay - CalculateEntrySlack(LowOfDay, Entry_Slack_ATR)
  792. // Update 9/22/16
  793. // If the last valid swing pivot low was broken calculate the lowest price that occurred after the break to reverse a long
  794. else if HAPiv_LowBreakBar = Valid_HAPiv_LowBar and MarketPosition=1 then
  795. HAPiv_LowRev=MR_Low - CalculateEntrySlack(MR_Low, Entry_Slack_ATR)
  796. else if (not TradeOnlySameDay) or Valid_HAPiv_LowBar >= NewDayBarNum then
  797. HAPiv_LowRev = Valid_HAPiv_Low - CalculateEntrySlack(Valid_HAPiv_Low, Entry_Slack_ATR)
  798. else
  799. HAPiv_LowRev = 0;
  800. end;
  801.  
  802. #endregion
  803.  
  804.  
  805.  
  806.  
  807.  
  808.  
  809.  
  810.  
  811.  
  812.  
  813.  
  814.  
  815.  
  816.  
  817.  
  818.  
  819.  
  820.  
  821.  
  822.  
  823.  
  824.  
  825.  
  826.  
  827.  
  828.  
  829.  
  830. // ########################################### Heikin Ashi orders ##############################################
  831.  
  832. #region Heikin Ashi Entries
  833.  
  834. // vars:
  835. // HAUp_Entry(false),
  836. // HADn_Entry(false),
  837. // HAUp_Price(0),
  838. // HADn_Price(0);
  839.  
  840.  
  841. // vars:
  842. // HA_Entry2_LE(false),
  843. // HA_Entry2_SE(false),
  844. // intrabarpersist HA_Entry2_Candles_LE(0),
  845. // intrabarpersist HA_Entry2_Candles_SE(0);
  846.  
  847. // Vars : isNewSess(false), inBreakTime(false);
  848.  
  849. isNewSess = TradeOnlySameDay and SessStartTime <> -1 and SessEndTime <> -1 and (time[1] <= SessStartTime or IsFirstBarOfDay);
  850. inBreakTime = TradeOnlySameDay and BreakStartTime <> -1 and BreakEndTime <> -1 and (time[1] > BreakStartTime and time[1] <= BreakEndTime);
  851.  
  852. if HA_Entry_____________ONOFF = 1 then begin
  853.  
  854. // ##### HA type 1
  855. if (HA_EntryType = 1) then begin
  856.  
  857. // AVP_HeikinAshi3 called previously
  858. // Check for break of last pivot high
  859. if high[1] >= HAPiv_HighRev[1] then
  860. HAPiv_HighBreakBar = Valid_HAPiv_HighBar[1];
  861.  
  862. // Break on current bar when long also needs to be a break bar
  863. if high >= HAPiv_HighRev and MarketPosition=1 then
  864. HAPiv_HighBreakBar = Valid_HAPiv_HighBar;
  865.  
  866. // Check for break of last pivot low
  867. if low[1] <= HAPiv_LowRev[1] then
  868. HAPiv_LowBreakBar = Valid_HAPiv_LowBar[1];
  869.  
  870. // Break on current bar when short also needs to be a break bar
  871. if low <= HAPiv_LowRev and MarketPosition=-1 then
  872. HAPiv_LowBreakBar = Valid_HAPiv_LowBar;
  873.  
  874. // First bar
  875. if isNewSess then begin
  876. HAUp_Entry = false;
  877. HADn_Entry = false;
  878. end
  879. // Second bar
  880. else if HA_UseFirstBarBreak = 1 and time[1] > SessStartTime and isNewSess[1] then begin
  881. if HAColor[1] = green then begin
  882. HAUp_Entry = true;
  883. HADn_Entry = false;
  884. HAUp_Price = high[1] + CalculateEntrySlack(high[1], Entry_Slack_ATR);
  885. end
  886. else if HAColor[1] = red then begin
  887. HAUp_Entry = false;
  888. HADn_Entry = true;
  889. HADn_Price = low[1] - CalculateEntrySlack(low[1], Entry_Slack_ATR);
  890. end;
  891. end
  892. // Break
  893. else if inBreakTime then begin
  894. HAUp_Entry = false;
  895. HADn_Entry = false;
  896. end
  897. // Second bar after break
  898. else if HA_UseFirstBarBreak = 1 and inBreakTime[1] then begin
  899. if HAColor[1] = green then begin
  900. HAUp_Entry = true;
  901. HADn_Entry = false;
  902. HAUp_Price = high[1] + CalculateEntrySlack(high[1], Entry_Slack_ATR);
  903. end
  904. else if HAColor[1] = red then begin
  905. HADn_Entry = true;
  906. HAUp_Entry = false;
  907. HADn_Price = low[1] - CalculateEntrySlack(low[1], Entry_Slack_ATR);
  908. end;
  909. end
  910. // Normal, should not consider prior dates for signal when TradeOnlySameDay
  911. else if (not TradeOnlySameDay) or (time[2] > SessStartTime and date[1]=date[2]) then begin
  912.  
  913. if HASignalUp[1] then begin
  914. HAUp_Entry = true;
  915. HADn_Entry = false;
  916. HAUp_Price = high[1] + CalculateEntrySlack(high[1], Entry_Slack_ATR);
  917. end;
  918.  
  919. if HASignalDown[1] then begin
  920. HADn_Entry = true;
  921. HAUp_Entry = false;
  922. HADn_Price = low[1] - CalculateEntrySlack(low[1], Entry_Slack_ATR);
  923. end;
  924. end;
  925.  
  926. // if TradeOnlySameDay and (IsFirstBarOfDay or date[1] <> date[2])
  927. if HA_LEBar = barnumber[0] then HAUp_Entry = false;
  928. if HA_SEBar = barnumber[0] then HADn_Entry = false;
  929.  
  930. // Entries (if allowed)
  931. if TradeOK_ORB_ed then begin
  932.  
  933. // Entry is triggered when a candle goes above highest price of signal candle
  934. if HAUp_Entry then
  935. If (HA_Entry_main_filters_ONOFF=0 or filters_ok_LONG_Entry) then
  936. If permit_LONG then
  937. order_Entry(true, HAUp_Price, "LONG", RT, HAUp_Price, "LE HA", HA_order_type);
  938.  
  939. // Entry is triggered when a candle goes below lowest price of signal candle
  940. if HADn_Entry then
  941. If (HA_Entry_main_filters_ONOFF=0 or filters_ok_SHORT_Entry) then
  942. If permit_SHORT then
  943. order_Entry(true, HADn_Price, "SHORT", RT, HADn_Price, "SE HA", HA_order_type);
  944. end
  945.  
  946. // Exit (if no new entries allowed)
  947. else begin
  948. if HAUp_Entry then
  949. If (HA_Exit_main_filters_ONOFF=0 or filters_ok_SHORT_Exit) then
  950. order_Exit("SX HA", 0, HAUp_Price);
  951.  
  952. if HADn_Entry then
  953. If (HA_Exit_main_filters_ONOFF=0 or filters_ok_LONG_Exit) then
  954. order_Exit("LX HA", 0, HADn_Price);
  955. end;
  956.  
  957.  
  958.  
  959. // HA Pivot reversal
  960. if HA_UsePivReversalEntry = 1 then begin
  961.  
  962. if MarketPosition=1 then begin
  963.  
  964. //Rev entry
  965. if LastEntry.Contains("LE HA ") and Valid_HAPiv_Low > 0 then begin
  966. if
  967. (HA_Entry_main_filters_ONOFF=0 or filters_ok_SHORT_Entry ) and
  968. (HA_Exit_main_filters_ONOFF=0 or filters_ok_LONG_Exit )
  969. and permit_SHORT then
  970. order_Entry(false, Valid_HAPiv_Low, "SHORT", RT, Valid_HAPiv_Low, "SE HA Rev", HA_order_type)
  971. else If (HA_Exit_main_filters_ONOFF=0 or filters_ok_LONG_Exit) then
  972. order_Exit("LX HA Exit", 0, Valid_HAPiv_Low);
  973.  
  974. DrawLine(TL_HAPivRevStop,TXT_HAPivRevStop,NumToStr(Valid_HAPiv_Low,2),Valid_HAPiv_Low,HAPivRevStop_Color,"HAPivRev Stop");
  975. end;
  976.  
  977. //RevExit
  978. if LastEntry.Contains("LE HA RevExit") and HAPiv_LowRev > 0 then
  979. If (HA_Exit_main_filters_ONOFF=0 or filters_ok_LONG_Exit) then
  980. order_Exit("LX HA RevExit", 0, HAPiv_LowRev);
  981. end;
  982.  
  983. if MarketPosition=-1 then begin
  984.  
  985. //Rev entry
  986. if LastEntry.Contains("SE HA ") and Valid_HAPiv_High > 0 then begin
  987. if
  988. (HA_Entry_main_filters_ONOFF=0 or filters_ok_LONG_Entry ) and
  989. (HA_Exit_main_filters_ONOFF=0 or filters_ok_SHORT_Exit )
  990. and permit_LONG then
  991. order_Entry(false, Valid_HAPiv_High, "LONG", RT, Valid_HAPiv_High, "LE HA Rev", HA_order_type)
  992. else If (HA_Exit_main_filters_ONOFF=0 or filters_ok_SHORT_Exit) then
  993. order_Exit("SX HA Exit", 0, Valid_HAPiv_High);
  994.  
  995. DrawLine(TL_HAPivRevStop,TXT_HAPivRevStop,NumToStr(Valid_HAPiv_High,2),Valid_HAPiv_High,HAPivRevStop_Color,"HAPivRev Stop");
  996. end;
  997.  
  998. //RevExit
  999. if LastEntry.Contains("SE HA Rev ") and HAPiv_HighRev > 0 then
  1000. If (HA_Exit_main_filters_ONOFF=0 or filters_ok_SHORT_Exit) then
  1001. order_Exit("SX HA RevExit", 0, HAPiv_HighRev);
  1002. end;
  1003. end;
  1004.  
  1005.  
  1006.  
  1007. end;
  1008.  
  1009.  
  1010.  
  1011.  
  1012. // ##x Heikin-Ashi 2 orders ##
  1013. if HA_EntryType = 2 then begin
  1014.  
  1015. if TradeOK_ORB_ed then begin
  1016.  
  1017. HA_Entry2_LE = false;
  1018. HA_Entry2_SE = false;
  1019.  
  1020. if BarComplete then begin
  1021.  
  1022. if HAColor = green then HA_Entry2_Candles_LE += 1;
  1023. if HAColor = red then HA_Entry2_Candles_SE += 1;
  1024.  
  1025. if HASignalUp and
  1026. // Don't allow signal if DT and prior bar was on a different date
  1027. ((not TradeOnlySameDay) or (time[1] >= SessStartTime and date=date[1])) then
  1028. begin
  1029. if HA_Entry2_Candles <= 1 then
  1030. HA_Entry2_LE = true;
  1031. HA_Entry2_Candles_LE = 1;
  1032. end
  1033. else if HA_Entry2_Candles > 1 then begin
  1034. if HA_Entry2_Candles_LE = HA_Entry2_Candles then
  1035. HA_Entry2_LE = true;
  1036. end;
  1037.  
  1038. if HASignalDown and
  1039. // Don't allow signal if DT and prior bar was on a different date
  1040. ((not TradeOnlySameDay) or (time[1] >= SessStartTime and date=date[1])) then
  1041. begin
  1042. if HA_Entry2_Candles <= 1 then HA_Entry2_SE = true;
  1043. HA_Entry2_Candles_SE = 1;
  1044. end
  1045. else if HA_Entry2_Candles > 1 then begin
  1046. if HA_Entry2_Candles_SE = HA_Entry2_Candles then
  1047. HA_Entry2_SE = true;
  1048. end;
  1049. end;
  1050.  
  1051. // Entry is triggered at close of bar where Heikin Ashi signal occurs
  1052. if HA_Entry2_LE then
  1053. If (HA_Entry_main_filters_ONOFF=0 or filters_ok_LONG_Entry) then
  1054. if permit_LONG then
  1055. order_Entry(true, close, "LONG", RT, close, "LE HA2", HA_2_order_type);
  1056.  
  1057. if HA_Entry2_SE then
  1058. // Entry is triggered at close of bar where Heikin Ashi signal occurs
  1059. If (HA_Entry_main_filters_ONOFF=0 or filters_ok_SHORT_Entry) then
  1060. if permit_SHORT then
  1061. order_Entry(true, close, "SHORT", RT, close, "SE HA2", HA_2_order_type);
  1062. end;
  1063. end;
  1064.  
  1065. end;
  1066.  
  1067. #endregion
  1068. // #####################################################################################################################
  1069.  
  1070.  
  1071.  
  1072.  
  1073. // ############################################## Pivot1 orders ################################################
  1074.  
  1075. #region Pivot1 Exit / Reversal
  1076.  
  1077. if Pivot1_Exit______________ONOFF = 1 then begin
  1078.  
  1079. // Check for break of last pivot high
  1080. if high[1] >= Pivot1_HighRev[1] then
  1081. Pivot1_HighBreakBar = Valid_Pivot1_HighBar[1];
  1082.  
  1083. // Check for break of last pivot low
  1084. if low[1] <= Pivot1_LowRev[1] then
  1085. Pivot1_LowBreakBar = Valid_Pivot1_LowBar[1];
  1086.  
  1087. // Break on current bar when short also needs to be a break bar
  1088. if MarketPosition=-1 and low <= Pivot1_LowRev then
  1089. Pivot1_LowBreakBar = Valid_Pivot1_LowBar;
  1090.  
  1091.  
  1092. if Pivot1_TrailingReversal = 1 and TradeOK_ORB_ed then begin
  1093.  
  1094. if MarketPosition=1 and Pivot1_LowRev > 0 and close > Pivot1_LowRev {and Pivot1_LowBreakBar < Valid_Pivot1_LowBar} then begin
  1095.  
  1096. //FirstStop = Pivot1_LowRev * (1 + FirstProtectiveStopPctDistance * 0.01);
  1097. If permit_SHORT and
  1098. (Pivot1_Entry_main_filters_ONOFF=0 or filters_ok_SHORT_Entry) and
  1099. (Pivot1_Exit_main_filters_ONOFF=0 or filters_ok_LONG_Exit ) and
  1100. Pivot1_MA2Filt_Short_Entry_Allow
  1101. then
  1102. order_Entry(false, Pivot1_LowRev, "SHORT", RT, Pivot1_LowRev, "SE Pivot1 Rev", Pivot1_order_type)
  1103. else If (Pivot1_Exit_main_filters_ONOFF=0 or filters_ok_LONG_Exit) then
  1104. order_Exit("LX Pivot1 RevExit", 0, Pivot1_LowRev);
  1105.  
  1106. DrawLine(TL_Pivot1RevStop,TXT_Pivot1RevStop,NumToStr(Pivot1_LowRev,2), Pivot1_LowRev, Pivot1_RevStop_Color,"Pivot1 Stop");
  1107. end;
  1108.  
  1109. if MarketPosition=-1 and Pivot1_HighRev > 0 and close < Pivot1_HighRev {and Pivot1_HighBreakBar < Valid_Pivot1_HighBar} then begin
  1110.  
  1111. //FirstStop = Pivot1_HighRev * (1 - FirstProtectiveStopPctDistance * 0.01);
  1112. If permit_LONG and
  1113. (Pivot1_Entry_main_filters_ONOFF=0 or filters_ok_LONG_Entry) and
  1114. (Pivot1_Exit_main_filters_ONOFF=0 or filters_ok_SHORT_Exit ) and
  1115. Pivot1_MA2Filt_Long_Entry_Allow
  1116. then
  1117. order_Entry(false, Pivot1_HighRev, "LONG", RT, Pivot1_HighRev, "LE Pivot1 Rev", Pivot1_order_type) //buy ("LE Pivot Reversal") LE_Shares shares next bar at Pivot1_HighRev stop
  1118. else If (Pivot1_Exit_main_filters_ONOFF=0 or filters_ok_SHORT_Exit) then
  1119. order_Exit("SX Pivot1 RevExit", 0, Pivot1_HighRev);
  1120.  
  1121. DrawLine(TL_Pivot1RevStop,TXT_Pivot1RevStop,NumToStr(Pivot1_HighRev,2),Pivot1_HighRev,Pivot1_RevStop_Color,"Pivot1 Stop");
  1122.  
  1123. end;
  1124. end
  1125. else begin
  1126. // break of the last trailing pivot high/low as the exit point
  1127.  
  1128.  
  1129. if MarketPosition=1 and Pivot1_LowRev > 0 and close > Pivot1_LowRev then begin
  1130. If (Pivot1_Exit_main_filters_ONOFF=0 or filters_ok_LONG_Exit) then begin
  1131. order_Exit("LX Pivot1 Exit", 0, Pivot1_LowRev);
  1132. DrawLine(TL_Pivot1RevStop,TXT_Pivot1RevStop,NumToStr(Pivot1_LowRev,2),Pivot1_LowRev,Pivot1_RevStop_Color,"Pivot1 Stop");
  1133. end;
  1134. end;
  1135.  
  1136. if MarketPosition=-1 and Pivot1_HighRev > 0 and close < Pivot1_HighRev then begin
  1137. If (Pivot1_Exit_main_filters_ONOFF=0 or filters_ok_SHORT_Exit) then begin
  1138. order_Exit("SX Pivot1 Exit", 0, Pivot1_HighRev);
  1139. DrawLine(TL_Pivot1RevStop,TXT_Pivot1RevStop,NumToStr(Pivot1_HighRev,2),Pivot1_HighRev,Pivot1_RevStop_Color,"Pivot1 Stop");
  1140. end;
  1141. end;
  1142. end;
  1143. end;
  1144.  
  1145. #endregion
  1146.  
  1147. // ##########
  1148. #region Pivot1 Entries
  1149.  
  1150. if Pivot1_TrailingReversal = 1 and TradeOK_ORB_ed then begin //and (ORB_Entry_____________ONOFF = 0 or NumTradesToday > 0 or SkipORSetup)}
  1151.  
  1152. // Check for break of last pivot high
  1153. if high[1] >= Pivot1_HighRev[1] then
  1154. Pivot1_HighBreakBar = Valid_Pivot1_HighBar[1];
  1155.  
  1156. // Check for break of last pivot low
  1157. if low[1] <= Pivot1_LowRev[1] then
  1158. Pivot1_LowBreakBar = Valid_Pivot1_LowBar[1];
  1159.  
  1160. // Break on current bar also triggers a break bar, provided we don't already have a pivot entry active
  1161. if LE_Pivot1_Entry_Active = false and high >= Pivot1_HighRev then
  1162. Pivot1_HighBreakBar = Valid_Pivot1_HighBar;
  1163.  
  1164. if SE_Pivot1_Entry_Active = false and low <= Pivot1_LowRev then
  1165. Pivot1_LowBreakBar = Valid_Pivot1_LowBar;
  1166.  
  1167. // if ORB_Entry_____________ONOFF is off then the first trade of the day will be triggered by the first break of the first swing pivot high or low instead.
  1168.  
  1169. LE_Pivot1_Entry_Active = false;
  1170. SE_Pivot1_Entry_Active = false;
  1171.  
  1172. if MarketPosition=0 then begin
  1173.  
  1174. // When we first send the entry price must be above the pivot
  1175. // However once entry is activated the order should stay open
  1176.  
  1177. RT_Log("SE Pivot1 Conditions - " +
  1178. " 1:" + iffstring(Pivot1_LowRev > 0,"T","F") +
  1179. " 2:" + iffstring((close > Pivot1_LowRev or SE_Pivot1_Entry_Active),"T","F") +
  1180. " 3:" + iffstring(Pivot1_LowBreakBar < Valid_Pivot1_LowBar,"T","F")
  1181. );
  1182.  
  1183. // When we first send the entry price must be above the pivot
  1184. // However once entry is activated the order should stay open
  1185. if
  1186. Pivot1_LowRev > 0 and
  1187. (close > Pivot1_LowRev or SE_Pivot1_Entry_Active) and
  1188. Pivot1_LowBreakBar < Valid_Pivot1_LowBar and
  1189. Pivot1_MA2Filt_Short_Entry_Allow
  1190. then begin
  1191. //FirstStop = Pivot1_LowRev * (1 + FirstProtectiveStopPctDistance * 0.01);
  1192. If (Pivot1_Entry_main_filters_ONOFF=0 or filters_ok_SHORT_Entry) then
  1193. If permit_SHORT then
  1194. SE_Pivot1_Entry_Active = order_Entry(false, Pivot1_LowRev, "SHORT", RT, Pivot1_LowRev, "SE Pivot1", Pivot1_order_type);
  1195.  
  1196. end;
  1197.  
  1198. RT_Log("LE Pivot1 Conditions - " +
  1199. " 1:" + iffstring(Pivot1_HighRev > 0,"T","F") +
  1200. " 2:" + iffstring((close < Pivot1_HighRev or LE_Pivot1_Entry_Active),"T","F") +
  1201. " 3:" + iffstring(Pivot1_HighBreakBar < Valid_Pivot1_HighBar,"T","F")
  1202. );
  1203.  
  1204. // When we first send the entry price must be below the pivot
  1205. // However once entry is activated the order should stay open
  1206. if
  1207. Pivot1_HighRev > 0 and
  1208. (close < Pivot1_HighRev or LE_Pivot1_Entry_Active) and
  1209. Pivot1_HighBreakBar < Valid_Pivot1_HighBar and
  1210. Pivot1_MA2Filt_Long_Entry_Allow
  1211. then begin
  1212. //FirstStop = Pivot1_HighRev * (1 - FirstProtectiveStopPctDistance * 0.01);
  1213. If (Pivot1_Entry_main_filters_ONOFF=0 or filters_ok_LONG_Entry) then begin
  1214. If (permit_LONG) then
  1215. LE_Pivot1_Entry_Active = order_Entry(false, Pivot1_HighRev, "LONG", RT, Pivot1_HighRev, "LE Pivot1", Pivot1_order_type); // at Pivot1_HighRev stop;
  1216. end;
  1217. end;
  1218. end;
  1219. end;
  1220.  
  1221. #endregion
  1222. // #####################################################################################################################
  1223.  
  1224.  
  1225.  
  1226.  
  1227.  
  1228.  
  1229. // ############################################## Pivot2 orders ################################################
  1230.  
  1231. #region Pivot 2 Exit / Reversal
  1232.  
  1233. if Pivot2_Entry_____________ONOFF = 1 or Pivot2_Exit______________ONOFF = 1 then begin
  1234.  
  1235. check_for_exit=false;
  1236.  
  1237. // Turn f Pivot2_Low entry if long position and not Pivot2 or Trade not OK and
  1238. // entry price is broken
  1239. if Pivot2_Low_EntryOK and high >= Pivot2_Low_EntryPr {and
  1240. ((MarketPosition = 1 and LastEntryFilled) or TradeOK_ORB_ed = false or Pivot2_MA1Filt_Long_Entry_Allow=false or NOT permit_LONG)} then
  1241. Pivot2_Low_EntryOK = false;
  1242.  
  1243. // Turn off Pivot2_High entry if short position and not Pivot2 or Trade not OK and
  1244. // entry price is broken
  1245. if Pivot2_High_EntryOK and low <= Pivot2_High_EntryPr {and
  1246. ((MarketPosition = -1 and LastEntryFilled) or TradeOK_ORB_ed = false or Pivot2_MA1Filt_Short_Entry_Allow=false or NOT permit_SHORT)} then
  1247. Pivot2_High_EntryOK = false;
  1248.  
  1249. if high >= Pivot2_HighRev then
  1250. Pivot2_High_EntryOK = false;
  1251. if low <= Pivot2_LowRev then
  1252. Pivot2_Low_EntryOK = false;
  1253.  
  1254. // Check for break of last pivot high
  1255. if high[1] >= Pivot2_HighRev[1] then begin
  1256. Pivot2_HighBreakBar = Valid_Pivot2_HighBar[1];
  1257. end;
  1258.  
  1259. // Check for break of last pivot low
  1260. if low[1] <= Pivot2_LowRev[1] then begin
  1261. Pivot2_LowBreakBar = Valid_Pivot2_LowBar[1];
  1262. end;
  1263.  
  1264. if Pivot2_TrailingReversal = 1 then begin
  1265.  
  1266. If TradeOK_ORB_ed then begin
  1267. if MarketPosition=1 then begin
  1268.  
  1269. // 2 different possibilities
  1270. // 1. A new pivot high is formed and break below the right bars
  1271. // 2. a break of the pivot low
  1272.  
  1273. // New pivot high
  1274. if Pivot2_High_EntryOK and Pivot2_High_EntryPr > 0 and
  1275. (not TradeOnlySameDay or Valid_Pivot2_HighBar > NewDayBarNum)
  1276. then begin
  1277.  
  1278. If permit_SHORT and
  1279. (Pivot2_Entry_main_filters_ONOFF=0 or filters_ok_SHORT_Entry) and
  1280. (Pivot2_Exit_main_filters_ONOFF=0 or filters_ok_LONG_Exit) and
  1281. Pivot2_MA1Filt_Short_Entry_Allow
  1282. then
  1283. order_Entry(false, Pivot2_High_EntryPr, "SHORT", RT, Pivot2_High_EntryPr, "SE Pivot2 eRev", Pivot2_order_type)
  1284. else if (Pivot2_Exit_main_filters_ONOFF=0 or filters_ok_LONG_EXIT) then
  1285. order_Exit("LX Pivot2 Exit3", 0, Pivot2_High_EntryPr);
  1286. end;
  1287.  
  1288.  
  1289. if Pivot2_LowRev > 0 and close > Pivot2_LowRev then begin
  1290. if permit_SHORT and
  1291. (Pivot2_Entry_main_filters_ONOFF=0 or filters_ok_SHORT_Entry) and
  1292. (Pivot2_Exit_main_filters_ONOFF=0 or filters_ok_LONG_Exit) and
  1293. Pivot2_MA1Filt_Short_Entry_Allow
  1294. then begin
  1295. FirstStop = CalculateFirstStop("SHORT", false, Pivot1_LowRev); //<-------------------------- should there be Pivot2 ???
  1296. SE_Shares = CalculateSharesToTrade(Pivot2_LowRev, FirstStop - Pivot2_LowRev);
  1297. order_Entry("SHORT", RT, SE_Shares, Pivot2_LowRev, "SE Pivot2 Rev", Pivot2_order_type) ;
  1298. end
  1299. else If (Pivot2_Exit_main_filters_ONOFF=0 or filters_ok_LONG_EXIT) then
  1300. order_Exit("LX Pivot2 RevExit", 0, Pivot2_LowRev);
  1301.  
  1302. DrawLine(TL_Pivot2RevStop,TXT_Pivot2RevStop,NumToStr(Pivot2_LowRev,2),Pivot2_LowRev,Pivot1_RevStop_Color,"Pivot2 Stop");
  1303. end;
  1304. end;
  1305.  
  1306.  
  1307. if MarketPosition=-1 then begin
  1308.  
  1309. // 2 different possibilities
  1310. // 1. A new pivot low is formed and break above the right bars
  1311. // 2. a break of the pivot high
  1312.  
  1313. if Pivot2_Low_EntryOK and Pivot2_Low_EntryPr > 0 and (not TradeOnlySameDay or Valid_Pivot2_LowBar > NewDayBarNum) then begin
  1314. if permit_LONG and
  1315. (Pivot2_Entry_main_filters_ONOFF=0 or filters_ok_LONG_Entry) and
  1316. (Pivot2_Exit_main_filters_ONOFF=0 or filters_ok_SHORT_Exit) and
  1317. Pivot2_MA1Filt_Long_Entry_Allow
  1318. then
  1319. order_Entry(false, Pivot2_Low_EntryPr, "LONG", RT, Pivot2_Low_EntryPr, "LE Pivot2 eRev", Pivot2_order_type)
  1320. else If (Pivot2_Exit_main_filters_ONOFF=0 or filters_ok_SHORT_Exit) then
  1321. order_Exit("SX Pivot2 Exit3", 0, Pivot2_Low_EntryPr);
  1322. end;
  1323.  
  1324. if Pivot2_HighRev > 0 and close < Pivot2_HighRev then begin
  1325. if permit_LONG and
  1326. (Pivot2_Entry_main_filters_ONOFF=0 or filters_ok_LONG_Entry) and
  1327. (Pivot2_Exit_main_filters_ONOFF=0 or filters_ok_SHORT_Exit) and
  1328. Pivot2_MA1Filt_Long_Entry_Allow
  1329. then begin
  1330. FirstStop = CalculateFirstStop("LONG",false, Pivot1_HighRev); //<-------------------------- should there be Pivot2 ???
  1331. LE_Shares = CalculateSharesToTrade(Pivot2_HighRev, Pivot2_HighRev - FirstStop);
  1332. order_Entry("LONG",RT, LE_Shares, Pivot2_HighRev, "LE Pivot2 Rev", Pivot2_order_type);
  1333. end
  1334. else If (Pivot2_Exit_main_filters_ONOFF=0 or filters_ok_SHORT_Exit) then
  1335. order_Exit("SX Pivot2 RevExit", 0, Pivot2_HighRev);
  1336.  
  1337. DrawLine(TL_Pivot2RevStop,TXT_Pivot2RevStop,NumToStr(Pivot2_HighRev,2),Pivot2_HighRev,Pivot1_RevStop_Color,"Pivot2 Stop");
  1338. end;
  1339. end;
  1340.  
  1341. end
  1342. else
  1343. check_for_exit=true;
  1344. end
  1345. else
  1346. check_for_exit=true;
  1347.  
  1348. If (check_for_exit) then begin
  1349. // break of the last trailing pivot high/low as the exit point
  1350. if MarketPosition=1 and IsBetween(0, Pivot2_LowRev, close) then begin
  1351. If Pivot2_Exit_main_filters_ONOFF=0 or filters_ok_LONG_EXIT then begin
  1352. order_Exit("LX Pivot2 Exit", 0, Pivot2_LowRev);
  1353. DrawLine(TL_Pivot2RevStop,TXT_Pivot2RevStop,NumToStr(Pivot2_LowRev,2),Pivot2_LowRev, Pivot1_RevStop_Color,"Pivot2 Stop");
  1354. end;
  1355. end;
  1356. if MarketPosition=-1 and IsBetween(0, close, Pivot2_HighRev) then begin
  1357. if Pivot2_Exit_main_filters_ONOFF=0 or filters_ok_SHORT_Exit then begin
  1358. order_Exit("SX Pivot2 Exit", 0, Pivot2_HighRev);
  1359. DrawLine(TL_Pivot2RevStop,TXT_Pivot2RevStop,NumToStr(Pivot2_HighRev,2),Pivot2_HighRev,Pivot1_RevStop_Color,"Pivot2 Stop");
  1360. end;
  1361. end;
  1362. end;
  1363. end;
  1364. #endregion
  1365.  
  1366.  
  1367. // ############
  1368. #region Pivot 2 Entries
  1369.  
  1370. // vars:
  1371. // intrabarpersist Pivot2_LE_ThisTick(false),
  1372. // intrabarpersist Pivot2_SE_ThisTick(false),
  1373. // intrabarpersist Pivot2_LE_UsePrevPrice(false),
  1374. // intrabarpersist Pivot2_SE_UsePrevPrice(false),
  1375. // intrabarpersist Pivot2_LE_Price(0),
  1376. // intrabarpersist Pivot2_SE_Price(0);
  1377.  
  1378.  
  1379. // Use previously set intrabar price for limit entry if we had a Pivot2 entry last tick
  1380. if Pivot2_SE_ThisTick then
  1381. Pivot2_SE_UsePrevPrice = true;
  1382.  
  1383. if Pivot2_LE_ThisTick then
  1384. Pivot2_LE_UsePrevPrice = true;
  1385.  
  1386. Pivot2_SE_ThisTick = false;
  1387. Pivot2_LE_ThisTick = false;
  1388.  
  1389. if Pivot2_Entry_____________ONOFF =1 then begin
  1390. if TradeOK_ORB_ed and MarketPosition=0 then begin //and (ORB_Entry_____________ONOFF = 0 or NumTradesToday > 0 or SkipORSetup)
  1391.  
  1392. // if ORB_Entry_____________ONOFF is off then the first trade of the day will be triggered by the first break of the first swing pivot high or low instead.
  1393.  
  1394. if Pivot2_High_EntryOK and Pivot2_High_EntryPr > 0 and
  1395. (Valid_Pivot2_HighBar > NewDayBarNum or not TradeOnlySameDay)
  1396. then
  1397. If (Pivot2_Entry_main_filters_ONOFF=0 or filters_ok_SHORT_Entry) and Pivot2_MA1Filt_Short_Entry_Allow then begin
  1398. If permit_SHORT then
  1399. order_Entry(false, Pivot2_High_EntryPr, "SHORT", RT, Pivot2_High_EntryPr, "SE Pivot2", Pivot2_order_type);
  1400. end;
  1401.  
  1402. if Pivot2_Low_EntryOK and Pivot2_Low_EntryPr > 0 and
  1403. (Valid_Pivot2_LowBar > NewDayBarNum or not TradeOnlySameDay)
  1404. then
  1405. If (Pivot2_Entry_main_filters_ONOFF=0 or filters_ok_LONG_Entry) and Pivot2_MA1Filt_Long_Entry_Allow then begin
  1406. If permit_LONG then
  1407. order_Entry(false, Pivot2_Low_EntryPr, "LONG", RT, Pivot2_Low_EntryPr, "LE Pivot2", Pivot2_order_type);
  1408. end;
  1409.  
  1410.  
  1411. end;
  1412.  
  1413. end;
  1414.  
  1415. #endregion
  1416. // ##################################################################################################################
  1417.  
  1418.  
  1419.  
  1420.  
  1421. // ############################################## Pivot3 orders ################################################
  1422.  
  1423. #region Pivot3 Exit / Reversal
  1424.  
  1425. if Pivot3_Entry_____________ONOFF = 1 or Pivot3_Exit______________ONOFF = 1 then begin
  1426.  
  1427. // Check for break of last pivot high
  1428. if high[1] >= Pivot3_HighRev[1] then
  1429. Pivot3_HighBreakBar = Valid_Pivot3_HighBar[1];
  1430.  
  1431. // Check for break of last pivot low
  1432. if low[1] <= Pivot3_LowRev[1] then
  1433. Pivot3_LowBreakBar = Valid_Pivot3_LowBar[1];
  1434.  
  1435. // Break on current bar when long also needs to be a break bar
  1436. if high >= Pivot3_HighRev and MarketPosition=1 then
  1437. Pivot3_HighBreakBar = Valid_Pivot3_HighBar;
  1438.  
  1439. // Break on current bar when short also needs to be a break bar
  1440. if low <= Pivot3_LowRev and MarketPosition=-1 then
  1441. Pivot3_LowBreakBar = Valid_Pivot3_LowBar;
  1442.  
  1443.  
  1444. if Pivot3_TrailingReversal = 1
  1445. and TradeOK_ORB_ed
  1446. then begin
  1447.  
  1448. if MarketPosition=1 and IsBetween(0, Pivot3_LowRev, close) then begin
  1449. if (Pivot3_Entry_main_filters_ONOFF=0 or filters_ok_SHORT_Entry) and
  1450. (Pivot3_Exit_main_filters_ONOFF=0 or filters_ok_LONG_Exit)
  1451. then
  1452. If permit_SHORT then
  1453. order_Entry(false, Pivot1_LowRev, "SHORT", RT, Pivot3_LowRev, "SE Pivot3 Rev", Pivot3_order_type) //<-------------------------- should there be Pivot3_LowRev ???
  1454. else If (Pivot3_Exit_main_filters_ONOFF=0 or filters_ok_LONG_EXIT) then
  1455. order_Exit("LX Pivot3 RevExit", 0, Pivot3_LowRev);
  1456.  
  1457. DrawLine(TL_Pivot3RevStop,TXT_Pivot3RevStop,NumToStr(Pivot3_LowRev,2),Pivot3_LowRev,Pivot3_RevStop_Color,"Pivot3 Stop");
  1458. end;
  1459.  
  1460. if MarketPosition=-1 and IsBetween(0, close, Pivot3_HighRev) then begin
  1461. if (Pivot3_Entry_main_filters_ONOFF=0 or filters_ok_LONG_Entry) and
  1462. (Pivot3_Exit_main_filters_ONOFF=0 or filters_ok_SHORT_Exit)
  1463. then
  1464. If permit_LONG then
  1465. order_Entry(false, Pivot1_HighRev, "LONG", RT, Pivot3_HighRev, "LE Pivot3 Rev", Pivot3_order_type) //<-------------------------- should there be Pivot3_HighRev ??
  1466. else If (Pivot3_Exit_main_filters_ONOFF=0 or filters_ok_SHORT_Exit) then
  1467. order_Exit("SX Pivot3 RevExit", 0, Pivot3_HighRev);
  1468.  
  1469. DrawLine(TL_Pivot3RevStop,TXT_Pivot3RevStop,NumToStr(Pivot3_HighRev,2),Pivot3_HighRev,Pivot3_RevStop_Color,"Pivot3 Stop");
  1470.  
  1471. end;
  1472.  
  1473. end
  1474. else begin
  1475.  
  1476. // break of the last trailing pivot high/low as the exit point
  1477. if MarketPosition=1 and Pivot3_LowRev > 0 and close > Pivot3_LowRev then
  1478. If (Pivot3_Exit_main_filters_ONOFF=0 or filters_ok_LONG_EXIT) then begin
  1479. order_Exit("LX Pivot3 Exit", 0, Pivot3_LowRev);
  1480. DrawLine(TL_Pivot3RevStop,TXT_Pivot3RevStop,NumToStr(Pivot3_LowRev,2),Pivot3_LowRev,Pivot3_RevStop_Color,"Pivot3 Stop");
  1481. end;
  1482.  
  1483. if MarketPosition=-1 and Pivot3_HighRev > 0 and close < Pivot3_HighRev then
  1484. If (Pivot3_Exit_main_filters_ONOFF=0 or filters_ok_SHORT_Exit) then begin
  1485. order_Exit("SX Pivot3 Exit", 0, Pivot3_HighRev);
  1486. DrawLine(TL_Pivot3RevStop,TXT_Pivot3RevStop,NumToStr(Pivot3_HighRev,2),Pivot3_HighRev,Pivot3_RevStop_Color,"Pivot3 Stop");
  1487. end;
  1488. end;
  1489. end;
  1490.  
  1491. #endregion
  1492.  
  1493.  
  1494.  
  1495.  
  1496. // ##########
  1497. #region Pivot3 Entries
  1498.  
  1499. // vars:
  1500. // intrabarpersist LEPivot3Entry_Active(false),
  1501. // intrabarpersist SEPivot3Entry_Active(false);
  1502.  
  1503. if Pivot3_Entry_____________ONOFF = 1 then begin // and (ORB_Entry_____________ONOFF = 0 or NumTradesToday > 0 or SkipORSetup)}
  1504.  
  1505. if TradeOK_ORB_ed then begin
  1506. // Check for break of last pivot high
  1507. if high[1] >= Pivot3_HighRev[1] then
  1508. Pivot3_HighBreakBar = Valid_Pivot3_HighBar[1];
  1509.  
  1510. // Check for break of last pivot low
  1511. if low[1] <= Pivot3_LowRev[1] then
  1512. Pivot3_LowBreakBar = Valid_Pivot3_LowBar[1];
  1513.  
  1514. // Break on current bar also triggers a break bar, provided we don't already have a pivot entry active
  1515. if LEPivot3Entry_Active = false and high >= Pivot3_HighRev then
  1516. Pivot3_HighBreakBar = Valid_Pivot3_HighBar;
  1517.  
  1518. if SEPivot3Entry_Active = false and low <= Pivot3_LowRev then
  1519. Pivot3_LowBreakBar = Valid_Pivot3_LowBar;
  1520.  
  1521.  
  1522. // if ORB_Entry_____________ONOFF is off then the first trade of the day will be triggered by the first break of the first swing pivot high or low instead.
  1523. LEPivot3Entry_Active = false;
  1524. SEPivot3Entry_Active = false;
  1525.  
  1526. if MarketPosition=0 then begin
  1527.  
  1528. // When we first send the entry price must be above the pivot
  1529. // However once entry is activated the order should stay open
  1530. if
  1531. Pivot3_LowRev > 0 and
  1532. (close > Pivot3_LowRev or SEPivot3Entry_Active) and
  1533. Pivot3_LowBreakBar < Valid_Pivot3_LowBar and
  1534. (Not TradeOnlySameDay or
  1535. (
  1536. ( Pivot3_Complete_Reset_new_day=0 ) or
  1537. ( Pivot3_Complete_Reset_new_day=1 and Valid_Pivot3_LowBar > NewDayBarNum ) or
  1538. ( Pivot3_Complete_Reset_new_day=2 and Valid_Pivot3_LowBar > NewDayBarNum and Pivot3_LowBreakBar > NewDayBarNum )
  1539. )
  1540. )
  1541. then begin
  1542.  
  1543. If (Pivot3_Entry_main_filters_ONOFF=0 or filters_ok_SHORT_Entry) then begin
  1544. If permit_SHORT then begin
  1545. //FirstStop = Pivot1_LowRev * (1 + FirstProtectiveStopPctDistance * 0.01);
  1546. SEPivot3Entry_Active = order_Entry(false, Pivot3_LowRev, "SHORT", RT, Pivot3_LowRev, "SE Pivot3", Pivot3_order_type);
  1547. end;
  1548. end;
  1549. end;
  1550.  
  1551.  
  1552.  
  1553. // When we first send the entry price must be below the pivot
  1554. // However once entry is activated the order should stay open
  1555. if
  1556. Pivot3_HighRev > 0 and
  1557. (close < Pivot3_HighRev or LEPivot3Entry_Active) and
  1558. Pivot3_HighBreakBar < Valid_Pivot3_HighBar and
  1559. (Not TradeOnlySameDay or
  1560. (
  1561. ( Pivot3_Complete_Reset_new_day=0 ) or
  1562. ( Pivot3_Complete_Reset_new_day=1 and Valid_Pivot3_HighBar > NewDayBarNum ) or
  1563. ( Pivot3_Complete_Reset_new_day=2 and Valid_Pivot3_HighBar > NewDayBarNum and Pivot3_HighBreakBar > NewDayBarNum )
  1564. )
  1565. )
  1566. then
  1567. If (Pivot3_Entry_main_filters_ONOFF=0 or filters_ok_LONG_Entry) then begin
  1568. If (permit_LONG) then
  1569. LEPivot3Entry_Active= order_Entry(false, Pivot3_HighRev, "LONG", RT, Pivot3_HighRev, "LE Pivot3", Pivot3_order_type);
  1570. end;
  1571. end;
  1572. end;
  1573. end;
  1574.  
  1575. #endregion
  1576. // #####################################################################################################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement