No Description

Pulse Balance Indicator Sim.mq5 38KB

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