Bal_Eq_Ind_v1
Download scriptThis script is designed to provide a visual history of your trading account's financial health by tracking two key metrics: Balance and Equity. It generates a dual-line chart that allows you to see how your account funds have changed over time, distinguishing between realized gains (closed trades) and unrealized fluctuations (open trades).
Core Functionality
The primary goal of this tool is to reconstruct your account's financial journey by analyzing every trade in your history as well as your currently active positions. It calculates the values for your chart based on a starting deposit amount, which you can define in the settings.
How it Works
The logic follows a systematic three-step process:
1. Data Collection The script scans your entire trading history and identifies every "Buy" or "Sell" order. For each trade, it captures specific details including:
- The date and time the trade was opened and closed.
- The currency pair (symbol) traded.
- The volume (lots) and the opening/closing prices.
- Any overnight swap fees or realized profits.
2. Time-Based Mapping Instead of just showing a final profit number, the script maps every trade onto your trading chart's timeline. It identifies exactly which "bars" (time segments) each trade occupied. This allows the tool to calculate what your account looked like at any specific moment in the past.
3. Dynamic Calculation The indicator then performs a continuous calculation for every point on your chart:
- Balance Line: This represents your "locked-in" funds. Every time a trade is closed, its final profit or loss (including swaps) is added to your total balance. This line typically moves in steps as trades are completed.
- Equity Line: This represents your "live" account value. It takes your current Balance and adds the floating profit or loss of all currently open positions. Because market prices move constantly, this line reflects the real-time fluctuations of your capital while trades are still active.
Key Features for Users
- Real-Time Tracking: By calculating the difference between price and entry for open orders, it provides a dynamic view of your "floating" profit.
- Multi-Symbol Awareness: It can be configured to monitor every trade across all currency pairs you trade, or restricted to only show the performance of the specific chart you are currently viewing.
- Historical Visualization: By looking back at the lines on the chart, you can identify periods of high volatility, see how quickly your equity dropped during certain trades, and observe the "gap" between your balance and equity to understand your current risk exposure.
// +------------------------------------------------------------------+
// | i-BalEq.mq4 |
// | Kim Igor aka KimIV |
// | http: // www.kimiv.ru |
// | 01.10.2006 Schedule of balance and equity. |
// +------------------------------------------------------------------+
#property copyright " Kim Igor 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
//-------External parameters of the indicator
extern double Deposit = 1000; // the initial deposit
// To consider only the current symbol
extern bool CurSymbolOnly = False;
//-------Buffer for the indicator
double dBuf0[], dBuf1[];
//-------Global variables of the indicator
int oob[]; // index of opening bar
int oty[]; // order type
double olo[]; // lot
string osy[]; // symbol
double oop[]; // opening price
int ocb[]; // index of closing bar
double ocp[]; // closing price
double osw[]; // swap
double opr[]; // profit
// +------------------------------------------------------------------+
// | Custom indicator initialization function |
// +------------------------------------------------------------------+
void init()
{
IndicatorDigits(2);
// - ---
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 += osw[j] + (iClose(osy[j], 0, i) - oop[j]) / p*olo[j]*t;
else
e += osw[j] + (oop[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;
}
}
// +------------------------------------------------------------------+
// | Reading of transactions |
// +------------------------------------------------------------------+
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 = OrdersHistoryTotal(), i, k;
// - ---
for (i = 0; i < h; i++) // read closed orders
{
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()); // index of opening bar
oty[k] = OrderType(); // type
olo[k] = OrderLots(); // lot
osy[k] = OrderSymbol(); // symbol
oop[k] = OrderOpenPrice(); // opening price
ocb[k] = iBarShift(NULL, 0, OrderCloseTime()); // index of closing bar
ocp[k] = OrderClosePrice(); // closing price
osw[k] = OrderSwap(); // swap
opr[k] = OrderProfit(); // profit
}
}
}
}
h = OrdersTotal();
// - ---
for (i = 0; i < h; i++) // read active orders
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
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 ()); // index of opening bar
oty[k] = OrderType(); // type
olo[k] = OrderLots(); // lot
osy[k] = OrderSymbol(); // symbol
oop[k] = OrderOpenPrice(); // opening price
ocb[k] = 0; // index of closing bar (no closing bar yet)
ocp[k] = 0; // closing price (no closing price yet)
osw[k] = OrderSwap(); // swap
opr[k] = OrderProfit(); // profit
}
}
}
}
}
// +------------------------------------------------------------------+
Comments