PSAR trader

Author: Copyright � 2012, www.FxAutomated.com
Profit factor:
1.02

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:

  1. 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").
  2. 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.

  3. 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.

  4. 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
  5. 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.
  6. 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.
Price Data Components
Series array that contains close prices for each bar
Orders Execution
Checks for the total of open ordersIt automatically opens orders when conditions are reachedIt Closes Orders by itself It can change open orders parameters, due to possible stepping strategy
Indicators Used
Parabolic Stop and Reverse system
Miscellaneous
It issuies visual alerts to the screen
11 Views
0 Downloads
0 Favorites
PSAR trader
//+------------------------------------------------------------------+
//|                                                         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);
  }
//+------------------------------------------------------------------+

Profitability Reports

USD/CHF Jul 2025 - Sep 2025
1.04
Total Trades 15
Won Trades 7
Lost trades 8
Win Rate 46.67 %
Expected payoff 1.23
Gross Profit 438.49
Gross Loss -420.09
Total Net Profit 18.40
-100%
-50%
0%
50%
100%
NZD/USD Jul 2025 - Sep 2025
1.15
Total Trades 15
Won Trades 8
Lost trades 7
Win Rate 53.33 %
Expected payoff 3.12
Gross Profit 354.60
Gross Loss -307.80
Total Net Profit 46.80
-100%
-50%
0%
50%
100%
GBP/USD Jul 2025 - Sep 2025
1.15
Total Trades 15
Won Trades 8
Lost trades 7
Win Rate 53.33 %
Expected payoff 3.57
Gross Profit 399.50
Gross Loss -345.90
Total Net Profit 53.60
-100%
-50%
0%
50%
100%
GBP/CAD Jul 2025 - Sep 2025
0.79
Total Trades 18
Won Trades 8
Lost trades 10
Win Rate 44.44 %
Expected payoff -3.99
Gross Profit 268.35
Gross Loss -340.24
Total Net Profit -71.89
-100%
-50%
0%
50%
100%
GBP/AUD Jul 2025 - Sep 2025
1.14
Total Trades 15
Won Trades 8
Lost trades 7
Win Rate 53.33 %
Expected payoff 2.23
Gross Profit 263.93
Gross Loss -230.53
Total Net Profit 33.40
-100%
-50%
0%
50%
100%
EUR/USD Jul 2025 - Sep 2025
0.00
Total Trades 202
Won Trades 1
Lost trades 201
Win Rate 0.50 %
Expected payoff -49.51
Gross Profit 49.90
Gross Loss -10050.00
Total Net Profit -10000.10
-100%
-50%
0%
50%
100%
AUD/USD Jul 2025 - Sep 2025
2.39
Total Trades 14
Won Trades 9
Lost trades 5
Win Rate 64.29 %
Expected payoff 18.69
Gross Profit 449.50
Gross Loss -187.90
Total Net Profit 261.60
-100%
-50%
0%
50%
100%
USD/JPY Jan 2025 - Jul 2025
1.06
Total Trades 43
Won Trades 22
Lost trades 21
Win Rate 51.16 %
Expected payoff 0.95
Gross Profit 743.59
Gross Loss -702.88
Total Net Profit 40.71
-100%
-50%
0%
50%
100%
USD/CHF Jan 2025 - Jul 2025
1.59
Total Trades 38
Won Trades 23
Lost trades 15
Win Rate 60.53 %
Expected payoff 12.87
Gross Profit 1323.10
Gross Loss -834.16
Total Net Profit 488.94
-100%
-50%
0%
50%
100%
USD/CAD Jan 2025 - Jul 2025
1.36
Total Trades 36
Won Trades 21
Lost trades 15
Win Rate 58.33 %
Expected payoff 5.34
Gross Profit 721.18
Gross Loss -528.92
Total Net Profit 192.26
-100%
-50%
0%
50%
100%

Comments