Nav apraksta

Pulse Balance Indicator.mq5 30KB

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