OrderScreenShot_v1
Download scriptThis script is designed to automate the process of capturing visual evidence of your trading activities directly within the MetaTrader platform. Its primary purpose is to automatically take a screenshot of your chart at the exact moment a new trade is opened, creating an organized gallery of your market entries.
Core Logic and Workflow
The script operates by constantly monitoring your active trading accounts. Here is the step-by-step logic it follows:
1. Trade Identification The script scans all currently open positions. It specifically looks for "manual" trades (those without a specific magic number) that match the current currency pair you are viewing. It filters out pending orders, focusing only on active Buy or Sell positions.
2. New Order Detection To ensure it doesn't capture the same trade repeatedly, the script maintains a memory of "Last Tickets." Every time the price updates, it compares the current list of open trades against the ones it saw during the previous update. If it detects a brand-new ticket number that wasn't there before, it triggers the capture sequence.
3. Verification and Context Before taking a screenshot, the script performs a final check to ensure it is running on the correct chart window corresponding to the trade's currency pair and timeframe. This prevents the script from capturing irrelevant data if you have multiple charts open simultaneously.
4. Automated File Organization Once a new trade is confirmed, the script performs several actions in rapid succession:
- Visual Feedback: It temporarily displays a comment on your chart showing the ticket number, order type (Buy/Sell), lot size, and opening price.
- Folder Creation: It automatically generates a specific file path based on the exact time the trade was opened. This includes the day, month, year, hour, minute, and second of the trade.
- Screenshot Capture: It captures a high-quality image of the current chart window and saves it as a
.giffile. The filename is uniquely identified by the trade's ticket number. - Cleanup: It clears the temporary comment from your screen immediately after the capture is complete.
Summary of Benefits
Instead of a trader having to manually press a "Print Screen" button every time they enter a position, this script provides a hands-off solution for journaling. It creates a chronological, organized archive of your trades, allowing you to review your entries and market conditions later by simply looking through the generated image folders.
//+------------------------------------------------------------------+
//| OrderScreenShot_v1.mq4 |
//| * |
//| * |
//+------------------------------------------------------------------+
#import "user32.dll"
int GetAncestor(int hWnd, int gaFlags);
int GetDlgItem(int hDlg, int nIDDlgItem);
int SendMessageA(int hWnd, int Msg, int wParam, int lParam);
int GetWindowTextA(int hWnd, string lpString, int nMaxCount);
int GetWindowTextLengthA(int hWnd);
int GetWindowRect(int hWnd,int & lpRect[]);
#import
#property copyright "*"
#property link "*"
#property indicator_chart_window
//---- input parameters
string Path="OrderScreenShot";
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start(){
if(!IsDllsAllowed()){
Alert("Ðàçðåøèòå DLL â íàñòðîéêàõ òåðìèíàëà");
}
static bool ft=false;
static int LastTickets[];
int NowTickets[];
int Size=0;
ArrayResize(NowTickets,OrdersTotal());
for(int i=0;i<OrdersTotal();i++){
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
if(OrderMagicNumber()==0){
if(OrderSymbol()==Symbol()){
if(OrderType()==OP_BUY || OrderType()==OP_SELL){
NowTickets[Size]=OrderTicket();
Size++;
}
}
}
}
}
ArrayResize(NowTickets,Size);
//===================================================================
if(ft){
for(i=0;i<Size;i++){
bool exist=false;
for(int j=0;j<ArraySize(LastTickets);j++){
if(NowTickets[i]==LastTickets[j]){
exist=true;
}
}
if(!exist){
if(OrderSelect(NowTickets[i],SELECT_BY_TICKET)){
if(Symbol()+","+fTimeFrameName(Period())==GetNameActiveChart()){
int Rect[4];
GetWindowRect(WindowHandle(Symbol(),Period()),Rect);
Comment(OrderTicket()+" "+fNameOrderType(OrderType())+" "+Symbol()+" "+DS2(OrderLots())+", price: "+DS(OrderOpenPrice()));
int h=FileOpen(Path+"\\1.txt",FILE_CSV|FILE_WRITE);
FileClose(h);
FileDelete(Path+"\\1.txt");
string Path2=Path+"\\";
Path2=Path2+FM2(TimeDay(OrderOpenTime()))+"."+FM2(TimeMonth(OrderOpenTime()))+"."+TimeYear(OrderOpenTime())+"\\";
Path2=Path2+FM2(TimeHour(OrderOpenTime()))+"h"+FM2(TimeMinute(OrderOpenTime()))+"m"+FM2(TimeSeconds(OrderOpenTime()))+"s "+OrderTicket()+".gif";
WindowScreenShot(Path2,Rect[2]-Rect[0],Rect[3]-Rect[1]);
Comment("");
}
}
else{
return;
}
}
}
}
//===================================================================
ArrayResize(LastTickets,Size);
if(Size>0){
ArrayCopy(LastTickets,NowTickets);
}
ft=true;
return(0);
}
//+------------------------------------------------------------------+
string FM2(int aArg){
if(aArg<10){
return("0"+aArg);
}
return(aArg);
}
string DS2(double v){return(DoubleToStr(v,2));}
string DS(double v){return(DoubleToStr(v, Digits));}
string fNameOrderType(int aOrderType){
switch (aOrderType){
case OP_BUY: return("BUY");
case OP_SELL: return("SELL");
case OP_BUYLIMIT: return("BUYLIMIT");
case OP_SELLLIMIT: return("SELLLIMIT");
case OP_BUYSTOP: return("BUYSTOP");
case OP_SELLSTOP: return("SELLSTOP");
default: return("?");
}
}
string GetNameActiveChart(){
int ThWnd,hMDICWnd,hMDIAWnd,nMxCnt;
string sActiveChartName=" ";
ThWnd=GetAncestor(WindowHandle(Symbol(),Period()),2);
hMDICWnd=GetDlgItem(ThWnd,0xE900);
hMDIAWnd=SendMessageA(hMDICWnd,0x0229,0,0);
nMxCnt=GetWindowTextLengthA(hMDIAWnd);
GetWindowTextA(hMDIAWnd,sActiveChartName,nMxCnt+1);
return(sActiveChartName);
}
string fTimeFrameName(int arg){
// fTimeFrameName();
int v;
if(arg==0){
v=Period();
}
else{
v=arg;
}
switch(v){
case 0:
return("0");
case 1:
return("M1");
case 5:
return("M5");
case 15:
return("M15");
case 30:
return("M30");
case 60:
return("H1");
case 240:
return("H4");
case 1440:
return("D1");
case 10080:
return("W1");
case 43200:
return("MN1");
default:
return("M"+arg);
}
}
Comments