This script is designed to automatically trade based on the Parabolic SAR indicator. The Parabolic SAR is a tool used in technical analysis that helps identify potential trend direction and reversal points in the price of an asset.
Here's the breakdown of what the script does:
-
Initial Setup:
- The script starts by setting up some basic information, including copyright details and links.
- It also defines three adjustable settings:
StopLoss
,TakeProfit
, andLots
.StopLoss
sets a limit on how much you're willing to lose on a trade.TakeProfit
sets a target profit level for a trade.Lots
determines the size of the trades the script will make.
-
Trading Logic:
- The core of the script is a
start()
function that runs repeatedly. Each time it runs, it checks the current market conditions and decides whether to open or close trades. - It uses the Parabolic SAR indicator to generate trading signals. The Parabolic SAR plots dots on a chart, which are used to determine potential buy or sell signals.
- Buy Signal: If the current Parabolic SAR dot is below the current closing price, and the previous SAR dot was above the previous closing price, it's considered a buy signal.
- Sell Signal: Conversely, if the current Parabolic SAR dot is above the current closing price, and the previous SAR dot was below the previous closing price, it's a sell signal.
- The core of the script is a
-
Trade Execution:
- If a buy signal is detected, the script first checks if there is an open sell order. If there is, it closes the sell order. Then, if there are no open orders, the script opens a buy order with the specified
Lots
,StopLoss
, andTakeProfit
settings. - If a sell signal is detected, the script first checks if there is an open buy order. If there is, it closes the buy order. Then, if there are no open orders, the script opens a sell order with the specified
Lots
,StopLoss
, andTakeProfit
settings. - The script includes a retry mechanism to handle potential errors when closing orders, such as busy server issues.
- If a buy signal is detected, the script first checks if there is an open sell order. If there is, it closes the sell order. Then, if there are no open orders, the script opens a buy order with the specified
In essence, the script is a simple automated trading system that aims to buy when the Parabolic SAR suggests an upward trend and sell when it suggests a downward trend. It manages risk by using stop-loss and take-profit levels and trades fixed lot sizes.
//+------------------------------------------------------------------+
//| pp2.mq4 |
//| Copyright © 2010, |
//| |
//+------------------------------------------------------------------+
#property copyright " www.Forexyangu.com"
#property link "http://www.ForexYangu.com"
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------
extern int StopLoss =40; // SL for an opened order
extern int TakeProfit =70; // ?? for an opened order
extern double Lots =0.1; // Strictly set amount of lots
/*****************************************************-----READ THIS-------******************************************************
*******************************************************************************************************************************/
//-----------------------------------------------------------------------------------------------------------------------------
/*DONATE TO SUPPORT MY FREE PROJECTS AND TO RECEIVE NON OPEN PROJECTS AND ADVANCED VERSIONS OF EXISTING PROJECTS WHEN AVAILABLE:
//------------------------------------------------------------------------------------------------------------------------------
__my moneybookers email is admin@forexyangu.com anyone can easily join moneybookers and pay people via their email
through numerous payment methods__*/
//------------------------------------------------------------------------------------------------------------------------------
//SUPPORT AND INQUIRIES EMAIL: admin@forexyangu.com
//------------------------------------------------------------------------------------------------------------------------------
/*******************************************************************************************************************************
*************************************************--------END------*************************************************************/
/* Expert designed to open and close trades at first Parabolic SAR dots. Performance depends on your custom parameters
Contact me at tonnyochieng@gmail.com */
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//trading criteria
bool result;
double price;
int cmd,error;
//close
if ((iSAR(NULL, 0, 0.02, 0.2, 0)<iClose(NULL,0,0))&&(iSAR(NULL, 0, 0.02, 0.2, 1)>iClose(NULL,0,1))) //Signal Buy
{
if(OrderSelect(0,SELECT_BY_POS,MODE_TRADES))
{
cmd=OrderType();
//---- open order is sell
if(cmd==OP_SELL)
{
while(true)
{
if(cmd==OP_BUY) price=Bid;
else price=Ask;
result=OrderClose(OrderTicket(),OrderLots(),price,3,CLR_NONE);
if(result!=TRUE) { error=GetLastError(); Print("LastError = ",error); }
else error=0;
if(error==135) RefreshRates();
else break;
}
}
}
else Print( "Error when order select ", GetLastError());
// Closing Sell
//open buy
if(OrdersTotal()==0)
{
int openbuy=OrderSend(Symbol(),OP_BUY,Lots,Ask,5,Ask-StopLoss*Point,Ask+TakeProfit*Point);//Opening Buy
}
}
if ((iSAR(NULL, 0, 0.02, 0.2, 0)>iClose(NULL,0,0))&&(iSAR(NULL, 0, 0.02, 0.2, 1)<iClose(NULL,0,1))) //Signal Sell
{
if(OrderSelect(0,SELECT_BY_POS,MODE_TRADES))
{
cmd=OrderType();
//---- open order is buy
if(cmd==OP_BUY)
{
while(true)
{
if(cmd==OP_BUY) price=Bid;
else price=Ask;
result=OrderClose(OrderTicket(),OrderLots(),price,3,CLR_NONE);
if(result!=TRUE) { error=GetLastError(); Print("LastError = ",error); }
else error=0;
if(error==135) RefreshRates();
else break;
}
}
}
else Print( "Error when order select ", GetLastError());
// Closing Buy
//open sell
if(OrdersTotal()==0)
{
int opensell=OrderSend(Symbol(),OP_SELL,Lots,Bid,5,Bid+StopLoss*Point,Bid-TakeProfit*Point);//Opening Sell
}
}
//----------
return(0);
}
//------------------------------------------------------------------+
Comments