This script is designed to automatically trade on the Forex market using the Parabolic SAR (Stop and Reverse) indicator. Think of the Parabolic SAR as a tool that helps identify potential trend changes in the price of a currency pair.
Here's how it works:
-
Setup: The script starts by reading in settings you provide, such as the amount of currency to trade per transaction (Lots), how much price slippage to allow (Slip), the desired profit target (TakeProfit), and the acceptable loss level (StopLoss). It also takes settings specific to the Parabolic SAR indicator (Step, Maximum), which affect its sensitivity to price changes. You can also specify trading hours (StartHour, EndHour).
-
Time Check: Before doing anything, the script checks if the current time is within the hours you've allowed it to trade. If not, it simply waits.
-
Trend Identification and Opening Orders: The core logic revolves around the Parabolic SAR. The script compares the Parabolic SAR value to the current price.
- If the Parabolic SAR suggests the price is about to rise (a "buy" signal) AND there are no existing open orders, the script opens a "buy" order (hoping to profit from the price increase).
- Conversely, if the Parabolic SAR signals a potential price decrease (a "sell" signal) AND there are no existing open orders, it opens a "sell" order.
-
Closing Orders: The script can be set to close an existing trade when an opposite signal occurs. This is controlled by the
CloseOnOppositeSignalsetting. If it's turned on:- If the script opened a "buy" order and now a "sell" signal appears, it closes the "buy" order.
- If a "sell" order was opened and a "buy" signal is seen, it closes the "sell" order.
-
Managing Open Positions (Take Profit and Stop Loss): For any open positions associated with the script, it attempts to set "take profit" and "stop loss" levels. "Take profit" is the price level where the script automatically closes the trade to secure a profit. "Stop loss" is the price level where the script automatically closes the trade to limit potential losses.
-
Error Handling: The script includes some basic error detection. If it encounters problems (like incorrect stop-loss values or trading being prohibited), it displays alerts.
In simple terms, the script watches the market, uses the Parabolic SAR to try and predict price movements, and automatically buys or sells currency based on those predictions. It also tries to protect your investment by automatically closing trades when specific profit or loss levels are reached and can close trades as new signals are generated in the opposite direction.
//+------------------------------------------------------------------+
//| PSAR trader v2.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 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 CloseOnOppositeSignal=true;
extern string TimeSettings="Set the hour range the EA should trade";
extern int StartHour=0;
extern int EndHour=23;
extern int MagicNumber=220101;
string Trend;
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
int StopMultd=10;
int Slippage=Slip*StopMultd;
int i,closesell=0,closebuy=0;
//------------------------------------------------------------
double TP=NormalizeDouble(TakeProfit*StopMultd,Digits);
double SL=NormalizeDouble(StopLoss*StopMultd,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()==MagicNumber&&OrderSymbol()==Symbol()) {int halt=1;}
}
}
}
//-------------------------------------------------------------------+
// time check
//-------------------------------------------------------------------
if((Hour()>=StartHour)&&(Hour()<=EndHour))
{
int TradeTimeOk=1;
}
else
{ TradeTimeOk=0; }
//-----------------------------------------------------------------
// Bar checks
//-----------------------------------------------------------------
//-------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------
// Opening criteria
//-----------------------------------------------------------------------------------------------------
// 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)&&(halt!=1)&&Trend!="BUY"){
int openbuy=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"PSAR trader buy order",MagicNumber,0,Blue);
if(CloseOnOppositeSignal==true)closesell=1;
Trend="BUY";
}
// 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)&&(halt!=1)&&Trend!="SELL"){
int opensell=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,"PSAR trader sell order",MagicNumber,0,Green);
if(CloseOnOppositeSignal==true)closebuy=1;
Trend="SELL";
}
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
// 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()==MagicNumber&&closebuy==1&&OrderSymbol()==Symbol()&&OrderType()==OP_BUY) { OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,CLR_NONE); }
if(OrderMagicNumber()==MagicNumber&&closesell==1&&OrderSymbol()==Symbol()&&OrderType()==OP_SELL) { OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,CLR_NONE); }
// set stops
// Calculate take profit
double tpb=NormalizeDouble(OrderOpenPrice()+TP*Point,Digits);
double tps=NormalizeDouble(OrderOpenPrice()-TP*Point,Digits);
// Calculate stop loss
double slb=NormalizeDouble(OrderOpenPrice()-SL*Point,Digits);
double sls=NormalizeDouble(OrderOpenPrice()+SL*Point,Digits);
if((OrderMagicNumber()==MagicNumber)&&(OrderTakeProfit()==0)&&(OrderSymbol()==Symbol())&&OrderType()==OP_BUY){ OrderModify(OrderTicket(),0,OrderStopLoss(),tpb,0,CLR_NONE); }
if((OrderMagicNumber()==MagicNumber)&&(OrderTakeProfit()==0)&&(OrderSymbol()==Symbol())&&OrderType()==OP_SELL){ OrderModify(OrderTicket(),0,OrderStopLoss(),tps,0,CLR_NONE); }
if((OrderMagicNumber()==MagicNumber)&&(OrderStopLoss()==0)&&(OrderSymbol()==Symbol())&&OrderType()==OP_BUY){ OrderModify(OrderTicket(),0,slb,OrderTakeProfit(),0,CLR_NONE); }
if((OrderMagicNumber()==MagicNumber)&&(OrderStopLoss()==0)&&(OrderSymbol()==Symbol())&&OrderType()==OP_SELL){ 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);
}
//+------------------------------END------------------------------------+
int init()
{
//----
Alert("Visit www.FxAutomated.com for more goodies!");
//----
return(0);
}
Comments