暫無描述

vol_hedge_strategy_mt5_v2.mq5 128KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603
  1. //+------------------------------------------------------------------+
  2. //| vol_hedge_strategy_mt5.mq5 |
  3. //| Copyright 2025, MQL Development |
  4. //| https://www.mqldevelopment.com/ |
  5. //+------------------------------------------------------------------+
  6. #property copyright "Copyright 2025, MQL Development"
  7. #property link "https://www.mqldevelopment.com/"
  8. #property version "1.20"
  9. #define MaxOrders 100
  10. #include <Trade\Trade.mqh>
  11. CTrade trade;
  12. //+------------------------------------------------------------------+
  13. //| Expert initialization function |
  14. //+------------------------------------------------------------------+
  15. enum selectDay
  16. {
  17. prev, // Previous Day
  18. curr, // Current Day
  19. };
  20. struct new_trade_store
  21. {
  22. ulong buy_ticket; // Buy Ticket
  23. ulong sell_ticket; // Sell Ticket
  24. string symbol; // Symbol
  25. double price; // Price
  26. double stop_loss_buy; // StopLoss Buy
  27. double take_profit_buy; // TakeProfit Buy
  28. double stop_loss_sell; // StopLoss Sell
  29. double take_profit_sell; // TakeProfit Sell
  30. datetime start_time; // Start time
  31. datetime end_time; // End Time
  32. bool demand_level_hit; // Demand Level Hit
  33. bool buy_hit_virtual_sl; // Buy Hit Virtual StopLoss
  34. bool sell_hit_virtual_sl; // Sell Hit Virtual StopLoss
  35. new_trade_store()
  36. {
  37. buy_ticket = -1;
  38. sell_ticket = -1;
  39. price = 0;
  40. demand_level_hit = false;
  41. buy_hit_virtual_sl = false;
  42. sell_hit_virtual_sl = false;
  43. }
  44. };
  45. new_trade_store newTradeStore[MaxOrders];
  46. enum lotcalculator
  47. {
  48. fix, //Fixed Lot Size
  49. rsk, //Risk in Percentage
  50. dollar, // Risk in Dollars
  51. };
  52. sinput string string_0 = "<><><><><><> General SETTINGS <><><><><><>"; //__
  53. input string tempString = "Enter your text"; // Preset Name
  54. input int magic_no = 333; // Magic no
  55. input bool useTpSlPips = false; // Use Relative Tp/Sl in Points
  56. input double stopLoss = 1000; // Fixed Stop Loss in Points
  57. input double takeProfit = 1000; // Fixed Take Profit in Points
  58. input bool bothHitsSl = false; // Enable Topped & Tailed Pre-Demand Level
  59. input bool hitDemandFirst = false; // Hit Demand First to Check Topped & Tailed
  60. input int maxTrades = 2; // Max Concurrent Trades
  61. input int maxSlippage = 5; // Max Slippage (Points)
  62. input bool enableSpreadFilter = false; // Enable Spread Filter
  63. input double maximumSpread = 10; // Maximum Spread (Points)
  64. input string tradeComment = "Trade Placed"; // Trade Comment Prefix
  65. input string dataFileName = "vol_hedge_data.csv"; // CSV File Name
  66. input bool enableDrawLevels = false; // Draw Levels
  67. input string string_0_8 = "<><><><><><> Data Filing Setting<><><><><><>"; //__
  68. input string file_name = "data_store_file.csv";// Data Store File Name
  69. input string string_0_2 = "<><><><><><> Trailing Setting<><><><><><>"; //__
  70. input bool indivial_trailing = false; // Indiviual Trailing
  71. input double ts_sl = 150; // Trailing Start in Points
  72. input double ts = 50; // Trailing Stop in Points
  73. input string strMA14 ="<><><><><><> BreakEven Settings <><><><><><>";//_
  74. input bool UseBreakEven = false; // Use Break Even
  75. input int breakEvenPoints = 150; // BreakEven Trigger Points
  76. input int breakStopPoint = 50; // BreakEven Above OpenPrice Points
  77. input string string_1 = "<><><><><><> Lot Management<><><><><><>"; //__
  78. input lotcalculator lot_calculator = fix; // Lot Size Option
  79. input double lot_amount = 0.1; // Lot Size
  80. input double risk = 0.5; // Risk in Percentage %
  81. input double dollars = 10; // Risk in GBP
  82. input string time_setting = "<><><><><> Time Filter Settings <><><><><>"; //_
  83. input bool enableTimeFilter = false; // Enable Time Filter
  84. input string startTime = "03:00"; // Start Time Session
  85. input string endTime = "09:00"; // End Time Session
  86. input string weekFilterSettings = "<><><><><> Week Filter Settings <><><><><>"; //_
  87. input bool enableWeekFilter = false; // Enable Week Filter (true/false)
  88. input int filterMonthNumber = 9; // Month number to apply week filter (1..12)
  89. input int filterWeekNumber = 2; // Week number of month to block (1..5)
  90. input string monthFilterSettings = "<><><><><> Month Filter Settings <><><><><>"; //_
  91. input bool allowJanuary = true; // Allow Trading in January
  92. input bool allowFebruary = true; // Allow Trading in February
  93. input bool allowMarch = true; // Allow Trading in March
  94. input bool allowApril = true; // Allow Trading in April
  95. input bool allowMay = true; // Allow Trading in May
  96. input bool allowJune = true; // Allow Trading in June
  97. input bool allowJuly = true; // Allow Trading in July
  98. input bool allowAugust = true; // Allow Trading in August
  99. input bool allowSeptember = true; // Allow Trading in September
  100. input bool allowOctober = true; // Allow Trading in October
  101. input bool allowNovember = true; // Allow Trading in November
  102. input bool allowDecember = true; // Allow Trading in December
  103. // Global Variables
  104. static double tickCurrentBid = 0;
  105. double tickPreviousBid = 0;
  106. static double tickCurrentAsk = 0;
  107. double tickPreviousAsk = 0;
  108. int newYorkStartTime = 0, newYorkStartMin = 0, newYorkEndHour = 0, newYorkEndMin = 0;
  109. datetime newYorkStartTrading = 0, newYorkEndTrading = 0;
  110. int GMT_Broker_Time = +2; // GMT_Broker_Time Time of your Broker
  111. int gmt = 0; // GMT_Broker_Time Time of your Broker
  112. double levelsAre[];
  113. selectDay newYorkSessionDay = curr; // Select Day for Start Time
  114. //+------------------------------------------------------------------+
  115. //| |
  116. //+------------------------------------------------------------------+
  117. int OnInit()
  118. {
  119. //---
  120. Print(" OnInIt. ");
  121. trade.SetExpertMagicNumber(magic_no);
  122. trade.SetDeviationInPoints(maxSlippage);
  123. trade.SetTypeFilling(ORDER_FILLING_IOC);
  124. trade.LogLevel(LOG_LEVEL_ALL);
  125. trade.SetAsyncMode(false);
  126. if(!MQLInfoInteger(MQL_TESTER))
  127. {
  128. loadNewTradeStoreFile();
  129. }
  130. int filehandle = FileOpen(dataFileName, FILE_READ | FILE_CSV | FILE_COMMON | FILE_ANSI);
  131. if(filehandle != INVALID_HANDLE)
  132. {
  133. Print(" Valid Handler. ");
  134. while(!FileIsEnding(filehandle))
  135. {
  136. string orderToRead = FileReadString(filehandle);
  137. string orderData[];
  138. //Print("Data: ", OrderToRead);
  139. StringSplit(orderToRead, StringGetCharacter(",",0), orderData);
  140. Print("Array Size: ", ArraySize(orderData));
  141. Print(" Order is: ", orderToRead);
  142. for(int i = 0 ; i < ArraySize(orderData) ; i++)
  143. {
  144. Print(" Order Data: ", orderData[i], " i: ", i);
  145. }
  146. if(ArraySize(orderData) >= 8)
  147. {
  148. if(orderData[0] == Symbol())
  149. {
  150. // store into local variables first (trim if needed)
  151. ulong buy_ticket_local = (ulong)-1; // keep -1 as per your convention
  152. ulong sell_ticket_local = (ulong)-1;
  153. string symbol_local = orderData[0];
  154. double price_local = StringToDouble(orderData[1]);
  155. double sl_buy_local = StringToDouble(orderData[2]);
  156. double tp_buy_local = StringToDouble(orderData[3]);
  157. double sl_sell_local = StringToDouble(orderData[4]);
  158. double tp_sell_local = StringToDouble(orderData[5]);
  159. // if your CSV has extra fields (tp2,tp3, etc.) parse here as needed
  160. datetime start_local = StringToTime(orderData[6]);
  161. datetime end_local = StringToTime(orderData[7]);
  162. if(orderData[6] == "0" || orderData[6] == NULL)
  163. {
  164. start_local = 0;
  165. }
  166. if(orderData[7] == "0" || orderData[7] == NULL)
  167. {
  168. end_local = 0;
  169. }
  170. ArrayResize(levelsAre,ArraySize(levelsAre)+1);
  171. levelsAre[ArraySize(levelsAre) - 1] = price_local;
  172. // OPTIONAL: only add when price == 0:
  173. // if(MathAbs(price_local) > 1e-9) { Print("Skipped: price != 0"); continue; }
  174. bool buy_virtual_tp_hit = true;
  175. bool sell_virtual_tp_hit = true;
  176. bool demand_level_hit_first = true;
  177. if(bothHitsSl)
  178. {
  179. buy_virtual_tp_hit = false;
  180. sell_virtual_tp_hit = false;
  181. if(hitDemandFirst)
  182. {
  183. demand_level_hit_first = false;
  184. }
  185. }
  186. // call the single-responsibility function that writes into struct array
  187. if(!level_present(price_local))
  188. {
  189. Print(" --------------- Price: ", price_local, " Start Time: ", start_local, " End Time: ", end_local, " --------------------");
  190. addToNewTradeStore(buy_ticket_local, sell_ticket_local,
  191. symbol_local, price_local,
  192. sl_buy_local, tp_buy_local,
  193. sl_sell_local, tp_sell_local,
  194. start_local, end_local,
  195. demand_level_hit_first, buy_virtual_tp_hit, sell_virtual_tp_hit);
  196. }
  197. else
  198. {
  199. Print("Level is already Present. Level: ", price_local);
  200. }
  201. }
  202. }
  203. }
  204. FileClose(filehandle);
  205. }
  206. else
  207. {
  208. Print(" InValid Handler. Error: ", GetLastError());
  209. }
  210. struct_level_check();
  211. print_newTradeStore();
  212. string time[];
  213. StringSplit(startTime,':',time);
  214. newYorkStartTime = (int)StringToInteger(time[0]);
  215. newYorkStartMin = (int)StringToInteger(time[1]);
  216. Print("NewYork Start Time Hour: ",newYorkStartTime," Start Time Min: ",newYorkStartMin);
  217. time[0] = "";
  218. time[1] = "";
  219. StringSplit(endTime,':',time);
  220. newYorkEndHour = (int)StringToInteger(time[0]);
  221. newYorkEndMin = (int)StringToInteger(time[1]);
  222. Print("NewYork End Time Hour: ",newYorkEndHour," End Time Min: ",newYorkEndMin);
  223. StringSplit(startTime,':',time);
  224. int newYorkStartHour = (int)StringToInteger(time[0]);
  225. newYorkStartMin = (int)StringToInteger(time[1]);
  226. EventSetMillisecondTimer(500);
  227. time[0] = "";
  228. time[1] = "";
  229. StringSplit(endTime,':',time);
  230. newYorkEndHour = (int)StringToInteger(time[0]);
  231. newYorkEndMin = (int)StringToInteger(time[1]);
  232. datetime startDateTime;
  233. MqlDateTime st;
  234. TimeCurrent(st); // get current date
  235. st.hour = newYorkStartHour;
  236. st.min = newYorkStartMin;
  237. st.sec = 0;
  238. startDateTime = StructToTime(st);
  239. datetime endDateTime;
  240. MqlDateTime et;
  241. TimeCurrent(et); // get current date
  242. et.hour = newYorkEndHour;
  243. et.min = newYorkEndMin;
  244. et.sec = 0;
  245. endDateTime = StructToTime(et);
  246. datetime start_Time = 0, end_Time = 0;
  247. if(startDateTime > endDateTime)
  248. {
  249. Print(" --------------------------- Previous -------------------------------------- ");
  250. newYorkSessionDay = prev;
  251. }
  252. else
  253. {
  254. Print(" --------------------------- Current -------------------------------------- ");
  255. newYorkSessionDay = curr;
  256. }
  257. timeFilter(true);
  258. int timeDifference = (int)TimeCurrent() - (int)TimeGMT();
  259. Print("Time Difference is: ", timeDifference);
  260. if(timeDifference > 0)
  261. {
  262. GMT_Broker_Time = (int)MathRound((double)timeDifference / 3600.0);
  263. }
  264. else
  265. if(timeDifference < 0)
  266. {
  267. GMT_Broker_Time = (int)MathRound((double)timeDifference / 3600.0);
  268. }
  269. else // timeDifference == 0
  270. {
  271. GMT_Broker_Time = 0;
  272. }
  273. Print("Gmt Time: ", TimeGMT(), " Broker Gmt Time: ", GMT_Broker_Time);
  274. gmt = GMT_Broker_Time;
  275. if(enableDrawLevels)
  276. {
  277. drawLevels();
  278. }
  279. //---
  280. return(INIT_SUCCEEDED);
  281. }
  282. //+------------------------------------------------------------------+
  283. //| Expert deinitialization function |
  284. //+------------------------------------------------------------------+
  285. void OnDeinit(const int reason)
  286. {
  287. //---
  288. ObjectsDeleteAll(0, 0, OBJ_HLINE);
  289. if(!MQLInfoInteger(MQL_TESTER))
  290. {
  291. saveNewTradeStoreFile();
  292. }
  293. }
  294. //+------------------------------------------------------------------+
  295. //| Expert tick function |
  296. //+------------------------------------------------------------------+
  297. void OnTick()
  298. {
  299. mainActivity();
  300. }
  301. //+------------------------------------------------------------------+
  302. //| |
  303. //+------------------------------------------------------------------+
  304. void mainActivity()
  305. {
  306. //---
  307. if(enableDrawLevels)
  308. {
  309. drawLevels();
  310. }
  311. newBar();
  312. if(indivial_trailing)
  313. {
  314. Individual_Trailing();
  315. }
  316. if(UseBreakEven)
  317. {
  318. breakEven();
  319. }
  320. double Bid = SymbolInfoDouble(Symbol(), SYMBOL_BID);
  321. double Ask = SymbolInfoDouble(Symbol(), SYMBOL_ASK);
  322. if(tickPreviousBid == 0 && tickCurrentBid == 0)
  323. {
  324. tickPreviousBid = Bid;
  325. tickCurrentBid = Bid;
  326. }
  327. else
  328. {
  329. tickPreviousBid = tickCurrentBid;
  330. tickCurrentBid = Bid;
  331. }
  332. if(tickPreviousAsk == 0 && tickCurrentAsk == 0)
  333. {
  334. tickPreviousAsk = Ask;
  335. tickCurrentAsk = Ask;
  336. }
  337. else
  338. {
  339. tickPreviousAsk = tickCurrentAsk;
  340. tickCurrentAsk = Ask;
  341. }
  342. timeFilter(false);
  343. // Comment(" Session Start = ", newYorkStartTrading, " Asian Session End = ", newYorkEndTrading);
  344. if((enableTimeFilter && TimeCurrent() >= newYorkStartTrading && TimeCurrent() <= newYorkEndTrading) || !enableTimeFilter)
  345. {
  346. removeFromStructure();
  347. if(bothHitsSl)
  348. {
  349. virtualSLHitCheck();
  350. }
  351. tradePlacingCheck();
  352. }
  353. }
  354. //+------------------------------------------------------------------+
  355. //+------------------------------------------------------------------+
  356. //| |
  357. //+------------------------------------------------------------------+
  358. bool isTradingAllowedByWeek()
  359. {
  360. if(!enableWeekFilter) // filter disabled -> allow trading
  361. return true;
  362. // validate inputs quickly
  363. if(filterMonthNumber < 1 || filterMonthNumber > 12)
  364. {
  365. PrintFormat("⚠️ filterMonthNumber out of range (%d). Week filter disabled.", filterMonthNumber);
  366. return true;
  367. }
  368. if(filterWeekNumber < 1 || filterWeekNumber > 5)
  369. {
  370. PrintFormat("⚠️ filterWeekNumber out of range (%d). Week filter disabled.", filterWeekNumber);
  371. return true;
  372. }
  373. MqlDateTime t;
  374. TimeToStruct(TimeCurrent(), t); // t.mon (1..12), t.day (1..31)
  375. // if the months don't match, week filter doesn't apply -> allow
  376. if(t.mon != filterMonthNumber)
  377. return true;
  378. // compute week of month: days 1-7 -> week 1, 8-14 -> week 2, etc.
  379. int weekOfMonth = ((t.day - 1) / 7) + 1; // yields 1..5
  380. // if current week equals the filtered week -> block trading (return false)
  381. if(weekOfMonth == filterWeekNumber)
  382. {
  383. return false; // trading NOT allowed this week of the specified month
  384. }
  385. return true; // otherwise allow trading
  386. }
  387. //+------------------------------------------------------------------+
  388. //| |
  389. //+------------------------------------------------------------------+
  390. bool isTradingAllowedByMonth()
  391. {
  392. MqlDateTime t;
  393. TimeToStruct(TimeCurrent(), t);
  394. switch(t.mon)
  395. {
  396. case 1:
  397. return allowJanuary;
  398. case 2:
  399. return allowFebruary;
  400. case 3:
  401. return allowMarch;
  402. case 4:
  403. return allowApril;
  404. case 5:
  405. return allowMay;
  406. case 6:
  407. return allowJune;
  408. case 7:
  409. return allowJuly;
  410. case 8:
  411. return allowAugust;
  412. case 9:
  413. return allowSeptember;
  414. case 10:
  415. return allowOctober;
  416. case 11:
  417. return allowNovember;
  418. case 12:
  419. return allowDecember;
  420. default:
  421. return true; // Safe fallback: allow trading if month invalid
  422. }
  423. }
  424. //+------------------------------------------------------------------+
  425. //| |
  426. //+------------------------------------------------------------------+
  427. bool newBar()
  428. {
  429. static datetime lastbar;
  430. datetime curbar = iTime(Symbol(), PERIOD_CURRENT, 0);
  431. if(lastbar != curbar)
  432. {
  433. lastbar = curbar;
  434. Print(" ---------------------- New Bar :: ---------------------- ",lastbar);
  435. return (true);
  436. }
  437. else
  438. {
  439. return (false);
  440. }
  441. }
  442. //+------------------------------------------------------------------+
  443. //| |
  444. //+------------------------------------------------------------------+
  445. void closeTrades(int type,int magicno)
  446. {
  447. Print("Total order: ",OrdersTotal());
  448. for(int i= PositionsTotal()-1; i>=0; i--)
  449. {
  450. // Print(" Selection: ",OrderSelect(i,SELECT_BY_POS)," i: ",i," OrdersTotal(): ", OrdersTotal());
  451. ulong ticket = PositionGetTicket(i);
  452. if(PositionSelectByTicket(ticket))
  453. {
  454. if(PositionGetInteger(POSITION_TYPE) == type)
  455. {
  456. if(PositionGetInteger(POSITION_MAGIC) == magicno && PositionGetString(POSITION_SYMBOL) == Symbol())
  457. {
  458. //trade.PositionClose(PositionGetInteger(POSITION_TICKET))
  459. if(!trade.PositionClose(PositionGetInteger(POSITION_TICKET)))
  460. {Print("Problem in closing order order ",PositionGetInteger(POSITION_TICKET)); }
  461. else
  462. {
  463. Print("Order Closed by closeTrades() new filter",PositionGetInteger(POSITION_TICKET));
  464. }
  465. }
  466. }
  467. }
  468. }
  469. }
  470. //+------------------------------------------------------------------+
  471. //| |
  472. //+------------------------------------------------------------------+
  473. int orderCount_1(int type,int magic)
  474. {
  475. int count1=0;
  476. for(int i= PositionsTotal()-1; i>=0; i--)
  477. {
  478. // Print(" Selection: ",OrderSelect(i,SELECT_BY_POS)," i: ",i," OrdersTotal(): ", OrdersTotal());
  479. ulong ticket = PositionGetTicket(i);
  480. if(PositionSelectByTicket(ticket))
  481. {
  482. // if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY || PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
  483. if(PositionGetInteger(POSITION_MAGIC) == magic && PositionGetString(POSITION_SYMBOL) == Symbol())
  484. {
  485. //trade.PositionClose(PositionGetInteger(POSITION_TICKET))
  486. if(PositionGetInteger(POSITION_TYPE) == type)
  487. {
  488. count1++;
  489. }
  490. }
  491. }
  492. }
  493. return count1;
  494. }
  495. //+------------------------------------------------------------------+
  496. //| |
  497. //+------------------------------------------------------------------+
  498. int orderCount(int type)
  499. {
  500. int count = 0;
  501. for(int i= PositionsTotal()-1; i>=0; i--)
  502. {
  503. ulong ticket = PositionGetTicket(i);
  504. if(PositionSelectByTicket(ticket))
  505. {
  506. if(PositionGetInteger(POSITION_MAGIC) == magic_no && PositionGetString(POSITION_SYMBOL) == Symbol())
  507. {
  508. if(PositionGetInteger(POSITION_TYPE) == type)
  509. {
  510. count++;
  511. }
  512. }
  513. }
  514. }
  515. return count;
  516. }
  517. //+------------------------------------------------------------------+
  518. //| |
  519. //+------------------------------------------------------------------+
  520. bool level_present(double priceIs)
  521. {
  522. for(int i = 0 ; i < MaxOrders ; i++)
  523. {
  524. if(newTradeStore[i].price > 0)
  525. {
  526. if(priceIs == newTradeStore[i].price)
  527. {
  528. return true;
  529. }
  530. }
  531. }
  532. return false;
  533. }
  534. //+------------------------------------------------------------------+
  535. //| |
  536. //+------------------------------------------------------------------+
  537. void struct_level_check()
  538. {
  539. for(int i = 0; i < MaxOrders; i++)
  540. {
  541. if(newTradeStore[i].price > 0)
  542. {
  543. bool found = false;
  544. for(int j = 0; j < ArraySize(levelsAre); j++)
  545. {
  546. if(newTradeStore[i].price == levelsAre[j])
  547. {
  548. found = true;
  549. break;
  550. }
  551. }
  552. if(!found)
  553. {
  554. Print("Price not found in levelsAre[] -> index:", i, " | price:", newTradeStore[i].price);
  555. newTradeStore[i].buy_ticket = (ulong)-1;
  556. newTradeStore[i].sell_ticket = (ulong)-1;
  557. bool buy_virtual_tp_hit = true;
  558. bool sell_virtual_tp_hit = true;
  559. if(bothHitsSl)
  560. {
  561. buy_virtual_tp_hit = false;
  562. sell_virtual_tp_hit = false;
  563. }
  564. newTradeStore[i].buy_hit_virtual_sl = buy_virtual_tp_hit;
  565. newTradeStore[i].sell_hit_virtual_sl = sell_virtual_tp_hit;
  566. newTradeStore[i].symbol = "";
  567. newTradeStore[i].price = 0.0;
  568. newTradeStore[i].stop_loss_buy = 0.0;
  569. newTradeStore[i].take_profit_buy = 0.0;
  570. newTradeStore[i].stop_loss_sell = 0.0;
  571. newTradeStore[i].take_profit_sell = 0.0;
  572. newTradeStore[i].start_time = 0;
  573. newTradeStore[i].end_time = 0;
  574. return;
  575. }
  576. }
  577. }
  578. return;
  579. }
  580. //+------------------------------------------------------------------+
  581. //| |
  582. //+------------------------------------------------------------------+
  583. void print_newTradeStore()
  584. {
  585. bool anyPrinted = false;
  586. Print("=== newTradeStore DUMP ===");
  587. for(int i = 0; i < MaxOrders; i++)
  588. {
  589. // only print populated slots (price > 0)
  590. if(newTradeStore[i].price > 0.0)
  591. {
  592. anyPrinted = true;
  593. // convert booleans to readable text
  594. string buyHit = newTradeStore[i].buy_hit_virtual_sl ? "true" : "false";
  595. string sellHit = newTradeStore[i].sell_hit_virtual_sl ? "true" : "false";
  596. // header line for the entry
  597. Print("---- Entry ", i, " ----");
  598. // summary line (symbol, price, times)
  599. Print(" symbol:", newTradeStore[i].symbol,
  600. " | price:", DoubleToString(newTradeStore[i].price, Digits()),
  601. " | start:", IntegerToString(newTradeStore[i].start_time),
  602. " | end:", IntegerToString(newTradeStore[i].end_time));
  603. // ticket line
  604. Print(" tickets -> buy:", newTradeStore[i].buy_ticket,
  605. " | sell:", newTradeStore[i].sell_ticket);
  606. // flags line
  607. Print(" flags -> buy_hit_virtual_sl:", buyHit,
  608. " | sell_hit_virtual_sl:", sellHit,
  609. " | bothHitsSl:", (bothHitsSl ? "true" : "false"));
  610. // SL/TP line
  611. Print(" SL/TP -> buySL:", DoubleToString(newTradeStore[i].stop_loss_buy, Digits()),
  612. " | buyTP:", DoubleToString(newTradeStore[i].take_profit_buy, Digits()),
  613. " | sellSL:", DoubleToString(newTradeStore[i].stop_loss_sell, Digits()),
  614. " | sellTP:", DoubleToString(newTradeStore[i].take_profit_sell, Digits()));
  615. }
  616. }
  617. if(!anyPrinted)
  618. Print(" (no active newTradeStore entries found)");
  619. Print("=== end dump ===");
  620. }
  621. //+------------------------------------------------------------------+
  622. //| |
  623. //+------------------------------------------------------------------+
  624. void addToNewTradeStore(ulong r_buy_ticket, ulong r_sell_ticket,
  625. string r_symbol, double r_price,
  626. double r_stop_loss_buy, double r_take_profit_buy,
  627. double r_stop_loss_sell, double r_take_profit_sell,
  628. datetime r_start_time, datetime r_end_time, bool r_demand_level_hit, bool r_buy_hit_sl, bool r_sell_hit_sl)
  629. {
  630. Print(" Tier 1. ");
  631. for(int i = 0; i < MaxOrders; i++)
  632. {
  633. // treat slot as empty when both tickets are -1 (same convention as constructor)
  634. if(newTradeStore[i].buy_ticket == -1 && newTradeStore[i].sell_ticket == -1)
  635. {
  636. if(newTradeStore[i].price == 0)
  637. {
  638. newTradeStore[i].buy_ticket = r_buy_ticket;
  639. newTradeStore[i].sell_ticket = r_sell_ticket;
  640. newTradeStore[i].symbol = r_symbol;
  641. newTradeStore[i].price = r_price;
  642. newTradeStore[i].stop_loss_buy = r_stop_loss_buy;
  643. newTradeStore[i].take_profit_buy = r_take_profit_buy;
  644. newTradeStore[i].stop_loss_sell = r_stop_loss_sell;
  645. newTradeStore[i].take_profit_sell = r_take_profit_sell;
  646. newTradeStore[i].start_time = r_start_time;
  647. newTradeStore[i].end_time = r_end_time;
  648. newTradeStore[i].demand_level_hit = r_demand_level_hit;
  649. newTradeStore[i].buy_hit_virtual_sl = r_buy_hit_sl;
  650. newTradeStore[i].sell_hit_virtual_sl = r_sell_hit_sl;
  651. Print("Stored -> idx: ", i,
  652. " | Symbol: ", newTradeStore[i].symbol,
  653. " | price: ", DoubleToString(newTradeStore[i].price, Digits()),
  654. " | Sl Buy: ", DoubleToString(newTradeStore[i].stop_loss_buy, Digits()),
  655. " | Tp Buy: ", DoubleToString(newTradeStore[i].take_profit_buy, Digits()),
  656. "\n | Sl Sell: ", DoubleToString(newTradeStore[i].stop_loss_sell, Digits()),
  657. " | Tp Sell: ", DoubleToString(newTradeStore[i].take_profit_sell, Digits()),
  658. " | start: ", TimeToString(newTradeStore[i].start_time, TIME_DATE|TIME_SECONDS),
  659. " | end: ", TimeToString(newTradeStore[i].end_time, TIME_DATE|TIME_SECONDS),
  660. " | Demand Level Hit: ", newTradeStore[i].demand_level_hit,
  661. " | Buy Virtal Sl Hit: ", newTradeStore[i].buy_hit_virtual_sl,
  662. " | Sell Virtual Sl Hit: ", newTradeStore[i].sell_hit_virtual_sl);
  663. break;
  664. }
  665. }
  666. }
  667. }
  668. //+------------------------------------------------------------------+
  669. //| |
  670. //+------------------------------------------------------------------+
  671. void tradePlacingCheck()
  672. {
  673. for(int i = 0; i < MaxOrders; i++)
  674. {
  675. if(newTradeStore[i].buy_ticket == -1 && newTradeStore[i].sell_ticket == -1)
  676. {
  677. if(newTradeStore[i].price > 0)
  678. {
  679. if((TimeCurrent() > newTradeStore[i].start_time && TimeCurrent() < newTradeStore[i].end_time) || (newTradeStore[i].start_time == 0 || newTradeStore[i].end_time == 0))
  680. {
  681. if(newTradeStore[i].buy_hit_virtual_sl == true && newTradeStore[i].sell_hit_virtual_sl == true)
  682. {
  683. double levelPriceIs = newTradeStore[i].price;
  684. if((tickPreviousBid > levelPriceIs && tickCurrentBid < levelPriceIs) ||
  685. (tickPreviousBid < levelPriceIs && tickCurrentBid > levelPriceIs))
  686. {
  687. Print(" ------------------- Level Crossed. Level is: ", levelPriceIs, " -------------------- ");
  688. if(isTradingAllowedByWeek())
  689. {
  690. if(isTradingAllowedByMonth())
  691. {
  692. if((enableSpreadFilter && spreadFilter()) || !enableSpreadFilter)
  693. {
  694. ulong buyTicket = -1, sellTicket = -1;
  695. if(countLiveTrades() < maxTrades)
  696. {
  697. buyTicket = placeBuyTrade(newTradeStore[i].stop_loss_buy, newTradeStore[i].take_profit_buy);
  698. sellTicket = placeSellTrade(newTradeStore[i].stop_loss_sell, newTradeStore[i].take_profit_sell);
  699. newTradeStore[i].buy_ticket = buyTicket;
  700. newTradeStore[i].sell_ticket = sellTicket;
  701. }
  702. }
  703. }
  704. }
  705. }
  706. }
  707. }
  708. }
  709. }
  710. }
  711. }
  712. //+------------------------------------------------------------------+
  713. //| |
  714. //+------------------------------------------------------------------+
  715. void draw_lines(string name, datetime dateTime, double price, color selectColor, long lineStyle)
  716. {
  717. if(!ObjectCreate(0, name, OBJ_HLINE, 0, dateTime, price))
  718. {
  719. Print(" Error in creating ", name," : ","line: "," ",GetLastError());
  720. }
  721. else
  722. {
  723. ObjectSetInteger(0, name, OBJPROP_COLOR, selectColor);
  724. ObjectSetInteger(0, name, OBJPROP_STYLE, lineStyle);
  725. }
  726. }
  727. //+------------------------------------------------------------------+
  728. //| |
  729. //+------------------------------------------------------------------+
  730. void drawLevels()
  731. {
  732. for(int i = 0; i < MaxOrders; i++)
  733. {
  734. if(newTradeStore[i].price > 0)
  735. {
  736. if((TimeCurrent() > newTradeStore[i].start_time && TimeCurrent() < newTradeStore[i].end_time) || (newTradeStore[i].start_time == 0 || newTradeStore[i].end_time == 0))
  737. {
  738. if(ObjectFind(0, "level" + (string)newTradeStore[i].price) < 0)
  739. {
  740. draw_lines("level" + (string)newTradeStore[i].price, TimeCurrent(), newTradeStore[i].price, clrAqua, STYLE_SOLID);
  741. }
  742. }
  743. else
  744. {
  745. if(ObjectFind(0, "level" + (string)newTradeStore[i].price) >= 0)
  746. {
  747. ObjectDelete(0, "level" + (string)newTradeStore[i].price);
  748. }
  749. }
  750. }
  751. }
  752. }
  753. //+------------------------------------------------------------------+
  754. //| |
  755. //+------------------------------------------------------------------+
  756. ulong placeBuyTrade(double stoploss, double takeprofit)
  757. {
  758. double buySL = 0, buyTp=0;
  759. //openPrice = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
  760. double Ask = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
  761. double Bid = SymbolInfoDouble(Symbol(),SYMBOL_BID);
  762. if(useTpSlPips)
  763. {
  764. if(stopLoss != 0)
  765. {
  766. buySL = Ask - (stopLoss * Point());
  767. }
  768. if(takeProfit != 0)
  769. {
  770. buyTp = Ask + (takeProfit * Point());
  771. }
  772. }
  773. else
  774. {
  775. if(stoploss > 0)
  776. {
  777. buySL = stoploss;
  778. }
  779. if(takeprofit > 0)
  780. {
  781. buyTp = takeprofit;
  782. }
  783. }
  784. double distance = MathAbs((Ask - buySL) / Point());
  785. if(trade.PositionOpen(Symbol(),ORDER_TYPE_BUY,getLot(distance),Ask,buySL,buyTp,tradeComment+" Buy"))
  786. {
  787. Print("Buy Trade Placed: ",trade.ResultOrder());
  788. return trade.ResultOrder();
  789. }
  790. else
  791. {
  792. Print("Error in placing Buy: "+Symbol()+" ",GetLastError());
  793. return -1;
  794. }
  795. return -1;
  796. }
  797. //+------------------------------------------------------------------+
  798. //| |
  799. //+------------------------------------------------------------------+
  800. ulong placeSellTrade(double stoploss, double takeprofit)
  801. {
  802. double sellSL = 0, sellTp = 0;
  803. double Ask = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
  804. double Bid = SymbolInfoDouble(Symbol(),SYMBOL_BID);
  805. if(useTpSlPips)
  806. {
  807. if(stopLoss != 0)
  808. {
  809. sellSL = Bid + (stopLoss * Point());
  810. }
  811. if(takeProfit != 0)
  812. {
  813. sellTp = Bid - (takeProfit * Point());
  814. }
  815. }
  816. else
  817. {
  818. if(stoploss > 0)
  819. {
  820. sellSL = stoploss;
  821. }
  822. if(takeprofit > 0)
  823. {
  824. sellTp = takeprofit;
  825. }
  826. }
  827. double distance = MathAbs((Bid - sellSL) / Point());
  828. if(trade.PositionOpen(Symbol(),ORDER_TYPE_SELL,getLot(distance),Bid,sellSL,sellTp,tradeComment+ " Sell"))
  829. {
  830. Print("Sell Trade PLaced: ",trade.ResultOrder());
  831. return trade.ResultOrder();
  832. }
  833. else
  834. {
  835. Print("Error in placing Sell: "+Symbol()+" ",GetLastError());
  836. return -1;
  837. }
  838. return -1;
  839. }
  840. //+------------------------------------------------------------------+
  841. //| |
  842. //+------------------------------------------------------------------+
  843. double getLot(double stop_loss)
  844. {
  845. Print("Tick Value: ",SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_VALUE));
  846. Print("Tick Size: ",SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_SIZE));
  847. double modeTickV=SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_VALUE)
  848. ,modeTickS=SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_SIZE);
  849. // Print("Pip value: ", NormalizeDouble(((SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_VALUE)/(SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_SIZE)/Point))*10),2));
  850. double pipvalue = NormalizeDouble(((SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_VALUE)/(SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_SIZE)/Point()))*10),2);
  851. // pipvalue=NormalizeDouble((modeTickV/modeTickS/Point()),)
  852. // pipvalue=
  853. pipvalue = pipvalue / 10;
  854. double lotSize = lot_amount;
  855. if(lot_calculator == rsk || lot_calculator == dollar) //calculating risk
  856. {
  857. double riskamount = 0;
  858. if(lot_calculator == rsk)
  859. {
  860. riskamount = (risk/100)*AccountInfoDouble(ACCOUNT_BALANCE);
  861. }
  862. if(lot_calculator == dollar)
  863. {
  864. riskamount = dollars;
  865. }
  866. double pipvalue_required=riskamount/stop_loss;
  867. lotSize = pipvalue_required/pipvalue;
  868. //sl=riskamount/pipValuelot
  869. int roundDigit=0;
  870. double step=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_STEP);
  871. while(step<1)
  872. {
  873. roundDigit++;
  874. step=step*10;
  875. }
  876. Print("Round Digits:",roundDigit);
  877. lotSize = NormalizeDouble(lotSize,roundDigit);
  878. //
  879. }
  880. Print(" Before Normalizing Lot Size By Step : ",lotSize);
  881. lotSize = NormalizeLot(lotSize,SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_STEP));
  882. Print(" After Normalizing Lot Size By Step : ",lotSize);
  883. if(lotSize > SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MAX))
  884. {
  885. lotSize=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MAX);
  886. }
  887. else
  888. if(lotSize<SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MIN))
  889. {
  890. lotSize=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MIN);
  891. }
  892. //---
  893. return lotSize;
  894. }
  895. //+------------------------------------------------------------------+
  896. //| |
  897. //+------------------------------------------------------------------+
  898. double NormalizeLot(double lot, double step)
  899. {
  900. double steps = lot / step;
  901. double rounded_steps = MathRound(steps); // round to nearest whole step
  902. double rounded_lot = rounded_steps * step;
  903. return NormalizeDouble(rounded_lot, 2); // 2 decimals for safety
  904. }
  905. //+------------------------------------------------------------------+
  906. //| |
  907. //+------------------------------------------------------------------+
  908. void timeFilter(bool onInit)
  909. {
  910. MqlDateTime sdate,edate;
  911. datetime start_Time = 0, end_Time = 0;
  912. if(newYorkSessionDay == prev)
  913. {
  914. if(onInit)
  915. {
  916. start_Time = iTime(Symbol(),PERIOD_D1,1);
  917. end_Time = iTime(Symbol(),PERIOD_D1,0);
  918. }
  919. else
  920. {
  921. start_Time = newYorkStartTrading;
  922. end_Time = newYorkEndTrading;
  923. if(TimeCurrent() >= newYorkEndTrading && newYorkEndTrading != 0)
  924. {
  925. start_Time = iTime(Symbol(),PERIOD_D1,0);
  926. end_Time = start_Time + 86400;
  927. }
  928. }
  929. }
  930. else
  931. {
  932. start_Time = iTime(Symbol(),PERIOD_D1,0);
  933. end_Time = iTime(Symbol(),PERIOD_D1,0);
  934. }
  935. if(TimeToStruct(end_Time,edate))
  936. {
  937. edate.hour = newYorkEndHour;
  938. edate.min = newYorkEndMin;
  939. edate.sec = 0;
  940. }
  941. else
  942. Print("Error in Converting Time: ",GetLastError());
  943. newYorkEndTrading = StructToTime(edate);
  944. if(TimeToStruct(start_Time,sdate))
  945. {
  946. sdate.hour = newYorkStartTime;
  947. sdate.min = newYorkStartMin;
  948. sdate.sec = 0;
  949. }
  950. else
  951. Print("Error in Converting Time: ",GetLastError());
  952. newYorkStartTrading = StructToTime(sdate);
  953. // if(onInit)
  954. //Print("NewYork Start Time ",newYorkStartTrading,"End Date: ",newYorkEndTrading);
  955. //Print("Edate: ",edate.hour," ",edate.min," Sdate: ",sdate.hour," ",sdate.min);
  956. }
  957. //+------------------------------------------------------------------+
  958. //| |
  959. //+------------------------------------------------------------------+
  960. void removeFromStructure()
  961. {
  962. for(int i = 0 ; i < MaxOrders ; i++)
  963. {
  964. bool isBuyPresent=false, isSellPresent=false;
  965. if(newTradeStore[i].buy_ticket != -1 && newTradeStore[i].sell_ticket != -1)
  966. {
  967. for(int j = PositionsTotal()-1; j>=0; j--)
  968. {
  969. ulong ticket = PositionGetTicket(j);
  970. if(PositionSelectByTicket(ticket))
  971. {
  972. if(ticket == newTradeStore[i].buy_ticket)
  973. {
  974. isBuyPresent=true;
  975. }
  976. }
  977. }
  978. for(int j = PositionsTotal()-1; j>=0; j--)
  979. {
  980. ulong ticket = PositionGetTicket(j);
  981. if(PositionSelectByTicket(ticket))
  982. {
  983. if(ticket == newTradeStore[i].sell_ticket)
  984. {
  985. isSellPresent = true;
  986. }
  987. }
  988. }
  989. if(!isBuyPresent && !isSellPresent)
  990. {
  991. Print("Buy/Sell Ticket is closed so removed from struct. Buy Ticket: ", newTradeStore[i].buy_ticket, " Sell Ticket: ", newTradeStore[i].sell_ticket);
  992. newTradeStore[i].buy_ticket = (ulong)-1;
  993. newTradeStore[i].sell_ticket = (ulong)-1;
  994. bool buy_virtual_tp_hit = true;
  995. bool sell_virtual_tp_hit = true;
  996. bool demand_level_hit_value = true;
  997. if(bothHitsSl)
  998. {
  999. buy_virtual_tp_hit = false;
  1000. sell_virtual_tp_hit = false;
  1001. if(hitDemandFirst)
  1002. {
  1003. demand_level_hit_value = false;
  1004. }
  1005. }
  1006. newTradeStore[i].buy_hit_virtual_sl = buy_virtual_tp_hit;
  1007. newTradeStore[i].sell_hit_virtual_sl = sell_virtual_tp_hit;
  1008. newTradeStore[i].demand_level_hit = demand_level_hit_value;
  1009. //newTradeStore[i].symbol = "";
  1010. //newTradeStore[i].price = 0.0;
  1011. //newTradeStore[i].stop_loss = 0.0;
  1012. //newTradeStore[i].take_profit = 0.0;
  1013. //newTradeStore[i].start_time = 0;
  1014. //newTradeStore[i].end_time = 0;
  1015. }
  1016. }
  1017. }
  1018. }
  1019. //+------------------------------------------------------------------+
  1020. //| |
  1021. //+------------------------------------------------------------------+
  1022. void virtualSLHitCheck()
  1023. {
  1024. for(int i = 0 ; i < MaxOrders ; i++)
  1025. {
  1026. if(newTradeStore[i].buy_ticket == -1 && newTradeStore[i].sell_ticket == -1)
  1027. {
  1028. if(TimeCurrent() > newTradeStore[i].start_time && TimeCurrent() < newTradeStore[i].end_time)
  1029. {
  1030. if(!newTradeStore[i].demand_level_hit)
  1031. {
  1032. double levelPriceIs = newTradeStore[i].price;
  1033. if((tickPreviousBid > levelPriceIs && tickCurrentBid < levelPriceIs) ||
  1034. (tickPreviousBid < levelPriceIs && tickCurrentBid > levelPriceIs))
  1035. {
  1036. newTradeStore[i].demand_level_hit = true;
  1037. Print(" Demand Level Hit. newTradeStore[i].demand_level_hit: ", newTradeStore[i].demand_level_hit, " Order Price is: ", newTradeStore[i].price, " Time Demand Hit is: ", TimeCurrent());
  1038. }
  1039. }
  1040. if(newTradeStore[i].demand_level_hit || !hitDemandFirst)
  1041. {
  1042. double buy_sl = 0, sell_sl = 0;
  1043. if(!useTpSlPips)
  1044. {
  1045. buy_sl = newTradeStore[i].stop_loss_buy;
  1046. sell_sl = newTradeStore[i].stop_loss_sell;
  1047. }
  1048. else
  1049. {
  1050. buy_sl = stopLoss != 0 ? newTradeStore[i].price - (stopLoss * Point()) : 0;
  1051. sell_sl = stopLoss != 0 ? newTradeStore[i].price + (stopLoss * Point()) : 0;
  1052. }
  1053. if(newTradeStore[i].buy_hit_virtual_sl == false)
  1054. {
  1055. if((tickPreviousBid > buy_sl && tickCurrentBid <= buy_sl))
  1056. {
  1057. newTradeStore[i].buy_hit_virtual_sl = true;
  1058. Print(" Buy Virtual Sl Hit. newTradeStore[i].buy_hit_virtual_sl: ", newTradeStore[i].buy_hit_virtual_sl, " Order Price is: ", newTradeStore[i].price, " Stop Loss Price is: ", buy_sl, " Time Virtual Sl Hit is: ", TimeCurrent());
  1059. }
  1060. }
  1061. if(newTradeStore[i].sell_hit_virtual_sl == false)
  1062. {
  1063. if((tickPreviousAsk < sell_sl && tickCurrentAsk >= sell_sl))
  1064. {
  1065. newTradeStore[i].sell_hit_virtual_sl = true;
  1066. Print(" Sell Virtual Sl Hit. newTradeStore[i].sell_hit_virtual_sl: ", newTradeStore[i].sell_hit_virtual_sl, " Order Price is: ", newTradeStore[i].price, " Stop Loss Price is: ", sell_sl, " Time Virtual Sl Hit is: ", TimeCurrent());
  1067. }
  1068. }
  1069. }
  1070. }
  1071. }
  1072. }
  1073. }
  1074. //+------------------------------------------------------------------+
  1075. //| |
  1076. //+------------------------------------------------------------------+
  1077. int countLiveTrades()
  1078. {
  1079. int count = 0;
  1080. for(int i = 0; i < PositionsTotal(); i++)
  1081. {
  1082. ulong ticket = PositionGetTicket(i);
  1083. if(PositionSelectByTicket(ticket))
  1084. {
  1085. if(PositionGetInteger(POSITION_MAGIC) == magic_no)
  1086. {
  1087. if(PositionGetString(POSITION_SYMBOL) == Symbol())
  1088. {
  1089. if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY || PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
  1090. {
  1091. count++;
  1092. }
  1093. }
  1094. }
  1095. }
  1096. }
  1097. return count;
  1098. }
  1099. //+------------------------------------------------------------------+
  1100. //| |
  1101. //+------------------------------------------------------------------+
  1102. bool spreadFilter()
  1103. {
  1104. long spreadIs = SymbolInfoInteger(Symbol(), SYMBOL_SPREAD);
  1105. if(spreadIs > maximumSpread)
  1106. {
  1107. return false;
  1108. }
  1109. return true;
  1110. }
  1111. //+------------------------------------------------------------------+
  1112. //| |
  1113. //+------------------------------------------------------------------+
  1114. void breakEven()
  1115. {
  1116. for(int i = PositionsTotal()-1; i>=0; i--)
  1117. {
  1118. ulong ticket = PositionGetTicket(i);
  1119. string symbol=PositionGetSymbol(i);
  1120. double mySL = 0,newSL = 0;
  1121. double SymbolTickSize = SymbolInfoDouble(symbol, SYMBOL_TRADE_TICK_SIZE);
  1122. ////Print("ticket ",ticket," Symbol : ",symbol);
  1123. if(PositionSelectByTicket(ticket))
  1124. {
  1125. if(PositionGetString(POSITION_SYMBOL)==Symbol() && PositionGetInteger(POSITION_MAGIC) == magic_no)
  1126. {
  1127. if(isCounterpartOpen(ticket))
  1128. {
  1129. // counterpart still open -> skip trailing for this position
  1130. continue;
  1131. }
  1132. //========================================================Buy Condition=========================================================================
  1133. if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
  1134. {
  1135. mySL = PositionGetDouble(POSITION_PRICE_OPEN) + (breakEvenPoints * SymbolTickSize);
  1136. if(SymbolInfoDouble(Symbol(),SYMBOL_BID) >= mySL && PositionGetDouble(POSITION_PRICE_OPEN) > PositionGetDouble(POSITION_SL))
  1137. {
  1138. newSL= PositionGetDouble(POSITION_PRICE_OPEN) + (breakStopPoint * SymbolTickSize);
  1139. if(trade.PositionModify(ticket,newSL,PositionGetDouble(POSITION_TP)))
  1140. {
  1141. Print("Buy Order BreakEven Successfully ");
  1142. }
  1143. else
  1144. {
  1145. Print("Error in BreakEven Buy Position ",GetLastError());
  1146. }
  1147. }
  1148. }
  1149. //=======================================================Sell condition ===============================================================
  1150. if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
  1151. {
  1152. mySL = PositionGetDouble(POSITION_PRICE_OPEN) - (breakEvenPoints * SymbolTickSize);
  1153. if(SymbolInfoDouble(Symbol(),SYMBOL_ASK) <= mySL && PositionGetDouble(POSITION_SL) > PositionGetDouble(POSITION_PRICE_OPEN))
  1154. {
  1155. newSL = PositionGetDouble(POSITION_PRICE_OPEN) - (breakStopPoint * SymbolTickSize);
  1156. if(trade.PositionModify(ticket,newSL,PositionGetDouble(POSITION_TP)))
  1157. {
  1158. Print("Order Sell BreakEven Successfully ");
  1159. }
  1160. else
  1161. {
  1162. Print("Error in BreakEven Sell Position ",GetLastError());
  1163. }
  1164. }
  1165. }
  1166. }
  1167. }
  1168. }
  1169. }
  1170. //+------------------------------------------------------------------+
  1171. //| |
  1172. //+------------------------------------------------------------------+
  1173. bool isCounterpartOpen(ulong ticket)
  1174. {
  1175. // scan stored pairs
  1176. for(int k=0; k<MaxOrders; k++)
  1177. {
  1178. // check if this ticket is the buy side in the pair
  1179. if(newTradeStore[k].buy_ticket == ticket)
  1180. {
  1181. ulong other = newTradeStore[k].sell_ticket;
  1182. if(other <= 0)
  1183. return false; // no counterpart recorded -> treat as closed
  1184. // check if counterpart ticket exists in current positions
  1185. for(int p = PositionsTotal()-1; p >= 0; p--)
  1186. {
  1187. ulong t = PositionGetTicket(p);
  1188. if(t == other)
  1189. return true; // counterpart still open
  1190. }
  1191. return false; // counterpart not found -> closed
  1192. }
  1193. // check if this ticket is the sell side in the pair
  1194. if(newTradeStore[k].sell_ticket == ticket)
  1195. {
  1196. ulong other = newTradeStore[k].buy_ticket;
  1197. if(other <= 0)
  1198. return false; // no counterpart recorded -> treat as closed
  1199. for(int p = PositionsTotal()-1; p >= 0; p--)
  1200. {
  1201. ulong t = PositionGetTicket(p);
  1202. if(t == other)
  1203. return true; // counterpart still open
  1204. }
  1205. return false; // counterpart not found -> closed
  1206. }
  1207. }
  1208. // ticket not found in the stored pairs -> treat as no counterpart open
  1209. return false;
  1210. }
  1211. //+------------------------------------------------------------------+
  1212. //| |
  1213. //+------------------------------------------------------------------+
  1214. void Individual_Trailing()
  1215. {
  1216. int openedpositions;
  1217. double mySL,myResult;
  1218. openedpositions=PositionsTotal();
  1219. if((openedpositions>0))
  1220. {
  1221. int totalorders=PositionsTotal();
  1222. for(int i=0; i<totalorders; i++) // scan all orders and positions. ..
  1223. {
  1224. ulong ticket = PositionGetTicket(i);
  1225. if(PositionSelectByTicket(ticket))
  1226. {
  1227. if(PositionGetString(POSITION_SYMBOL)==Symbol() && PositionGetInteger(POSITION_MAGIC) == magic_no) // only look if mygrid and symbol. ..
  1228. {
  1229. if(isCounterpartOpen(ticket))
  1230. {
  1231. // counterpart still open -> skip trailing for this position
  1232. continue;
  1233. }
  1234. double SymbolAsk = SymbolInfoDouble(PositionGetString(POSITION_SYMBOL), SYMBOL_ASK);
  1235. double SymbolBid = SymbolInfoDouble(PositionGetString(POSITION_SYMBOL), SYMBOL_BID);
  1236. int SymbolDigits = (int)SymbolInfoInteger(PositionGetString(POSITION_SYMBOL), SYMBOL_DIGITS);
  1237. double SymbolTickSize = SymbolInfoDouble(PositionGetString(POSITION_SYMBOL), SYMBOL_TRADE_TICK_SIZE);
  1238. int type= (int)PositionGetInteger(POSITION_TYPE);
  1239. if(type==POSITION_TYPE_BUY) // its a long position
  1240. {
  1241. mySL=NormalizeDouble(SymbolAsk-(ts*SymbolTickSize),Digits()); // new SL
  1242. double startSl=PositionGetDouble(POSITION_PRICE_OPEN)+(ts_sl*SymbolTickSize);
  1243. if(PositionGetDouble(POSITION_SL) != mySL)
  1244. if(mySL>PositionGetDouble(POSITION_SL) && SymbolAsk>=startSl)
  1245. {
  1246. myResult=trade.PositionModify(ticket,mySL,PositionGetDouble(POSITION_TP)); //OrderModify(OrderTicket(),OrderOpenPrice(),mySL,OrderTakeProfit(),0,clrGreen);
  1247. if(!myResult)
  1248. {
  1249. Print(" Buy Trade Trailing Error: ",GetLastError());
  1250. }
  1251. }
  1252. }
  1253. if(type==POSITION_TYPE_SELL)
  1254. {
  1255. mySL=NormalizeDouble(SymbolBid+(ts*SymbolTickSize),Digits()); // new SL
  1256. double startSlSell=PositionGetDouble(POSITION_PRICE_OPEN)-(ts_sl*SymbolTickSize);
  1257. if(PositionGetDouble(POSITION_SL) != mySL)
  1258. if(((mySL<PositionGetDouble(POSITION_SL)) || (PositionGetDouble(POSITION_SL)<SymbolTickSize)) && SymbolBid<=startSlSell)
  1259. {
  1260. myResult=trade.PositionModify(ticket,mySL,PositionGetDouble(POSITION_TP)); //OrderModify(OrderTicket(),OrderOpenPrice(),mySL,OrderTakeProfit(),0,clrRed);
  1261. if(!myResult)
  1262. {
  1263. Print(" Sell Trade Trailing Error: ",GetLastError());
  1264. }
  1265. }
  1266. }
  1267. }
  1268. }
  1269. }
  1270. }
  1271. }
  1272. //+------------------------------------------------------------------+
  1273. //| |
  1274. //+------------------------------------------------------------------+
  1275. void saveNewTradeStoreFile()
  1276. {
  1277. // don't run in strategy tester
  1278. if(MQLInfoInteger(MQL_TESTER))
  1279. return;
  1280. int fileHandle = FileOpen(file_name,
  1281. FILE_WRITE | FILE_CSV | FILE_COMMON |
  1282. FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_READ);
  1283. if(fileHandle == INVALID_HANDLE)
  1284. {
  1285. PrintFormat("saveNewTradeStoreFile() -> Cannot open file '%s'. Error: %d", file_name, GetLastError());
  1286. return;
  1287. }
  1288. // header (optional) - comment out if you don't want header
  1289. //string header = "buy_ticket,sell_ticket,symbol,price,stop_loss,take_profit,start_time,end_time,buy_hit_virtual_sl,sell_hit_virtual_sl\r\n";
  1290. //FileWriteString(fileHandle, header);
  1291. for(int i = 0; i < MaxOrders; i++)
  1292. {
  1293. // if(newTradeStore[i].buy_ticket != -1 && newTradeStore[i].sell_ticket != -1)
  1294. {
  1295. // decide whether this slot has useful data:
  1296. // you can tweak this condition as you prefer (symbol != "" is simple)
  1297. bool slotPopulated = (StringLen(newTradeStore[i].symbol) > 0)
  1298. || (newTradeStore[i].price != 0.0)
  1299. || (newTradeStore[i].start_time != 0);
  1300. if(!slotPopulated)
  1301. continue;
  1302. // convert values to strings
  1303. string buyTicketStr = IntegerToString((long)newTradeStore[i].buy_ticket);
  1304. string sellTicketStr = IntegerToString((long)newTradeStore[i].sell_ticket);
  1305. string priceStr = DoubleToString(newTradeStore[i].price, Digits());
  1306. string slBuyStr = DoubleToString(newTradeStore[i].stop_loss_buy, Digits());
  1307. string tpBuyStr = DoubleToString(newTradeStore[i].take_profit_buy, Digits());
  1308. string slSellStr = DoubleToString(newTradeStore[i].stop_loss_sell, Digits());
  1309. string tpSellStr = DoubleToString(newTradeStore[i].take_profit_sell, Digits());
  1310. string startTimeStr = (newTradeStore[i].start_time != 0) ? TimeToString(newTradeStore[i].start_time, TIME_DATE|TIME_SECONDS) : "";
  1311. string endTimeStr = (newTradeStore[i].end_time != 0) ? TimeToString(newTradeStore[i].end_time, TIME_DATE|TIME_SECONDS) : "";
  1312. string buyHitStr = ""; // IntegerToString(newTradeStore[i].buy_hit_virtual_sl ? 1 : 0);
  1313. string sellHitStr = ""; // IntegerToString(newTradeStore[i].sell_hit_virtual_sl ? 1 : 0);
  1314. string demandHitStr = "";
  1315. if(newTradeStore[i].buy_hit_virtual_sl == true)
  1316. {
  1317. buyHitStr = "true";
  1318. }
  1319. else
  1320. {
  1321. buyHitStr = "false";
  1322. }
  1323. if(newTradeStore[i].sell_hit_virtual_sl == true)
  1324. {
  1325. sellHitStr = "true";
  1326. }
  1327. else
  1328. {
  1329. sellHitStr = "false";
  1330. }
  1331. if(newTradeStore[i].demand_level_hit == true)
  1332. {
  1333. demandHitStr = "true";
  1334. }
  1335. else
  1336. {
  1337. demandHitStr = "false";
  1338. }
  1339. // build CSV line and write
  1340. string line = buyTicketStr + "," + sellTicketStr + "," +
  1341. newTradeStore[i].symbol + "," +
  1342. priceStr + "," + slBuyStr + "," + tpBuyStr + "," +
  1343. slSellStr + "," + tpSellStr + "," +
  1344. startTimeStr + "," + endTimeStr + "," +
  1345. demandHitStr + "," + buyHitStr + "," + sellHitStr + "\r\n";
  1346. FileWriteString(fileHandle, line);
  1347. }
  1348. }
  1349. FileClose(fileHandle);
  1350. PrintFormat("saveNewTradeStoreFile() -> saved to '%s'.", file_name);
  1351. }
  1352. //+------------------------------------------------------------------+
  1353. //| |
  1354. //+------------------------------------------------------------------+
  1355. void loadNewTradeStoreFile()
  1356. {
  1357. if(MQLInfoInteger(MQL_TESTER))
  1358. return;
  1359. int fileHandle = FileOpen(file_name,
  1360. FILE_READ | FILE_CSV | FILE_COMMON |
  1361. FILE_SHARE_READ | FILE_SHARE_WRITE);
  1362. if(fileHandle == INVALID_HANDLE)
  1363. {
  1364. Print("loadNewTradeStoreFile() -> Cannot open file '", file_name, "'. Error: ", GetLastError());
  1365. return;
  1366. }
  1367. // reset defaults
  1368. for(int j = 0; j < MaxOrders; j++)
  1369. {
  1370. newTradeStore[j].buy_ticket = (ulong)-1;
  1371. newTradeStore[j].sell_ticket = (ulong)-1;
  1372. newTradeStore[j].symbol = "";
  1373. newTradeStore[j].price = 0.0;
  1374. newTradeStore[j].stop_loss_buy = 0.0;
  1375. newTradeStore[j].take_profit_buy = 0.0;
  1376. newTradeStore[j].stop_loss_sell = 0.0;
  1377. newTradeStore[j].take_profit_sell = 0.0;
  1378. newTradeStore[j].start_time = 0;
  1379. newTradeStore[j].end_time = 0;
  1380. newTradeStore[j].demand_level_hit = false;
  1381. newTradeStore[j].buy_hit_virtual_sl = false;
  1382. newTradeStore[j].sell_hit_virtual_sl = false;
  1383. }
  1384. int idx = 0;
  1385. while(!FileIsEnding(fileHandle) && idx < MaxOrders)
  1386. {
  1387. string line = FileReadString(fileHandle);
  1388. if(StringLen(line) == 0)
  1389. continue;
  1390. string tokens[];
  1391. int total = StringSplit(line, ',', tokens);
  1392. if(total >= 13)
  1393. {
  1394. // if((ulong)tokens[0] != -1 && (ulong)tokens[1] != -1)
  1395. {
  1396. newTradeStore[idx].buy_ticket = (ulong)tokens[0];
  1397. newTradeStore[idx].sell_ticket = (ulong)tokens[1];
  1398. newTradeStore[idx].symbol = tokens[2];
  1399. newTradeStore[idx].price = StringToDouble(tokens[3]);
  1400. newTradeStore[idx].stop_loss_buy = StringToDouble(tokens[4]);
  1401. newTradeStore[idx].take_profit_buy = StringToDouble(tokens[5]);
  1402. newTradeStore[idx].stop_loss_sell = StringToDouble(tokens[6]);
  1403. newTradeStore[idx].take_profit_sell = StringToDouble(tokens[7]);
  1404. string sStart = tokens[8];
  1405. string sEnd = tokens[9];
  1406. newTradeStore[idx].start_time = (StringLen(sStart) > 0) ? StringToTime(sStart) : 0;
  1407. newTradeStore[idx].end_time = (StringLen(sEnd) > 0) ? StringToTime(sEnd) : 0;
  1408. bool bHit = false;
  1409. bool sHit = false;
  1410. bool dHit = false;
  1411. if(tokens[10] == "true")
  1412. {
  1413. dHit = true;
  1414. }
  1415. else
  1416. {
  1417. dHit = false;
  1418. }
  1419. if(tokens[11] == "true")
  1420. {
  1421. bHit = true;
  1422. }
  1423. else
  1424. {
  1425. bHit = false;
  1426. }
  1427. if(tokens[12] == "true")
  1428. {
  1429. sHit = true;
  1430. }
  1431. else
  1432. {
  1433. sHit = false;
  1434. }
  1435. if(bothHitsSl == false)
  1436. {
  1437. newTradeStore[idx].buy_hit_virtual_sl = true;
  1438. newTradeStore[idx].sell_hit_virtual_sl = true;
  1439. newTradeStore[idx].demand_level_hit = true;
  1440. }
  1441. if(bothHitsSl == true)
  1442. {
  1443. newTradeStore[idx].buy_hit_virtual_sl = bHit;
  1444. newTradeStore[idx].sell_hit_virtual_sl = sHit;
  1445. if(hitDemandFirst)
  1446. {
  1447. newTradeStore[idx].demand_level_hit = dHit;
  1448. }
  1449. else
  1450. {
  1451. newTradeStore[idx].demand_level_hit = true;
  1452. }
  1453. }
  1454. // --- simple Print instead of PrintFormat ---
  1455. Print(
  1456. "Loaded newTradeStore[", idx, "]:",
  1457. " buy_ticket=", newTradeStore[idx].buy_ticket,
  1458. " sell_ticket=", newTradeStore[idx].sell_ticket,
  1459. " symbol=", newTradeStore[idx].symbol,
  1460. " price=", DoubleToString(newTradeStore[idx].price, Digits()),
  1461. " \n sl buy=", DoubleToString(newTradeStore[idx].stop_loss_buy, Digits()),
  1462. " tp buy=", DoubleToString(newTradeStore[idx].take_profit_buy, Digits()),
  1463. " sl sell=", DoubleToString(newTradeStore[idx].stop_loss_sell, Digits()),
  1464. " tp sell=", DoubleToString(newTradeStore[idx].take_profit_sell, Digits()),
  1465. " start=", (newTradeStore[idx].start_time != 0 ? TimeToString(newTradeStore[idx].start_time, TIME_DATE|TIME_SECONDS) : ""),
  1466. " end=", (newTradeStore[idx].end_time != 0 ? TimeToString(newTradeStore[idx].end_time, TIME_DATE|TIME_SECONDS) : ""),
  1467. " demandHit=", newTradeStore[idx].demand_level_hit,
  1468. " buyHit=", newTradeStore[idx].buy_hit_virtual_sl,
  1469. " sellHit=", newTradeStore[idx].sell_hit_virtual_sl
  1470. );
  1471. idx++;
  1472. }
  1473. }
  1474. else
  1475. {
  1476. Print("loadNewTradeStoreFile(): skipping malformed line (tokens=", total, "): ", line);
  1477. }
  1478. }
  1479. FileClose(fileHandle);
  1480. Print("loadNewTradeStoreFile() -> loaded ", idx, " entries from '", file_name, "'.");
  1481. }
  1482. //+------------------------------------------------------------------+
  1483. //| |
  1484. //+------------------------------------------------------------------+
  1485. //+------------------------------------------------------------------+