No Description

Pulse Balance Indicator Sim.mq5 38KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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"+Symbol()
  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. GlobalVariableSet(global,1);
  133. int handle = FileOpen(filename, FILE_READ | FILE_COMMON);
  134. // Print("startIndexToConsiderTrade: ",startIndexToConsiderTrade);
  135. if(handle == INVALID_HANDLE)
  136. {
  137. Print("ind => Handler invalid: MyStart() ", filename," error: ",GetLastError());
  138. GlobalVariableSet(global,0);
  139. return;
  140. }
  141. while(!FileIsEnding(handle))
  142. {
  143. string line = FileReadString(handle);
  144. // Skip empty line
  145. if(StringLen(line) < 2)
  146. continue;
  147. string parts[];
  148. int count = StringSplit(line, ',', parts);
  149. int ticket = (int)StringToInteger(parts[0]);
  150. int type = (int)StringToInteger(parts[1]);
  151. int magic = (int)StringToInteger(parts[2]);
  152. double open_price = StringToDouble(parts[3]);
  153. double close_price = StringToDouble(parts[4]);
  154. datetime open_time = StringToTime(parts[5]);
  155. datetime close_time = StringToTime(parts[6]);
  156. double sl = StringToDouble(parts[7]);
  157. double tp = StringToDouble(parts[8]);
  158. double lot = StringToDouble(parts[9]);
  159. double profit = StringToDouble(parts[10]);
  160. if(isMagicMatch(magic) && ticket >= startIndexToConsiderTrade && total > 0)
  161. {
  162. // Print(" startIndexToConsiderTrade ",startIndexToConsiderTrade," ticket = ",ticket," total + 1: ",total + 1," totalOrderCount ",totalOrderCount," historyTrades = ",historyTrades);
  163. double deal_profit = profit;
  164. ACCOUNTBALANCE[total + 1] = BAL;
  165. string name = "Start Balance: " + DoubleToString(ACCOUNTBALANCE[total + 1], 2) + "\n" + TimeToString(close_time, TIME_DATE|TIME_SECONDS) + "\n";
  166. CreateDotLabel(name, "=", iTime(Symbol(), PERIOD_CURRENT, total + 1), ACCOUNTBALANCE[total + 1], Dot_Color);
  167. BAL = BAL + profit;
  168. ACCOUNTBALANCE[total] = BAL;
  169. GlobalVariableSet("TIME:" + IntegerToString(total), close_time);
  170. total--;
  171. }
  172. }
  173. FileClose(handle);
  174. GlobalVariableSet(global,0);
  175. // Calculate moving averages and create labels
  176. for(int x = totalLoop; x > 0/*startIndexToConsiderTrade*/; x--)
  177. {
  178. if(x < /*orderCount()*/totalLoop - MA_Period)
  179. {
  180. MA[x] = iMAOnArray(ACCOUNTBALANCE, 0, MA_Period, 0, MA_Method, x);
  181. datetime time_val = (datetime)GlobalVariableGet("TIME:" + IntegerToString(x));
  182. string name = "Balance: " + DoubleToString(ACCOUNTBALANCE[x], 2) + "\n" + TimeToString(time_val, TIME_DATE|TIME_SECONDS) + "\n" + DoubleToString(MA[x], 2);
  183. //CreateDotLabel(name, "=", iTime(Symbol(), PERIOD_CURRENT, x), ACCOUNTBALANCE[x], Dot_Color);
  184. }
  185. else
  186. {
  187. datetime time_val = (datetime)GlobalVariableGet("TIME:" + IntegerToString(x));
  188. string name = "Balance: " + DoubleToString(ACCOUNTBALANCE[x], 2) + "\n" + TimeToString(time_val, TIME_DATE|TIME_SECONDS);
  189. //CreateDotLabel(name, "=", iTime(Symbol(), PERIOD_CURRENT, x), ACCOUNTBALANCE[x], Dot_Color);
  190. }
  191. }
  192. TOTAL_HISTORY = currTradeCount;
  193. TT = iTime(Symbol(), PERIOD_CURRENT, 0);
  194. }
  195. }
  196. }
  197. }
  198. //+------------------------------------------------------------------+
  199. //| |
  200. //+------------------------------------------------------------------+
  201. void CreateDotLabel(string name, string text, datetime T1, double P1, color C)
  202. {
  203. int window_x = ChartWindowFind();
  204. if(ObjectFind(0, name) == -1)
  205. {
  206. ObjectCreate(0, name, OBJ_TEXT, window_x, T1, NormalizeDouble(P1, Digits()));
  207. }
  208. ObjectSetInteger(0, name, OBJPROP_ANCHOR, ANCHOR_CENTER);
  209. ObjectSetString(0, name, OBJPROP_TEXT, text);
  210. ObjectSetString(0, name, OBJPROP_FONT, "webdings");
  211. ObjectSetInteger(0, name, OBJPROP_FONTSIZE, 8);
  212. ObjectSetInteger(0, name, OBJPROP_COLOR, C);
  213. ObjectSetDouble(0, name, OBJPROP_PRICE, NormalizeDouble(P1, Digits()));
  214. ObjectSetInteger(0, name, OBJPROP_TIME, T1);
  215. ObjectSetInteger(0, name, OBJPROP_SELECTABLE, false);
  216. }
  217. //+------------------------------------------------------------------+
  218. //| |
  219. //+------------------------------------------------------------------+
  220. void subDeleteObjects(string ObjName)
  221. {
  222. int obj_total = ObjectsTotal(0);
  223. string name;
  224. int i = 0;
  225. for(i = obj_total - 1; i >= 0; i--)
  226. {
  227. name = ObjectName(0, i);
  228. if(StringFind(name, ObjName, 0) != -1)
  229. {
  230. ObjectDelete(0, name);
  231. }
  232. }
  233. }
  234. //+------------------------------------------------------------------+
  235. //| |
  236. //+------------------------------------------------------------------+
  237. double iMAOnArray(double &array[],
  238. int total,
  239. int period,
  240. int ma_shift,
  241. int ma_method,
  242. int shift)
  243. {
  244. double buf[],arr[];
  245. if(total==0)
  246. total=ArraySize(array);
  247. if(total>0 && total<=period)
  248. return(0);
  249. if(shift>total-period-ma_shift)
  250. return(0);
  251. switch(ma_method)
  252. {
  253. case MODE_SMA :
  254. {
  255. total=ArrayCopy(arr,array,0,shift+ma_shift,period);
  256. if(ArrayResize(buf,total)<0)
  257. return(0);
  258. double sum=0;
  259. int i,pos=total-1;
  260. for(i=1;i<period;i++,pos--)
  261. sum+=arr[pos];
  262. while(pos>=0)
  263. {
  264. sum+=arr[pos];
  265. buf[pos]=sum/period;
  266. sum-=arr[pos+period-1];
  267. pos--;
  268. }
  269. return(buf[0]);
  270. }
  271. case MODE_EMA :
  272. {
  273. if(ArrayResize(buf,total)<0)
  274. return(0);
  275. double pr=2.0/(period+1);
  276. int pos=total-2;
  277. while(pos>=0)
  278. {
  279. if(pos==total-2)
  280. {
  281. buf[pos+1]=array[pos+1];
  282. }
  283. buf[pos]=array[pos]*pr+buf[pos+1]*(1-pr);
  284. pos--;
  285. }
  286. return(buf[shift+ma_shift]);
  287. }
  288. case MODE_SMMA :
  289. {
  290. if(ArrayResize(buf,total)<0)
  291. return(0);
  292. double sum=0;
  293. int i,k,pos;
  294. pos=total-period;
  295. while(pos>=0)
  296. {
  297. if(pos==total-period)
  298. {
  299. for(i=0,k=pos;i<period;i++,k++)
  300. {
  301. sum+=array[k];
  302. buf[k]=0;
  303. }
  304. }
  305. else
  306. sum=buf[pos+1]*(period-1)+array[pos];
  307. buf[pos]=sum/period;
  308. pos--;
  309. }
  310. return(buf[shift+ma_shift]);
  311. }
  312. case MODE_LWMA :
  313. {
  314. if(ArrayResize(buf,total)<0)
  315. return(0);
  316. double sum=0.0,lsum=0.0;
  317. double price;
  318. int i,weight=0,pos=total-1;
  319. for(i=1;i<=period;i++,pos--)
  320. {
  321. price=array[pos];
  322. sum+=price*i;
  323. lsum+=price;
  324. weight+=i;
  325. }
  326. pos++;
  327. i=pos+period;
  328. while(pos>=0)
  329. {
  330. buf[pos]=sum/weight;
  331. if(pos==0)
  332. break;
  333. pos--;
  334. i--;
  335. price=array[pos];
  336. sum=sum-lsum+price*period;
  337. lsum-=array[i];
  338. lsum+=price;
  339. }
  340. return(buf[shift+ma_shift]);
  341. }
  342. default:
  343. return(0);
  344. }
  345. return(0);
  346. }
  347. //+------------------------------------------------------------------+
  348. //| |
  349. //+------------------------------------------------------------------+
  350. void parseMagicNumbers()
  351. {
  352. string magicList[];
  353. int count = StringSplit(MagicNumbers, ',', magicList);
  354. //Print("Magic Count: ", count);
  355. if(count > 0)
  356. {
  357. ArrayResize(MagicArray, count);
  358. for(int i = 0; i < count; i++)
  359. {
  360. string cleanMagic = trimString(magicList[i]);
  361. MagicArray[i] = (int)StringToInteger(cleanMagic);
  362. }
  363. }
  364. }
  365. //+------------------------------------------------------------------+
  366. //| |
  367. //+------------------------------------------------------------------+
  368. bool isMagicMatch(long deal_magic)
  369. {
  370. // If "0" Magic Num, process all trades
  371. if(MagicNumbers == "0")
  372. {
  373. return true;
  374. }
  375. for(int i = 0; i < ArraySize(MagicArray); i++)
  376. {
  377. if(deal_magic == MagicArray[i])
  378. return true;
  379. }
  380. return false;
  381. }
  382. //+------------------------------------------------------------------+
  383. //| |
  384. //+------------------------------------------------------------------+
  385. string trimString(string inputt)
  386. {
  387. // Remove spaces from the left and right sides
  388. int start = 0;
  389. int end = StringLen(inputt) - 1;
  390. // Find the first non-space character
  391. while(start <= end && StringGetCharacter(inputt, start) == ' ')
  392. start++;
  393. // Find the last non-space character
  394. while(end >= start && StringGetCharacter(inputt, end) == ' ')
  395. end--;
  396. // Extract the substring without leading or trailing spaces
  397. return StringSubstr(inputt, start, end - start + 1);
  398. }
  399. //+------------------------------------------------------------------+
  400. //| |
  401. //+------------------------------------------------------------------+
  402. void ReadDataFromFile()
  403. {
  404. int handle = FileOpen(filename, FILE_READ | FILE_COMMON);
  405. if(handle == INVALID_HANDLE)
  406. {
  407. Print("ind => Handler invalid: ReadDataFromFile() ", filename," error: ",GetLastError());
  408. GlobalVariableSet(global,0);
  409. return;
  410. }
  411. while(!FileIsEnding(handle))
  412. {
  413. string line = FileReadString(handle);
  414. // Skip empty line
  415. if(StringLen(line) < 2)
  416. continue;
  417. string parts[];
  418. int count = StringSplit(line, ',', parts);
  419. int ticket = (int)StringToInteger(parts[0]);
  420. int type = (int)StringToInteger(parts[1]);
  421. int magic = (int)StringToInteger(parts[2]);
  422. double open_price = StringToDouble(parts[3]);
  423. double close_price = StringToDouble(parts[4]);
  424. datetime open_time = StringToTime(parts[5]);
  425. datetime close_time = StringToTime(parts[6]);
  426. double sl = StringToDouble(parts[7]);
  427. double tp = StringToDouble(parts[8]);
  428. double lot = StringToDouble(parts[9]);
  429. double profit = StringToDouble(parts[10]);
  430. // Print or use values
  431. Print("READ: ticket=", ticket,
  432. " type=", type,
  433. " magic=", magic,
  434. " open=", open_price,
  435. " close=", close_price,
  436. " open_time=", open_time,
  437. " close_time=", close_time,
  438. " sl=", sl,
  439. " tp=", tp,
  440. " lot=", lot,
  441. " profit=", profit);
  442. }
  443. FileClose(handle);
  444. }
  445. //+------------------------------------------------------------------+
  446. //| |
  447. //+------------------------------------------------------------------+
  448. int orderCount()
  449. {
  450. int index = -1;
  451. if(GlobalVariableGet(global) == 0)
  452. {
  453. GlobalVariableSet(global,1);
  454. int handle = FileOpen(filename, FILE_READ | FILE_COMMON);
  455. if(handle == INVALID_HANDLE)
  456. {
  457. Print("ind => Handler invalid: orderCount() ", filename," error: ",GetLastError());
  458. GlobalVariableSet(global,0);
  459. return 0;
  460. }
  461. while(!FileIsEnding(handle))
  462. {
  463. string line = FileReadString(handle);
  464. // Skip empty line
  465. if(StringLen(line) < 2)
  466. continue;
  467. string parts[];
  468. int count = StringSplit(line, ',', parts);
  469. int ticket = (int)StringToInteger(parts[0]);
  470. int type = (int)StringToInteger(parts[1]);
  471. int magic = (int)StringToInteger(parts[2]);
  472. double open_price = StringToDouble(parts[3]);
  473. double close_price = StringToDouble(parts[4]);
  474. datetime open_time = StringToTime(parts[5]);
  475. datetime close_time = StringToTime(parts[6]);
  476. double sl = StringToDouble(parts[7]);
  477. double tp = StringToDouble(parts[8]);
  478. double lot = StringToDouble(parts[9]);
  479. double profit = StringToDouble(parts[10]);
  480. index++;
  481. }
  482. FileClose(handle);
  483. GlobalVariableSet(global,0);
  484. }
  485. return index;
  486. }
  487. //+------------------------------------------------------------------+
  488. //| |
  489. //+------------------------------------------------------------------+
  490. void DeleteStartBalanceObjects()
  491. {
  492. int total = ObjectsTotal(0);
  493. // Loop from last to first (important for deletion)
  494. for(int i = total - 1; i >= 0; i--)
  495. {
  496. string objName = ObjectName(0, i);
  497. // Check if object name starts with "Start Balance:"
  498. if(StringFind(objName,"Start Balance:") > 0 || StringFind(objName,"Balance:") > 0)
  499. {
  500. ObjectDelete(0, objName);
  501. }
  502. }
  503. }
  504. //+------------------------------------------------------------------+