Sen descrición

Pulse Balance Indicator Sim.mq5 33KB

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