Bal_Eq_Ind
Download scriptThis script is a financial monitoring tool designed to provide a visual representation of your trading account's health over time. It specifically tracks two primary metrics: your Balance and your Equity, allowing you to see how your account funds have fluctuated based on both closed and currently open positions.
Core Functionality
The logic operates by scanning through your entire trading history to reconstruct a chronological map of your account's performance. Instead of just showing you the current numbers, it "replays" your trading history on a chart to show you exactly how your money moved at any specific point in the past.
How it Works
To provide this data, the script follows a structured three-step logic:
1. Data Collection (History Retrieval) The tool first scans your historical trade logs. It extracts essential details for every completed trade, including:
- The specific currency pair traded.
- Whether the trade was a "Buy" or a "Sell."
- The volume (lots) and the prices at which you entered and exited.
- Additional costs such as swaps and realized profits/losses.
2. Balance Tracking The script calculates your Balance by starting with your initial deposit and adding up the net results of every trade that has been fully closed. This represents your "hard" capital—the money that is no longer at risk in the market.
3. Equity Calculation (Real-Time Valuation) The most significant feature of this tool is how it calculates Equity. While Balance only looks at finished trades, Equity takes into account your active, "floating" positions.
- For every point in time on the chart, the script checks which trades were open at that specific moment.
- It calculates the current profit or loss of those live trades based on the market price at that exact time.
- It adds these floating gains or losses to your Balance to produce the Equity value.
Visual Output
The results are displayed in a separate window on your trading platform as two distinct lines:
- Balance: A steady line showing your account growth based only on finalized trades.
- Equity: A more dynamic line that reflects your actual account value, moving up and down as your open trades fluctuate with the market.
By comparing these two lines, you can easily visualize your "floating" risk versus your realized gains, helping you understand how much of your current account value is tied up in active market movements.
//+----------------------------------------------------------------------------+
//| i-BalEq.mq4 |
//| Êèì Èãîðü Â. aka KimIV |
//| http://www.kimiv.ru |
//| 01.10.2006 Ãðàôèê áàëàíñà è ýêâèòè. |
//+----------------------------------------------------------------------------+
#property copyright "Êèì Èãîðü Â. aka KimIV"
#property link "http://www.kimiv.ru"
//----
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Lime
#property indicator_width1 2
#property indicator_color2 Red
#property indicator_width2 1
//------- Ïîêëþ÷åíèå âíåøíèõ ìîäóëåé -------------------------------------------
//------- Âíåøíèå ïàðàìåòðû èíäèêàòîðà -----------------------------------------
extern double Deposit = 1000; // Íà÷àëüíûé äåïîçèò
extern bool CurSymbolOnly = False; // Ó÷èòûâàòü òîëüêî òåêóùèé èíñòðóìåíò
//------- Áóôåðû èíäèêàòîðà ----------------------------------------------------
double dBuf0[], dBuf1[];
//------- Ãëîáàëüíûå ïåðåìåííûå èíäèêàòîðà -------------------------------------
int oob[]; // íîìåð áàðà îòêðûòèÿ
int oty[]; // òèï
double olo[]; // ëîò
string osy[]; // èíñòðóìåíò
double oop[]; // öåíà îòêðûòèÿ
int ocb[]; // íîìåð áàðà çàêðûòèÿ
double ocp[]; // öåíà çàêðûòèÿ
double osw[]; // ñâîï
double opr[]; // ïðèáûëü
//+----------------------------------------------------------------------------+
//| Custom indicator initialization function |
//+----------------------------------------------------------------------------+
void init()
{
SetIndexBuffer(0, dBuf0);
SetIndexLabel (0, "Balance");
SetIndexStyle (0, DRAW_LINE);
SetIndexBuffer(1, dBuf1);
SetIndexLabel (1, "Equity");
SetIndexStyle (1, DRAW_LINE);
}
//+----------------------------------------------------------------------------+
//| Custom indicator iteration function |
//+----------------------------------------------------------------------------+
void start()
{
double b, e, p, t;
int i, j, k;
ReadDeals();
if(oob[0] < 0)
return;
k = ArraySize(oob);
for(i = Bars; i >= oob[0]; i--)
{
dBuf0[i] = EMPTY_VALUE;
dBuf1[i] = EMPTY_VALUE;
}
for(i = oob[0]; i >= 0; i--)
{
b= Deposit; e=0;
for(j = 0; j < k; j++)
{
if(i <= oob[j] && i > ocb[j])
{
p = MarketInfo(osy[j], MODE_POINT);
t = MarketInfo(osy[j], MODE_TICKVALUE);
if(t == 0)
t = 10;
if(p == 0)
if(StringFind(osy[j], "JPY") < 0)
p = 0.0001;
else
p = 0.01;
if(oty[j] == OP_BUY)
e += (iClose(osy[j], 0, i) - ocp[j]) / p*olo[j]*t;
else
e += (ocp[j] - iClose(osy[j], 0, i)) / p*olo[j]*t;
}
else
if(i <= ocb[j])
b += osw[j] + opr[j];
}
dBuf0[i] = b;
dBuf1[i] = b + e;
}
}
//+----------------------------------------------------------------------------+
//| ×òåíèå ñäåëîê |
//+----------------------------------------------------------------------------+
void ReadDeals()
{
ArrayResize(oob, 0);
ArrayResize(oty, 0);
ArrayResize(olo, 0);
ArrayResize(osy, 0);
ArrayResize(oop, 0);
ArrayResize(ocb, 0);
ArrayResize(ocp, 0);
ArrayResize(osw, 0);
ArrayResize(opr, 0);
int h = HistoryTotal(), i, k;
for(i = 0; i < h; i++)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))
{
if(!CurSymbolOnly || OrderSymbol() == Symbol())
{
if(OrderType() == OP_BUY || OrderType() == OP_SELL)
{
k = ArraySize(oob);
ArrayResize(oob, k + 1);
ArrayResize(oty, k + 1);
ArrayResize(olo, k + 1);
ArrayResize(osy, k + 1);
ArrayResize(oop, k + 1);
ArrayResize(ocb, k + 1);
ArrayResize(ocp, k + 1);
ArrayResize(osw, k + 1);
ArrayResize(opr, k + 1);
oob[k] = iBarShift(NULL, 0, OrderOpenTime()); // íîìåð áàðà îòêðûòèÿ
oty[k] = OrderType(); // òèï
olo[k] = OrderLots(); // ëîò
osy[k] = OrderSymbol(); // èíñòðóìåíò
oop[k] = OrderOpenPrice(); // öåíà îòêðûòèÿ
ocb[k] = iBarShift(NULL, 0, OrderCloseTime()); // íîìåð áàðà çàêðûòèÿ
ocp[k] = OrderClosePrice(); // öåíà çàêðûòèÿ
osw[k] = OrderSwap(); // ñâîï
opr[k] = OrderProfit(); // ïðèáûëü
}
}
}
}
}
//+----------------------------------------------------------------------------+
Comments