Train_05_02a_001
Download scriptThis script is designed as a specialized analytical tool for MetaTrader 4 that performs a "look-back" simulation to evaluate potential trading opportunities based on historical price action. Essentially, it acts as a data processing engine that scans past market behavior to identify patterns where specific profit targets were met without hitting predefined risk limits.
Core Logic Overview
The script operates by analyzing historical price movements across different timeframes. Its primary goal is to determine whether a trade initiated at a specific point in the past would have been successful or failed based on two main criteria: your desired Take Profit (the goal) and your Stop Loss (the safety net).
How the Process Works
- Data Collection: The script identifies a significant window of historical data (referred to as the "Training" area). It looks back at thousands of previous price points to create a database of market behavior.
- The Simulation Loop: For every bar in this historical period, the script simulates two hypothetical scenarios simultaneously:
- A Buy Scenario: It calculates what would happen if you entered a trade at the closing price of that specific moment.
- A Sell Scenario: It simultaneously calculates what would happen if you entered a trade in the opposite direction.
- Success Validation: For each simulated trade, the script scans forward into the "Future Window" (the price movement immediately following the entry point). It checks for two conditions:
- Risk Check: Did the price move against the position far enough to hit the Stop Loss? If yes, the trade is marked as invalid.
- Reward Check: Did the price move in the desired direction far enough to reach the Take Profit? If this was achieved without hitting the Stop Loss first, the trade is considered a "success."
- Visualization: The script records these outcomes into visual buffers on your chart:
- Successful Buys are represented as histograms.
- Successful Sells are represented as histograms.
- If neither condition was met (the price moved sideways or hit the stop loss first), no mark is made.
Key Parameters Explained
To help you manage the tool, here is what the primary settings control:
- TrainPeriod: Determines the timeframe (e.g., 5-minute charts) used to analyze the historical data.
- StopLoss: The maximum amount of price movement the script will tolerate against your position before it considers the trade "failed."
- TakeProfit: The minimum amount of price movement required in your favor for the script to consider the trade a "success."
- Future Window: This defines how far into the future the script looks from any given historical point to see if the profit target was eventually hit.
Summary for the User
Instead of just telling you what the market is doing now, this script reconstructs the past to show you where successful trades actually occurred. It filters out "noise" by only highlighting instances where the price successfully reached your profit goals while staying safely away from your stop-loss limits.
//+------------------------------------------------------------------+
//| Train_04_01.mq4 |
//| Copyright © 2008, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net"
#define NR_OF_SAMPLE_LINES 2000
#define NR_OF_TEST_LINES 800
#define NR_OF_TEST_BARS 10
#define FUTURE_WINDOW 200
#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1 Blue
#property indicator_color2 Red
#property indicator_color3 Lime
#property indicator_color4 Magenta
extern int TrainPeriod=PERIOD_M5;
extern int StopLoss=70; //70
extern int TakeProfit=70; //70
int NrTrainBars=NR_OF_SAMPLE_LINES;
int NrTestBars=NR_OF_TEST_LINES;
int FutureWindow=FUTURE_WINDOW;
int c;
double Buffer1[];
double Buffer2[];
double Buffer3[];
double Buffer4[];
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int myBars1;
bool isNewBar1(int tf1)
{
//----
bool res=false;
if (myBars1!=iBars(Symbol(),tf1))
{
res=true;
myBars1=iBars(Symbol(),tf1);
}
//----
return(res);
}
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
//---- 3 indicator buffers mapping
SetIndexBuffer(0,Buffer1);
SetIndexBuffer(1,Buffer2);
SetIndexBuffer(2,Buffer3);
SetIndexBuffer(4,Buffer4);
//---- drawing settings
SetIndexStyle(0,DRAW_HISTOGRAM);
SetIndexStyle(1,DRAW_HISTOGRAM);
SetIndexStyle(2,DRAW_LINE);
SetIndexStyle(3,DRAW_LINE);
SetIndexEmptyValue(0,EMPTY_VALUE);
SetIndexEmptyValue(1,EMPTY_VALUE);
SetIndexEmptyValue(2,EMPTY_VALUE);
SetIndexEmptyValue(3,EMPTY_VALUE);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int limit;
int counted_bars=IndicatorCounted();
//---- check for possible errors
if(counted_bars<0) return(-1);
//---- last counted bar will be recounted
if(counted_bars>0) counted_bars--;
limit=Bars-counted_bars;
//---- main loop
if(isNewBar1(PERIOD_M1))
{
int spread=MarketInfo(Symbol(),MODE_SPREAD);
double Point1=MarketInfo(Symbol(),MODE_POINT);
double sp=spread*Point;
double entry_buy;//entry_buy=bid0+sp
double entry_sell;//entry_sell=bid0
bool longOk;
bool shortOk;
int FW=FutureWindow;
int i,j,fw;
double l,h;
datetime t1=iTime(Symbol(),TrainPeriod,FW);
int FW2=iBarShift(Symbol(),0,t1,true);
datetime t2=iTime(Symbol(),TrainPeriod,NrTrainBars);
int NrTrainBars2=iBarShift(Symbol(),0,t2,true);
for(i=FW2,j=0;j<NrTrainBars2;i++,j++)
//for ( i=nr_of_sample_lines-1; i>=0; i--)
{
c=-2;
datetime time1=iTime(Symbol(),0,i);
int ii=iBarShift(Symbol(),TrainPeriod,time1,true);
entry_buy = iClose(Symbol(),TrainPeriod,ii)+sp;//entry_buy=bid0+sp
entry_sell = iClose(Symbol(),TrainPeriod,ii);//entry_sell=bid0
longOk=true;
shortOk=true;
for (fw=0; fw<FW; fw++)
{
l=iLow(Symbol(),TrainPeriod,ii-fw);
h=iHigh(Symbol(),TrainPeriod,ii-fw);
if (l<=entry_buy-StopLoss*Point) longOk=false;
if (h+sp>=entry_sell+StopLoss*Point) shortOk=false;
if (longOk && (h>=entry_buy+TakeProfit*Point)) { c=(+1)*fw; break; }
if (shortOk && (l+sp<=entry_sell-TakeProfit*Point)) { c=(-1)*fw; break; }
}
if(c != -2)
if(c>0)
Buffer1[i]=c;
else
Buffer2[i]=c;
else
{
Buffer1[i]=0;
Buffer2[i]=0;
}
}
/*
//for( i=0; i<limit; i++)
for(i=FUTURE_WINDOW ;i<NR_OF_SAMPLE_LINES;i++)
{
Buffer1[i]=Close[i];
//if(svm_samples_class_dyn_1[i]==-1)
// Buffer2[i]=-1;
}
*/
/*
for(i=0;i<NR_OF_TEST_LINES;i++)
{
if(ptr_predict_label_1[i]==1)
Buffer3[i]=1.1;
if(ptr_predict_label_1[i]==-1)
Buffer4[i]=-1.1;
}
*/
}
//----
return(0);
}
//+------------------------------------------------------------------+
Comments