| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598 |
- //+------------------------------------------------------------------+
- //| pulseBalaceIndicator.mq5|
- //| Copyright 2025, MQL Development |
- //| https://www.mqldevelopment.com/ |
- //+------------------------------------------------------------------+
- #property copyright "Copyright 2025, MQL Development"
- #property link "https://www.mqldevelopment.com/"
- #property version "1.1"
-
- #define global "var"
-
- #property indicator_separate_window
-
- #property indicator_buffers 2
- #property indicator_plots 2
-
- #property indicator_label1 "ACCOUNT BALANCE"
- #property indicator_type1 DRAW_LINE
- #property indicator_color1 clrRed
- #property indicator_width1 1
-
- #property indicator_label2 "MA"
- #property indicator_type2 DRAW_LINE
- #property indicator_color2 clrDodgerBlue
- #property indicator_width2 1
- #property indicator_style2 STYLE_DOT
-
-
- input color Dot_Color = clrOrangeRed;
- input double StartingBalance = 10000;
- input string MagicNumbers = "123";
- input int MA_Period = 5;
- input ENUM_MA_METHOD MA_Method = MODE_SMA;
- input string fileName = "TradeDataFile"; // File Name
- input int historyTrades = 50;
-
- double ACCOUNTBALANCE[];
- double MA[];
- int TOTAL_HISTORY = 0;
- datetime TT = 0;
-
- int MagicArray[];
-
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- string filename = fileName + Symbol() + ".csv";
- //+------------------------------------------------------------------+
- //| Custom indicator initialization function |
- //+------------------------------------------------------------------+
- int OnInit()
- {
- //--- indicator buffers mapping
-
- if(!GlobalVariableCheck(global)) //for creating
- {
- GlobalVariableSet(global,0);
- // 0 means Free
- // 1 means busy
- }
-
- parseMagicNumbers();
-
- SetIndexBuffer(0, ACCOUNTBALANCE, INDICATOR_DATA);
- SetIndexBuffer(1, MA, INDICATOR_DATA);
-
- ArraySetAsSeries(ACCOUNTBALANCE, true);
- ArraySetAsSeries(MA, true);
-
- TOTAL_HISTORY = 0;
- TT = 0;
-
- //---
- return(INIT_SUCCEEDED);
- }
-
- //+------------------------------------------------------------------+
- //| Custom indicator deinitialization function |
- //+------------------------------------------------------------------+
- void OnDeinit(const int reason)
- {
- //--- destroy timer
- GlobalVariableSet(global,0);
- subDeleteObjects("Balance: ");
- subDeleteObjects("Start Balance: ");
- }
-
- //+------------------------------------------------------------------+
- //| Custom indicator iteration function |
- //+------------------------------------------------------------------+
- int OnCalculate(const int rates_total,
- const int prev_calculated,
- const datetime &time[],
- const double &open[],
- const double &high[],
- const double &low[],
- const double &close[],
- const long &tick_volume[],
- const long &volume[],
- const int &spread[])
- {
- //---
-
- MyStart(rates_total, prev_calculated);
-
- //--- return value of prev_calculated for next call
- return(rates_total);
- }
-
-
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- void MyStart(int ratesTotal, int prevCalculated)
- {
- int currTradeCount = orderCount();
-
- if(currTradeCount != -1)
- {
-
- if(TOTAL_HISTORY != currTradeCount || TT != iTime(Symbol(), PERIOD_CURRENT, 0))
- {
-
- int limit = 0;
- if(prevCalculated == 0)
- {
- limit = ratesTotal - prevCalculated -2;
- }
- else
- {
- limit = ratesTotal - prevCalculated;
- }
-
- for(int i=1000; i>= 0; i--)
- {
- ACCOUNTBALANCE[i] = EMPTY_VALUE;
- MA[i] = EMPTY_VALUE;
- DeleteStartBalanceObjects();
- }
-
- int total = 0;
-
- double AB = StartingBalance;
- double BAL = AB;
- Print("ACCOUNT BALANCE: " + DoubleToString(AB));
-
- total = currTradeCount;
-
- int totalOrderCount = currTradeCount;
-
- // Print(" Total Order Count = ",totalOrderCount);
-
- int startIndexToConsiderTrade = totalOrderCount - historyTrades;
-
- int totalLoop = historyTrades;
-
- total = historyTrades;
- if(startIndexToConsiderTrade < 0)
- {
- startIndexToConsiderTrade = 1;
- total = totalOrderCount;
- totalLoop = totalOrderCount;
- }
-
- if(GlobalVariableGet(global) == 0)
- {
-
- GlobalVariableSet(global,1);
-
- int handle = FileOpen(filename, FILE_READ | FILE_COMMON);
- // Print("startIndexToConsiderTrade: ",startIndexToConsiderTrade);
- if(handle == INVALID_HANDLE)
- {
- Print("ind => Handler invalid: MyStart() ", filename," error: ",GetLastError());
- return;
- }
-
- while(!FileIsEnding(handle))
- {
- string line = FileReadString(handle);
-
- // Skip empty line
- if(StringLen(line) < 2)
- continue;
-
- string parts[];
- int count = StringSplit(line, ',', parts);
-
- int ticket = (int)StringToInteger(parts[0]);
- int type = (int)StringToInteger(parts[1]);
- int magic = (int)StringToInteger(parts[2]);
- double open_price = StringToDouble(parts[3]);
- double close_price = StringToDouble(parts[4]);
- datetime open_time = StringToTime(parts[5]);
- datetime close_time = StringToTime(parts[6]);
- double sl = StringToDouble(parts[7]);
- double tp = StringToDouble(parts[8]);
- double lot = StringToDouble(parts[9]);
- double profit = StringToDouble(parts[10]);
-
-
- if(isMagicMatch(magic) && ticket >= startIndexToConsiderTrade && total > 0)
- {
- // Print(" startIndexToConsiderTrade ",startIndexToConsiderTrade," ticket = ",ticket," total + 1: ",total + 1," totalOrderCount ",totalOrderCount," historyTrades = ",historyTrades);
-
- double deal_profit = profit;
-
- ACCOUNTBALANCE[total + 1] = BAL;
- string name = "Start Balance: " + DoubleToString(ACCOUNTBALANCE[total + 1], 2) + "\n" + TimeToString(close_time, TIME_DATE|TIME_SECONDS) + "\n";
- CreateDotLabel(name, "=", iTime(Symbol(), PERIOD_CURRENT, total + 1), ACCOUNTBALANCE[total + 1], Dot_Color);
-
- BAL = BAL + profit;
- ACCOUNTBALANCE[total] = BAL;
-
- GlobalVariableSet("TIME:" + IntegerToString(total), close_time);
-
- total--;
- }
- }
-
- FileClose(handle);
-
- GlobalVariableSet(global,0);
-
- // Calculate moving averages and create labels
- for(int x = totalLoop; x > 0/*startIndexToConsiderTrade*/; x--)
- {
- if(x < /*orderCount()*/totalLoop - MA_Period)
- {
- MA[x] = iMAOnArray(ACCOUNTBALANCE, 0, MA_Period, 0, MA_Method, x);
-
- datetime time_val = (datetime)GlobalVariableGet("TIME:" + IntegerToString(x));
- string name = "Balance: " + DoubleToString(ACCOUNTBALANCE[x], 2) + "\n" + TimeToString(time_val, TIME_DATE|TIME_SECONDS) + "\n" + DoubleToString(MA[x], 2);
- //CreateDotLabel(name, "=", iTime(Symbol(), PERIOD_CURRENT, x), ACCOUNTBALANCE[x], Dot_Color);
- }
- else
- {
- datetime time_val = (datetime)GlobalVariableGet("TIME:" + IntegerToString(x));
- string name = "Balance: " + DoubleToString(ACCOUNTBALANCE[x], 2) + "\n" + TimeToString(time_val, TIME_DATE|TIME_SECONDS);
- //CreateDotLabel(name, "=", iTime(Symbol(), PERIOD_CURRENT, x), ACCOUNTBALANCE[x], Dot_Color);
- }
- }
-
- TOTAL_HISTORY = currTradeCount;
- TT = iTime(Symbol(), PERIOD_CURRENT, 0);
- }
- }
- }
- }
-
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- void CreateDotLabel(string name, string text, datetime T1, double P1, color C)
- {
-
-
- int window_x = ChartWindowFind();
-
- if(ObjectFind(0, name) == -1)
- {
- ObjectCreate(0, name, OBJ_TEXT, window_x, T1, NormalizeDouble(P1, Digits()));
- }
- ObjectSetInteger(0, name, OBJPROP_ANCHOR, ANCHOR_CENTER);
- ObjectSetString(0, name, OBJPROP_TEXT, text);
- ObjectSetString(0, name, OBJPROP_FONT, "webdings");
- ObjectSetInteger(0, name, OBJPROP_FONTSIZE, 8);
- ObjectSetInteger(0, name, OBJPROP_COLOR, C);
- ObjectSetDouble(0, name, OBJPROP_PRICE, NormalizeDouble(P1, Digits()));
- ObjectSetInteger(0, name, OBJPROP_TIME, T1);
- ObjectSetInteger(0, name, OBJPROP_SELECTABLE, false);
- }
-
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- void subDeleteObjects(string ObjName)
- {
- int obj_total = ObjectsTotal(0);
- string name;
- int i = 0;
-
- for(i = obj_total - 1; i >= 0; i--)
- {
- name = ObjectName(0, i);
- if(StringFind(name, ObjName, 0) != -1)
- {
- ObjectDelete(0, name);
- }
- }
-
- }
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- double iMAOnArray(double &array[],
- int total,
- int period,
- int ma_shift,
- int ma_method,
- int shift)
- {
- double buf[],arr[];
- if(total==0)
- total=ArraySize(array);
- if(total>0 && total<=period)
- return(0);
- if(shift>total-period-ma_shift)
- return(0);
- switch(ma_method)
- {
- case MODE_SMA :
- {
- total=ArrayCopy(arr,array,0,shift+ma_shift,period);
- if(ArrayResize(buf,total)<0)
- return(0);
- double sum=0;
- int i,pos=total-1;
- for(i=1;i<period;i++,pos--)
- sum+=arr[pos];
- while(pos>=0)
- {
- sum+=arr[pos];
- buf[pos]=sum/period;
- sum-=arr[pos+period-1];
- pos--;
- }
- return(buf[0]);
- }
- case MODE_EMA :
- {
- if(ArrayResize(buf,total)<0)
- return(0);
- double pr=2.0/(period+1);
- int pos=total-2;
- while(pos>=0)
- {
- if(pos==total-2)
- {
- buf[pos+1]=array[pos+1];
-
- }
- buf[pos]=array[pos]*pr+buf[pos+1]*(1-pr);
- pos--;
- }
- return(buf[shift+ma_shift]);
- }
- case MODE_SMMA :
- {
- if(ArrayResize(buf,total)<0)
- return(0);
- double sum=0;
- int i,k,pos;
- pos=total-period;
- while(pos>=0)
- {
- if(pos==total-period)
- {
- for(i=0,k=pos;i<period;i++,k++)
- {
- sum+=array[k];
- buf[k]=0;
- }
- }
- else
- sum=buf[pos+1]*(period-1)+array[pos];
- buf[pos]=sum/period;
- pos--;
- }
- return(buf[shift+ma_shift]);
- }
- case MODE_LWMA :
- {
- if(ArrayResize(buf,total)<0)
- return(0);
- double sum=0.0,lsum=0.0;
- double price;
- int i,weight=0,pos=total-1;
- for(i=1;i<=period;i++,pos--)
- {
- price=array[pos];
- sum+=price*i;
- lsum+=price;
- weight+=i;
- }
- pos++;
- i=pos+period;
- while(pos>=0)
- {
- buf[pos]=sum/weight;
- if(pos==0)
- break;
- pos--;
- i--;
- price=array[pos];
- sum=sum-lsum+price*period;
- lsum-=array[i];
- lsum+=price;
- }
- return(buf[shift+ma_shift]);
- }
- default:
- return(0);
- }
- return(0);
- }
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- void parseMagicNumbers()
- {
- string magicList[];
- int count = StringSplit(MagicNumbers, ',', magicList);
-
- //Print("Magic Count: ", count);
-
- if(count > 0)
- {
- ArrayResize(MagicArray, count);
- for(int i = 0; i < count; i++)
- {
- string cleanMagic = trimString(magicList[i]);
- MagicArray[i] = (int)StringToInteger(cleanMagic);
- }
- }
- }
-
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- bool isMagicMatch(long deal_magic)
- {
- // If "0" Magic Num, process all trades
- if(MagicNumbers == "0")
- {
- return true;
- }
-
- for(int i = 0; i < ArraySize(MagicArray); i++)
- {
- if(deal_magic == MagicArray[i])
- return true;
- }
- return false;
- }
-
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- string trimString(string inputt)
- {
- // Remove spaces from the left and right sides
- int start = 0;
- int end = StringLen(inputt) - 1;
-
- // Find the first non-space character
- while(start <= end && StringGetCharacter(inputt, start) == ' ')
- start++;
-
- // Find the last non-space character
- while(end >= start && StringGetCharacter(inputt, end) == ' ')
- end--;
-
- // Extract the substring without leading or trailing spaces
- return StringSubstr(inputt, start, end - start + 1);
- }
-
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- void ReadDataFromFile()
- {
- int handle = FileOpen(filename, FILE_READ | FILE_COMMON);
-
- if(handle == INVALID_HANDLE)
- {
- Print("ind => Handler invalid: ReadDataFromFile() ", filename," error: ",GetLastError());
- return;
- }
-
- while(!FileIsEnding(handle))
- {
- string line = FileReadString(handle);
-
- // Skip empty line
- if(StringLen(line) < 2)
- continue;
-
- string parts[];
- int count = StringSplit(line, ',', parts);
-
- int ticket = (int)StringToInteger(parts[0]);
- int type = (int)StringToInteger(parts[1]);
- int magic = (int)StringToInteger(parts[2]);
- double open_price = StringToDouble(parts[3]);
- double close_price = StringToDouble(parts[4]);
- datetime open_time = StringToTime(parts[5]);
- datetime close_time = StringToTime(parts[6]);
- double sl = StringToDouble(parts[7]);
- double tp = StringToDouble(parts[8]);
- double lot = StringToDouble(parts[9]);
- double profit = StringToDouble(parts[10]);
-
- // Print or use values
- Print("READ: ticket=", ticket,
- " type=", type,
- " magic=", magic,
- " open=", open_price,
- " close=", close_price,
- " open_time=", open_time,
- " close_time=", close_time,
- " sl=", sl,
- " tp=", tp,
- " lot=", lot,
- " profit=", profit);
- }
-
- FileClose(handle);
- }
-
-
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- int orderCount()
- {
- int index = -1;
-
- if(GlobalVariableGet(global) == 0)
- {
-
- GlobalVariableSet(global,1);
-
- int handle = FileOpen(filename, FILE_READ | FILE_COMMON);
-
- if(handle == INVALID_HANDLE)
- {
- Print("ind => Handler invalid: orderCount() ", filename," error: ",GetLastError());
- return 0;
- }
-
-
-
- while(!FileIsEnding(handle))
- {
- string line = FileReadString(handle);
-
- // Skip empty line
- if(StringLen(line) < 2)
- continue;
-
- string parts[];
- int count = StringSplit(line, ',', parts);
-
- int ticket = (int)StringToInteger(parts[0]);
- int type = (int)StringToInteger(parts[1]);
- int magic = (int)StringToInteger(parts[2]);
- double open_price = StringToDouble(parts[3]);
- double close_price = StringToDouble(parts[4]);
- datetime open_time = StringToTime(parts[5]);
- datetime close_time = StringToTime(parts[6]);
- double sl = StringToDouble(parts[7]);
- double tp = StringToDouble(parts[8]);
- double lot = StringToDouble(parts[9]);
- double profit = StringToDouble(parts[10]);
-
- index++;
-
- }
-
- FileClose(handle);
- GlobalVariableSet(global,0);
- }
-
- return index;
- }
-
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- void DeleteStartBalanceObjects()
- {
- int total = ObjectsTotal(0);
-
- // Loop from last to first (important for deletion)
- for(int i = total - 1; i >= 0; i--)
- {
- string objName = ObjectName(0, i);
-
- // Check if object name starts with "Start Balance:"
- if(StringFind(objName,"Start Balance:") > 0 || StringFind(objName,"Balance:") > 0)
- {
- ObjectDelete(0, objName);
- }
- }
- }
- //+------------------------------------------------------------------+
|