Brak opisu

Pulse Balance Indicator.mq5 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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 int MagicNumber = 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. //+------------------------------------------------------------------+
  37. //| Custom indicator initialization function |
  38. //+------------------------------------------------------------------+
  39. int OnInit()
  40. {
  41. //--- indicator buffers mapping
  42. EventSetTimer(1);
  43. SetIndexBuffer(0, ACCOUNTBALANCE, INDICATOR_DATA);
  44. SetIndexBuffer(1, MA, INDICATOR_DATA);
  45. ArraySetAsSeries(ACCOUNTBALANCE, true);
  46. ArraySetAsSeries(MA, true);
  47. TOTAL_HISTORY = 0;
  48. TT = 0;
  49. //---
  50. return(INIT_SUCCEEDED);
  51. }
  52. //+------------------------------------------------------------------+
  53. //| Custom indicator deinitialization function |
  54. //+------------------------------------------------------------------+
  55. void OnDeinit(const int reason)
  56. {
  57. //--- destroy timer
  58. EventKillTimer();
  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. //| Timer function |
  83. //+------------------------------------------------------------------+
  84. void OnTimer()
  85. {
  86. //MyStart();
  87. }
  88. //+------------------------------------------------------------------+
  89. //| |
  90. //+------------------------------------------------------------------+
  91. void MyStart(int ratesTotal, int prevCalculated)
  92. {
  93. if(TOTAL_HISTORY != HistoryDealsTotal() || TT != iTime(Symbol(), PERIOD_CURRENT, 0))
  94. {
  95. int limit = 0;
  96. if(prevCalculated == 0)
  97. {
  98. limit = ratesTotal - prevCalculated -2;
  99. }
  100. else
  101. {
  102. limit = ratesTotal - prevCalculated;
  103. }
  104. //subDeleteObjects("Balance: ");
  105. //subDeleteObjects("Start Balance: ");
  106. for(int i=limit; i>= 0; i--)
  107. {
  108. ACCOUNTBALANCE[i] = EMPTY_VALUE;
  109. MA[i] = EMPTY_VALUE;
  110. }
  111. if(!HistorySelect(0, TimeCurrent()))
  112. return;
  113. int deals = HistoryDealsTotal();
  114. int total = 0;
  115. int total2 = 0;
  116. int ticket = 0;
  117. double AB = StartingBalance;
  118. double BAL = AB;
  119. Print("ACCOUNT BALANCE: " + DoubleToString(AB));
  120. int tradesCount = 0;
  121. // First pass to count relevant deals
  122. //for(int i = 0; i < deals; i++)
  123. for(int i = deals - 1; i >= 0; i--)
  124. {
  125. ulong deal_ticket = HistoryDealGetTicket(i);
  126. if(deal_ticket == 0)
  127. {
  128. Print("Error getting deal ticket!");
  129. break;
  130. }
  131. long deal_magic = HistoryDealGetInteger(deal_ticket, DEAL_MAGIC);
  132. long deal_type = HistoryDealGetInteger(deal_ticket, DEAL_TYPE);
  133. // Filter for position deals (open/close) with matching magic number
  134. if(HistoryDealGetInteger(deal_ticket, DEAL_ENTRY) == DEAL_ENTRY_OUT)
  135. {
  136. if((deal_type == DEAL_TYPE_BUY || deal_type == DEAL_TYPE_SELL) && deal_magic == MagicNumber)
  137. {
  138. total2++;
  139. tradesCount++;
  140. if(tradesCount >= PastHistory)
  141. break;
  142. }
  143. }
  144. }
  145. total = total2;
  146. tradesCount = 0;
  147. // Second pass to calculate balance
  148. //for(int i = 0; i < deals; i++)
  149. for(int i = deals - 1; i >= 0; i--)
  150. {
  151. ulong deal_ticket = HistoryDealGetTicket(i);
  152. if(deal_ticket == 0)
  153. {
  154. Print("Error getting deal ticket!");
  155. break;
  156. }
  157. long deal_magic = HistoryDealGetInteger(deal_ticket, DEAL_MAGIC);
  158. long deal_type = HistoryDealGetInteger(deal_ticket, DEAL_TYPE);
  159. datetime deal_time = (datetime)HistoryDealGetInteger(deal_ticket, DEAL_TIME);
  160. //dealout check add
  161. if(HistoryDealGetInteger(deal_ticket, DEAL_ENTRY) == DEAL_ENTRY_OUT)
  162. {
  163. if((deal_type == DEAL_TYPE_BUY || deal_type == DEAL_TYPE_SELL) && deal_magic == MagicNumber)
  164. {
  165. tradesCount++;
  166. double deal_profit = HistoryDealGetDouble(deal_ticket, DEAL_PROFIT);
  167. double deal_swap = HistoryDealGetDouble(deal_ticket, DEAL_SWAP);
  168. double deal_commission = HistoryDealGetDouble(deal_ticket, DEAL_COMMISSION);
  169. double profit = deal_profit + deal_swap + deal_commission;
  170. if(total == total2)
  171. {
  172. ACCOUNTBALANCE[total + 1] = BAL;
  173. string name = "Start Balance: " + DoubleToString(ACCOUNTBALANCE[total + 1], 2) + "\n" + TimeToString(deal_time, TIME_DATE|TIME_SECONDS) + "\n";
  174. CreateDotLabel(name, "=", iTime(Symbol(), PERIOD_CURRENT, total + 1), ACCOUNTBALANCE[total + 1], Dot_Color);
  175. }
  176. BAL = BAL + profit;
  177. ACCOUNTBALANCE[total] = BAL;
  178. GlobalVariableSet("TIME:" + IntegerToString(total), deal_time);
  179. total--;
  180. if(tradesCount >= PastHistory)
  181. break;
  182. }
  183. }
  184. }
  185. // Calculate moving averages and create labels
  186. for(int x = total2; x > 0; x--)
  187. {
  188. if(x < total2 - MA_Period)
  189. {
  190. MA[x] = iMAOnArray(ACCOUNTBALANCE, 0, MA_Period, 0, MA_Method, x);
  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. //+------------------------------------------------------------------+