Nessuna descrizione

pulseBalanceIndicator.mq5 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. ////+------------------------------------------------------------------+
  2. ////| AccountBalance Indicator.mq5 |
  3. ////| Copyright © 2016, MethodTraders.com |
  4. ////| http://www.MethodTraders.com |
  5. ////| PROGRAMMED BY: JLo |
  6. ////+------------------------------------------------------------------+
  7. #property copyright "Copyright © 2016, MethodTraders.com"
  8. #property link "http://www.MethodTraders.com"
  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 int MA2_Period = 12;
  31. input ENUM_MA_METHOD MA_Method = MODE_SMA;
  32. double ACCOUNTBALANCE[];
  33. double MA[];
  34. double MA2[];
  35. double BUFF;
  36. int TOTAL_HISTORY;
  37. datetime TT;
  38. int g_prev_calculated = 0;
  39. //+------------------------------------------------------------------+
  40. //| Custom indicator initialization function |
  41. //+------------------------------------------------------------------+
  42. int OnInit()
  43. {
  44. //--- indicator buffers mapping
  45. EventSetTimer(1);
  46. SetIndexBuffer(0, ACCOUNTBALANCE, INDICATOR_DATA);
  47. SetIndexBuffer(1, MA, INDICATOR_DATA);
  48. SetIndexBuffer(2, MA2, INDICATOR_DATA);
  49. TOTAL_HISTORY = 0;
  50. TT = 0;
  51. BUFF = AccountInfoDouble(ACCOUNT_BALANCE);
  52. ArraySetAsSeries(ACCOUNTBALANCE, true);
  53. ArraySetAsSeries(MA, true);
  54. ArraySetAsSeries(MA2, true);
  55. //EventSetMillisecondTimer(1000);
  56. //---
  57. return(INIT_SUCCEEDED);
  58. }
  59. //+------------------------------------------------------------------+
  60. //| Custom indicator deinitialization function |
  61. //+------------------------------------------------------------------+
  62. void OnDeinit(const int reason)
  63. {
  64. //--- destroy timer
  65. EventKillTimer();
  66. }
  67. //+------------------------------------------------------------------+
  68. //| Custom indicator iteration function |
  69. //+------------------------------------------------------------------+
  70. int OnCalculate(const int rates_total,
  71. const int prev_calculated,
  72. const datetime &time[],
  73. const double &open[],
  74. const double &high[],
  75. const double &low[],
  76. const double &close[],
  77. const long &tick_volume[],
  78. const long &volume[],
  79. const int &spread[])
  80. {
  81. //---
  82. g_prev_calculated = prev_calculated;
  83. MyStart();
  84. //ArraySetAsSeries(time, true);
  85. //--- return value of prev_calculated for next call
  86. return(rates_total);
  87. }
  88. //+------------------------------------------------------------------+
  89. //| Timer function |
  90. //+------------------------------------------------------------------+
  91. void OnTimer()
  92. {
  93. MyStart();
  94. }
  95. //+------------------------------------------------------------------+
  96. //| |
  97. //+------------------------------------------------------------------+
  98. void MyStart()
  99. {
  100. if(TOTAL_HISTORY != HistoryDealsTotal() || TT != iTime(Symbol(), 0, 0))
  101. {
  102. // DELETE ALL OBJECTS
  103. int limit;
  104. //int counted_bars = IndicatorCounted();
  105. int counted_bars = g_prev_calculated;
  106. //Print("Counted Bars: ", counted_bars);
  107. //---- last counted bar will be recounted
  108. if(counted_bars > 0)
  109. counted_bars--;
  110. limit = Bars(Symbol(), Period()) - counted_bars;
  111. subDeleteObjects("Balance: ");
  112. subDeleteObjects("Balance: ");
  113. for(int i = counted_bars; i >= 0; i--)
  114. {
  115. ACCOUNTBALANCE[i] = EMPTY_VALUE;
  116. MA[i] = EMPTY_VALUE;
  117. MA2[i] = EMPTY_VALUE;
  118. }
  119. int deals = HistoryDealsTotal();
  120. int total = 0;
  121. int total2 = 0;
  122. int ticket = 0;
  123. double AB = StartingBalance;
  124. double BAL = AB;
  125. Print("ACCOUNT BALANCE: " + DoubleToString(AB));
  126. // First pass to count relevant deals
  127. for(int i = 0; i < deals; i++)
  128. {
  129. ulong deal_ticket = HistoryDealGetTicket(i);
  130. if(deal_ticket == 0)
  131. {
  132. Print("Error getting deal ticket!");
  133. break;
  134. }
  135. long deal_magic = HistoryDealGetInteger(deal_ticket, DEAL_MAGIC);
  136. long deal_type = HistoryDealGetInteger(deal_ticket, DEAL_TYPE);
  137. // Filter for position deals (open/close) with matching magic number
  138. if((deal_type == DEAL_TYPE_BUY || deal_type == DEAL_TYPE_SELL ||
  139. deal_type == DEAL_TYPE_BALANCE) && deal_magic == MagicNumber)
  140. {
  141. total2++;
  142. }
  143. }
  144. total = total2;
  145. // Second pass to calculate balance
  146. for(int i = 0; i < deals; i++)
  147. {
  148. ulong deal_ticket = HistoryDealGetTicket(i);
  149. if(deal_ticket == 0)
  150. {
  151. Print("Error getting deal ticket!");
  152. break;
  153. }
  154. long deal_magic = HistoryDealGetInteger(deal_ticket, DEAL_MAGIC);
  155. long deal_type = HistoryDealGetInteger(deal_ticket, DEAL_TYPE);
  156. datetime deal_time = (datetime)HistoryDealGetInteger(deal_ticket, DEAL_TIME);
  157. if((deal_type == DEAL_TYPE_BUY || deal_type == DEAL_TYPE_SELL ||
  158. deal_type == DEAL_TYPE_BALANCE) && deal_magic == MagicNumber)
  159. {
  160. double deal_profit = HistoryDealGetDouble(deal_ticket, DEAL_PROFIT);
  161. double deal_swap = HistoryDealGetDouble(deal_ticket, DEAL_SWAP);
  162. double deal_commission = HistoryDealGetDouble(deal_ticket, DEAL_COMMISSION);
  163. double profit = deal_profit + deal_swap + deal_commission;
  164. if(total == total2)
  165. {
  166. ACCOUNTBALANCE[total + 1] = BAL;
  167. string name = "Start Balance: " + DoubleToString(ACCOUNTBALANCE[total + 1], 2) + "\n" + TimeToString(deal_time, TIME_DATE|TIME_SECONDS) + "\n";
  168. //CreateDotLabel(name, "=", Time[total + 1], ACCOUNTBALANCE[total + 1], Dot_Color);
  169. CreateDotLabel(name, "=", iTime(Symbol(), 0, total + 1), ACCOUNTBALANCE[total + 1], Dot_Color);
  170. }
  171. BAL = BAL + profit;
  172. ACCOUNTBALANCE[total] = BAL;
  173. GlobalVariableSet("TIME:" + IntegerToString(total), deal_time);
  174. total--;
  175. }
  176. }
  177. // Calculate moving averages and create labels
  178. for(int x = total2; x > 0; x--)
  179. {
  180. if(x < total2 - MathMax(MA_Period, MA2_Period))
  181. {
  182. MA[x] = iMAOnArray(ACCOUNTBALANCE, 0, MA_Period, 0, MA_Method, x);
  183. MA2[x] = iMAOnArray(ACCOUNTBALANCE, 0, MA2_Period, 0, MA_Method, x);
  184. datetime time_val = (datetime)GlobalVariableGet("TIME:" + IntegerToString(x));
  185. string name = "Balance: " + DoubleToString(ACCOUNTBALANCE[x], 2) + "\n" + TimeToString(time_val, TIME_DATE|TIME_SECONDS) + "\n" + DoubleToString(MA[x], 2) + ", " + DoubleToString(MA2[x], 2);
  186. //CreateDotLabel(name, "=", Time[x], ACCOUNTBALANCE[x], Dot_Color);
  187. CreateDotLabel(name, "=", iTime(Symbol(), 0, x), ACCOUNTBALANCE[x], Dot_Color);
  188. }
  189. else
  190. {
  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);
  193. //CreateDotLabel(name, "=", Time[x], ACCOUNTBALANCE[x], Dot_Color);
  194. CreateDotLabel(name, "=", iTime(Symbol(), 0, x), ACCOUNTBALANCE[x], Dot_Color);
  195. }
  196. }
  197. TOTAL_HISTORY = HistoryDealsTotal();
  198. TT = iTime(Symbol(), 0, 0);
  199. }
  200. }
  201. //+------------------------------------------------------------------+
  202. //| |
  203. //+------------------------------------------------------------------+
  204. void CreateDotLabel(string name, string text, datetime T1, double P1, color C)
  205. {
  206. int window_x = ChartWindowFind();
  207. Print("window_x",window_x);
  208. if(ObjectFind(0, name) == -1)
  209. {
  210. ObjectCreate(0, name, OBJ_TEXT, window_x, T1, NormalizeDouble(P1, Digits()));
  211. }
  212. ObjectSetInteger(0, name, OBJPROP_ANCHOR, ANCHOR_CENTER);
  213. ObjectSetString(0, name, OBJPROP_TEXT, text);
  214. ObjectSetString(0, name, OBJPROP_FONT, "webdings");
  215. ObjectSetInteger(0, name, OBJPROP_FONTSIZE, 8);
  216. ObjectSetInteger(0, name, OBJPROP_COLOR, C);
  217. ObjectSetDouble(0, name, OBJPROP_PRICE, NormalizeDouble(P1, Digits()));
  218. ObjectSetInteger(0, name, OBJPROP_TIME, T1);
  219. ObjectSetInteger(0, name, OBJPROP_SELECTABLE, false);
  220. }
  221. //+------------------------------------------------------------------+
  222. //| |
  223. //+------------------------------------------------------------------+
  224. void subDeleteObjects(string ObjName)
  225. {
  226. int obj_total = ObjectsTotal(0);
  227. string name;
  228. int i = 0;
  229. for(i = obj_total - 1; i >= 0; i--)
  230. {
  231. name = ObjectName(0, i);
  232. if(StringFind(name, ObjName, 0) != -1)
  233. {
  234. ObjectDelete(0, name);
  235. }
  236. }
  237. }
  238. //+------------------------------------------------------------------+
  239. //| |
  240. //+------------------------------------------------------------------+
  241. double subDollarProfitTotal()//COUNTS THE TOTAL PROFIT IN DOLLAR
  242. {
  243. double Profit = 0;
  244. HistorySelect(0, TimeCurrent());
  245. int deals = HistoryDealsTotal();
  246. for(int i = 0; i < deals; i++)
  247. {
  248. ulong deal_ticket = HistoryDealGetTicket(i);
  249. if(deal_ticket == 0)
  250. {
  251. Print("Error getting deal ticket!");
  252. break;
  253. }
  254. long deal_magic = HistoryDealGetInteger(deal_ticket, DEAL_MAGIC);
  255. long deal_type = HistoryDealGetInteger(deal_ticket, DEAL_TYPE);
  256. if((deal_type == DEAL_TYPE_BUY || deal_type == DEAL_TYPE_SELL ||
  257. deal_type == DEAL_TYPE_BALANCE) && deal_magic == MagicNumber)
  258. {
  259. double deal_profit = HistoryDealGetDouble(deal_ticket, DEAL_PROFIT);
  260. double deal_swap = HistoryDealGetDouble(deal_ticket, DEAL_SWAP);
  261. double deal_commission = HistoryDealGetDouble(deal_ticket, DEAL_COMMISSION);
  262. Profit = Profit + deal_profit + deal_swap + deal_commission;
  263. }
  264. }
  265. return(Profit);
  266. }
  267. //+------------------------------------------------------------------+
  268. //| |
  269. //+------------------------------------------------------------------+
  270. double iMAOnArray(double &array[],
  271. int total,
  272. int period,
  273. int ma_shift,
  274. int ma_method,
  275. int shift)
  276. {
  277. double buf[],arr[];
  278. if(total==0)
  279. total=ArraySize(array);
  280. if(total>0 && total<=period)
  281. return(0);
  282. if(shift>total-period-ma_shift)
  283. return(0);
  284. switch(ma_method)
  285. {
  286. case MODE_SMA :
  287. {
  288. total=ArrayCopy(arr,array,0,shift+ma_shift,period);
  289. if(ArrayResize(buf,total)<0)
  290. return(0);
  291. double sum=0;
  292. int i,pos=total-1;
  293. for(i=1;i<period;i++,pos--)
  294. sum+=arr[pos];
  295. while(pos>=0)
  296. {
  297. sum+=arr[pos];
  298. buf[pos]=sum/period;
  299. sum-=arr[pos+period-1];
  300. pos--;
  301. }
  302. return(buf[0]);
  303. }
  304. case MODE_EMA :
  305. {
  306. if(ArrayResize(buf,total)<0)
  307. return(0);
  308. double pr=2.0/(period+1);
  309. int pos=total-2;
  310. while(pos>=0)
  311. {
  312. if(pos==total-2)
  313. {
  314. buf[pos+1]=array[pos+1];
  315. }
  316. buf[pos]=array[pos]*pr+buf[pos+1]*(1-pr);
  317. pos--;
  318. }
  319. return(buf[shift+ma_shift]);
  320. }
  321. case MODE_SMMA :
  322. {
  323. if(ArrayResize(buf,total)<0)
  324. return(0);
  325. double sum=0;
  326. int i,k,pos;
  327. pos=total-period;
  328. while(pos>=0)
  329. {
  330. if(pos==total-period)
  331. {
  332. for(i=0,k=pos;i<period;i++,k++)
  333. {
  334. sum+=array[k];
  335. buf[k]=0;
  336. }
  337. }
  338. else
  339. sum=buf[pos+1]*(period-1)+array[pos];
  340. buf[pos]=sum/period;
  341. pos--;
  342. }
  343. return(buf[shift+ma_shift]);
  344. }
  345. case MODE_LWMA :
  346. {
  347. if(ArrayResize(buf,total)<0)
  348. return(0);
  349. double sum=0.0,lsum=0.0;
  350. double price;
  351. int i,weight=0,pos=total-1;
  352. for(i=1;i<=period;i++,pos--)
  353. {
  354. price=array[pos];
  355. sum+=price*i;
  356. lsum+=price;
  357. weight+=i;
  358. }
  359. pos++;
  360. i=pos+period;
  361. while(pos>=0)
  362. {
  363. buf[pos]=sum/weight;
  364. if(pos==0)
  365. break;
  366. pos--;
  367. i--;
  368. price=array[pos];
  369. sum=sum-lsum+price*period;
  370. lsum-=array[i];
  371. lsum+=price;
  372. }
  373. return(buf[shift+ma_shift]);
  374. }
  375. default:
  376. return(0);
  377. }
  378. return(0);
  379. }
  380. //+------------------------------------------------------------------+