Açıklama Yok

Pulse Balance Indicator Sim.mq5 35KB

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