Нема описа

Pulse Balance Indicator Sim.mq5 36KB

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