GetHistoryInd
Download scriptThis tool is designed to automatically track and record your trading activity by exporting specific data points into organized spreadsheet files (CSV format). It functions as a background logger that monitors both your completed trades and the real-time price movements of your currently active positions.
Core Functionality
The script operates on a minute-by-minute basis. Instead of running constant calculations that could slow down your platform, it checks the time once every minute. When a new minute begins, it executes two primary tasks:
1. Historical Trade Logging The tool scans your account history to identify all closed trades. It extracts key details for each completed transaction, including:
- Unique ticket numbers and trade types.
- The specific symbols traded and the quantity (lots) used.
- Entry and exit prices, along with any Stop Loss or Take Profit levels.
- The exact date and time of opening and closing.
- The final profit/loss amount and any comments attached to the trade.
This information is compiled into a file named ClosedPositions.csv, providing you with a clean record of your past performance.
2. Real-Time Price Action Tracking If the tracking feature is enabled, the script also monitors your active (open or pending) positions. For every open trade, it captures the highest and lowest prices reached during the previous minute on a 1-minute timeframe.
This "Price Action" data is recorded in a separate log file that includes a timestamp for each entry. This allows you to see exactly how much price volatility occurred while your trades were live, providing a granular view of market movement relative to your open positions.
Summary of Operation
Essentially, this script acts as an automated bookkeeper. It ensures that every completed trade is documented with full detail and, simultaneously, creates a chronological diary of price movements for any positions you are currently holding, making it easier to review trading behavior and market conditions later.
//+------------------------------------------------------------------+
//|GetHistoryInd.mq4 - For creating file of closed and open/pending positions
//|Updated 4/30/07 - Added Collect Price Action - collect M1 data while position open
//+------------------------------------------------------------------+
#property copyright ""
#property link ""
#property indicator_chart_window
extern string OpenOrderComment;
extern bool CollectPriceAction=true;
static int mPrev;
int m;
//+------------------------------------------------------------------+
int init()
{
mPrev=Minute();
return(0);
}
//+------------------------------------------------------------------+
int start()
{
int i,handle,hstTotal=HistoryTotal();
m=Minute();
if(m!=mPrev) //Get updates every minute
{
mPrev=m;
GetClosedPositionData();
if(CollectPriceAction) GetPriceAction();
}
return(0);
}
//+------------------------------------------------------------------+
void GetClosedPositionData()
{
int i, handle;
handle=FileOpen("ClosedPositions.csv",FILE_WRITE|FILE_CSV,",");
if(handle<0) return(0);
for(i=0;i<HistoryTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==true)
{
FileWrite(handle,OrderTicket(),TimeToStr(OrderOpenTime(),TIME_DATE|TIME_MINUTES),OrderType(),OrderLots(),OrderSymbol(),OrderOpenPrice(),OrderStopLoss(),OrderTakeProfit(),TimeToStr(OrderCloseTime(),TIME_DATE|TIME_MINUTES),OrderClosePrice(),OrderProfit(),OrderComment());
}
}
FileClose(handle);
}
void GetPriceAction()
{
int i,iOrderTicket;
string sSymbol;
double dHigh,dLow;
for (i = 0; i <= OrdersTotal(); i++)
{
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
iOrderTicket=OrderTicket();
sSymbol=OrderSymbol();
dHigh=iHigh(sSymbol,PERIOD_M1,1);
dLow=iLow(sSymbol,PERIOD_M1,1);
WriteLog(sSymbol+iOrderTicket,DoubleToStr(dHigh,4)+","+DoubleToStr(dLow,4));
}
return(0);
}
void WriteLog(string FileName,string sLine)
{
int handle=FileOpen(FileName+".csv",FILE_CSV|FILE_WRITE|FILE_READ,",");
string DateStamp=TimeToStr(TimeCurrent(),TIME_DATE|TIME_MINUTES);
if(handle>0)
{
FileSeek(handle,0,SEEK_END);
FileWrite(handle,DateStamp+","+sLine);
FileClose(handle);
}
}
Comments