No Description

Pulse Balance Indicator Sim.mq5 33KB

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