EquityOnChart
Download scriptThis script is designed to visualize historical account equity data directly on a trading chart. Instead of showing standard price movements, it imports external data from a file and maps it against the visual timeline of the market.
Core Functionality
The logic follows a structured sequence to transform raw data into a visual representation:
1. Data Source Integration The script looks for an external CSV file (by default named "equity.csv"). It is designed to read this file and extract multiple rows of information. Each row represents a different set of data points—likely representing different accounts, categories, or specific equity metrics.
2. Time Alignment and Synchronization A key feature of this script is how it places the data on your chart:
- The Anchor Point: The script looks for a specific vertical line on your chart labeled "start". This acts as the chronological anchor.
- Dynamic Mapping: It identifies the exact moment in history where that vertical line exists and uses it as the starting point for drawing the imported data.
- Data Distribution: It then takes the historical numbers from your file and distributes them backwards across the chart bars, ensuring that the data points align with the correct dates and times shown on your screen.
3. Data Parsing and Scaling The script performs several "cleanup" steps on the raw text within the CSV file:
- Separation: It takes long strings of data (separated by semicolons) and breaks them down into individual numerical values.
- Conversion: It converts these text values into usable numbers that the trading platform can plot.
- Buffer Assignment: It organizes these numbers into five distinct "buffers." This allows the script to display up to five different lines simultaneously on your chart, each representing a different data series from your file.
4. Visual Environment Setup To ensure the data is readable, the script:
- Sets Drawing Styles: It defines specific colors and line styles (e.g., Forest Green, Blue, Red) for the different data sets.
- Establishes Reference Levels: It creates horizontal grid levels (ranging from 6,000 to 13,000) to provide a visual context for where the equity values sit relative to specific targets or thresholds.
Summary of User Experience
When you run this script, it automatically searches for your saved data file, finds your "start" marker on the chart, and draws five separate lines representing your history. This allows you to see how your account equity fluctuated over time in direct relation to the price action of the market.
//+------------------------------------------------------------------+
//| EquityOnChart.mq4 |
//| Copyright © 2007, MetaQuotes Software Corp. |
//| http://www.metaquotes.ru/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, MetaQuotes Software Corp."
#property link "http://www.metaquotes.ru/"
//#property indicator_separate_window
#property indicator_chart_window
#property indicator_buffers 5
#property indicator_color5 Red
#property indicator_color4 DarkOrange
#property indicator_color3 Blue
#property indicator_color2 Black
#property indicator_color1 ForestGreen
#property indicator_level1 13000
#property indicator_level2 12000
#property indicator_level3 11000
#property indicator_level4 10000
#property indicator_level5 9000
#property indicator_level6 8000
#property indicator_level7 7000
#property indicator_level8 6000
#property indicator_levelcolor Red
//---- input parameters
extern string fileName="equity.csv";
extern double MaxEquity=13000;
extern double MinEquity=6000;
//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
double ExtMapBuffer5[];
bool Calculated=false;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0,DRAW_LINE,0,3);
SetIndexBuffer(0,ExtMapBuffer1);
SetIndexStyle(1,DRAW_LINE,0,3);
SetIndexBuffer(1,ExtMapBuffer2);
SetIndexStyle(2,DRAW_LINE,0,3);
SetIndexBuffer(2,ExtMapBuffer3);
SetIndexStyle(3,DRAW_LINE,0,3);
SetIndexBuffer(3,ExtMapBuffer4);
SetIndexStyle(4,DRAW_LINE,0,5);
SetIndexBuffer(4,ExtMapBuffer5);
IndicatorShortName("");
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| Ïðîâåðèòü Ìàêñèìàëüíóþ è ìèíèìàëüíóþ öåíû íà ãðàôèêå |
//+------------------------------------------------------------------+
void CheckLevels(double & MaxPrice, double & MinPrice)
{
//----
int period=WindowBarsPerChart();
MaxPrice=High[iHighest(Symbol(),0,MODE_HIGH,period,0)];
MinPrice=Low[iLowest(Symbol(),0,MODE_LOW,period,0)];
Comment("");
//Comment("MaxPrice=",MaxPrice," MinPrice=",MinPrice);
//----
return;
}
//+------------------------------------------------------------------+
//| çàïèñü ñîäåðæèìîãî ôàéëà â ìàññèâ ñòðîê array[] |
//| â ñëó÷àå íåóäà÷è âåðíåì false |
//+------------------------------------------------------------------+
bool ReadFileToArray(string &array[],string FileName,string WorkFolderName,int devider='\x90')
{
bool res=false;
int FileHandle;
string tempArray[64000],currString;
int stringCounter;
string FullFileName;
if (StringLen(WorkFolderName)>0) FullFileName=StringConcatenate(WorkFolderName,"\\",FileName);
else FullFileName=FileName;
//----
//Print("Ïîïûòêà ïðî÷èòàòü ôàéë ",FileName);
FileHandle=FileOpen(FullFileName,FILE_READ|FILE_CSV,devider);
if (FileHandle!=-1)
{
while(!FileIsEnding(FileHandle))
{
currString=FileReadString(FileHandle);
tempArray[stringCounter]=currString;
//Print(stringCounter,": ",currString);
stringCounter++;
}
stringCounter--;
if (stringCounter>0)
{
ArrayResize(array,stringCounter);
for (int i=0;i<stringCounter;i++) array[i]=tempArray[i];
res=true;
}
FileClose(FileHandle);
}
else
{
Print("Íå óäàëîñü ïðî÷èòàòü ôàéë ",FileName,"| îøèáêà ",GetLastError());
}
//----
return(res);
}
//+------------------------------------------------------------------+
//| fill array of strings |
//+------------------------------------------------------------------+
void SplitString(string &ArrayRes[],string InputString,string splitter)
{
string temp,tempArray[100];
int pos,splitLength=StringLen(splitter),InputStrLength=StringLen(InputString),counter;
pos=StringFind(InputString,splitter);
if (pos!=-1)
{
if (pos==0) InputString=StringSubstr(InputString,splitLength,InputStrLength-splitLength);
while (StringFind(InputString,splitter)!=-1)
{
pos=StringFind(InputString,splitter);
InputStrLength=StringLen(InputString);
tempArray[counter]=StringSubstr(InputString,0,pos);
InputString=StringSubstr(InputString,pos+splitLength,InputStrLength-splitLength-pos);
counter++;
}
if (StringLen(InputString)!=0)
{
tempArray[counter]=InputString;
counter++;
}
}
ArrayResize(ArrayRes,counter);
for (int i=0;i<counter;i++)
{
ArrayRes[i]=tempArray[i];
//Print("i=",i," string=",ArrayRes[i]);
}
return;
}
//+------------------------------------------------------------------+
//| âîçâðàùàåò ïåðâûé èíäåêñ, ñ êîòîðîãî îòîáðàæàåòñÿ èíäèêàòîð |
//+------------------------------------------------------------------+
int GetFirstBarIndex()
{
int res;
//----
int window=ObjectFind("start");
if (window==0)
{
if (ObjectType("start")==OBJ_VLINE)
{
datetime Time1=ObjectGet("start",OBJPROP_TIME1);
//Print("Time1=",TimeToStr(Time1));
}
res=iBarShift(NULL,0,Time1);
}
//----
return(res);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int i,counted_bars=IndicatorCounted();
double max,min;
int firstBar=GetFirstBarIndex();
if (firstBar==0 && !Calculated)
{
Alert("Íå íàéäåíà âåðòèêàëüíàÿ ëèíèÿ ñ îïèñàíèåì \" start\" ");
Calculated=true;
return;
}
//firstBar;
//Print("firstBar=",firstBar);
//----
if (!Calculated)
{
CheckLevels(max,min); // íå òðåáóåòñÿ â äàííîì ñëó÷àå
double equityArray[][5];
string EquityStrings[];
string ArrayFromString[];
double scale=(MaxEquity-MinEquity)/(max-min);
if (!ReadFileToArray(EquityStrings,fileName,"")) Print("Íå óäàëîñü ïðî÷èòàòü äàííûå èç ôàéëà, îøèáêà ",GetLastError());
SplitString(ArrayFromString,EquityStrings[0],";");
int days=ArraySize(ArrayFromString);
ArrayResize(equityArray,days);
ExtMapBuffer1[firstBar]=10000;
for (i=1;i<days;i++)
{
ExtMapBuffer1[firstBar-i]=StrToDouble(ArrayFromString[i]);
// Print("i=",i, " ExtMapBuffer1=",ExtMapBuffer1[firstBar-i]," ArrayFromString=",ArrayFromString[i]);
}
SplitString(ArrayFromString,EquityStrings[1],";");
ExtMapBuffer2[firstBar]=10000;
for (i=1;i<days;i++)
{
ExtMapBuffer2[firstBar-i]=StrToDouble(ArrayFromString[i]);
// Print("i=",i, " ExtMapBuffer1=",ExtMapBuffer1[firstBar-i]," ArrayFromString=",ArrayFromString[i]);
}
SplitString(ArrayFromString,EquityStrings[2],";");
ExtMapBuffer3[firstBar]=10000;
for (i=1;i<days;i++)
{
ExtMapBuffer3[firstBar-i]=StrToDouble(ArrayFromString[i]);
// Print("i=",i, " ExtMapBuffer1=",ExtMapBuffer1[firstBar-i]," ArrayFromString=",ArrayFromString[i]);
}
SplitString(ArrayFromString,EquityStrings[3],";");
ExtMapBuffer4[firstBar]=10000;
for (i=1;i<days;i++)
{
ExtMapBuffer4[firstBar-i]=StrToDouble(ArrayFromString[i]);
// Print("i=",i, " ExtMapBuffer1=",ExtMapBuffer1[firstBar-i]," ArrayFromString=",ArrayFromString[i]);
}
SplitString(ArrayFromString,EquityStrings[4],";");
ExtMapBuffer5[firstBar]=10000;
for (i=1;i<days;i++)
{
ExtMapBuffer5[firstBar-i]=StrToDouble(ArrayFromString[i]);
// Print("i=",i, " ExtMapBuffer1=",ExtMapBuffer1[firstBar-i]," ArrayFromString=",ArrayFromString[i]);
}
Calculated=true;
}
//----
return(0);
}
//+------------------------------------------------------------------+
Comments