Няма описание

vol_hedge_strategy_mt5.mq5 55KB

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