Okay, here's a breakdown of what this trading script does, explained in plain language.
Overall Goal:
This script is designed to automatically trade currencies in the Forex market based on signals from a custom indicator. Think of it as a robot that watches for certain patterns and then buys or sells a currency, trying to make a profit.
Here's how it works, step-by-step:
-
Setup:
- When the script starts, it first figures out which currency pair it's running on (like EURUSD, or GBPJPY). Based on the currency pair, it assigns a unique "Magic Number". This is like a secret code so the script only manages trades it has opened itself.
- It also gets information about the trading account and the currency pair, like how much leverage the account has, the minimum and maximum trade sizes allowed, and the smallest trade size increment.
-
Money Management (Optional):
- The script can manage the trade size automatically, based on how much money is in the account and a specified "Risk Percent". For example, if you set RiskPercent to 2.0, the script will risk 2% of the account balance on each trade.
- If automatic money management is turned off, the script will use a fixed "LotSize" for each trade.
-
Signal Check:
- The script uses a custom indicator (named "super-signals_v2b") to generate buy or sell signals. It checks the values of this indicator to determine if it should buy or sell.
-
Trade Management:
- Closing Existing Trades: Before opening any new trades, the script will close any existing trades opened by itself (identified by the Magic Number) that are in the opposite direction of the new signal. For example, if the script receives a buy signal, it will first close any existing sell trades.
- Opening New Trades: If the script sees a "buy" signal and no "buy" orders are already open, it opens a new "buy" order. If the script sees a "sell" signal and no "sell" orders are already open, it opens a new "sell" order. The trade size is determined by either the money management calculations or the fixed lot size.
- When it opens an order, it includes:
- Stop Loss: an order to automatically close the trade if the price moves against you by a certain amount, limiting potential losses.
- Take Profit: an order to automatically close the trade if the price moves in your favor by a certain amount, locking in profits.
- Slippage: the maximum acceptable difference between the requested price of the trade and the actual price when the order is executed.
-
Error Handling:
- The script includes basic error handling. If there's a problem opening a trade (e.g., connection issues, invalid trade size), it will print an error message. It also retries a few times if the error is related to the server being busy.
In Summary:
The script is an automated trading system that uses a custom indicator to generate buy and sell signals. It manages the trade size, stop loss, and take profit levels. It aims to automatically trade a currency pair based on the signals from the indicator, closing opposite trades before entering a new one.
//+------------------------------------------------------------------+
//| |
//| Super Signals EA.mq4 - Ver 1.1 @ 02/11/2007 by Bluto |
//| |
//+------------------------------------------------------------------+
// 2/22/2007 Ver 1.2 Commented out OrderModify and Changed StopLoss from 20 to 200
#property copyright "Bluto"
#property link "None"
extern double LotSize=5;
extern int Slippage=3;
extern double StopLoss=200; //original 20
extern double TakeProfit=500;
extern double RiskPercent=2.0;
extern bool UseMoneyMgmt=true;
int MagicNumber=0;
int ticket;
int BuyOrders=0;
int SellOrders=0;
int i;
double SS_SELL_0=0, SS_SELL_1=0, SS_BUY_0=0, SS_BUY_1=0;
double MM_MinLotSize=0;
double MM_MaxLotSize=0;
double MM_LotStep=0;
double MM_Decimals=0;
double MM_OrderLotSize=0;
int MM_AcctLeverage=0;
int MM_CurrencyLotSize=0;
bool SSIsBuy=false, SSIsSell=false;
int init()
{
if (Symbol()=="AUDCADm" || Symbol()=="AUDCAD") {MagicNumber=200001;}
if (Symbol()=="AUDJPYm" || Symbol()=="AUDJPY") {MagicNumber=200002;}
if (Symbol()=="AUDNZDm" || Symbol()=="AUDNZD") {MagicNumber=200003;}
if (Symbol()=="AUDUSDm" || Symbol()=="AUDUSD") {MagicNumber=200004;}
if (Symbol()=="CHFJPYm" || Symbol()=="CHFJPY") {MagicNumber=200005;}
if (Symbol()=="EURAUDm" || Symbol()=="EURAUD") {MagicNumber=200006;}
if (Symbol()=="EURCADm" || Symbol()=="EURCAD") {MagicNumber=200007;}
if (Symbol()=="EURCHFm" || Symbol()=="EURCHF") {MagicNumber=200008;}
if (Symbol()=="EURGBPm" || Symbol()=="EURGBP") {MagicNumber=200009;}
if (Symbol()=="EURJPYm" || Symbol()=="EURJPY") {MagicNumber=200010;}
if (Symbol()=="EURUSDm" || Symbol()=="EURUSD") {MagicNumber=200011;}
if (Symbol()=="GBPCHFm" || Symbol()=="GBPCHF") {MagicNumber=200012;}
if (Symbol()=="GBPJPYm" || Symbol()=="GBPJPY") {MagicNumber=200013;}
if (Symbol()=="GBPUSDm" || Symbol()=="GBPUSD") {MagicNumber=200014;}
if (Symbol()=="NZDJPYm" || Symbol()=="NZDJPY") {MagicNumber=200015;}
if (Symbol()=="NZDUSDm" || Symbol()=="NZDUSD") {MagicNumber=200016;}
if (Symbol()=="USDCHFm" || Symbol()=="USDCHF") {MagicNumber=200017;}
if (Symbol()=="USDJPYm" || Symbol()=="USDJPY") {MagicNumber=200018;}
if (Symbol()=="USDCADm" || Symbol()=="USDCAD") {MagicNumber=200019;}
if (MagicNumber==0) {MagicNumber = 200999;}
return(0);
}
int deinit()
{
return(0);
}
int start()
{
MM_AcctLeverage = AccountLeverage();
MM_MinLotSize = MarketInfo(Symbol(),MODE_MINLOT);
MM_MaxLotSize = MarketInfo(Symbol(),MODE_MAXLOT);
MM_LotStep = MarketInfo(Symbol(),MODE_LOTSTEP);
MM_CurrencyLotSize = MarketInfo(Symbol(),MODE_LOTSIZE);
if(MM_LotStep == 0.01) {MM_Decimals = 2;}
if(MM_LotStep == 0.1) {MM_Decimals = 1;}
if (UseMoneyMgmt == true)
{
MM_OrderLotSize = AccountEquity() * (RiskPercent * 0.01) / (MM_CurrencyLotSize / MM_AcctLeverage);
MM_OrderLotSize = StrToDouble(DoubleToStr(MM_OrderLotSize,MM_Decimals));
}
else
{
MM_OrderLotSize = LotSize;
}
if (MM_OrderLotSize < MM_MinLotSize) {MM_OrderLotSize = MM_MinLotSize;}
if (MM_OrderLotSize > MM_MaxLotSize) {MM_OrderLotSize = MM_MaxLotSize;}
SS_SELL_0 = iCustom(Symbol(),Period(),"super-signals_v2b",4,0,0);
SS_SELL_1 = iCustom(Symbol(),Period(),"super-signals_v2b",4,0,1);
SS_BUY_0 = iCustom(Symbol(),Period(),"super-signals_v2b",4,1,0);
SS_BUY_1 = iCustom(Symbol(),Period(),"super-signals_v2b",4,1,1);
if (SS_SELL_0 > 0 || SS_SELL_1 > 0) {SSIsSell=true;SSIsBuy=false;}
if (SS_BUY_0 > 0 || SS_BUY_1 > 0) {SSIsSell=false;SSIsBuy=true;}
BuyOrders=0;
SellOrders=0;
for(i=OrdersTotal();i >= 0;i--)
{
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
{
if (OrderType() == OP_BUY)
{
BuyOrders++;
if (StopLoss > 0 && Bid - (StopLoss * Point) > OrderStopLoss())
{
// OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Bid - (StopLoss * Point),Digits),OrderTakeProfit(),0);
}
}
if (OrderType() == OP_SELL)
{
SellOrders++;
if (StopLoss > 0 && Ask + (StopLoss * Point) < OrderStopLoss())
{
// OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Ask + (StopLoss * Point),Digits),OrderTakeProfit(),0);
}
}
}
}
if (SSIsBuy==true)
{
if(SellOrders > 0)
{
CloseShorts(MagicNumber);
SellOrders = 0;
}
if(BuyOrders == 0)
{
ticket = OpenPendingOrder(OP_BUY,MM_OrderLotSize,Ask,Slippage,Bid,StopLoss,TakeProfit,"Super Signals",MagicNumber,0,Lime);
if(ticket<0)
{
Print("OrderSend failed with error #",GetLastError());
return(0);
}
else
{
BuyOrders++;
}
}
}
if (SSIsSell==true)
{
if(BuyOrders > 0)
{
CloseLongs(MagicNumber);
BuyOrders = 0;
}
if (SellOrders == 0)
{
ticket = OpenPendingOrder(OP_SELL,MM_OrderLotSize,Bid,Slippage,Ask,StopLoss,TakeProfit,"Super Signals",MagicNumber,0,HotPink);
if(ticket<0)
{
Print("OrderSend failed with error #",GetLastError());
return(0);
}
else
{
SellOrders++;
}
}
}
return(0);
}
//----- Order Processing Functions
void CloseLongs(int MagicNumber)
{
int trade;
for(trade=OrdersTotal()-1;trade>=0;trade--)
{
if(OrderSelect(trade,SELECT_BY_POS,MODE_TRADES)==false)
continue;
if(OrderSymbol()!=Symbol()||OrderMagicNumber()!=MagicNumber)
continue;
if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
if(OrderType()==OP_BUY)
OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Blue);
}//for
}
void CloseShorts(int MagicNumber)
{
int trade;
for(trade=OrdersTotal()-1;trade>=0;trade--)
{
if(OrderSelect(trade,SELECT_BY_POS,MODE_TRADES)==false)
continue;
if(OrderSymbol()!=Symbol()||OrderMagicNumber()!=MagicNumber)
continue;
if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
if(OrderType()==OP_SELL)
OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,Red);
}//for
}
int OpenPendingOrder(int pType,double pLots,double pLevel,int sp, double pr, int sl, int tp,string pComment,int pMagic,datetime pExpiration,color pColor)
{
int ticket=0;
int err=0;
int c = 0;
int NumberOfTries = 10;
switch (pType)
{
case OP_BUY:
for(c = 0 ; c < NumberOfTries ; c++)
{
RefreshRates();
ticket=OrderSend(Symbol(),OP_BUY,pLots,Ask,sp,StopLong(Bid,sl),TakeLong(Bid,tp),pComment,pMagic,pExpiration,pColor);
if (ticket > 0) break;
err=GetLastError();
if(err==0)
{
break;
}
else
{
if(err==4 || err==137 ||err==146 || err==136) //Busy errors
{
Sleep(5000);
continue;
}
else //normal error
{
Print("Error Code= ", err);
break;
}
}
}
break;
case OP_SELL:
for(c = 0 ; c < NumberOfTries ; c++)
{
RefreshRates();
ticket=OrderSend(Symbol(),OP_SELL,pLots,Bid,sp,StopShort(Ask,sl),TakeShort(Ask,tp),pComment,pMagic,pExpiration,pColor);
if (ticket > 0) break;
err=GetLastError();
if(err==0)
{
break;
}
else
{
if(err==4 || err==137 ||err==146 || err==136) //Busy errors
{
Sleep(5000);
continue;
}
else //normal error
{
Print("Error Code= ", err);
break;
}
}
}
break;
}
return(ticket);
}
double StopLong(double price,int stop)
{
if(stop==0)
return(0);
else
return(price-(stop*Point));
}
double StopShort(double price,int stop)
{
if(stop==0)
return(0);
else
return(price+(stop*Point));
}
double TakeLong(double price,int take)
{
if(take==0)
return(0);
else
return(price+(take*Point));
}
double TakeShort(double price,int take)
{
if(take==0)
return(0);
else
return(price-(take*Point));
}
Comments