Okay, here's the explanation of what the code does, designed for someone who doesn't know programming:
This script is designed to automatically trade on the Forex market using the MetaTrader platform. It's like a robot that tries to make profitable trades for you based on a set of rules you define.
Here's a breakdown of what it does:
-
Setup and Configuration:
- The script starts by setting up various parameters that control how it will trade. These include things like:
- A unique "Magic Number" (to identify trades made by this specific script).
- The amount of money to risk on each trade (Lot size).
- Maximum Open Trades to have at one time
- Settings related to something called "Bollinger Bands" (a common trading indicator).
- Settings related to profit protection parameters.
- If swap is to be consider for profits or not
- The script starts by setting up various parameters that control how it will trade. These include things like:
-
Checking Existing Trades:
- The script constantly monitors any trades it has already opened. It keeps track of:
- How many trades are currently open.
- The total profit or loss from those trades.
- The profit is added or substracted from the swap.
- The script constantly monitors any trades it has already opened. It keeps track of:
-
Closing Trades:
- The script has the ability to close all open trades under certain conditions:
- If you manually set a "Close All" switch to "true".
- If the total profit from all open trades reaches a certain target you've set (RecycleProfit). The script then closes all open positions. This allows the "robot" to take the profit and start trading again with a clean slate.
- The script has the ability to close all open trades under certain conditions:
-
Deciding When to Open a New Trade (Buy Only):
- The script uses a set of rules to determine when to open a new "buy" trade. Several conditions must be met before a trade is placed:
- The number of open trades is below a maximum limit.
- A technical indicator, called Bollinger Bands, provides a "buy" signal.
- There is no recycle profit and the account can accept new trades.
- Secure Orders are enabled and above minimum pips
- AutoTrade is enabled
- The script uses a set of rules to determine when to open a new "buy" trade. Several conditions must be met before a trade is placed:
-
Opening a Trade:
- If all the conditions for opening a trade are met, the script sends a command to the MetaTrader platform to open a "buy" order for a specific amount (Lot Size).
-
Managing Stop Losses (Profit Protection):
- The script includes a feature to automatically adjust the "Stop Loss" on open trades. A Stop Loss is like an emergency exit: it's an order to automatically close a trade if the price moves against you by a certain amount. This helps to limit potential losses. The script can:
- Move the Stop Loss to breakeven (the original entry price) to guarantee no loss.
- Move the Stop Loss in steps as the price moves in a favorable direction (to lock in profits).
- The script includes a feature to automatically adjust the "Stop Loss" on open trades. A Stop Loss is like an emergency exit: it's an order to automatically close a trade if the price moves against you by a certain amount. This helps to limit potential losses. The script can:
-
Calculating Lot Size (Money Management):
- The script can automatically calculate the appropriate trade size (Lot Size) based on the size of your account and the amount of risk you're willing to take on each trade. This helps to prevent you from risking too much on any single trade.
-
Displaying Information:
- The script displays information on the chart, such as the number of open trades, total profit/loss, etc.
In simple terms, this script is an automated trading system that follows a specific strategy. It uses technical indicators to identify potential trading opportunities, manages risk by adjusting stop losses, and can automatically close trades to lock in profits or limit losses. You control its behavior by adjusting various settings to match your trading preferences.
/*-----------------------------+
| |
| Shared by www.Aptrafx.com |
| |
+------------------------------*/
//+------------------------------------------------------------------+
//| Samuray.mq4 |
//| Kokas |
//| http://www.forexforums.org |
//+------------------------------------------------------------------+
#property copyright "Kokas"
#property link "http://www.forexforums.org"
//---- input parameters
extern string ExpertName = "Samuray GBPJPY";
//extern double MarginTrade = 1;
extern bool AutoTrade = true;
extern int MaxTrades = 50;
extern double RecycleProfit = 300;
extern bool UseSwap = false;
extern double InitialStop = 0;
extern double BreakEven = 10; // Profit Lock in pips
extern double StepSize = 10;
extern double MinDistance = 15;
extern bool SecureOrders = true; // If set to true only open orders when in profi
extern double MinPip = 10; // Min Profit Pips to open another order
extern bool CloseAll = false; // Set true to close all current Orders
extern int Magic = 5665; // Magic Number of the EA
extern double Lots = 0.01; // Lot size when not using MM
extern bool MoneyManagement = false;
extern bool AccountIsMicro = false;
extern double MaxLotSize = 100;
extern double Risk = 10;
extern string Param2 = "Bollinger Band Settings";
extern double Bollinger_Period = 20;
extern double Bollinger_TF = 60;
extern double Bollinger_Dev = 2;
int k, digit=0;
bool BE = false;
double AccountSize,totalPips,totalProfits;
double pBid, pAsk, pp,valueswap;
bool signal1 = true, signal2 =true, signal3 = true , signal4 = true, SecureSignal = true;
string comment = "";
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
void ChartComment()
{
string sComment = "";
string sp = "****************************\n";
string NL = "\n";
sComment = sp;
sComment = sComment + "Open Positions = " + ScanOpenTrades() + NL;
sComment = sComment + "Current Profit(Pip)= " + totalPips + NL;
sComment = sComment + "SWAP Value (Pip) = " + DoubleToStr(valueswap,2) + NL;
if(UseSwap){
sComment = sComment + "SWAP Enabled" + NL;
} else {
sComment = sComment + "SWAP Disabled" + NL;
}
sComment = NL + sComment + "Net Value (Pip) = " + DoubleToStr(totalPips+valueswap,2) + NL;
sComment = sComment + NL + sp;
Comment(sComment);
}
int ScanOpenTrades()
{
int total = OrdersTotal();
int numords = 0;
for(int cnt=0; cnt<=total-1; cnt++)
{
OrderSelect(cnt, SELECT_BY_POS);
if(OrderType()<=OP_SELL)
{
if(Magic > 0) if(OrderMagicNumber() == Magic) numords++;
if(Magic == 0) numords++;
}
}
return(numords);
}
// Closing of Open Orders
void OpenOrdClose()
{
int total=OrdersTotal();
for (int cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS);
int mode=OrderType();
bool res = false;
bool condition = false;
if ( Magic>0 && OrderMagicNumber()==Magic ) condition = true;
else if ( Magic==0 ) condition = true;
if (condition && ( mode==OP_BUY || mode==OP_SELL ))
{
// - BUY Orders
if(mode==OP_BUY)
{
res = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),3,Yellow);
if( !res )
{
Print(" BUY: OrderClose failed with error #",GetLastError());
Print(" Ticket=",OrderTicket());
Sleep(3000);
}
break;
}
else
// - SELL Orders
if( mode == OP_SELL)
{
res = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),3,White);
if( !res )
{
Print(" SELL: OrderClose failed with error #",GetLastError());
Print(" Ticket=",OrderTicket());
Sleep(3000);
}
break;
}
}
}
}
void TotalProfit()
{
int total=OrdersTotal();
totalPips = 0;
totalProfits = 0;
for (int cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS);
int mode=OrderType();
bool condition = false;
if ( Magic>0 && OrderMagicNumber()==Magic ) condition = true;
else if ( Magic==0 ) condition = true;
if (condition)
{
switch (mode)
{
case OP_BUY:
totalPips += MathRound((MarketInfo(OrderSymbol(),MODE_BID)-OrderOpenPrice())/MarketInfo(OrderSymbol(),MODE_POINT));
//totalPips += MathRound((Bid-OrderOpenPrice())/Point);
totalProfits += OrderProfit();
break;
case OP_SELL:
totalPips += MathRound((OrderOpenPrice()-MarketInfo(OrderSymbol(),MODE_ASK))/MarketInfo(OrderSymbol(),MODE_POINT));
//totalPips += MathRound((OrderOpenPrice()-Ask)/Point);
totalProfits += OrderProfit();
break;
}
}
if (UseSwap) {
SwapProfit();
totalProfits = totalProfits + valueswap;
}
}
}
// MoneyManagement with Account Leverage Protection
double LotSize()
{
double lotMM = MathCeil(AccountFreeMargin() * Risk / 1000) / AccountLeverage() / 2;
if(AccountIsMicro==false) //normal account
{
if(lotMM < 0.1) lotMM = 0.1;
if((lotMM >= 0.1) && (lotMM < 0.2)) lotMM = 0.2;
if((lotMM >= 0.2) && (lotMM < 0.3)) lotMM = 0.3;
if((lotMM >= 0.3) && (lotMM < 0.4)) lotMM = 0.4;
if((lotMM >= 0.4) && (lotMM < 1)) lotMM = 0.5;
if(lotMM >= 1.0) lotMM = MathCeil(lotMM);
if(lotMM >= MaxLotSize) lotMM = MaxLotSize;
}
else //micro account
{
if(lotMM < 0.01) lotMM = 0.01;
if((lotMM >= 0.01) && (lotMM < 0.02)) lotMM = 0.02;
if((lotMM >= 0.02) && (lotMM < 0.03)) lotMM = 0.03;
if((lotMM >= 0.03) && (lotMM < 0.04)) lotMM = 0.04;
if((lotMM >= 0.05) && (lotMM < 0.06)) lotMM = 0.05;
if((lotMM >= 0.06) && (lotMM < 0.07)) lotMM = 0.06;
if((lotMM >= 0.07) && (lotMM < 0.08)) lotMM = 0.08;
if((lotMM >= 0.08) && (lotMM < 0.09)) lotMM = 0.09;
if((lotMM >= 0.09) && (lotMM < 0.10)) lotMM = 0.1;
if((lotMM >= 0.1) && (lotMM < 0.2)) lotMM = 0.2;
if((lotMM >= 0.2) && (lotMM < 0.3)) lotMM = 0.3;
if((lotMM >= 0.3) && (lotMM < 0.4)) lotMM = 0.4;
if((lotMM >= 0.4) && (lotMM < 1)) lotMM = 0.5;
if(lotMM >= 1.0) lotMM = MathCeil(lotMM);
if(lotMM >= MaxLotSize) lotMM = MaxLotSize;
}
if(AccountIsMicro) {
AccountSize=2;
} else {
AccountSize=1;
}
lotMM = NormalizeDouble(lotMM,AccountSize);
return (lotMM);
}
void StepStops()
{
double BuyStop, SellStop;
int total=OrdersTotal();
for (int cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS);
int mode=OrderType();
if ( OrderSymbol()==Symbol() && OrderMagicNumber()==Magic )
{
if ( mode==OP_BUY )
{
BuyStop = OrderStopLoss();
if ( Bid-OrderOpenPrice()>0 || OrderStopLoss()==0)
{
if ( Bid-OrderOpenPrice()>=Point*BreakEven && !BE) {BuyStop = OrderOpenPrice();BE = true;}
if (OrderStopLoss()==0) {BuyStop = OrderOpenPrice() - InitialStop * Point; k=1; BE = false;}
if ( Bid-OrderOpenPrice()>= k*StepSize*Point)
{
BuyStop = OrderStopLoss()+ StepSize*Point;
if (Bid - BuyStop >= MinDistance*Point)
{ BuyStop = BuyStop; k=k+1;}
else
BuyStop = OrderStopLoss();
}
//Print( " k=",k ," del=", k*StepSize*Point, " BuyStop=", BuyStop," digit=", digit);
OrderModify(OrderTicket(),OrderOpenPrice(),
NormalizeDouble(BuyStop, digit),
OrderTakeProfit(),0,LightGreen);
return(0);
}
}
if ( mode==OP_SELL )
{
SellStop = OrderStopLoss();
if ( OrderOpenPrice()-Ask>0 || OrderStopLoss()==0)
{
if ( OrderOpenPrice()-Ask>=Point*BreakEven && !BE) {SellStop = OrderOpenPrice(); BE = true;}
if ( OrderStopLoss()==0 ) { SellStop = OrderOpenPrice() + InitialStop * Point; k=1; BE = false;}
if ( OrderOpenPrice()-Ask>=k*StepSize*Point)
{
SellStop = OrderStopLoss() - StepSize*Point;
if (SellStop - Ask >= MinDistance*Point)
{ SellStop = SellStop; k=k+1;}
else
SellStop = OrderStopLoss();
}
//Print( " k=",k," del=", k*StepSize*Point, " SellStop=",SellStop," digit=", digit);
OrderModify(OrderTicket(),OrderOpenPrice(),
NormalizeDouble(SellStop, digit),
OrderTakeProfit(),0,Yellow);
return(0);
}
}
}
}
}
void SwapProfit()
{
int total=OrdersTotal();
valueswap = 0;
for (int cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS);
int mode=OrderType();
bool condition = false;
if ( Magic>0 && OrderMagicNumber()==Magic ) condition = true;
else if ( Magic==0 ) condition = true;
if (condition && OrderSwap()!=0)
{
valueswap = valueswap + OrderSwap()/PipCost(OrderSymbol()); // ERROOOOOOOOOOOOOOOOOOOOOOOOOOO
}
}
}
//+--------- --------- --------- --------- --------- --------- ----+
//+ Calculate cost in USD of 1pip of given symbol
//+--------- --------- --------- --------- --------- --------- ----+
double PipCost (string TradeSymbol) {
double Base, Cost;
string TS_13, TS_46, TS_4L;
TS_13 = StringSubstr (TradeSymbol, 0, 3);
TS_46 = StringSubstr (TradeSymbol, 3, 3);
TS_4L = StringSubstr (TradeSymbol, 3, StringLen(TradeSymbol)-3);
Base = MarketInfo (TradeSymbol, MODE_LOTSIZE) * MarketInfo (TradeSymbol,MODE_POINT);
if ( TS_46 == "USD" )
Cost = Base;
else if ( TS_13 == "USD" )
Cost = Base / MarketInfo (TradeSymbol, MODE_BID);
else if ( PairExists ("USD"+TS_4L) )
Cost = Base / MarketInfo ("USD"+TS_4L, MODE_BID);
else
Cost = Base * MarketInfo (TS_46+"USD" , MODE_BID);
return(Cost) ;
}
//+--------- --------- --------- --------- --------- --------- ----+
//+ Returns true if given symbol exists
//+--------- --------- --------- --------- --------- --------- ----+
bool PairExists (string TradeSymbol) {
return ( MarketInfo (TradeSymbol, MODE_LOTSIZE) > 0 );
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
digit = MarketInfo(Symbol(),MODE_DIGITS);
double BandsLower = iBands(Symbol(),Bollinger_TF,Bollinger_Period,Bollinger_Dev,0,0,2,0); // Lower Bollinger
double BandsUpper = iBands(Symbol(),Bollinger_TF,Bollinger_Period,Bollinger_Dev,0,0,1,0); // Upper Bollinger
double Bands = (BandsLower + BandsUpper) / 2; // Middle Bollinger
signal1 = false;
signal2 = false;
signal3 = false;
signal4 = true;
if (CloseAll) {
OpenOrdClose();
signal4 = false;
}
TotalProfit();
if (totalPips > RecycleProfit) {
OpenOrdClose();
signal4 = false;
}
if (ScanOpenTrades() < MaxTrades) signal1 = true;
if (ScanOpenTrades() == 0){
signal2 = true;
} else {
signal2 = true; // if (AccountFreeMargin() > (AccountBalance()+AccountBalance()*MarginTrade/100)) signal2 = true; /// ERROOOOOOOOOOOOOOOOOOOOOOOOOOO
}
if (MarketInfo(Symbol(),MODE_ASK) < Bands) signal3 = true;
if (MoneyManagement) Lots = LotSize();
if (SecureOrders){
if (totalPips > MinPip || ScanOpenTrades() == 0){
SecureSignal = true;
} else {
SecureSignal = false;
}
} else {
SecureSignal = true;
}
comment = "O:" + DoubleToStr(ScanOpenTrades(),0);
//+------------------------------------------------------------------+
if (signal1 && signal2 && signal3 && signal4 && AutoTrade && SecureSignal){
// signal1 - Control the number of open orders
// signal2 - Check Margin (INACTIVE)
// signal3 - Bollinger Bands Filter
// signal4 - Control Recycle Profits
OrderSend(Symbol(),OP_BUY,Lots,MarketInfo(Symbol(),MODE_ASK),3,0,0,comment,Magic,0,Blue);
}
if (BreakEven>0 || InitialStop>0 || StepSize>0) StepStops();
ChartComment();
return(0);
}
//+------------------------------------------------------------------+
Comments