Aucune description

Pulse Balance Indicator Sim.mq5 33KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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 = "123";
  24. input int MA_Period = 5;
  25. input ENUM_MA_METHOD MA_Method = MODE_SMA;
  26. input string fileName = "TradeDataFile.csv"; // File Name
  27. input int historyTrades = 50;
  28. double ACCOUNTBALANCE[];
  29. double MA[];
  30. int TOTAL_HISTORY = 0;
  31. datetime TT = 0;
  32. int MagicArray[];
  33. //+------------------------------------------------------------------+
  34. //| Custom indicator initialization function |
  35. //+------------------------------------------------------------------+
  36. int OnInit()
  37. {
  38. //--- indicator buffers mapping
  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. subDeleteObjects("Balance: ");
  56. subDeleteObjects("Start Balance: ");
  57. }
  58. //+------------------------------------------------------------------+
  59. //| Custom indicator iteration function |
  60. //+------------------------------------------------------------------+
  61. int OnCalculate(const int rates_total,
  62. const int prev_calculated,
  63. const datetime &time[],
  64. const double &open[],
  65. const double &high[],
  66. const double &low[],
  67. const double &close[],
  68. const long &tick_volume[],
  69. const long &volume[],
  70. const int &spread[])
  71. {
  72. //---
  73. MyStart(rates_total, prev_calculated);
  74. //--- return value of prev_calculated for next call
  75. return(rates_total);
  76. }
  77. //+------------------------------------------------------------------+
  78. //| |
  79. //+------------------------------------------------------------------+
  80. void MyStart(int ratesTotal, int prevCalculated)
  81. {
  82. if(TOTAL_HISTORY != orderCount() || TT != iTime(Symbol(), PERIOD_CURRENT, 0))
  83. {
  84. int limit = 0;
  85. if(prevCalculated == 0)
  86. {
  87. limit = ratesTotal - prevCalculated -2;
  88. }
  89. else
  90. {
  91. limit = ratesTotal - prevCalculated;
  92. }
  93. for(int i=limit; i>= 0; i--)
  94. {
  95. ACCOUNTBALANCE[i] = EMPTY_VALUE;
  96. MA[i] = EMPTY_VALUE;
  97. }
  98. int total = 0;
  99. double AB = StartingBalance;
  100. double BAL = AB;
  101. Print("ACCOUNT BALANCE: " + DoubleToString(AB));
  102. total = orderCount();
  103. int totalOrderCount = orderCount();
  104. Print(" Total Order Count = ",totalOrderCount);
  105. int startIndexToConsiderTrade = totalOrderCount - historyTrades;
  106. int handle = FileOpen(fileName, FILE_READ | FILE_COMMON);
  107. if(handle == INVALID_HANDLE)
  108. {
  109. Print("File not found: ", fileName);
  110. return;
  111. }
  112. while(!FileIsEnding(handle))
  113. {
  114. string line = FileReadString(handle);
  115. // Skip empty line
  116. if(StringLen(line) < 2)
  117. continue;
  118. string parts[];
  119. int count = StringSplit(line, ',', parts);
  120. int ticket = (int)StringToInteger(parts[0]);
  121. int type = (int)StringToInteger(parts[1]);
  122. int magic = (int)StringToInteger(parts[2]);
  123. double open_price = StringToDouble(parts[3]);
  124. double close_price = StringToDouble(parts[4]);
  125. datetime open_time = StringToTime(parts[5]);
  126. datetime close_time = StringToTime(parts[6]);
  127. double sl = StringToDouble(parts[7]);
  128. double tp = StringToDouble(parts[8]);
  129. double lot = StringToDouble(parts[9]);
  130. double profit = StringToDouble(parts[10]);
  131. if(isMagicMatch(magic) && ticket >= startIndexToConsiderTrade)
  132. {
  133. Print(" startIndexToConsiderTrade ",startIndexToConsiderTrade," ticket = ",ticket);
  134. double deal_profit = profit;
  135. ACCOUNTBALANCE[total + 1] = BAL;
  136. string name = "Start Balance: " + DoubleToString(ACCOUNTBALANCE[total + 1], 2) + "\n" + TimeToString(close_time, TIME_DATE|TIME_SECONDS) + "\n";
  137. CreateDotLabel(name, "=", iTime(Symbol(), PERIOD_CURRENT, total + 1), ACCOUNTBALANCE[total + 1], Dot_Color);
  138. BAL = BAL + profit;
  139. ACCOUNTBALANCE[total] = BAL;
  140. GlobalVariableSet("TIME:" + IntegerToString(total), close_time);
  141. total--;
  142. }
  143. }
  144. FileClose(handle);
  145. // Calculate moving averages and create labels
  146. for(int x = orderCount(); x > startIndexToConsiderTrade; x--)
  147. {
  148. if(x < orderCount() - MA_Period)
  149. {
  150. MA[x] = iMAOnArray(ACCOUNTBALANCE, 0, MA_Period, 0, MA_Method, x);
  151. datetime time_val = (datetime)GlobalVariableGet("TIME:" + IntegerToString(x));
  152. string name = "Balance: " + DoubleToString(ACCOUNTBALANCE[x], 2) + "\n" + TimeToString(time_val, TIME_DATE|TIME_SECONDS) + "\n" + DoubleToString(MA[x], 2);
  153. CreateDotLabel(name, "=", iTime(Symbol(), PERIOD_CURRENT, x), ACCOUNTBALANCE[x], Dot_Color);
  154. }
  155. else
  156. {
  157. datetime time_val = (datetime)GlobalVariableGet("TIME:" + IntegerToString(x));
  158. string name = "Balance: " + DoubleToString(ACCOUNTBALANCE[x], 2) + "\n" + TimeToString(time_val, TIME_DATE|TIME_SECONDS);
  159. CreateDotLabel(name, "=", iTime(Symbol(), PERIOD_CURRENT, x), ACCOUNTBALANCE[x], Dot_Color);
  160. }
  161. }
  162. TOTAL_HISTORY = orderCount();
  163. TT = iTime(Symbol(), PERIOD_CURRENT, 0);
  164. }
  165. }
  166. //+------------------------------------------------------------------+
  167. //| |
  168. //+------------------------------------------------------------------+
  169. void CreateDotLabel(string name, string text, datetime T1, double P1, color C)
  170. {
  171. int window_x = ChartWindowFind();
  172. if(ObjectFind(0, name) == -1)
  173. {
  174. ObjectCreate(0, name, OBJ_TEXT, window_x, T1, NormalizeDouble(P1, Digits()));
  175. }
  176. ObjectSetInteger(0, name, OBJPROP_ANCHOR, ANCHOR_CENTER);
  177. ObjectSetString(0, name, OBJPROP_TEXT, text);
  178. ObjectSetString(0, name, OBJPROP_FONT, "webdings");
  179. ObjectSetInteger(0, name, OBJPROP_FONTSIZE, 8);
  180. ObjectSetInteger(0, name, OBJPROP_COLOR, C);
  181. ObjectSetDouble(0, name, OBJPROP_PRICE, NormalizeDouble(P1, Digits()));
  182. ObjectSetInteger(0, name, OBJPROP_TIME, T1);
  183. ObjectSetInteger(0, name, OBJPROP_SELECTABLE, false);
  184. }
  185. //+------------------------------------------------------------------+
  186. //| |
  187. //+------------------------------------------------------------------+
  188. void subDeleteObjects(string ObjName)
  189. {
  190. int obj_total = ObjectsTotal(0);
  191. string name;
  192. int i = 0;
  193. for(i = obj_total - 1; i >= 0; i--)
  194. {
  195. name = ObjectName(0, i);
  196. if(StringFind(name, ObjName, 0) != -1)
  197. {
  198. ObjectDelete(0, name);
  199. }
  200. }
  201. }
  202. //+------------------------------------------------------------------+
  203. //| |
  204. //+------------------------------------------------------------------+
  205. double iMAOnArray(double &array[],
  206. int total,
  207. int period,
  208. int ma_shift,
  209. int ma_method,
  210. int shift)
  211. {
  212. double buf[],arr[];
  213. if(total==0)
  214. total=ArraySize(array);
  215. if(total>0 && total<=period)
  216. return(0);
  217. if(shift>total-period-ma_shift)
  218. return(0);
  219. switch(ma_method)
  220. {
  221. case MODE_SMA :
  222. {
  223. total=ArrayCopy(arr,array,0,shift+ma_shift,period);
  224. if(ArrayResize(buf,total)<0)
  225. return(0);
  226. double sum=0;
  227. int i,pos=total-1;
  228. for(i=1;i<period;i++,pos--)
  229. sum+=arr[pos];
  230. while(pos>=0)
  231. {
  232. sum+=arr[pos];
  233. buf[pos]=sum/period;
  234. sum-=arr[pos+period-1];
  235. pos--;
  236. }
  237. return(buf[0]);
  238. }
  239. case MODE_EMA :
  240. {
  241. if(ArrayResize(buf,total)<0)
  242. return(0);
  243. double pr=2.0/(period+1);
  244. int pos=total-2;
  245. while(pos>=0)
  246. {
  247. if(pos==total-2)
  248. {
  249. buf[pos+1]=array[pos+1];
  250. }
  251. buf[pos]=array[pos]*pr+buf[pos+1]*(1-pr);
  252. pos--;
  253. }
  254. return(buf[shift+ma_shift]);
  255. }
  256. case MODE_SMMA :
  257. {
  258. if(ArrayResize(buf,total)<0)
  259. return(0);
  260. double sum=0;
  261. int i,k,pos;
  262. pos=total-period;
  263. while(pos>=0)
  264. {
  265. if(pos==total-period)
  266. {
  267. for(i=0,k=pos;i<period;i++,k++)
  268. {
  269. sum+=array[k];
  270. buf[k]=0;
  271. }
  272. }
  273. else
  274. sum=buf[pos+1]*(period-1)+array[pos];
  275. buf[pos]=sum/period;
  276. pos--;
  277. }
  278. return(buf[shift+ma_shift]);
  279. }
  280. case MODE_LWMA :
  281. {
  282. if(ArrayResize(buf,total)<0)
  283. return(0);
  284. double sum=0.0,lsum=0.0;
  285. double price;
  286. int i,weight=0,pos=total-1;
  287. for(i=1;i<=period;i++,pos--)
  288. {
  289. price=array[pos];
  290. sum+=price*i;
  291. lsum+=price;
  292. weight+=i;
  293. }
  294. pos++;
  295. i=pos+period;
  296. while(pos>=0)
  297. {
  298. buf[pos]=sum/weight;
  299. if(pos==0)
  300. break;
  301. pos--;
  302. i--;
  303. price=array[pos];
  304. sum=sum-lsum+price*period;
  305. lsum-=array[i];
  306. lsum+=price;
  307. }
  308. return(buf[shift+ma_shift]);
  309. }
  310. default:
  311. return(0);
  312. }
  313. return(0);
  314. }
  315. //+------------------------------------------------------------------+
  316. //| |
  317. //+------------------------------------------------------------------+
  318. void parseMagicNumbers()
  319. {
  320. string magicList[];
  321. int count = StringSplit(MagicNumbers, ',', magicList);
  322. //Print("Magic Count: ", count);
  323. if(count > 0)
  324. {
  325. ArrayResize(MagicArray, count);
  326. for(int i = 0; i < count; i++)
  327. {
  328. string cleanMagic = trimString(magicList[i]);
  329. MagicArray[i] = (int)StringToInteger(cleanMagic);
  330. }
  331. }
  332. }
  333. //+------------------------------------------------------------------+
  334. //| |
  335. //+------------------------------------------------------------------+
  336. bool isMagicMatch(long deal_magic)
  337. {
  338. // If "0" Magic Num, process all trades
  339. if(MagicNumbers == "0")
  340. {
  341. return true;
  342. }
  343. for(int i = 0; i < ArraySize(MagicArray); i++)
  344. {
  345. if(deal_magic == MagicArray[i])
  346. return true;
  347. }
  348. return false;
  349. }
  350. //+------------------------------------------------------------------+
  351. //| |
  352. //+------------------------------------------------------------------+
  353. string trimString(string inputt)
  354. {
  355. // Remove spaces from the left and right sides
  356. int start = 0;
  357. int end = StringLen(inputt) - 1;
  358. // Find the first non-space character
  359. while(start <= end && StringGetCharacter(inputt, start) == ' ')
  360. start++;
  361. // Find the last non-space character
  362. while(end >= start && StringGetCharacter(inputt, end) == ' ')
  363. end--;
  364. // Extract the substring without leading or trailing spaces
  365. return StringSubstr(inputt, start, end - start + 1);
  366. }
  367. //+------------------------------------------------------------------+
  368. //| |
  369. //+------------------------------------------------------------------+
  370. void ReadDataFromFile()
  371. {
  372. int handle = FileOpen(fileName, FILE_READ | FILE_COMMON);
  373. if(handle == INVALID_HANDLE)
  374. {
  375. Print("File not found: ", fileName);
  376. return;
  377. }
  378. while(!FileIsEnding(handle))
  379. {
  380. string line = FileReadString(handle);
  381. // Skip empty line
  382. if(StringLen(line) < 2)
  383. continue;
  384. string parts[];
  385. int count = StringSplit(line, ',', parts);
  386. int ticket = (int)StringToInteger(parts[0]);
  387. int type = (int)StringToInteger(parts[1]);
  388. int magic = (int)StringToInteger(parts[2]);
  389. double open_price = StringToDouble(parts[3]);
  390. double close_price = StringToDouble(parts[4]);
  391. datetime open_time = StringToTime(parts[5]);
  392. datetime close_time = StringToTime(parts[6]);
  393. double sl = StringToDouble(parts[7]);
  394. double tp = StringToDouble(parts[8]);
  395. double lot = StringToDouble(parts[9]);
  396. double profit = StringToDouble(parts[10]);
  397. // Print or use values
  398. Print("READ: ticket=", ticket,
  399. " type=", type,
  400. " magic=", magic,
  401. " open=", open_price,
  402. " close=", close_price,
  403. " open_time=", open_time,
  404. " close_time=", close_time,
  405. " sl=", sl,
  406. " tp=", tp,
  407. " lot=", lot,
  408. " profit=", profit);
  409. }
  410. FileClose(handle);
  411. }
  412. //+------------------------------------------------------------------+
  413. //| |
  414. //+------------------------------------------------------------------+
  415. int orderCount()
  416. {
  417. int handle = FileOpen(fileName, FILE_READ | FILE_COMMON);
  418. if(handle == INVALID_HANDLE)
  419. {
  420. Print("File not found: ", fileName);
  421. return 0;
  422. }
  423. int index = 0;
  424. while(!FileIsEnding(handle))
  425. {
  426. string line = FileReadString(handle);
  427. // Skip empty line
  428. if(StringLen(line) < 2)
  429. continue;
  430. string parts[];
  431. int count = StringSplit(line, ',', parts);
  432. int ticket = (int)StringToInteger(parts[0]);
  433. int type = (int)StringToInteger(parts[1]);
  434. int magic = (int)StringToInteger(parts[2]);
  435. double open_price = StringToDouble(parts[3]);
  436. double close_price = StringToDouble(parts[4]);
  437. datetime open_time = StringToTime(parts[5]);
  438. datetime close_time = StringToTime(parts[6]);
  439. double sl = StringToDouble(parts[7]);
  440. double tp = StringToDouble(parts[8]);
  441. double lot = StringToDouble(parts[9]);
  442. double profit = StringToDouble(parts[10]);
  443. index++;
  444. }
  445. FileClose(handle);
  446. return index;
  447. }
  448. //+------------------------------------------------------------------+