Nav apraksta

vol_hedge_strategy_mt5.mq5 55KB

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