Bez popisu

localHedgingClientCopierMt5.mq5 70KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700
  1. //+------------------------------------------------------------------+
  2. //| localHedgingClientCopierMt5.mq5 |
  3. //| Copyright 2025, MetaQuotes Ltd. |
  4. //| https://www.mqldevelopment.com/ |
  5. //+------------------------------------------------------------------+
  6. #property copyright "Copyright 2025, MetaQuotes Ltd."
  7. #property link "https://www.mqldevelopment.com/"
  8. #property version "1.00"
  9. #define orderCount 2000
  10. #define count1 50
  11. #define testCounter 50
  12. #define MaxOrders 20000
  13. #define daysOfTradesToAdd 20
  14. #include <Trade\Trade.mqh>
  15. CTrade trade;
  16. //+------------------------------------------------------------------+
  17. //| Expert initialization function |
  18. //+------------------------------------------------------------------+
  19. struct historyTradesClientSide
  20. {
  21. ulong ticket;
  22. ulong magic;
  23. historyTradesClientSide()
  24. {
  25. ticket = -1;
  26. magic = 0;
  27. }
  28. };
  29. historyTradesClientSide historyTradeStore[MaxOrders];
  30. struct order
  31. {
  32. ulong ticket;
  33. double price;
  34. datetime opentime;
  35. string symbol;
  36. int ordertype;
  37. double lots;
  38. double stoploss;
  39. double takeprofit;
  40. double accountBalance;
  41. int contractSize;
  42. string comments;
  43. int magicNo;
  44. double closedPercentageOfTrade;
  45. order()
  46. {
  47. ticket =-1;
  48. closedPercentageOfTrade = 0;
  49. }
  50. };
  51. struct clientOrder
  52. {
  53. ulong ticket;
  54. double price;
  55. datetime opentime;
  56. string symbol;
  57. int ordertype;
  58. double lots;
  59. double stoploss;
  60. double takeprofit;
  61. ulong magic;
  62. string comments;
  63. double masterLot;
  64. clientOrder()
  65. {
  66. ticket=-1;
  67. price=-1;
  68. symbol=Symbol();
  69. ordertype=-1;
  70. lots=0;
  71. masterLot=0;
  72. stoploss=0;
  73. takeprofit=0;
  74. magic=0;
  75. comments="";
  76. }
  77. };
  78. clientOrder cod[orderCount];
  79. struct masterOrder
  80. {
  81. datetime opentime;
  82. string symbol;
  83. ulong ordertype;
  84. double price;
  85. double lots;
  86. double stoploss;
  87. double takeprofit;
  88. ulong ticket;
  89. int magic;
  90. double accountBalance;
  91. int contractSize;
  92. string comment;
  93. masterOrder()
  94. {
  95. ticket=-1;
  96. price=-1;
  97. symbol=Symbol();
  98. ordertype=-1;
  99. lots=0;
  100. stoploss=0;
  101. takeprofit=0;
  102. }
  103. };
  104. masterOrder mod1[orderCount];
  105. enum lot_option
  106. {
  107. fix, //Fixed Lot
  108. mul, //Multiplied Lot
  109. // Balance, // Balance Base Multiplier
  110. slaveLotCal, // Slave Lot formula based
  111. };
  112. enum tradeCopy_type
  113. {
  114. buy,//Buy Only
  115. sell,//Sell Only
  116. buySell,//Buy and Sell
  117. };
  118. enum operation_code
  119. {
  120. Master, //Master
  121. Client, //Client
  122. };
  123. sinput string string_0 = "<><><><><><> General SETTINGS <><><><><><>"; //__
  124. input int slip = 5; // Master Order Variation in Pips
  125. input int sec = 10; // Master Order Expired After Seconds
  126. input tradeCopy_type tradeType = buySell; // Trades To Copy
  127. input string suffix = ""; // Suffix
  128. input string symbolExclude = ""; // Symbol to Exclude
  129. input string prefix = ""; // Prefix
  130. input string mappingSymbol = "GOLD:XAUUSD,bitcoin:BTCUSD"; // Mapping Pairs Inp:Out,Inp:Out
  131. sinput string string_1 = "<><><><><><> Lotsize SETTINGS <><><><><><>"; //__
  132. input lot_option lotoption = mul; // Lot Option
  133. input double fixed = 0.1; // Fixed Lot (for Client)
  134. input double mult = 1.0; // Multiplier (for Client)
  135. double Bmult = 1; // Balance Multiplier (for Client)
  136. input double recoveryAmount = 3000; // Recovery Amount (for Client Lot)
  137. input double maxDd = 8000; // Max drawdown prop (for Client Lot)
  138. //+------------------------------------------------------------------+
  139. //| |
  140. //+------------------------------------------------------------------+
  141. //Global Variable
  142. int gmtDifference = 0;
  143. int filehandle;
  144. // Heart Beat
  145. long lastFileSize=0;
  146. datetime lastModified=0;
  147. bool contractChk = true;//Contract Checking
  148. bool doReversal = true; // Reverse Trades
  149. bool fileLastEmpty=false;
  150. long accountNumber=AccountInfoInteger(ACCOUNT_LOGIN);
  151. order order_array[];
  152. datetime lastModified1=0;
  153. long lastFileSize1=0;
  154. bool fileHandlerIssue=false;
  155. operation_code mode1=Client; //Working code
  156. int OnInit()
  157. {
  158. //---
  159. accountNumber=AccountInfoInteger(ACCOUNT_LOGIN);
  160. EventSetMillisecondTimer(20);
  161. if(!ObjectFind(0,"mode") || ObjectGetString(0,"mode",OBJPROP_TEXT,0)!=EnumToString(mode1)+" Account")
  162. {
  163. ObjectDelete(0,"mode");
  164. ObjectCreate(0,"mode",OBJ_LABEL,0,0,0);
  165. ObjectSetInteger(0,"mode",OBJPROP_XDISTANCE,10);
  166. ObjectSetInteger(0,"mode",OBJPROP_YDISTANCE,25);
  167. ObjectSetInteger(0,"mode",OBJPROP_CORNER,CORNER_RIGHT_UPPER);
  168. ObjectSetInteger(0,"mode",OBJPROP_ANCHOR,ANCHOR_RIGHT_UPPER);
  169. ObjectSetInteger(0,"mode",OBJPROP_COLOR,clrLightBlue);
  170. ObjectSetString(0,"mode",OBJPROP_TEXT,EnumToString(mode1)+" Account");
  171. ObjectSetInteger(0,"mode",OBJPROP_FONTSIZE,14);
  172. }
  173. gmtDifference = (int)TimeCurrent() - (int)TimeGMT();
  174. Print("GMT Difference(sec): ",gmtDifference);
  175. mapHistory();
  176. //---
  177. return(INIT_SUCCEEDED);
  178. }
  179. //+------------------------------------------------------------------+
  180. //| Expert deinitialization function |
  181. //+------------------------------------------------------------------+
  182. void OnDeinit(const int reason)
  183. {
  184. //---
  185. EventKillTimer();
  186. ObjectsDeleteAll(0,0,-1);
  187. }
  188. //+------------------------------------------------------------------+
  189. //| Expert tick function |
  190. //+------------------------------------------------------------------+
  191. void OnTick()
  192. {
  193. //---
  194. }
  195. //+------------------------------------------------------------------+
  196. void OnTimer()
  197. {
  198. gmtDifference = (int)TimeCurrent() - (int)TimeGMT();
  199. read_history_file();
  200. readData();
  201. writeDataClientCof();
  202. }
  203. //+------------------------------------------------------------------+
  204. //| |
  205. //+------------------------------------------------------------------+
  206. void writeDataClientCof()
  207. {
  208. int fileHandleClientCof;
  209. if(PositionsTotal()==0 && OrdersTotal()==0 && !fileLastEmpty && TerminalInfoInteger(TERMINAL_CONNECTED))
  210. {
  211. fileHandleClientCof = FileOpen("COF_client.csv", FILE_WRITE|FILE_CSV|FILE_COMMON);
  212. if(fileHandleClientCof != INVALID_HANDLE)
  213. {
  214. Print("Writing file to empty");
  215. FileWrite(fileHandleClientCof, "");
  216. FileClose(fileHandleClientCof);
  217. fileLastEmpty = true;
  218. emptyStruct();
  219. }
  220. else
  221. {
  222. Print(" Client Cof File handler issue:", GetLastError());
  223. }
  224. }
  225. else
  226. {
  227. bool updationReq = checkOrderUpdate();
  228. if(updationReq || fileHandlerIssue)
  229. {
  230. Print(" ------------------------------- Client Cof Start ------------------------------------------ ");
  231. fileHandleClientCof = FileOpen("COF_client.csv", FILE_WRITE|FILE_CSV|FILE_COMMON);
  232. if(fileHandleClientCof != INVALID_HANDLE)
  233. {
  234. fileHandlerIssue = false;
  235. for(int i = PositionsTotal()-1; i >= 0; i--)
  236. {
  237. ulong ticket = PositionGetTicket(i);
  238. if(PositionSelectByTicket(ticket))
  239. {
  240. string time = TimeToString(PositionGetInteger(POSITION_TIME)-gmtDifference,TIME_DATE|TIME_SECONDS);
  241. string symbol = PositionGetString(POSITION_SYMBOL);
  242. double digits=getDoubleSymbolInfo(symbol,"digits");
  243. int symbolContractSize=(int)getDoubleSymbolInfo(symbol,"contract");
  244. ulong tempTicket = PositionGetInteger(POSITION_TICKET);
  245. if(suffix != "")
  246. symbol = StringSubstr(symbol, 0, StringFind(symbol, suffix, 0));
  247. if(prefix != "")
  248. StringReplace(symbol, prefix, "");
  249. long orderType = PositionGetInteger(POSITION_TYPE);
  250. double takeProfit = PositionGetDouble(POSITION_TP), stopLoss = PositionGetDouble(POSITION_SL);
  251. if(doReversal)
  252. {
  253. if(orderType == POSITION_TYPE_BUY)
  254. orderType = POSITION_TYPE_SELL;
  255. else
  256. if(orderType == POSITION_TYPE_SELL)
  257. orderType = POSITION_TYPE_BUY;
  258. takeProfit = PositionGetDouble(POSITION_SL);
  259. stopLoss = PositionGetDouble(POSITION_TP);
  260. }
  261. double sumOfClosed = dealOutLotsCheck(0, PositionGetInteger(POSITION_TICKET));
  262. double currentLot = PositionGetDouble(POSITION_VOLUME);
  263. double totalLot = sumOfClosed + currentLot;
  264. double closedPercentage = 0.0;
  265. if(totalLot > 0.0)
  266. closedPercentage = (sumOfClosed / totalLot) * 100.0;
  267. Print(" Client Closed % = ", closedPercentage);
  268. string OrderToWrite = time+","+symbol+","+IntegerToString(orderType)+","+DoubleToString(PositionGetDouble(POSITION_PRICE_OPEN), (int)digits)+","+DoubleToString(PositionGetDouble(POSITION_VOLUME),2)+","+DoubleToString(stopLoss,(int) digits)+","+DoubleToString(takeProfit, (int) digits)+","+IntegerToString(PositionGetInteger(POSITION_TICKET))+","+IntegerToString(PositionGetInteger(POSITION_MAGIC))+","+(string)AccountInfoDouble(ACCOUNT_BALANCE)+","+DoubleToString(closedPercentage, 1)+","+(string)SymbolInfoDouble(PositionGetString(POSITION_SYMBOL),SYMBOL_TRADE_CONTRACT_SIZE)+PositionGetString(POSITION_COMMENT)+"\r\n";
  269. FileWrite(filehandle,OrderToWrite);
  270. }
  271. }
  272. for(int i = OrdersTotal()-1; i >= 0; i--)
  273. {
  274. ulong ticket = OrderGetTicket(i);
  275. if(OrderSelect(ticket))
  276. {
  277. string time = TimeToString(OrderGetInteger(ORDER_TIME_SETUP)-gmtDifference, TIME_DATE|TIME_SECONDS);
  278. string symbol = OrderGetString(ORDER_SYMBOL);
  279. double digits = getDoubleSymbolInfo(symbol,"digits");
  280. if(suffix != "")
  281. symbol = StringSubstr(symbol, 0, StringFind(symbol, suffix, 0));
  282. if(prefix != "")
  283. StringReplace(symbol, prefix, "");
  284. long orderType = OrderGetInteger(ORDER_TYPE);
  285. double takeProfit = OrderGetDouble(ORDER_TP);
  286. double stopLoss = OrderGetDouble(ORDER_SL);
  287. if(doReversal)
  288. {
  289. if(orderType == ORDER_TYPE_BUY_STOP)
  290. orderType = ORDER_TYPE_SELL_LIMIT;
  291. else
  292. if(orderType == ORDER_TYPE_SELL_STOP)
  293. orderType = ORDER_TYPE_BUY_LIMIT;
  294. else
  295. if(orderType == ORDER_TYPE_BUY_LIMIT)
  296. orderType = ORDER_TYPE_SELL_STOP;
  297. else
  298. if(orderType == ORDER_TYPE_SELL_LIMIT)
  299. orderType = ORDER_TYPE_BUY_STOP;
  300. takeProfit = OrderGetDouble(ORDER_SL);
  301. stopLoss = OrderGetDouble(ORDER_TP);
  302. }
  303. double closedPercentage = 0;
  304. string OrderToWrite =
  305. time + "," +
  306. symbol + "," +
  307. IntegerToString(orderType) + "," +
  308. DoubleToString(OrderGetDouble(ORDER_PRICE_OPEN),(int)digits) + "," +
  309. DoubleToString(OrderGetDouble(ORDER_VOLUME_CURRENT),2) + "," +
  310. DoubleToString(stopLoss,(int)digits) + "," +
  311. DoubleToString(takeProfit,(int)digits) + "," +
  312. IntegerToString(OrderGetInteger(ORDER_TICKET)) + "," +
  313. IntegerToString(OrderGetInteger(ORDER_MAGIC)) + "," +
  314. (string)AccountInfoDouble(ACCOUNT_BALANCE) + "," +
  315. DoubleToString(closedPercentage) +","+
  316. (string)SymbolInfoDouble(OrderGetString(ORDER_SYMBOL), SYMBOL_TRADE_CONTRACT_SIZE) +
  317. OrderGetString(ORDER_COMMENT) + "\r\n";
  318. Print("Client Cof Writing Pending Order Block.");
  319. FileWrite(fileHandleClientCof, OrderToWrite);
  320. }
  321. }
  322. FileClose(fileHandleClientCof);
  323. fileLastEmpty = false;
  324. Print("Client Cof File Updated:", TimeCurrent());
  325. }
  326. else
  327. {
  328. Print("Client Cof File Handle Issue:", GetLastError());
  329. }
  330. Print(" ------------------------------- Client Cof End ------------------------------------------ ");
  331. }
  332. }
  333. }
  334. //+------------------------------------------------------------------+
  335. //| |
  336. //+------------------------------------------------------------------+
  337. bool checkOrderUpdate()
  338. {
  339. bool updationRequired=false,currCheck=false;
  340. for(int i = PositionsTotal()-1 ; i >= 0 ; i--)
  341. {
  342. ulong ticket = PositionGetTicket(i);
  343. if(PositionSelectByTicket(ticket))
  344. {
  345. int orderIndex=-1;
  346. bool alreadyAdded=isOrderThere(ticket, orderIndex);
  347. if(!alreadyAdded)
  348. {
  349. currCheck=false;
  350. currCheck=addOrderMaster(ticket, "Market");
  351. if(!updationRequired)
  352. updationRequired=currCheck;
  353. }
  354. else
  355. {
  356. if(orderIndex!=-1)
  357. {
  358. currCheck=false;
  359. currCheck=IsOrderChanged(PositionGetInteger(POSITION_TICKET),orderIndex, "Market");
  360. if(!updationRequired)
  361. {
  362. updationRequired=currCheck;
  363. }
  364. }
  365. }
  366. }
  367. }
  368. for(int i = OrdersTotal()-1 ; i >= 0 ; i--)
  369. {
  370. ulong ticket = OrderGetTicket(i);
  371. if(OrderSelect(ticket))
  372. {
  373. if(OrderGetInteger(ORDER_TYPE) != ORDER_TYPE_BUY && OrderGetInteger(ORDER_TYPE) != ORDER_TYPE_SELL && OrderGetInteger(ORDER_TYPE) != ORDER_TYPE_CLOSE_BY)
  374. {
  375. int orderIndex=-1;
  376. bool alreadyAdded=isOrderThere(ticket, orderIndex);
  377. if(!alreadyAdded)
  378. {
  379. Print("Inside pending Adding to master order: ",ticket," type: ",OrderGetInteger(ORDER_TYPE)," ",OrdersTotal()," ",ORDER_TYPE_CLOSE_BY);
  380. currCheck=false;
  381. currCheck=addOrderMaster(ticket, "Pending");
  382. if(!updationRequired)
  383. updationRequired=currCheck;
  384. }
  385. else
  386. {
  387. if(orderIndex!=-1)
  388. {
  389. currCheck=false;
  390. currCheck=IsOrderChanged(OrderGetInteger(ORDER_TICKET), orderIndex, "Pending");
  391. if(!updationRequired)
  392. {
  393. updationRequired=currCheck;
  394. }
  395. }
  396. }
  397. }
  398. }
  399. }
  400. for(int i=0; i<ArraySize(mod1); i++)
  401. {
  402. bool isPresent=false;
  403. if(mod1[i].ticket !=-1)
  404. {
  405. for(int j = PositionsTotal()-1; j>=0; j--)
  406. {
  407. ulong ticket = PositionGetTicket(j);
  408. if(PositionSelectByTicket(ticket))
  409. {
  410. if(ticket==mod1[i].ticket)
  411. {
  412. isPresent=true;
  413. break;
  414. }
  415. }
  416. }
  417. // Pending Order
  418. for(int j = OrdersTotal()-1; j>=0; j--)
  419. {
  420. ulong ticket = OrderGetTicket(j);
  421. if(OrderSelect(ticket))
  422. {
  423. if(ticket==mod1[i].ticket)
  424. {
  425. isPresent=true;
  426. break;
  427. }
  428. }
  429. }
  430. if(!isPresent)
  431. {
  432. Print("Ticket is closed so removed from struct: ",mod1[i].ticket);
  433. mod1[i].ticket=-1;
  434. mod1[i].price=-1;
  435. mod1[i].symbol="";
  436. mod1[i].ordertype=-1;
  437. mod1[i].lots=0;
  438. mod1[i].stoploss=0;
  439. mod1[i].takeprofit=0;
  440. currCheck=true;
  441. //currCheck=clearStructIndex(mod1[i].ticket);
  442. if(!updationRequired)
  443. updationRequired=currCheck;
  444. }
  445. }
  446. }
  447. return updationRequired;
  448. }
  449. //+------------------------------------------------------------------+
  450. //| |
  451. //+------------------------------------------------------------------+
  452. bool IsOrderChanged(ulong orderTicket, int index, string calledBy)
  453. {
  454. if(mod1[index].ticket!=-1)
  455. {
  456. if(calledBy == "Market")
  457. {
  458. if(PositionSelectByTicket(orderTicket))
  459. {
  460. //Digits
  461. double digits=getDoubleSymbolInfo(PositionGetString(POSITION_SYMBOL),"digits");
  462. double tp=NormalizeDouble(PositionGetDouble(POSITION_TP), (int)digits),
  463. sl=NormalizeDouble(PositionGetDouble(POSITION_SL),(int)digits),
  464. lot=NormalizeDouble(PositionGetDouble(POSITION_VOLUME),2);
  465. if(mod1[index].takeprofit!=tp || mod1[index].stoploss != sl || mod1[index].lots!=lot || mod1[index].ordertype!=PositionGetInteger(POSITION_TYPE) || mod1[index].price != PositionGetDouble(POSITION_PRICE_OPEN))
  466. {
  467. Print(" Inside IsOrderChanged() ",mod1[index].lots," lot: ",lot," calledBy: ",calledBy," mod1 ticket ",mod1[index].ticket," orderTicket: ",orderTicket);
  468. // Time
  469. datetime time = (datetime)(PositionGetInteger(POSITION_TIME) - gmtDifference);
  470. mod1[index].opentime=time;
  471. mod1[index].lots=lot;
  472. mod1[index].stoploss=sl;
  473. mod1[index].takeprofit=tp;
  474. mod1[index].comment=PositionGetString(POSITION_COMMENT);
  475. mod1[index].ordertype=PositionGetInteger(POSITION_TYPE);
  476. mod1[index].price=PositionGetDouble(POSITION_PRICE_OPEN);
  477. return true;
  478. }
  479. }
  480. }
  481. if(calledBy == "Pending")
  482. {
  483. if(OrderSelect(orderTicket))
  484. {
  485. double digits=getDoubleSymbolInfo(OrderGetString(ORDER_SYMBOL),"digits");
  486. double tp=NormalizeDouble(OrderGetDouble(ORDER_TP), (int)digits),
  487. sl=NormalizeDouble(OrderGetDouble(ORDER_SL),(int)digits),
  488. lot=NormalizeDouble(OrderGetDouble(ORDER_VOLUME_CURRENT),2);
  489. if(mod1[index].takeprofit!=tp || mod1[index].stoploss != sl || mod1[index].lots!=lot || mod1[index].ordertype!=OrderGetInteger(ORDER_TYPE) || mod1[index].price != OrderGetDouble(ORDER_PRICE_OPEN))
  490. {
  491. //time
  492. datetime time = (datetime)(OrderGetDouble(ORDER_PRICE_OPEN) - gmtDifference);
  493. mod1[index].opentime=time;
  494. mod1[index].lots=lot;
  495. mod1[index].stoploss=sl;
  496. mod1[index].takeprofit=tp;
  497. mod1[index].comment=OrderGetString(ORDER_COMMENT);
  498. mod1[index].ordertype=OrderGetInteger(ORDER_TYPE);
  499. mod1[index].price=OrderGetDouble(ORDER_PRICE_OPEN);
  500. return true;
  501. }
  502. }
  503. }
  504. }
  505. return false;
  506. }
  507. //+------------------------------------------------------------------+
  508. //| |
  509. //+------------------------------------------------------------------+
  510. bool addOrderMaster(ulong ticket, string calledBy)
  511. {
  512. for(int i=0; i<ArraySize(mod1); i++)
  513. {
  514. if(mod1[i].ticket==-1)
  515. {
  516. if(calledBy == "Market")
  517. {
  518. //ulong ticket = PositionGetTicket(orderTicket);
  519. if(PositionSelectByTicket(ticket))
  520. {
  521. //Symbol Updation
  522. string symbol=PositionGetString(POSITION_SYMBOL);
  523. if(suffix!="")
  524. symbol = StringSubstr(symbol,0,StringFind(PositionGetString(POSITION_SYMBOL),suffix,0));
  525. if(prefix!="")
  526. StringReplace(symbol,prefix,"");
  527. //Digits
  528. double digits=getDoubleSymbolInfo(PositionGetString(POSITION_SYMBOL),"digits");
  529. //time
  530. datetime time = (datetime)(PositionGetInteger(POSITION_TIME)-gmtDifference);
  531. mod1[i].opentime=time;
  532. mod1[i].symbol=symbol;
  533. mod1[i].ordertype=PositionGetInteger(POSITION_TYPE);
  534. mod1[i].price=NormalizeDouble(PositionGetDouble(POSITION_PRICE_OPEN),(int)digits);
  535. mod1[i].lots=NormalizeDouble(PositionGetDouble(POSITION_VOLUME),2);
  536. mod1[i].stoploss=NormalizeDouble(PositionGetDouble(POSITION_SL),(int)digits);
  537. mod1[i].takeprofit=NormalizeDouble(PositionGetDouble(POSITION_TP),(int)digits);
  538. mod1[i].ticket=PositionGetInteger(POSITION_TICKET);
  539. mod1[i].magic=(int)PositionGetInteger(POSITION_MAGIC);
  540. mod1[i].comment=PositionGetString(POSITION_COMMENT);
  541. Print("Ticket:",PositionGetInteger(POSITION_TICKET)," stored in index:",i);
  542. return true;
  543. }
  544. }
  545. if(calledBy == "Pending")
  546. {
  547. if(OrderSelect(ticket))
  548. {
  549. string symbol = OrderGetString(ORDER_SYMBOL);
  550. if(suffix!="")
  551. symbol = StringSubstr(symbol,0,StringFind(OrderGetString(ORDER_SYMBOL),suffix,0));
  552. if(prefix!="")
  553. StringReplace(symbol,prefix,"");
  554. //Digits
  555. double digits=getDoubleSymbolInfo(OrderGetString(ORDER_SYMBOL),"digits");
  556. //time
  557. datetime time = (datetime)(OrderGetInteger(ORDER_TIME_SETUP) - gmtDifference);
  558. mod1[i].opentime=time;
  559. mod1[i].symbol=symbol;
  560. mod1[i].ordertype=OrderGetInteger(ORDER_TYPE);
  561. mod1[i].price=NormalizeDouble(OrderGetDouble(ORDER_PRICE_OPEN),(int)digits);
  562. mod1[i].lots=NormalizeDouble(OrderGetDouble(ORDER_VOLUME_CURRENT),2);
  563. mod1[i].stoploss=NormalizeDouble(OrderGetDouble(ORDER_SL),(int)digits);
  564. mod1[i].takeprofit=NormalizeDouble(OrderGetDouble(ORDER_TP),(int)digits);
  565. mod1[i].ticket=OrderGetInteger(ORDER_TICKET);
  566. mod1[i].magic=(int)OrderGetInteger(ORDER_MAGIC);
  567. mod1[i].comment=OrderGetString(ORDER_COMMENT);
  568. Print("Ticket: ",OrderGetInteger(ORDER_TICKET)," stored in index by pending order:",i);
  569. return true;
  570. }
  571. }
  572. }
  573. }
  574. return false;
  575. }
  576. //+------------------------------------------------------------------+
  577. //| |
  578. //+------------------------------------------------------------------+
  579. void readData()
  580. {
  581. if(EnumToString(mode1)=="Client")
  582. {
  583. filehandle=FileOpen("COF.csv",FILE_SHARE_READ|FILE_CSV|FILE_COMMON|FILE_READ|FILE_ANSI|FILE_TXT|FILE_COMMON);
  584. if(filehandle!=INVALID_HANDLE)
  585. {
  586. datetime lastFileModified=(datetime)FileGetInteger(filehandle,FILE_MODIFY_DATE);
  587. long fileSize=FileGetInteger(filehandle,FILE_SIZE);
  588. int a= 0;
  589. if(lastModified==lastFileModified && fileSize==lastFileSize) // lastModified==lastFileModified && fileSize==lastFileSize
  590. {
  591. FileClose(filehandle);
  592. }
  593. else
  594. {
  595. clearOrderArray();
  596. while(!FileIsEnding(filehandle))
  597. {
  598. string OrderToRead = FileReadString(filehandle);
  599. string Order_specs [];
  600. StringSplit(OrderToRead,StringGetCharacter(",",0),Order_specs);
  601. Print("Array Size: ", ArraySize(Order_specs));
  602. if(ArraySize(Order_specs) == 12 || ArraySize(Order_specs) == 13)
  603. {
  604. string symbol = getSymbol(Order_specs[1]);//prefix+Order_specs[1]+suffix;
  605. int orderType=StringToInteger(Order_specs[2]);
  606. if(StringFind(symbol,symbolExclude,0) < 0 &&
  607. (((orderType==POSITION_TYPE_BUY||orderType==ORDER_TYPE_BUY_STOP ||orderType==ORDER_TYPE_SELL_STOP)&& (tradeType==0||tradeType==2)) ||
  608. ((orderType==POSITION_TYPE_SELL||orderType==ORDER_TYPE_BUY_LIMIT ||orderType==ORDER_TYPE_SELL_LIMIT)&& (tradeType==1||tradeType==2)))
  609. )
  610. {
  611. order_array[ArraySize(order_array)-1].symbol = symbol;
  612. order_array[ArraySize(order_array)-1].opentime = StringToTime(Order_specs[0]);
  613. order_array[ArraySize(order_array)-1].price = StringToDouble(Order_specs[3]);
  614. order_array[ArraySize(order_array)-1].ordertype = StringToInteger(Order_specs[2]);
  615. order_array[ArraySize(order_array)-1].lots = StringToDouble(Order_specs[4]);
  616. order_array[ArraySize(order_array)-1].stoploss = StringToDouble(Order_specs[5]);
  617. order_array[ArraySize(order_array)-1].takeprofit = StringToDouble(Order_specs[6]);
  618. order_array[ArraySize(order_array)-1].ticket = StringToInteger(Order_specs[7]);
  619. //Saving master order ticket to order magic number
  620. order_array[ArraySize(order_array)-1].magicNo = StringToInteger(Order_specs[7]);
  621. order_array[ArraySize(order_array)-1].accountBalance=StringToDouble(Order_specs[9]);
  622. order_array[ArraySize(order_array)-1].closedPercentageOfTrade = StringToDouble(Order_specs[10]);
  623. order_array[ArraySize(order_array)-1].contractSize=StringToInteger(Order_specs[11]);
  624. if(ArraySize(Order_specs) == 13)
  625. order_array[ArraySize(order_array)-1].comments=Order_specs[12];
  626. else
  627. order_array[ArraySize(order_array)-1].comments="";
  628. if(ArraySize(Order_specs) == 13)
  629. {
  630. StringReplace(Order_specs[12],"from #","");
  631. StringReplace(Order_specs[12]," ","");
  632. Print(" Order_specs[12] ",Order_specs[12]," order_array[ArraySize(order_array)-1].ticket ",order_array[ArraySize(order_array)-1].ticket);
  633. for(int l=0; l<orderCount; l++)
  634. {
  635. if(cod[l].magic == int(Order_specs[12]))
  636. {
  637. cod[l].magic = order_array[ArraySize(order_array)-1].ticket;
  638. }
  639. }
  640. }
  641. Print(" Symbol: ", order_array[ArraySize(order_array)-1].symbol,
  642. " Open Price: ", order_array[ArraySize(order_array)-1].price,
  643. " Lot: ", order_array[ArraySize(order_array)-1].lots,
  644. " Open time: ", order_array[ArraySize(order_array)-1].opentime,
  645. " Closed %: ", order_array[ArraySize(order_array)-1].closedPercentageOfTrade);
  646. ArrayResize(order_array,ArraySize(order_array)+1);
  647. addToMarketWatch(symbol);
  648. }
  649. }
  650. }
  651. lastModified=lastFileModified;
  652. lastFileSize=fileSize;
  653. FileClose(filehandle);
  654. }
  655. int arraySize=ArraySize(order_array);
  656. if(ArraySize(order_array) == 1) //
  657. {
  658. if(PositionsTotal() > 0)
  659. {
  660. for(int i=PositionsTotal()-1; i>=0; i--)
  661. {
  662. ulong ticket = PositionGetTicket(i);
  663. if(PositionSelectByTicket(ticket))
  664. {
  665. if(history_ticket_present(PositionGetInteger(POSITION_MAGIC)))
  666. {
  667. // Print(" inside Array size 1: ",ticket);
  668. if(!trade.PositionClose(ticket))
  669. {
  670. Print(" Order Close Error on No Master Trade: "+IntegerToString(GetLastError()));
  671. }
  672. else
  673. Print(" ------------- Order Close by empty array ------------------ ");
  674. }
  675. }
  676. }
  677. }
  678. if(OrdersTotal() > 0)
  679. {
  680. for(int i=OrdersTotal()-1; i>=0; i--)
  681. {
  682. ulong ticket = OrderGetTicket(i);
  683. if(OrderSelect(ticket))
  684. {
  685. if(history_ticket_present(OrderGetInteger(ORDER_MAGIC)))
  686. if(!trade.OrderDelete(ticket))
  687. {
  688. Print(" Error in Deleting Pending Order (Empty Array) : "+IntegerToString(GetLastError()));
  689. }
  690. else
  691. Print("Order Close by empty array (Empty Array)");
  692. }
  693. }
  694. }
  695. deleteAllArray();
  696. }
  697. else
  698. {
  699. for(int i=0; i<ArraySize(order_array); i++)
  700. {
  701. //Print(" ------------------------------------------ Ticket: ", order_array[ArraySize(order_array)-1].ticket, " Magic Number: ", order_array[ArraySize(order_array)-1].magicNo, " Array Size: ", ArraySize(order_array), " Index: ", i);
  702. int trade_taken = 2;
  703. int partialCloseOrderPosition=-1;
  704. double percentageToClose = 0;
  705. double closedPercentage = 0.0;
  706. double currentLot = 0.0;
  707. double totalLot = 0;
  708. for(int j=0; j<orderCount; j++)
  709. {
  710. //if(OrderSelect(cod[j].ticket,SELECT_BY_POS))
  711. if(cod[j].ticket!=-1)
  712. {
  713. if(cod[j].magic==order_array[i].ticket)
  714. {
  715. trade_taken = 0;
  716. }
  717. if(PositionSelectByTicket(cod[j].ticket))
  718. {
  719. double sumOfClosed = dealOutLotsCheck(0, PositionGetInteger(POSITION_TICKET));
  720. currentLot = PositionGetDouble(POSITION_VOLUME);
  721. totalLot = sumOfClosed + currentLot;
  722. if(totalLot > 0.0)
  723. closedPercentage = NormalizeDouble((sumOfClosed / totalLot) * 100.0, 1);
  724. }
  725. //Print(" Magic: ", cod[j].magic, " Ticket: ", order_array[i].ticket, " Master Lot: ", cod[j].masterLot, " Client Lot: ", order_array[i].lots);
  726. if(cod[j].magic==order_array[i].ticket)
  727. {
  728. if(closedPercentage < order_array[i].closedPercentageOfTrade)
  729. {
  730. percentageToClose = MathAbs(order_array[i].closedPercentageOfTrade - closedPercentage);
  731. trade_taken=1;
  732. partialCloseOrderPosition=j;
  733. }
  734. }
  735. }
  736. }
  737. if(order_array[i].ticket != -1)
  738. {
  739. if(trade_taken==1)
  740. {
  741. //current order need to close partially
  742. if(partialCloseOrderPosition!=-1)
  743. {
  744. bool isOrderClose=false;
  745. int roundDigit=0;
  746. double step=SymbolInfoDouble(cod[partialCloseOrderPosition].symbol,SYMBOL_VOLUME_STEP);
  747. while(step<1)
  748. {
  749. roundDigit++;
  750. step=step*10;
  751. }
  752. double lotClosePer = NormalizeDouble((totalLot * percentageToClose) / 100, roundDigit);
  753. if(percentageToClose > 0 && lotClosePer > SymbolInfoDouble(cod[partialCloseOrderPosition].symbol,SYMBOL_VOLUME_MIN))
  754. {
  755. if(!trade.PositionClosePartial(cod[partialCloseOrderPosition].ticket, lotClosePer))
  756. {
  757. Print("Partial order did not close:",cod[partialCloseOrderPosition].ticket, " Lot To Close is: ", lotClosePer);
  758. }
  759. else
  760. {
  761. isOrderClose=true;
  762. Print("Order Partial Close. currentLot Was =", currentLot,
  763. " Percentage to close = ", percentageToClose,
  764. " Lot I am Going To close = ", lotClosePer);
  765. }
  766. }
  767. }
  768. }
  769. else
  770. if(trade_taken == 2)
  771. {
  772. int diff1=((int)TimeCurrent()-(int)order_array[i].opentime)-gmtDifference;// ,diff2=TimeSeconds(TimeCurrent()-order_array[i].opentime);
  773. if(diff1 <= sec)
  774. {
  775. Print("Symbol: ",order_array[i].opentime," ",diff1," <= ",sec);
  776. trade.SetExpertMagicNumber(IntegerToString(order_array[i].ticket));
  777. trade.SetDeviationInPoints(10);
  778. trade.SetTypeFilling(ORDER_FILLING_IOC);
  779. trade.LogLevel(LOG_LEVEL_ALL);
  780. trade.SetAsyncMode(false);
  781. if(order_array[i].ordertype == ORDER_TYPE_BUY)
  782. {
  783. double tempSlip=MathAbs(order_array[i].price- SymbolInfoDouble(order_array[i].symbol,SYMBOL_ASK))/SymbolInfoDouble(order_array[i].symbol,SYMBOL_POINT);
  784. if((MathAbs(order_array[i].price-SymbolInfoDouble(order_array[i].symbol,SYMBOL_ASK))/SymbolInfoDouble(order_array[i].symbol,SYMBOL_POINT)) <= slip*10)
  785. {
  786. double lotsize = getLotSize(order_array[i].symbol, order_array[i].accountBalance, order_array[i].lots, order_array[i].contractSize);
  787. double Stoploss=0.0;
  788. double ask=NormalizeDouble(SymbolInfoDouble(order_array[i].symbol,SYMBOL_ASK),(int)SymbolInfoInteger(order_array[i].symbol,SYMBOL_DIGITS));
  789. if(trade.PositionOpen(order_array[i].symbol,ORDER_TYPE_BUY,lotsize,ask,order_array[i].stoploss,order_array[i].takeprofit,""))
  790. {
  791. if(addOrderMaster(trade.ResultOrder(),order_array[i].ticket,order_array[i].lots, "Market"))
  792. {
  793. Print(" Buy Order Ticket:",trade.ResultOrder()," Stored!!!");
  794. }
  795. else
  796. {
  797. //Print("Order Ticket is not stotred: ",trade.ResultOrder()," Error is: ", GetLastError());
  798. }
  799. }
  800. else
  801. {
  802. Print("Copy Buy Order Send Error: "+IntegerToString(GetLastError()));
  803. }
  804. }
  805. }
  806. if(order_array[i].ordertype == ORDER_TYPE_SELL)
  807. {
  808. if((MathAbs(order_array[i].price-SymbolInfoDouble(order_array[i].symbol,SYMBOL_BID))/SymbolInfoDouble(order_array[i].symbol,SYMBOL_POINT)) <= slip*10)
  809. {
  810. double lotsize = getLotSize(order_array[i].symbol, order_array[i].accountBalance, order_array[i].lots, order_array[i].contractSize);
  811. double Stoploss=0.0;
  812. double bid=NormalizeDouble(SymbolInfoDouble(order_array[i].symbol,SYMBOL_BID),(int)SymbolInfoInteger(order_array[i].symbol,SYMBOL_DIGITS));
  813. if(trade.PositionOpen(order_array[i].symbol,ORDER_TYPE_SELL,lotsize,bid,order_array[i].stoploss,order_array[i].takeprofit,""))
  814. {
  815. if(addOrderMaster(trade.ResultOrder(),order_array[i].ticket,order_array[i].lots, "Market"))
  816. {
  817. Print(" Sell Order Ticket:",trade.ResultOrder()," Stored!!!");
  818. }
  819. }
  820. else
  821. {
  822. Print("Copy Buy Order Send Error: "+IntegerToString(GetLastError()));
  823. }
  824. }
  825. }
  826. if(order_array[i].ordertype == ORDER_TYPE_BUY_STOP)
  827. {
  828. // Not Checking Slippage
  829. double lotsize = getLotSize(order_array[i].symbol, order_array[i].accountBalance, order_array[i].lots, order_array[i].contractSize);
  830. if(trade.BuyStop(lotsize, order_array[i].price, order_array[i].symbol, order_array[i].stoploss, order_array[i].takeprofit, 0, 0, ""))
  831. {
  832. if(addOrderMaster(trade.ResultOrder(),order_array[i].ticket,order_array[i].lots, "Pending"))
  833. {
  834. Print("Order Ticket Buy Stop:",trade.ResultOrder()," Stored!!!");
  835. }
  836. }
  837. else
  838. {
  839. Print("Error in Order Buy Stop Order: "+Symbol()+" ",GetLastError());
  840. }
  841. }
  842. if(order_array[i].ordertype == ORDER_TYPE_SELL_STOP)
  843. {
  844. double lotsize = getLotSize(order_array[i].symbol, order_array[i].accountBalance, order_array[i].lots, order_array[i].contractSize);
  845. if(trade.SellStop(lotsize, order_array[i].price, order_array[i].symbol, order_array[i].stoploss, order_array[i].takeprofit, 0, 0, ""))
  846. {
  847. if(addOrderMaster(trade.ResultOrder(),order_array[i].ticket,order_array[i].lots, "Pending"))
  848. {
  849. Print("Order Ticket Sell Stop:",trade.ResultOrder()," Stored!!!");
  850. }
  851. }
  852. else
  853. {
  854. Print("Error in Order Sell Stop Order: "+Symbol()+" ",GetLastError());
  855. }
  856. }
  857. if(order_array[i].ordertype == ORDER_TYPE_BUY_LIMIT)
  858. {
  859. double lotsize = getLotSize(order_array[i].symbol, order_array[i].accountBalance, order_array[i].lots, order_array[i].contractSize);
  860. if(trade.BuyLimit(lotsize, order_array[i].price, order_array[i].symbol, order_array[i].stoploss, order_array[i].takeprofit, 0, 0, ""))
  861. {
  862. if(addOrderMaster(trade.ResultOrder(),order_array[i].ticket,order_array[i].lots, "Pending"))
  863. {
  864. Print("Order Ticket Buy Limit:",trade.ResultOrder()," Stored!!!");
  865. }
  866. }
  867. else
  868. {
  869. Print("Error in Order Buy Limit: "+Symbol()+" ",GetLastError());
  870. }
  871. }
  872. if(order_array[i].ordertype == ORDER_TYPE_SELL_LIMIT)
  873. {
  874. double lotsize = getLotSize(order_array[i].symbol, order_array[i].accountBalance, order_array[i].lots, order_array[i].contractSize);
  875. if(trade.SellLimit(lotsize, order_array[i].price, order_array[i].symbol, order_array[i].stoploss, order_array[i].takeprofit, 0, 0, ""))
  876. {
  877. if(addOrderMaster(trade.ResultOrder(),order_array[i].ticket,order_array[i].lots, "Pending"))
  878. {
  879. Print("Order Ticket Sell Limit: ",trade.ResultOrder()," Stored!!! ", " Tp: ", order_array[i].takeprofit, " Sl: ", order_array[i].stoploss);
  880. }
  881. }
  882. else
  883. {
  884. Print("Error in Order Sell Limit: "+Symbol()+" ",GetLastError());
  885. }
  886. }
  887. }
  888. }
  889. }
  890. }
  891. for(int i=0; i<orderCount; i++)
  892. {
  893. bool trade_close = true;
  894. if(PositionSelectByTicket(cod[i].ticket))
  895. {
  896. for(int j=0; j<ArraySize(order_array); j++)
  897. {
  898. if(cod[i].magic == (order_array[j].ticket))
  899. {
  900. int digitss = getDoubleSymbolInfo(PositionGetString(POSITION_SYMBOL), "digits");
  901. trade_close = false;
  902. //Print("TP/SL | norm cod TP=",NormalizeDouble(cod[i].takeprofit,digitss),
  903. // " norm order TP=",NormalizeDouble(order_array[j].takeprofit,digitss),
  904. // " | norm cod SL=",NormalizeDouble(cod[i].stoploss,digitss),
  905. // " norm order SL=",NormalizeDouble(order_array[j].stoploss,digitss));
  906. if(NormalizeDouble(cod[i].takeprofit, digitss) != NormalizeDouble(order_array[j].takeprofit, digitss))
  907. {
  908. if(NormalizeDouble(order_array[j].takeprofit, digitss) == NormalizeDouble(PositionGetDouble(POSITION_TP), digitss))
  909. {
  910. Print(" Client Position Tp Was Manually Modified. ");
  911. cod[i].takeprofit=NormalizeDouble(order_array[j].takeprofit, digitss);
  912. }
  913. }
  914. if(NormalizeDouble(cod[i].stoploss, digitss) != NormalizeDouble(order_array[j].stoploss, digitss))
  915. {
  916. if(NormalizeDouble(order_array[j].stoploss, digitss) == NormalizeDouble(PositionGetDouble(POSITION_SL), digitss))
  917. {
  918. Print(" Client Position SL Was Manually Modified. ");
  919. cod[i].stoploss = NormalizeDouble(order_array[j].stoploss, digitss);
  920. }
  921. }
  922. if(NormalizeDouble(cod[i].takeprofit, digitss) != NormalizeDouble(order_array[j].takeprofit, digitss) || NormalizeDouble(cod[i].stoploss, digitss) != NormalizeDouble(order_array[j].stoploss, digitss))
  923. {
  924. if(trade.PositionModify(PositionGetInteger(POSITION_TICKET),order_array[j].stoploss,order_array[j].takeprofit))
  925. {
  926. Print("Order modified ticket:",PositionGetInteger(POSITION_TICKET));
  927. cod[i].takeprofit=NormalizeDouble(order_array[j].takeprofit, digitss);
  928. cod[i].stoploss=NormalizeDouble(order_array[j].stoploss, digitss);
  929. }
  930. else
  931. {
  932. //ExpertRemove();
  933. Print("Order not modified error:",GetLastError());
  934. }
  935. }
  936. }
  937. }
  938. if(trade_close == true)
  939. {
  940. if(PositionGetInteger(POSITION_TYPE)== ORDER_TYPE_BUY)
  941. {
  942. if(!trade.PositionClose(PositionGetInteger(POSITION_TICKET)))
  943. {
  944. Print("Buy Order Close Error on Master Trade Close: "+IntegerToString(GetLastError()));
  945. }
  946. else
  947. {
  948. Print("Order close by no master copy of trade");
  949. cod[i].ticket=-1;
  950. }
  951. }
  952. if(PositionGetInteger(POSITION_TYPE)== ORDER_TYPE_SELL)
  953. {
  954. if(!trade.PositionClose(PositionGetInteger(POSITION_TICKET)))
  955. {
  956. Print("Sell Order Close Error on Master Trade Close: "+IntegerToString(GetLastError()));
  957. }
  958. else
  959. {
  960. Print("Order close by no master copy of trade");
  961. cod[i].ticket=-1;
  962. }
  963. }
  964. }
  965. }
  966. if(OrderSelect(cod[i].ticket))
  967. {
  968. for(int j=0; j<ArraySize(order_array); j++)
  969. {
  970. if(cod[i].magic == (order_array[j].ticket))
  971. {
  972. trade_close = false;
  973. int digitss = (int)getDoubleSymbolInfo(PositionGetString(POSITION_SYMBOL), "digits");
  974. if(NormalizeDouble(cod[i].takeprofit, digitss) != NormalizeDouble(order_array[j].takeprofit, digitss))
  975. {
  976. if(NormalizeDouble(order_array[j].takeprofit, digitss) == NormalizeDouble(OrderGetDouble(ORDER_TP), digitss))
  977. {
  978. Print(" Pending Master Position Tp Was Manually Modified. ");
  979. cod[i].takeprofit=NormalizeDouble(order_array[j].takeprofit, digitss);
  980. }
  981. }
  982. if(NormalizeDouble(cod[i].stoploss, digitss) != NormalizeDouble(order_array[j].stoploss, digitss))
  983. {
  984. if(NormalizeDouble(order_array[j].stoploss, digitss) == NormalizeDouble(OrderGetDouble(ORDER_SL), digitss))
  985. {
  986. Print(" Pending Master Position SL Was Manually Modified. ");
  987. cod[i].stoploss = NormalizeDouble(order_array[j].stoploss, digitss);
  988. }
  989. }
  990. if(NormalizeDouble(cod[i].price, digitss) != NormalizeDouble(order_array[j].price, digitss))
  991. {
  992. if(NormalizeDouble(order_array[j].price, digitss) == NormalizeDouble(OrderGetDouble(ORDER_PRICE_OPEN), digitss))
  993. {
  994. Print(" Pending Master Position Price Was Manually Modified. ");
  995. cod[i].price = NormalizeDouble(order_array[j].price, digitss);
  996. }
  997. }
  998. // if(OrderGetDouble(ORDER_TP)!=order_array[j].takeprofit || OrderGetDouble(ORDER_SL)!=order_array[j].stoploss || OrderGetDouble(ORDER_PRICE_OPEN)!=order_array[j].price)
  999. if(NormalizeDouble(cod[i].takeprofit, digitss) != NormalizeDouble(order_array[j].takeprofit, digitss) || NormalizeDouble(cod[i].stoploss, digitss) != NormalizeDouble(order_array[j].stoploss, digitss) || NormalizeDouble(cod[i].price, digitss) != NormalizeDouble(order_array[j].price, digitss))
  1000. {
  1001. if(trade.OrderModify(OrderGetInteger(ORDER_TICKET),order_array[j].price,order_array[j].stoploss,order_array[j].takeprofit, 0, 0, 0))
  1002. {
  1003. Print("Pending Order modified ticket:",PositionGetInteger(POSITION_TICKET));
  1004. cod[i].takeprofit=order_array[j].takeprofit;
  1005. cod[i].stoploss=order_array[j].stoploss;
  1006. cod[i].price = order_array[j].price;
  1007. }
  1008. else
  1009. {
  1010. Print("Order not modified error:",GetLastError());
  1011. }
  1012. }
  1013. }
  1014. }
  1015. if(trade_close == true)
  1016. {
  1017. if(OrderGetInteger(ORDER_TYPE)== ORDER_TYPE_BUY_STOP || OrderGetInteger(ORDER_TYPE)== ORDER_TYPE_SELL_STOP || OrderGetInteger(ORDER_TYPE)== ORDER_TYPE_BUY_LIMIT || OrderGetInteger(ORDER_TYPE)== ORDER_TYPE_SELL_LIMIT)
  1018. {
  1019. if(!trade.OrderDelete(OrderGetInteger(ORDER_TICKET)))
  1020. {
  1021. Print("Error in Deleting Pending Order: "+IntegerToString(GetLastError()));
  1022. }
  1023. else
  1024. {
  1025. Print("Pending Order Deleted. Ticket is: ", cod[i].ticket);
  1026. cod[i].ticket=-1;
  1027. }
  1028. }
  1029. }
  1030. }
  1031. }
  1032. }
  1033. }
  1034. }
  1035. }
  1036. //+------------------------------------------------------------------+
  1037. //| |
  1038. //+------------------------------------------------------------------+
  1039. void print()
  1040. {
  1041. for(int i=0; i<orderCount; i++)
  1042. {
  1043. if(cod[i].ticket!=-1)
  1044. {
  1045. Print("Ticket : ",cod[i].ticket," OPen price: ",cod[i].price, " Type: ",cod[i].ordertype);
  1046. }
  1047. }
  1048. }
  1049. //+------------------------------------------------------------------+
  1050. //| |
  1051. //+------------------------------------------------------------------+
  1052. bool isOrderThere(ulong ticket,int &index)
  1053. {
  1054. for(int i=0; i<ArraySize(mod1); i++)
  1055. {
  1056. if(ticket != -1)
  1057. {
  1058. if(mod1[i].ticket==ticket)
  1059. {
  1060. index=i;
  1061. return true;
  1062. }
  1063. }
  1064. }
  1065. return false;
  1066. }
  1067. //+------------------------------------------------------------------+
  1068. //| |
  1069. //+------------------------------------------------------------------+
  1070. void emptyStruct()
  1071. {
  1072. for(int i = 0 ; i < orderCount ; i++)
  1073. {
  1074. if(mod1[i].ticket != -1)
  1075. {
  1076. mod1[i].ticket=-1;
  1077. mod1[i].magic=0;
  1078. mod1[i].price=-1;
  1079. mod1[i].symbol="";
  1080. mod1[i].ordertype=-1;
  1081. mod1[i].lots=0;
  1082. mod1[i].stoploss=0;
  1083. mod1[i].takeprofit=0;
  1084. }
  1085. }
  1086. }
  1087. //+------------------------------------------------------------------+
  1088. //| |
  1089. //+------------------------------------------------------------------+
  1090. void deleteAllArray()
  1091. {
  1092. for(int i=0; i<orderCount; i++)
  1093. {
  1094. if(cod[i].ticket!=-1)
  1095. {
  1096. cod[i].ticket=-1;
  1097. }
  1098. }
  1099. }
  1100. //+------------------------------------------------------------------+
  1101. //| |
  1102. //+------------------------------------------------------------------+
  1103. double dealOutLotsCheck(datetime cutOffTimeIs, ulong positionId)
  1104. {
  1105. double sumOfLots = 0.0;
  1106. if(HistorySelect(cutOffTimeIs,LONG_MAX))
  1107. {
  1108. int totalDeals = HistoryDealsTotal();
  1109. for(int i = 0; i < totalDeals; i++)
  1110. {
  1111. ulong dealTicket = HistoryDealGetTicket(i);
  1112. ulong dealPositionID = (ulong)HistoryDealGetInteger(dealTicket, DEAL_POSITION_ID);
  1113. if(HistoryDealGetInteger(dealTicket, DEAL_ENTRY) == DEAL_ENTRY_OUT
  1114. && dealPositionID == positionId)
  1115. {
  1116. sumOfLots += HistoryDealGetDouble(dealTicket, DEAL_VOLUME);
  1117. }
  1118. }
  1119. }
  1120. return sumOfLots;
  1121. }
  1122. //+------------------------------------------------------------------+
  1123. //| |
  1124. //+------------------------------------------------------------------+
  1125. bool addOrderMaster(ulong ticket,ulong magic,double lot, string calledBy)
  1126. {
  1127. Print("in addOrderMaster: ", ticket);
  1128. bool notFoundPosition=false;//Use to look in to the order loop in case order isn't converted
  1129. if(OrderSelect(ticket))
  1130. {
  1131. Print("Inside order selection:");
  1132. Print("Order State:",(ENUM_ORDER_STATE)OrderGetInteger(ORDER_STATE));
  1133. Print("Order Time done:",(datetime)OrderGetInteger(ORDER_TIME_DONE));
  1134. Print("Order Type:",(ENUM_ORDER_TYPE)OrderGetInteger(ORDER_TYPE));
  1135. }
  1136. else
  1137. if(HistoryOrderSelect(ticket))
  1138. {
  1139. Print("Inside history order selection:");
  1140. Print("Order State:",(ENUM_ORDER_STATE)HistoryOrderGetInteger(ticket,ORDER_STATE));
  1141. Print("Order Time done:",(datetime)HistoryOrderGetInteger(ticket,ORDER_TIME_DONE));
  1142. Print("Order Type:",(ENUM_ORDER_TYPE)HistoryOrderGetInteger(ticket,ORDER_TYPE));
  1143. }
  1144. else
  1145. {
  1146. PrintFormat("OrderSelect(%I64u) failed. Error %d", ticket, GetLastError());
  1147. }
  1148. for(int i=0; i<orderCount; i++)
  1149. {
  1150. if(cod[i].ticket==-1)
  1151. {
  1152. if(calledBy == "Market")
  1153. {
  1154. if(PositionSelectByTicket(ticket))
  1155. {
  1156. cod[i].ticket=PositionGetInteger(POSITION_TICKET);
  1157. cod[i].price=PositionGetDouble(POSITION_PRICE_OPEN);
  1158. cod[i].symbol=PositionGetString(POSITION_SYMBOL);
  1159. cod[i].ordertype=PositionGetInteger(POSITION_TYPE);
  1160. cod[i].lots=PositionGetDouble(POSITION_VOLUME);
  1161. cod[i].stoploss=PositionGetDouble(POSITION_SL);
  1162. cod[i].takeprofit=PositionGetDouble(POSITION_TP);
  1163. cod[i].magic=PositionGetInteger(POSITION_MAGIC);
  1164. cod[i].comments=PositionGetString(POSITION_COMMENT);
  1165. cod[i].masterLot = lot;
  1166. Print("Ticket stored in master:",PositionGetInteger(POSITION_TICKET)," stored in index:",i);
  1167. Print("========== CLIENT ORDER STORED ==========");
  1168. Print(" index=", i,
  1169. " | master_magic=", cod[i].magic,
  1170. " | client_ticket=", cod[i].ticket,
  1171. " | symbol=", cod[i].symbol,
  1172. " | order_type=", cod[i].ordertype,
  1173. " | open_price=", DoubleToString(cod[i].price, _Digits),
  1174. " | lots=", DoubleToString(cod[i].lots, 2),
  1175. " | master_lot=", DoubleToString(cod[i].masterLot, 2),
  1176. " | sl=", DoubleToString(cod[i].stoploss, _Digits),
  1177. " | tp=", DoubleToString(cod[i].takeprofit, _Digits),
  1178. " | comment=", cod[i].comments);
  1179. Print("========================================");
  1180. return true;
  1181. }
  1182. else
  1183. {
  1184. Print("Ticket:",ticket," not found in the position list");
  1185. notFoundPosition=true;
  1186. }
  1187. }
  1188. if(calledBy == "Pending" || notFoundPosition)
  1189. {
  1190. if(OrderSelect(ticket))
  1191. {
  1192. cod[i].ticket=OrderGetInteger(ORDER_TICKET);
  1193. cod[i].price=OrderGetDouble(ORDER_PRICE_OPEN);
  1194. cod[i].symbol=OrderGetString(ORDER_SYMBOL);
  1195. cod[i].ordertype=OrderGetInteger(ORDER_TYPE);
  1196. cod[i].lots=OrderGetDouble(ORDER_VOLUME_CURRENT);
  1197. cod[i].stoploss=OrderGetDouble(ORDER_SL);
  1198. cod[i].takeprofit=OrderGetDouble(ORDER_TP);
  1199. cod[i].magic=OrderGetInteger(ORDER_MAGIC);
  1200. cod[i].comments=OrderGetString(ORDER_COMMENT);
  1201. cod[i].masterLot = lot;
  1202. Print("Pending Order Ticket in master:",OrderGetInteger(ORDER_TICKET)," stored in index:",i);
  1203. return true;
  1204. }
  1205. else
  1206. {
  1207. Print("Ticket:",ticket," not found in the order list");
  1208. }
  1209. }
  1210. }
  1211. }
  1212. return false;
  1213. }
  1214. //+------------------------------------------------------------------+
  1215. //+------------------------------------------------------------------+
  1216. //| |
  1217. //+------------------------------------------------------------------+
  1218. double getDoubleSymbolInfo(string symbol,string info)
  1219. {
  1220. long digits=8;
  1221. //Symbol without the suffix and prefix
  1222. string updatedSymbol=symbol;
  1223. if(suffix!="")
  1224. updatedSymbol = StringSubstr(updatedSymbol,0,StringFind(updatedSymbol,suffix,0));
  1225. if(prefix!="")
  1226. StringReplace(updatedSymbol,prefix,"");
  1227. //Digits calculation could be used in other calculations
  1228. digits = SymbolInfoInteger(symbol,SYMBOL_DIGITS);
  1229. if(digits==0)
  1230. {
  1231. digits=SymbolInfoInteger(updatedSymbol,SYMBOL_DIGITS);
  1232. if(digits==0)
  1233. digits=8;
  1234. }
  1235. if(info=="digits")
  1236. {
  1237. return (double)digits;
  1238. }
  1239. else
  1240. if(info=="point" || info=="points")
  1241. {
  1242. double points=SymbolInfoDouble(symbol, SYMBOL_POINT);
  1243. if(points==0)
  1244. {
  1245. points=SymbolInfoDouble(updatedSymbol,SYMBOL_POINT);
  1246. if(points==0)
  1247. points=0.00001;
  1248. }
  1249. return points;
  1250. }
  1251. else
  1252. if(info=="ask")
  1253. {
  1254. double ask = SymbolInfoDouble(symbol, SYMBOL_ASK);
  1255. if(ask==0)
  1256. {
  1257. ask=SymbolInfoDouble(updatedSymbol,SYMBOL_ASK);
  1258. if(ask==0)
  1259. ask=0.00001;
  1260. }
  1261. return NormalizeDouble(ask,(int)digits);
  1262. }
  1263. else
  1264. if(info=="bid")
  1265. {
  1266. double bid=SymbolInfoDouble(symbol, SYMBOL_BID);
  1267. if(bid==0)
  1268. {
  1269. bid=SymbolInfoDouble(updatedSymbol,SYMBOL_BID);
  1270. if(bid==0)
  1271. bid=0.00001;
  1272. }
  1273. return NormalizeDouble(bid,(int)digits);
  1274. }
  1275. else
  1276. if(info=="contract"||info=="contractSize")
  1277. {
  1278. double contract=SymbolInfoDouble(symbol,SYMBOL_TRADE_CONTRACT_SIZE);
  1279. if(contract==0)
  1280. {
  1281. contract=SymbolInfoDouble(updatedSymbol,SYMBOL_TRADE_CONTRACT_SIZE);
  1282. }
  1283. return contract;
  1284. }
  1285. else
  1286. if(info=="lotStep")
  1287. {
  1288. double step=SymbolInfoDouble(symbol, SYMBOL_VOLUME_STEP);
  1289. if(step==0)
  1290. {
  1291. step=SymbolInfoDouble(updatedSymbol,SYMBOL_VOLUME_STEP);
  1292. }
  1293. return step;
  1294. }
  1295. else
  1296. if(info=="minLot")
  1297. {
  1298. double minLot=SymbolInfoDouble(symbol, SYMBOL_VOLUME_MIN);
  1299. if(minLot==0)
  1300. {
  1301. minLot = SymbolInfoDouble(updatedSymbol, SYMBOL_VOLUME_MIN);
  1302. }
  1303. return minLot;
  1304. }
  1305. else
  1306. if(info=="maxLot")
  1307. {
  1308. double maxLot=SymbolInfoDouble(symbol,SYMBOL_VOLUME_MAX);
  1309. if(maxLot==0)
  1310. {
  1311. maxLot=SymbolInfoDouble(updatedSymbol,SYMBOL_VOLUME_MAX);
  1312. }
  1313. return maxLot;
  1314. }
  1315. return 0;
  1316. }
  1317. //+------------------------------------------------------------------+
  1318. //| |
  1319. //+------------------------------------------------------------------+
  1320. string getSymbol(string symbol)
  1321. {
  1322. if(mappingSymbol!="")
  1323. {
  1324. string symbollist[];
  1325. StringSplit(mappingSymbol,',',symbollist);
  1326. for(int i=0; i<ArraySize(symbollist); i++)
  1327. {
  1328. string symbolPairs[];
  1329. StringSplit(symbollist[i],':',symbolPairs);
  1330. if(ArraySize(symbolPairs)==2)
  1331. {
  1332. if(symbol==symbolPairs[0])
  1333. return symbolPairs[1];
  1334. }
  1335. }
  1336. }
  1337. else
  1338. {
  1339. return prefix+symbol+suffix;
  1340. }
  1341. return prefix+symbol+suffix;
  1342. }
  1343. //+------------------------------------------------------------------+
  1344. //| |
  1345. //+------------------------------------------------------------------+
  1346. double getLotSize(string symbol,double masterAccountBalance,double masterLot,int masterContractSize)
  1347. {
  1348. double lotsize = fixed;
  1349. Print("------> Symbol:",symbol," master Account balance:",masterAccountBalance," master lot:",masterLot," master Contract:",masterContractSize);
  1350. if(lotoption == mul)
  1351. {
  1352. lotsize = NormalizeDouble(masterLot*mult,2);
  1353. }
  1354. //else
  1355. // if(lotoption == Balance)
  1356. // {
  1357. // lotsize=((AccountInfoDouble(ACCOUNT_BALANCE)/masterAccountBalance)*masterLot)*Bmult;
  1358. // }
  1359. else
  1360. if(lotoption == slaveLotCal)
  1361. {
  1362. lotsize = masterLot * (recoveryAmount / maxDd);
  1363. }
  1364. Print("Before Lot Size: ",lotsize);
  1365. if(contractChk)
  1366. {
  1367. int clientContractSize=getDoubleSymbolInfo(symbol,"contract");
  1368. Print("Client Contract size:",clientContractSize);
  1369. if(masterContractSize!=0 && clientContractSize!=0)
  1370. {
  1371. lotsize=((masterContractSize*1.0)/clientContractSize)*lotsize;
  1372. }
  1373. }
  1374. int roundDigit=0;
  1375. double step=getDoubleSymbolInfo(symbol, "lotStep");
  1376. while(step<1)
  1377. {
  1378. roundDigit++;
  1379. step=step*10;
  1380. }
  1381. Print("Round Digits:",roundDigit);
  1382. lotsize = NormalizeDouble(lotsize,roundDigit);
  1383. if(lotsize<getDoubleSymbolInfo(symbol,"minLot"))
  1384. lotsize=getDoubleSymbolInfo(symbol,"minLot");
  1385. else
  1386. if(lotsize>getDoubleSymbolInfo(symbol,"maxLot"))
  1387. lotsize=getDoubleSymbolInfo(symbol,"maxLot");
  1388. Print("Lot Size: ",lotsize);
  1389. return lotsize;
  1390. }
  1391. //+------------------------------------------------------------------+
  1392. //| |
  1393. //+------------------------------------------------------------------+
  1394. void clearOrderArray()
  1395. {
  1396. //Resize to one
  1397. ArrayResize(order_array,1);
  1398. //Clear all data
  1399. order_array[0].symbol = "";
  1400. order_array[0].opentime = 0;
  1401. order_array[0].price = 0;
  1402. order_array[0].ordertype = 0;
  1403. order_array[0].lots = 0;
  1404. order_array[0].stoploss = 0;
  1405. order_array[0].takeprofit = 0;
  1406. order_array[0].ticket = -1;
  1407. order_array[0].magicNo = 0;
  1408. order_array[0].accountBalance=0;
  1409. order_array[0].contractSize=0;
  1410. order_array[0].comments="";
  1411. order_array[0].closedPercentageOfTrade = 0;
  1412. }
  1413. //+------------------------------------------------------------------+
  1414. //| |
  1415. //+------------------------------------------------------------------+
  1416. void mapHistory()
  1417. {
  1418. for(int i=0; i<PositionsTotal(); i++)
  1419. {
  1420. ulong ticket = PositionGetTicket(i);
  1421. if(PositionSelectByTicket(ticket))
  1422. {
  1423. if(PositionGetInteger(POSITION_MAGIC) > 999)
  1424. {
  1425. addOrderMaster(PositionGetInteger(POSITION_TICKET), PositionGetInteger(POSITION_MAGIC), PositionGetDouble(POSITION_VOLUME), "Market");
  1426. }
  1427. }
  1428. }
  1429. for(int i=0; i<OrdersTotal(); i++)
  1430. {
  1431. ulong ticket = OrderGetTicket(i);
  1432. if(OrderSelect(ticket))
  1433. {
  1434. if(OrderGetInteger(ORDER_MAGIC) > 999)
  1435. {
  1436. addOrderMaster(OrderGetInteger(ORDER_TICKET), OrderGetInteger(ORDER_MAGIC), OrderGetDouble(ORDER_VOLUME_CURRENT), "Pending");
  1437. }
  1438. }
  1439. }
  1440. }
  1441. //+------------------------------------------------------------------+
  1442. //| |
  1443. //+------------------------------------------------------------------+
  1444. bool addToMarketWatch(string symbol)
  1445. {
  1446. Print("Symbol:",symbol);
  1447. bool result=SymbolSelect(symbol,true);
  1448. if(result)
  1449. {
  1450. Print("Successfully added the symbol or already there:", symbol);
  1451. return true;
  1452. }
  1453. else
  1454. {
  1455. Print("Error in adding symbol:",symbol);
  1456. }
  1457. return false;
  1458. }
  1459. //+------------------------------------------------------------------+
  1460. //| |
  1461. //+------------------------------------------------------------------+
  1462. void read_history_file()
  1463. {
  1464. string file_name = "history_trades.csv";
  1465. if(!FileIsExist(file_name, FILE_COMMON))
  1466. {
  1467. Print("File not found: ", file_name);
  1468. return;
  1469. }
  1470. int h = FileOpen(file_name, FILE_SHARE_READ|FILE_CSV|FILE_COMMON|FILE_READ|FILE_ANSI|FILE_TXT|FILE_COMMON);
  1471. if(h == INVALID_HANDLE)
  1472. {
  1473. Print("Error opening file: ", file_name, " Err:", GetLastError());
  1474. return;
  1475. }
  1476. datetime lastFileModified=(datetime)FileGetInteger(h,FILE_MODIFY_DATE);
  1477. long fileSize=FileGetInteger(h,FILE_SIZE);
  1478. if(lastModified1==lastFileModified && fileSize==lastFileSize1)
  1479. {
  1480. FileClose(h);
  1481. return;
  1482. }
  1483. else
  1484. {
  1485. ulong fileTickets[];
  1486. ArrayResize(fileTickets, 0);
  1487. while(!FileIsEnding(h))
  1488. {
  1489. string line = FileReadString(h);
  1490. // split by comma
  1491. string parts[];
  1492. int count = StringSplit(line,',', parts);
  1493. if(count >= 2)
  1494. {
  1495. // convert to integers
  1496. ulong ticket = StringToInteger(parts[0]);
  1497. ulong magic = StringToInteger(parts[1]);
  1498. int newSize = ArraySize(fileTickets) + 1;
  1499. ArrayResize(fileTickets, newSize);
  1500. fileTickets[newSize - 1] = ticket;
  1501. if(!history_ticket_present(ticket))
  1502. add_history_ticket(ticket, magic);
  1503. }
  1504. else
  1505. {
  1506. // Print("Skipped malformed line while reading ", file_name, " : ", line);
  1507. }
  1508. }
  1509. FileClose(h);
  1510. lastModified1=lastFileModified;
  1511. lastFileSize1=fileSize;
  1512. for(int i = 0; i < MaxOrders; i++)
  1513. {
  1514. ulong tk = historyTradeStore[i].ticket;
  1515. if(tk == -1)
  1516. continue;
  1517. bool found = false;
  1518. for(int j = 0; j < ArraySize(fileTickets); j++)
  1519. {
  1520. if(fileTickets[j] == tk)
  1521. {
  1522. found = true;
  1523. break;
  1524. }
  1525. }
  1526. if(!found)
  1527. {
  1528. Print("Removing ticket not found in file: ", tk, " index=", i);
  1529. historyTradeStore[i].ticket = -1;
  1530. historyTradeStore[i].magic = 0;
  1531. }
  1532. }
  1533. }
  1534. }
  1535. //+------------------------------------------------------------------+
  1536. //| |
  1537. //+------------------------------------------------------------------+
  1538. void add_history_ticket(ulong ticket, ulong magicIs)
  1539. {
  1540. for(int i = 0; i < MaxOrders; i++)
  1541. {
  1542. if(historyTradeStore[i].ticket == -1)
  1543. {
  1544. historyTradeStore[i].ticket = ticket;
  1545. historyTradeStore[i].magic = magicIs;
  1546. Print(" -------- add_history_ticket() Added ticket: ", ticket, " Magic: ", magicIs, " ---------------------------- ");
  1547. return;
  1548. }
  1549. }
  1550. Print("historyTradeStore full, cannot add ticket: ", ticket);
  1551. }
  1552. //+------------------------------------------------------------------+
  1553. //| |
  1554. //+------------------------------------------------------------------+
  1555. bool history_ticket_present(ulong ticket)
  1556. {
  1557. for(int i = 0 ; i < MaxOrders ; i++)
  1558. {
  1559. if(historyTradeStore[i].ticket == ticket)
  1560. return true;
  1561. }
  1562. return false;
  1563. }
  1564. //+------------------------------------------------------------------+
  1565. //+------------------------------------------------------------------+
  1566. //| |
  1567. //+------------------------------------------------------------------+