Geen omschrijving

Pulse Balance Indicator Sim.mq5 38KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. //+------------------------------------------------------------------+
  2. //| pulseBalaceIndicator.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.1"
  9. #define global "var"
  10. #property indicator_separate_window
  11. #property indicator_buffers 2
  12. #property indicator_plots 2
  13. #property indicator_label1 "ACCOUNT BALANCE"
  14. #property indicator_type1 DRAW_LINE
  15. #property indicator_color1 clrRed
  16. #property indicator_width1 1
  17. #property indicator_label2 "MA"
  18. #property indicator_type2 DRAW_LINE
  19. #property indicator_color2 clrDodgerBlue
  20. #property indicator_width2 1
  21. #property indicator_style2 STYLE_DOT
  22. input color Dot_Color = clrOrangeRed;
  23. input double StartingBalance = 10000;
  24. input string MagicNumbers = "123";
  25. input int MA_Period = 5;
  26. input ENUM_MA_METHOD MA_Method = MODE_SMA;
  27. input string fileName = "TradeDataFile"; // File Name
  28. input int historyTrades = 50;
  29. double ACCOUNTBALANCE[];
  30. double MA[];
  31. int TOTAL_HISTORY = 0;
  32. datetime TT = 0;
  33. int MagicArray[];
  34. //+------------------------------------------------------------------+
  35. //| |
  36. //+------------------------------------------------------------------+
  37. string filename = fileName + Symbol() + ".csv";
  38. //+------------------------------------------------------------------+
  39. //| Custom indicator initialization function |
  40. //+------------------------------------------------------------------+
  41. int OnInit()
  42. {
  43. //--- indicator buffers mapping
  44. if(!GlobalVariableCheck(global)) //for creating
  45. {
  46. GlobalVariableSet(global,0);
  47. // 0 means Free
  48. // 1 means busy
  49. }
  50. parseMagicNumbers();
  51. SetIndexBuffer(0, ACCOUNTBALANCE, INDICATOR_DATA);
  52. SetIndexBuffer(1, MA, INDICATOR_DATA);
  53. ArraySetAsSeries(ACCOUNTBALANCE, true);
  54. ArraySetAsSeries(MA, true);
  55. TOTAL_HISTORY = 0;
  56. TT = 0;
  57. //---
  58. return(INIT_SUCCEEDED);
  59. }
  60. //+------------------------------------------------------------------+
  61. //| Custom indicator deinitialization function |
  62. //+------------------------------------------------------------------+
  63. void OnDeinit(const int reason)
  64. {
  65. //--- destroy timer
  66. GlobalVariableSet(global,0);
  67. subDeleteObjects("Balance: ");
  68. subDeleteObjects("Start Balance: ");
  69. }
  70. //+------------------------------------------------------------------+
  71. //| Custom indicator iteration function |
  72. //+------------------------------------------------------------------+
  73. int OnCalculate(const int rates_total,
  74. const int prev_calculated,
  75. const datetime &time[],
  76. const double &open[],
  77. const double &high[],
  78. const double &low[],
  79. const double &close[],
  80. const long &tick_volume[],
  81. const long &volume[],
  82. const int &spread[])
  83. {
  84. //---
  85. MyStart(rates_total, prev_calculated);
  86. //--- return value of prev_calculated for next call
  87. return(rates_total);
  88. }
  89. //+------------------------------------------------------------------+
  90. //| |
  91. //+------------------------------------------------------------------+
  92. void MyStart(int ratesTotal, int prevCalculated)
  93. {
  94. int currTradeCount = orderCount();
  95. if(currTradeCount != -1)
  96. {
  97. if(TOTAL_HISTORY != currTradeCount || TT != iTime(Symbol(), PERIOD_CURRENT, 0))
  98. {
  99. int limit = 0;
  100. if(prevCalculated == 0)
  101. {
  102. limit = ratesTotal - prevCalculated -2;
  103. }
  104. else
  105. {
  106. limit = ratesTotal - prevCalculated;
  107. }
  108. for(int i=1000; i>= 0; i--)
  109. {
  110. ACCOUNTBALANCE[i] = EMPTY_VALUE;
  111. MA[i] = EMPTY_VALUE;
  112. DeleteStartBalanceObjects();
  113. }
  114. int total = 0;
  115. double AB = StartingBalance;
  116. double BAL = AB;
  117. Print("ACCOUNT BALANCE: " + DoubleToString(AB));
  118. total = currTradeCount;
  119. int totalOrderCount = currTradeCount;
  120. // Print(" Total Order Count = ",totalOrderCount);
  121. int startIndexToConsiderTrade = totalOrderCount - historyTrades;
  122. int totalLoop = historyTrades;
  123. total = historyTrades;
  124. if(startIndexToConsiderTrade < 0)
  125. {
  126. startIndexToConsiderTrade = 1;
  127. total = totalOrderCount;
  128. totalLoop = totalOrderCount;
  129. }
  130. if(GlobalVariableGet(global) == 0)
  131. {
  132. int handle = FileOpen(filename, FILE_READ | FILE_COMMON);
  133. // Print("startIndexToConsiderTrade: ",startIndexToConsiderTrade);
  134. if(handle == INVALID_HANDLE)
  135. {
  136. Print("ind => Handler invalid: MyStart() ", filename," error: ",GetLastError());
  137. return;
  138. }
  139. while(!FileIsEnding(handle))
  140. {
  141. string line = FileReadString(handle);
  142. // Skip empty line
  143. if(StringLen(line) < 2)
  144. continue;
  145. string parts[];
  146. int count = StringSplit(line, ',', parts);
  147. int ticket = (int)StringToInteger(parts[0]);
  148. int type = (int)StringToInteger(parts[1]);
  149. int magic = (int)StringToInteger(parts[2]);
  150. double open_price = StringToDouble(parts[3]);
  151. double close_price = StringToDouble(parts[4]);
  152. datetime open_time = StringToTime(parts[5]);
  153. datetime close_time = StringToTime(parts[6]);
  154. double sl = StringToDouble(parts[7]);
  155. double tp = StringToDouble(parts[8]);
  156. double lot = StringToDouble(parts[9]);
  157. double profit = StringToDouble(parts[10]);
  158. if(isMagicMatch(magic) && ticket >= startIndexToConsiderTrade && total > 0)
  159. {
  160. // Print(" startIndexToConsiderTrade ",startIndexToConsiderTrade," ticket = ",ticket," total + 1: ",total + 1," totalOrderCount ",totalOrderCount," historyTrades = ",historyTrades);
  161. double deal_profit = profit;
  162. ACCOUNTBALANCE[total + 1] = BAL;
  163. string name = "Start Balance: " + DoubleToString(ACCOUNTBALANCE[total + 1], 2) + "\n" + TimeToString(close_time, TIME_DATE|TIME_SECONDS) + "\n";
  164. CreateDotLabel(name, "=", iTime(Symbol(), PERIOD_CURRENT, total + 1), ACCOUNTBALANCE[total + 1], Dot_Color);
  165. BAL = BAL + profit;
  166. ACCOUNTBALANCE[total] = BAL;
  167. GlobalVariableSet("TIME:" + IntegerToString(total), close_time);
  168. total--;
  169. }
  170. }
  171. FileClose(handle);
  172. // Calculate moving averages and create labels
  173. for(int x = totalLoop; x > 0/*startIndexToConsiderTrade*/; x--)
  174. {
  175. if(x < /*orderCount()*/totalLoop - MA_Period)
  176. {
  177. MA[x] = iMAOnArray(ACCOUNTBALANCE, 0, MA_Period, 0, MA_Method, x);
  178. datetime time_val = (datetime)GlobalVariableGet("TIME:" + IntegerToString(x));
  179. string name = "Balance: " + DoubleToString(ACCOUNTBALANCE[x], 2) + "\n" + TimeToString(time_val, TIME_DATE|TIME_SECONDS) + "\n" + DoubleToString(MA[x], 2);
  180. //CreateDotLabel(name, "=", iTime(Symbol(), PERIOD_CURRENT, x), ACCOUNTBALANCE[x], Dot_Color);
  181. }
  182. else
  183. {
  184. datetime time_val = (datetime)GlobalVariableGet("TIME:" + IntegerToString(x));
  185. string name = "Balance: " + DoubleToString(ACCOUNTBALANCE[x], 2) + "\n" + TimeToString(time_val, TIME_DATE|TIME_SECONDS);
  186. //CreateDotLabel(name, "=", iTime(Symbol(), PERIOD_CURRENT, x), ACCOUNTBALANCE[x], Dot_Color);
  187. }
  188. }
  189. TOTAL_HISTORY = currTradeCount;
  190. TT = iTime(Symbol(), PERIOD_CURRENT, 0);
  191. }
  192. }
  193. }
  194. }
  195. //+------------------------------------------------------------------+
  196. //| |
  197. //+------------------------------------------------------------------+
  198. void CreateDotLabel(string name, string text, datetime T1, double P1, color C)
  199. {
  200. int window_x = ChartWindowFind();
  201. if(ObjectFind(0, name) == -1)
  202. {
  203. ObjectCreate(0, name, OBJ_TEXT, window_x, T1, NormalizeDouble(P1, Digits()));
  204. }
  205. ObjectSetInteger(0, name, OBJPROP_ANCHOR, ANCHOR_CENTER);
  206. ObjectSetString(0, name, OBJPROP_TEXT, text);
  207. ObjectSetString(0, name, OBJPROP_FONT, "webdings");
  208. ObjectSetInteger(0, name, OBJPROP_FONTSIZE, 8);
  209. ObjectSetInteger(0, name, OBJPROP_COLOR, C);
  210. ObjectSetDouble(0, name, OBJPROP_PRICE, NormalizeDouble(P1, Digits()));
  211. ObjectSetInteger(0, name, OBJPROP_TIME, T1);
  212. ObjectSetInteger(0, name, OBJPROP_SELECTABLE, false);
  213. }
  214. //+------------------------------------------------------------------+
  215. //| |
  216. //+------------------------------------------------------------------+
  217. void subDeleteObjects(string ObjName)
  218. {
  219. int obj_total = ObjectsTotal(0);
  220. string name;
  221. int i = 0;
  222. for(i = obj_total - 1; i >= 0; i--)
  223. {
  224. name = ObjectName(0, i);
  225. if(StringFind(name, ObjName, 0) != -1)
  226. {
  227. ObjectDelete(0, name);
  228. }
  229. }
  230. }
  231. //+------------------------------------------------------------------+
  232. //| |
  233. //+------------------------------------------------------------------+
  234. double iMAOnArray(double &array[],
  235. int total,
  236. int period,
  237. int ma_shift,
  238. int ma_method,
  239. int shift)
  240. {
  241. double buf[],arr[];
  242. if(total==0)
  243. total=ArraySize(array);
  244. if(total>0 && total<=period)
  245. return(0);
  246. if(shift>total-period-ma_shift)
  247. return(0);
  248. switch(ma_method)
  249. {
  250. case MODE_SMA :
  251. {
  252. total=ArrayCopy(arr,array,0,shift+ma_shift,period);
  253. if(ArrayResize(buf,total)<0)
  254. return(0);
  255. double sum=0;
  256. int i,pos=total-1;
  257. for(i=1;i<period;i++,pos--)
  258. sum+=arr[pos];
  259. while(pos>=0)
  260. {
  261. sum+=arr[pos];
  262. buf[pos]=sum/period;
  263. sum-=arr[pos+period-1];
  264. pos--;
  265. }
  266. return(buf[0]);
  267. }
  268. case MODE_EMA :
  269. {
  270. if(ArrayResize(buf,total)<0)
  271. return(0);
  272. double pr=2.0/(period+1);
  273. int pos=total-2;
  274. while(pos>=0)
  275. {
  276. if(pos==total-2)
  277. {
  278. buf[pos+1]=array[pos+1];
  279. }
  280. buf[pos]=array[pos]*pr+buf[pos+1]*(1-pr);
  281. pos--;
  282. }
  283. return(buf[shift+ma_shift]);
  284. }
  285. case MODE_SMMA :
  286. {
  287. if(ArrayResize(buf,total)<0)
  288. return(0);
  289. double sum=0;
  290. int i,k,pos;
  291. pos=total-period;
  292. while(pos>=0)
  293. {
  294. if(pos==total-period)
  295. {
  296. for(i=0,k=pos;i<period;i++,k++)
  297. {
  298. sum+=array[k];
  299. buf[k]=0;
  300. }
  301. }
  302. else
  303. sum=buf[pos+1]*(period-1)+array[pos];
  304. buf[pos]=sum/period;
  305. pos--;
  306. }
  307. return(buf[shift+ma_shift]);
  308. }
  309. case MODE_LWMA :
  310. {
  311. if(ArrayResize(buf,total)<0)
  312. return(0);
  313. double sum=0.0,lsum=0.0;
  314. double price;
  315. int i,weight=0,pos=total-1;
  316. for(i=1;i<=period;i++,pos--)
  317. {
  318. price=array[pos];
  319. sum+=price*i;
  320. lsum+=price;
  321. weight+=i;
  322. }
  323. pos++;
  324. i=pos+period;
  325. while(pos>=0)
  326. {
  327. buf[pos]=sum/weight;
  328. if(pos==0)
  329. break;
  330. pos--;
  331. i--;
  332. price=array[pos];
  333. sum=sum-lsum+price*period;
  334. lsum-=array[i];
  335. lsum+=price;
  336. }
  337. return(buf[shift+ma_shift]);
  338. }
  339. default:
  340. return(0);
  341. }
  342. return(0);
  343. }
  344. //+------------------------------------------------------------------+
  345. //| |
  346. //+------------------------------------------------------------------+
  347. void parseMagicNumbers()
  348. {
  349. string magicList[];
  350. int count = StringSplit(MagicNumbers, ',', magicList);
  351. //Print("Magic Count: ", count);
  352. if(count > 0)
  353. {
  354. ArrayResize(MagicArray, count);
  355. for(int i = 0; i < count; i++)
  356. {
  357. string cleanMagic = trimString(magicList[i]);
  358. MagicArray[i] = (int)StringToInteger(cleanMagic);
  359. }
  360. }
  361. }
  362. //+------------------------------------------------------------------+
  363. //| |
  364. //+------------------------------------------------------------------+
  365. bool isMagicMatch(long deal_magic)
  366. {
  367. // If "0" Magic Num, process all trades
  368. if(MagicNumbers == "0")
  369. {
  370. return true;
  371. }
  372. for(int i = 0; i < ArraySize(MagicArray); i++)
  373. {
  374. if(deal_magic == MagicArray[i])
  375. return true;
  376. }
  377. return false;
  378. }
  379. //+------------------------------------------------------------------+
  380. //| |
  381. //+------------------------------------------------------------------+
  382. string trimString(string inputt)
  383. {
  384. // Remove spaces from the left and right sides
  385. int start = 0;
  386. int end = StringLen(inputt) - 1;
  387. // Find the first non-space character
  388. while(start <= end && StringGetCharacter(inputt, start) == ' ')
  389. start++;
  390. // Find the last non-space character
  391. while(end >= start && StringGetCharacter(inputt, end) == ' ')
  392. end--;
  393. // Extract the substring without leading or trailing spaces
  394. return StringSubstr(inputt, start, end - start + 1);
  395. }
  396. //+------------------------------------------------------------------+
  397. //| |
  398. //+------------------------------------------------------------------+
  399. void ReadDataFromFile()
  400. {
  401. int handle = FileOpen(filename, FILE_READ | FILE_COMMON);
  402. if(handle == INVALID_HANDLE)
  403. {
  404. Print("ind => Handler invalid: ReadDataFromFile() ", filename," error: ",GetLastError());
  405. return;
  406. }
  407. while(!FileIsEnding(handle))
  408. {
  409. string line = FileReadString(handle);
  410. // Skip empty line
  411. if(StringLen(line) < 2)
  412. continue;
  413. string parts[];
  414. int count = StringSplit(line, ',', parts);
  415. int ticket = (int)StringToInteger(parts[0]);
  416. int type = (int)StringToInteger(parts[1]);
  417. int magic = (int)StringToInteger(parts[2]);
  418. double open_price = StringToDouble(parts[3]);
  419. double close_price = StringToDouble(parts[4]);
  420. datetime open_time = StringToTime(parts[5]);
  421. datetime close_time = StringToTime(parts[6]);
  422. double sl = StringToDouble(parts[7]);
  423. double tp = StringToDouble(parts[8]);
  424. double lot = StringToDouble(parts[9]);
  425. double profit = StringToDouble(parts[10]);
  426. // Print or use values
  427. Print("READ: ticket=", ticket,
  428. " type=", type,
  429. " magic=", magic,
  430. " open=", open_price,
  431. " close=", close_price,
  432. " open_time=", open_time,
  433. " close_time=", close_time,
  434. " sl=", sl,
  435. " tp=", tp,
  436. " lot=", lot,
  437. " profit=", profit);
  438. }
  439. FileClose(handle);
  440. }
  441. //+------------------------------------------------------------------+
  442. //| |
  443. //+------------------------------------------------------------------+
  444. int orderCount()
  445. {
  446. int index = -1;
  447. if(GlobalVariableGet(global) == 0)
  448. {
  449. int handle = FileOpen(filename, FILE_READ | FILE_COMMON);
  450. if(handle == INVALID_HANDLE)
  451. {
  452. Print("ind => Handler invalid: orderCount() ", filename," error: ",GetLastError());
  453. return 0;
  454. }
  455. while(!FileIsEnding(handle))
  456. {
  457. string line = FileReadString(handle);
  458. // Skip empty line
  459. if(StringLen(line) < 2)
  460. continue;
  461. string parts[];
  462. int count = StringSplit(line, ',', parts);
  463. int ticket = (int)StringToInteger(parts[0]);
  464. int type = (int)StringToInteger(parts[1]);
  465. int magic = (int)StringToInteger(parts[2]);
  466. double open_price = StringToDouble(parts[3]);
  467. double close_price = StringToDouble(parts[4]);
  468. datetime open_time = StringToTime(parts[5]);
  469. datetime close_time = StringToTime(parts[6]);
  470. double sl = StringToDouble(parts[7]);
  471. double tp = StringToDouble(parts[8]);
  472. double lot = StringToDouble(parts[9]);
  473. double profit = StringToDouble(parts[10]);
  474. index++;
  475. }
  476. FileClose(handle);
  477. }
  478. return index;
  479. }
  480. //+------------------------------------------------------------------+
  481. //| |
  482. //+------------------------------------------------------------------+
  483. void DeleteStartBalanceObjects()
  484. {
  485. int total = ObjectsTotal(0);
  486. // Loop from last to first (important for deletion)
  487. for(int i = total - 1; i >= 0; i--)
  488. {
  489. string objName = ObjectName(0, i);
  490. // Check if object name starts with "Start Balance:"
  491. if(StringFind(objName,"Start Balance:") > 0 || StringFind(objName,"Balance:") > 0)
  492. {
  493. ObjectDelete(0, objName);
  494. }
  495. }
  496. }
  497. //+------------------------------------------------------------------+