Без опису

Pulse Balance Indicator.mq5 31KB

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