Okay, here's a breakdown of what this MetaTrader script does, explained in a way that doesn't require any programming knowledge.
This script is designed to automatically trade on your behalf, primarily based on a technical indicator called the Parabolic SAR (PSAR). Think of it as an automated assistant that watches the market and makes trading decisions according to a specific set of rules.
Here's the overall process:
-
Setup & Initialization: When the script starts, it first grabs information that you've set in the "input parameters." This includes things like:
- The amount of money to risk on each trade ("Lots").
- How much the price can "slip" before a trade is no longer valid ("Slip"). This is a buffer to account for rapid price movements.
- How much profit to aim for ("TakeProfit").
- How much of a loss you're willing to accept ("StopLoss").
- The settings for the Parabolic SAR indicator, which influences the buy and sell signals ("Step", "Maximum").
- Whether to close existing trades when an opposite signal appears ("CloseOnOpposite").
- The specific hours of the day it's allowed to trade ("StartHour", "EndHour").
-
Watching the Market: The script constantly monitors the price of the currency pair you're trading (e.g., EURUSD). It uses the Parabolic SAR indicator to generate potential buy or sell signals. The PSAR essentially provides hints as to when the price trend might be reversing.
-
Time Check: Before placing any trades, the script checks the current time to make sure it's within the hours you've specified (StartHour and EndHour). It will only trade during this allowed time window.
-
Trading Decisions (Opening Trades): Here's where the core logic kicks in. The script looks at the PSAR indicator and compares it to the current price:
- Buy Signal: If the PSAR suggests that the price is likely to go up (an upward trend), the script places a "buy" order. In plain language, it's betting that the price will increase.
- Sell Signal: Conversely, if the PSAR indicates a potential downward trend, the script places a "sell" order, betting that the price will decrease.
- There is a check to see if the trade is already open before opening another trade
-
Managing Existing Trades (Closing and Modifying Trades): The script manages any open trades and takes the following actions
- Close Opposite Trade: If "CloseOnOpposite" is set to true, and a buy signal appears while you have a sell trade open (or vice-versa), the script will close the original trade. The thinking here is to cut losses or lock in profits when the trend seems to be changing.
- Setting Take Profit and Stop Loss: The script also sets "Take Profit" and "Stop Loss" levels on open trades.
- Take Profit is a price level at which the script will automatically close the trade for a profit.
- Stop Loss is a price level at which the script will automatically close the trade to limit losses if the price moves against your bet.
-
Error Handling: The script includes some basic error checking. For example, it checks for common trading errors like invalid "Stop Loss" levels. If an error occurs, it will display an alert message.
In Summary:
This script is like a robot trader that:
- Watches the market price, using the PSAR indicator.
- Only trades during specific hours.
- Opens "buy" or "sell" trades based on signals from the PSAR.
- Closes trades when opposite signals appear.
- Automatically sets "Take Profit" and "Stop Loss" levels to manage risk.
//+------------------------------------------------------------------+
//| Vita.mq4 |
//| Copyright © 2012, www.FxAutomated.com |
//| http://www.FxAutomated.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2012, www.FxAutomated.com"
#property link "http://www.FxAutomated.com"
//---- input parameters
extern string Visit="www.fxautomated.com for more products";
extern string SignalsAndManagedAccounts="www.TradingBug.com";
extern double Lots=0.1;
extern int Slip=5;
extern string StopSettings="Set stops below";
extern double TakeProfit=50;
extern double StopLoss=50;
extern string PSARsettings="Parabolic sar settings follow";
extern double Step =0.001; //Parabolic setting
extern double Maximum =0.2; //Parabolic setting
extern bool CloseOnOpposite=true;
extern string TimeSettings="Set the hour range the EA should trade";
extern int StartHour=0;
extern int EndHour=23;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
Alert("Visit www.FxAutomated.com for more goodies!");
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
int digits=MarketInfo("EURUSD",MODE_DIGITS);
int StopMultd=10;
int Slippage=Slip*StopMultd;
int MagicNumber1=220101,MagicNumber2=220102,i,closesell=0,closebuy=0;
//------------------------------------------------------------
double TP=NormalizeDouble(TakeProfit*StopMultd,Digits);
double SL=NormalizeDouble(StopLoss*StopMultd,Digits);
double slb=NormalizeDouble(Ask-SL*Point,Digits);
double sls=NormalizeDouble(Bid+SL*Point,Digits);
double tpb=NormalizeDouble(Ask+TP*Point,Digits);
double tps=NormalizeDouble(Bid-TP*Point,Digits);
//-------------------------------------------------------------------+
//Check open orders
//-------------------------------------------------------------------+
if(OrdersTotal()>0){
for(i=1; i<=OrdersTotal(); i++) // Cycle searching in orders
{
if (OrderSelect(i-1,SELECT_BY_POS)==true) // If the next is available
{
if(OrderMagicNumber()==MagicNumber1) {int halt1=1;}
if(OrderMagicNumber()==MagicNumber2) {int halt2=1;}
}
}
}
//-------------------------------------------------------------------+
// time check
//-------------------------------------------------------------------
if((Hour()>=StartHour)&&(Hour()<=EndHour))
{
int TradeTimeOk=1;
}
else
{ TradeTimeOk=0; }
//-----------------------------------------------------------------
// Bar checks
//-----------------------------------------------------------------
//-------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------
// Opening criteria
//-----------------------------------------------------------------------------------------------------
Comment("For more goodies, managed accounts, forex signals and premium EAs visit www.FxAutomated.com");
// Open buy
if((iSAR(NULL, 0,Step,Maximum, 0)<iClose(NULL,0,0))&&(iSAR(NULL, 0,Step,Maximum, 1)>iClose(NULL,0,1))&&(TradeTimeOk==1)&&(halt1!=1)){
int openbuy=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"PSAR trader buy order",MagicNumber1,0,Blue);
if(CloseOnOpposite==true)closesell=1;
}
// Open sell
if((iSAR(NULL, 0,Step,Maximum, 0)>iClose(NULL,0,0))&&(iSAR(NULL, 0,Step,Maximum, 1)<iClose(NULL,0,1))&&(TradeTimeOk==1)&&(halt2!=1)){
int opensell=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,"PSAR trader sell order",MagicNumber2,0,Green);
if(CloseOnOpposite==true)closebuy=1;
}
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
// Closing criteria
//-------------------------------------------------------------------------------------------------
if(closesell==1||closebuy==1||openbuy<1||opensell<1){// start
if(OrdersTotal()>0){
for(i=1; i<=OrdersTotal(); i++){ // Cycle searching in orders
if (OrderSelect(i-1,SELECT_BY_POS)==true){ // If the next is available
if(OrderMagicNumber()==MagicNumber1&&closebuy==1) { OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,CLR_NONE); }
if(OrderMagicNumber()==MagicNumber2&&closesell==1) { OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,CLR_NONE); }
// set stops
if((OrderMagicNumber()==MagicNumber1)&&(OrderTakeProfit()==0)&&(OrderSymbol()==Symbol())){ OrderModify(OrderTicket(),0,OrderStopLoss(),tpb,0,CLR_NONE); }
if((OrderMagicNumber()==MagicNumber2)&&(OrderTakeProfit()==0)&&(OrderSymbol()==Symbol())){ OrderModify(OrderTicket(),0,OrderStopLoss(),tps,0,CLR_NONE); }
if((OrderMagicNumber()==MagicNumber1)&&(OrderStopLoss()==0)&&(OrderSymbol()==Symbol())){ OrderModify(OrderTicket(),0,slb,OrderTakeProfit(),0,CLR_NONE); }
if((OrderMagicNumber()==MagicNumber2)&&(OrderStopLoss()==0)&&(OrderSymbol()==Symbol())){ OrderModify(OrderTicket(),0,sls,OrderTakeProfit(),0,CLR_NONE); }
}
}
}
}// stop
//----
int Error=GetLastError();
if(Error==130){Alert("Wrong stops. Retrying."); RefreshRates();}
if(Error==133){Alert("Trading prohibited.");}
if(Error==2){Alert("Common error.");}
if(Error==146){Alert("Trading subsystem is busy. Retrying."); Sleep(500); RefreshRates();}
//----
//-------------------------------------------------------------------
return(0);
}
//+------------------------------------------------------------------+
Comments