Нет описания

Pulse Balance Indicator Sim.mq5 36KB

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