Bez popisu

Pulse Balance Indicator.mq5 30KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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.00"
  9. #property indicator_separate_window
  10. #property indicator_buffers 3
  11. #property indicator_plots 3
  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. #property indicator_label3 "MA2"
  22. #property indicator_type3 DRAW_LINE
  23. #property indicator_color3 clrGold
  24. #property indicator_width3 1
  25. #property indicator_style3 STYLE_DASH
  26. input color Dot_Color = clrOrangeRed;
  27. input double StartingBalance = 10000;
  28. input string MagicNumbers = "0";
  29. input int MA_Period = 5;
  30. input ENUM_MA_METHOD MA_Method = MODE_SMA;
  31. input int PastHistory = 30; // Number of most recent closed trades to include
  32. double ACCOUNTBALANCE[];
  33. double MA[];
  34. int TOTAL_HISTORY = 0;
  35. datetime TT = 0;
  36. int MagicArray[];
  37. //+------------------------------------------------------------------+
  38. //| Custom indicator initialization function |
  39. //+------------------------------------------------------------------+
  40. int OnInit()
  41. {
  42. //--- indicator buffers mapping
  43. EventSetTimer(1);
  44. parseMagicNumbers();
  45. SetIndexBuffer(0, ACCOUNTBALANCE, INDICATOR_DATA);
  46. SetIndexBuffer(1, MA, INDICATOR_DATA);
  47. ArraySetAsSeries(ACCOUNTBALANCE, true);
  48. ArraySetAsSeries(MA, true);
  49. TOTAL_HISTORY = 0;
  50. TT = 0;
  51. //---
  52. return(INIT_SUCCEEDED);
  53. }
  54. //+------------------------------------------------------------------+
  55. //| Custom indicator deinitialization function |
  56. //+------------------------------------------------------------------+
  57. void OnDeinit(const int reason)
  58. {
  59. //--- destroy timer
  60. EventKillTimer();
  61. subDeleteObjects("Balance: ");
  62. subDeleteObjects("Start Balance: ");
  63. }
  64. //+------------------------------------------------------------------+
  65. //| Custom indicator iteration function |
  66. //+------------------------------------------------------------------+
  67. int OnCalculate(const int rates_total,
  68. const int prev_calculated,
  69. const datetime &time[],
  70. const double &open[],
  71. const double &high[],
  72. const double &low[],
  73. const double &close[],
  74. const long &tick_volume[],
  75. const long &volume[],
  76. const int &spread[])
  77. {
  78. //---
  79. MyStart(rates_total, prev_calculated);
  80. //--- return value of prev_calculated for next call
  81. return(rates_total);
  82. }
  83. //+------------------------------------------------------------------+
  84. //| Timer function |
  85. //+------------------------------------------------------------------+
  86. void OnTimer()
  87. {
  88. //MyStart();
  89. }
  90. //+------------------------------------------------------------------+
  91. //| |
  92. //+------------------------------------------------------------------+
  93. void MyStart(int ratesTotal, int prevCalculated)
  94. {
  95. if(TOTAL_HISTORY != HistoryDealsTotal() || TT != iTime(Symbol(), PERIOD_CURRENT, 0))
  96. {
  97. int limit = 0;
  98. if(prevCalculated == 0)
  99. {
  100. limit = ratesTotal - prevCalculated -2;
  101. }
  102. else
  103. {
  104. limit = ratesTotal - prevCalculated;
  105. }
  106. //subDeleteObjects("Balance: ");
  107. //subDeleteObjects("Start Balance: ");
  108. for(int i=limit; i>= 0; i--)
  109. {
  110. ACCOUNTBALANCE[i] = EMPTY_VALUE;
  111. MA[i] = EMPTY_VALUE;
  112. }
  113. if(!HistorySelect(0, TimeCurrent()))
  114. return;
  115. int deals = HistoryDealsTotal();
  116. int total = 0;
  117. int total2 = 0;
  118. int ticket = 0;
  119. double AB = StartingBalance;
  120. double BAL = AB;
  121. Print("ACCOUNT BALANCE: " + DoubleToString(AB));
  122. int tradesCount = 0;
  123. // First pass to count relevant deals
  124. //for(int i = 0; i < deals; i++)
  125. for(int i = deals - 1; i >= 0; i--)
  126. {
  127. ulong deal_ticket = HistoryDealGetTicket(i);
  128. if(deal_ticket == 0)
  129. {
  130. Print("Error getting deal ticket!");
  131. break;
  132. }
  133. long deal_magic = HistoryDealGetInteger(deal_ticket, DEAL_MAGIC);
  134. long deal_type = HistoryDealGetInteger(deal_ticket, DEAL_TYPE);
  135. // Filter for position deals (open/close) with matching magic number
  136. if(HistoryDealGetInteger(deal_ticket, DEAL_ENTRY) == DEAL_ENTRY_OUT)
  137. {
  138. if((deal_type == DEAL_TYPE_BUY || deal_type == DEAL_TYPE_SELL) && isMagicMatch(deal_magic))
  139. {
  140. total2++;
  141. tradesCount++;
  142. if(tradesCount >= PastHistory)
  143. break;
  144. }
  145. }
  146. }
  147. total = total2;
  148. tradesCount = 0;
  149. // Second pass to calculate balance
  150. //for(int i = 0; i < deals; i++)
  151. for(int i = deals - 1; i >= 0; i--)
  152. {
  153. ulong deal_ticket = HistoryDealGetTicket(i);
  154. if(deal_ticket == 0)
  155. {
  156. Print("Error getting deal ticket!");
  157. break;
  158. }
  159. long deal_magic = HistoryDealGetInteger(deal_ticket, DEAL_MAGIC);
  160. long deal_type = HistoryDealGetInteger(deal_ticket, DEAL_TYPE);
  161. datetime deal_time = (datetime)HistoryDealGetInteger(deal_ticket, DEAL_TIME);
  162. //dealout check add
  163. if(HistoryDealGetInteger(deal_ticket, DEAL_ENTRY) == DEAL_ENTRY_OUT)
  164. {
  165. if((deal_type == DEAL_TYPE_BUY || deal_type == DEAL_TYPE_SELL) && isMagicMatch(deal_magic))
  166. {
  167. tradesCount++;
  168. double deal_profit = HistoryDealGetDouble(deal_ticket, DEAL_PROFIT);
  169. double deal_swap = HistoryDealGetDouble(deal_ticket, DEAL_SWAP);
  170. double deal_commission = HistoryDealGetDouble(deal_ticket, DEAL_COMMISSION);
  171. double profit = deal_profit + deal_swap + deal_commission;
  172. if(total == total2)
  173. {
  174. ACCOUNTBALANCE[total + 1] = BAL;
  175. string name = "Start Balance: " + DoubleToString(ACCOUNTBALANCE[total + 1], 2) + "\n" + TimeToString(deal_time, TIME_DATE|TIME_SECONDS) + "\n";
  176. CreateDotLabel(name, "=", iTime(Symbol(), PERIOD_CURRENT, total + 1), ACCOUNTBALANCE[total + 1], Dot_Color);
  177. }
  178. BAL = BAL + profit;
  179. ACCOUNTBALANCE[total] = BAL;
  180. GlobalVariableSet("TIME:" + IntegerToString(total), deal_time);
  181. total--;
  182. if(tradesCount >= PastHistory)
  183. break;
  184. }
  185. }
  186. }
  187. // Calculate moving averages and create labels
  188. for(int x = total2; x > 0; x--)
  189. {
  190. if(x < total2 - MA_Period)
  191. {
  192. MA[x] = iMAOnArray(ACCOUNTBALANCE, 0, MA_Period, 0, MA_Method, x);
  193. datetime time_val = (datetime)GlobalVariableGet("TIME:" + IntegerToString(x));
  194. string name = "Balance: " + DoubleToString(ACCOUNTBALANCE[x], 2) + "\n" + TimeToString(time_val, TIME_DATE|TIME_SECONDS) + "\n" + DoubleToString(MA[x], 2);
  195. CreateDotLabel(name, "=", iTime(Symbol(), PERIOD_CURRENT, x), ACCOUNTBALANCE[x], Dot_Color);
  196. }
  197. else
  198. {
  199. datetime time_val = (datetime)GlobalVariableGet("TIME:" + IntegerToString(x));
  200. string name = "Balance: " + DoubleToString(ACCOUNTBALANCE[x], 2) + "\n" + TimeToString(time_val, TIME_DATE|TIME_SECONDS);
  201. CreateDotLabel(name, "=", iTime(Symbol(), PERIOD_CURRENT, x), ACCOUNTBALANCE[x], Dot_Color);
  202. }
  203. }
  204. TOTAL_HISTORY = HistoryDealsTotal();
  205. TT = iTime(Symbol(), PERIOD_CURRENT, 0);
  206. }
  207. }
  208. //+------------------------------------------------------------------+
  209. //| |
  210. //+------------------------------------------------------------------+
  211. void CreateDotLabel(string name, string text, datetime T1, double P1, color C)
  212. {
  213. int window_x = ChartWindowFind();
  214. //Print("window_x",window_x);
  215. if(ObjectFind(0, name) == -1)
  216. {
  217. ObjectCreate(0, name, OBJ_TEXT, window_x, T1, NormalizeDouble(P1, Digits()));
  218. }
  219. ObjectSetInteger(0, name, OBJPROP_ANCHOR, ANCHOR_CENTER);
  220. ObjectSetString(0, name, OBJPROP_TEXT, text);
  221. ObjectSetString(0, name, OBJPROP_FONT, "webdings");
  222. ObjectSetInteger(0, name, OBJPROP_FONTSIZE, 8);
  223. ObjectSetInteger(0, name, OBJPROP_COLOR, C);
  224. ObjectSetDouble(0, name, OBJPROP_PRICE, NormalizeDouble(P1, Digits()));
  225. ObjectSetInteger(0, name, OBJPROP_TIME, T1);
  226. ObjectSetInteger(0, name, OBJPROP_SELECTABLE, false);
  227. }
  228. //+------------------------------------------------------------------+
  229. //| |
  230. //+------------------------------------------------------------------+
  231. void subDeleteObjects(string ObjName)
  232. {
  233. int obj_total = ObjectsTotal(0);
  234. string name;
  235. int i = 0;
  236. for(i = obj_total - 1; i >= 0; i--)
  237. {
  238. name = ObjectName(0, i);
  239. if(StringFind(name, ObjName, 0) != -1)
  240. {
  241. ObjectDelete(0, name);
  242. }
  243. }
  244. }
  245. //+------------------------------------------------------------------+
  246. //| |
  247. //+------------------------------------------------------------------+
  248. double iMAOnArray(double &array[],
  249. int total,
  250. int period,
  251. int ma_shift,
  252. int ma_method,
  253. int shift)
  254. {
  255. double buf[],arr[];
  256. if(total==0)
  257. total=ArraySize(array);
  258. if(total>0 && total<=period)
  259. return(0);
  260. if(shift>total-period-ma_shift)
  261. return(0);
  262. switch(ma_method)
  263. {
  264. case MODE_SMA :
  265. {
  266. total=ArrayCopy(arr,array,0,shift+ma_shift,period);
  267. if(ArrayResize(buf,total)<0)
  268. return(0);
  269. double sum=0;
  270. int i,pos=total-1;
  271. for(i=1;i<period;i++,pos--)
  272. sum+=arr[pos];
  273. while(pos>=0)
  274. {
  275. sum+=arr[pos];
  276. buf[pos]=sum/period;
  277. sum-=arr[pos+period-1];
  278. pos--;
  279. }
  280. return(buf[0]);
  281. }
  282. case MODE_EMA :
  283. {
  284. if(ArrayResize(buf,total)<0)
  285. return(0);
  286. double pr=2.0/(period+1);
  287. int pos=total-2;
  288. while(pos>=0)
  289. {
  290. if(pos==total-2)
  291. {
  292. buf[pos+1]=array[pos+1];
  293. }
  294. buf[pos]=array[pos]*pr+buf[pos+1]*(1-pr);
  295. pos--;
  296. }
  297. return(buf[shift+ma_shift]);
  298. }
  299. case MODE_SMMA :
  300. {
  301. if(ArrayResize(buf,total)<0)
  302. return(0);
  303. double sum=0;
  304. int i,k,pos;
  305. pos=total-period;
  306. while(pos>=0)
  307. {
  308. if(pos==total-period)
  309. {
  310. for(i=0,k=pos;i<period;i++,k++)
  311. {
  312. sum+=array[k];
  313. buf[k]=0;
  314. }
  315. }
  316. else
  317. sum=buf[pos+1]*(period-1)+array[pos];
  318. buf[pos]=sum/period;
  319. pos--;
  320. }
  321. return(buf[shift+ma_shift]);
  322. }
  323. case MODE_LWMA :
  324. {
  325. if(ArrayResize(buf,total)<0)
  326. return(0);
  327. double sum=0.0,lsum=0.0;
  328. double price;
  329. int i,weight=0,pos=total-1;
  330. for(i=1;i<=period;i++,pos--)
  331. {
  332. price=array[pos];
  333. sum+=price*i;
  334. lsum+=price;
  335. weight+=i;
  336. }
  337. pos++;
  338. i=pos+period;
  339. while(pos>=0)
  340. {
  341. buf[pos]=sum/weight;
  342. if(pos==0)
  343. break;
  344. pos--;
  345. i--;
  346. price=array[pos];
  347. sum=sum-lsum+price*period;
  348. lsum-=array[i];
  349. lsum+=price;
  350. }
  351. return(buf[shift+ma_shift]);
  352. }
  353. default:
  354. return(0);
  355. }
  356. return(0);
  357. }
  358. //+------------------------------------------------------------------+
  359. //| |
  360. //+------------------------------------------------------------------+
  361. void parseMagicNumbers()
  362. {
  363. string magicList[];
  364. int count = StringSplit(MagicNumbers, ',', magicList);
  365. //Print("Magic Count: ", count);
  366. if(count > 0)
  367. {
  368. ArrayResize(MagicArray, count);
  369. for(int i = 0; i < count; i++)
  370. {
  371. string cleanMagic = trimString(magicList[i]);
  372. MagicArray[i] = (int)StringToInteger(cleanMagic);
  373. //Print("Magic no: ", MagicArray[i]);
  374. }
  375. }
  376. }
  377. //+------------------------------------------------------------------+
  378. //| |
  379. //+------------------------------------------------------------------+
  380. bool isMagicMatch(long deal_magic)
  381. {
  382. for(int i = 0; i < ArraySize(MagicArray); i++)
  383. {
  384. if(deal_magic == MagicArray[i])
  385. return true;
  386. }
  387. return false;
  388. }
  389. //+------------------------------------------------------------------+
  390. //| |
  391. //+------------------------------------------------------------------+
  392. string trimString(string inputt)
  393. {
  394. // Remove spaces from the left and right sides
  395. int start = 0;
  396. int end = StringLen(inputt) - 1;
  397. // Find the first non-space character
  398. while(start <= end && StringGetCharacter(inputt, start) == ' ')
  399. start++;
  400. // Find the last non-space character
  401. while(end >= start && StringGetCharacter(inputt, end) == ' ')
  402. end--;
  403. // Extract the substring without leading or trailing spaces
  404. return StringSubstr(inputt, start, end - start + 1);
  405. }
  406. //+------------------------------------------------------------------+