Nessuna descrizione

Pulse Balance Indicator Sim.mq5 38KB

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