Description of the Logic in Non-Technical Terms
This script is designed to automate trading decisions using a system based on moving averages and other indicators. Here's an overview of how it works:
Purpose
The main goal of this script is to execute buy or sell orders automatically based on certain market conditions. It uses technical analysis tools like moving averages, the average true range (ATR), and the Parabolic SAR indicator.
Key Components
-
Moving Averages:
- Simple Moving Average (SMA): This is a basic tool that calculates the average price of an asset over a specific number of past periods.
- Exponential Moving Average (EMA): Similar to SMA but gives more weight to recent prices, making it more responsive to new information.
-
Average True Range (ATR):
- ATR measures market volatility by calculating the average range between high and low prices over a set period.
-
Parabolic SAR:
- This indicator is used to determine the direction of an asset's momentum and potential reversal points.
Trading Logic
-
Initial Checks:
- The script first ensures there are enough data points (bars) for analysis.
- It checks if the take-profit value is set above a minimum threshold.
-
Money Management:
- Depending on user settings, the script calculates how much capital to risk per trade. This can be a fixed amount or a percentage of the account balance.
-
Order Conditions:
- Sell Order:
- Triggered when the SMA is below a certain level compared to the EMA, indicating a potential downward trend.
- The Parabolic SAR must also align with this trend.
- Buy Order:
- Triggered under opposite conditions, where the SMA is above the EMA, suggesting an upward trend.
- Sell Order:
-
Order Execution:
- Once conditions are met, the script places either a buy or sell order with predefined parameters for stop loss and take profit.
-
Order Management:
- The script continuously monitors open orders.
- It can close orders if market conditions change unfavorably.
- Trailing stops are used to protect profits by adjusting stop-loss levels as the price moves favorably.
Visual Indicators
- Orders are visually distinguished using colors: Lime for buy orders and HotPink for sell orders. This helps in quickly identifying the type of order on a trading platform.
In summary, this script automates trading decisions based on technical indicators to potentially capitalize on market trends while managing risk through predefined parameters.
Orders Execution
Indicators Used
0
Views
0
Downloads
0
Favorites
2EMA_systemv032
//+------------------------------------------------------------------+
//| 2EMA system-v03.mq4 |
//| Gerry Sutton |
//| |
//+------------------------------------------------------------------+
#property copyright "Greybeard_xlx - Conversion of MQL-II"
#property link "http://groups.yahoo.com/group/MetaTrader_Experts_and_Indicators/"
//+------------------------------------------------------------------+
//| External Variables |
//+------------------------------------------------------------------+
extern double mm = -1;
extern double lp = 300;
extern double sp = 30;
extern double slip = 5;
extern double Risk = 40;
extern double Lots = 1.0;
extern double TakeProfit = 360;
extern double Stoploss = 50;
extern double TrailingStop = 15;
double Points;
//----
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int init ()
{
Points = MarketInfo (Symbol(), MODE_POINT);
//----
return(0);
}
//+------------------------------------------------------------------+
if(Bars<301)
{
Print("Not enough Bars");
return(0);
}
if(TakeProfit<10)
{
Print("TakeProfit less than 10");
return(0);
}
//+------------------------------------------------------------------+
//| Setting internal variables for quick access to data |
//+------------------------------------------------------------------+
int start()
{
double b=0;
double balance=0;
double Ilo=0;
balance=AccountBalance();
b=(5*Points+iATR(NULL,0,4,1)*5.5);
//+------------------------------------------------------------------+
//| Money Management mm=0(lots) mm=-1(Mini) mm=1(full compounding) |
//+------------------------------------------------------------------+
if (mm < 0) {
Ilo = MathCeil(balance*Risk/10000)/10;
if (Ilo > 100) {
Ilo = 100;
}
} else {
Ilo = Lots;
};
if (mm > 0)
{
Ilo = MathCeil(balance*Risk/10000)/10;
if (Ilo > 1)
{
Ilo = MathCeil(Ilo);
}
if (Ilo < 1)
{
Ilo = 1;
}
if (Ilo > 100)
{
Ilo = 100;
}
};
//----
Comment(" Account : ",AccountNumber(),"---",AccountName(),
"\n"," StopLoss : ",b,
"\n"," Lots : ",Ilo,);
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
if(OrdersTotal()<1)
{
if(AccountFreeMargin()<(100*Lots))
{
Print("We have no money");
return(0);
}
if((iMA(NULL,0,lp,0,MODE_SMA,PRICE_CLOSE,3)<
iMA(NULL,0,sp,0,MODE_EMA,PRICE_CLOSE,3)*0.998 &&
iMA(NULL,0,lp,0,MODE_SMA,PRICE_CLOSE,0)>
iMA(NULL,0,sp,0,MODE_EMA,PRICE_CLOSE,0)*0.998)&&
iSAR(NULL,0,0.02,0.2,6)<Open[6] &&
iSAR(NULL,0,0.02,0.2,0)>Open[0])
{
OrderSend(
OP_SELL,
Ilo,
Bid,
slip,
Bid+Stoploss*Points,
Bid-TakeProfit*Points,
0,0,
HotPink);
if(GetLastError()==0)Print("Order opened : ",OrderOpenPrice());
return(0);
}
if((iMA(NULL,0,lp,0,MODE_SMA,PRICE_CLOSE,3)>
iMA(NULL,0,sp,0,MODE_EMA,PRICE_CLOSE,3)*1.002 &&
iMA(NULL,0,lp,0,MODE_SMA,PRICE_CLOSE,0)<
iMA(NULL,0,sp,0,MODE_EMA,PRICE_CLOSE,0)*1.002) &&
iSAR(NULL,0,0.02,0.2,6)>Open[6] &&
iSAR(NULL,0,0.02,0.2,0)<Open[0])
{
OrderSend(
OP_BUY,
Ilo,
Ask,
slip,
Ask-Stoploss*Points,
Ask+TakeProfit*Points,
0,0,
Lime);
if(GetLastError()==0)Print("Order opened : ",OrderOpenPrice());
return(0);
}
return(0);
}
};
int cnt=0, total;
total=OrdersTotal();
for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL &&
OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUY)
{
if(OrderOpenPrice()>OrderStopLoss()&&
(Bid-OrderOpenPrice()>(Bid-OrderOpenPrice()*0.004)) &&
iMA(NULL,0,lp,0,MODE_SMA,PRICE_CLOSE,3)<
iMA(NULL,0,sp,0,MODE_EMA,PRICE_CLOSE,3)*0.998 &&
iMA(NULL,0,lp,0,MODE_SMA,PRICE_CLOSE,0)>
iMA(NULL,0,sp,0,MODE_EMA,PRICE_CLOSE,0)*0.9978)
{
OrderClose(
OrderTicket(),
OrderLots(),
Bid,
slip,
Red);
return(0);
};
if(TrailingStop>0)
{
if(Bid-OrderOpenPrice()>b)
{
if(OrderStopLoss()<Bid-b)
{
OrderModify(
OrderTicket(),
OrderOpenPrice(),
Bid-b,OrderTakeProfit(),
0,
LimeGreen);
return(0);
}
}
}
}
else
{
if(OrderOpenPrice()<OrderStopLoss()&&
(OrderOpenPrice()-Ask>(OrderOpenPrice()*0.004)) &&
iMA(NULL,0,lp,0,MODE_SMA,PRICE_CLOSE,3)>
iMA(NULL,0,sp,0,MODE_EMA,PRICE_CLOSE,3)*0.998 &&
iMA(NULL,0,lp,0,MODE_SMA,PRICE_CLOSE,0)<
iMA(NULL,0,sp,0,MODE_EMA,PRICE_CLOSE,0)*0.9978)
{
OrderClose(
OrderTicket(),
OrderLots(),
Ask,
slip,
Red);
return(0);
}
if(TrailingStop>0)
{
if((OrderOpenPrice()-Ask)>(b))
{
if(OrderStopLoss()==0.0 ||
OrderStopLoss()>(Ask+b))
{
OrderModify(
OrderTicket(),
OrderOpenPrice(),
Ask+b,OrderTakeProfit(),
0,
HotPink);
return(0);
}
}
}
}
}
}
return(0);
// }
Comments
Markdown Formatting Guide
# H1
## H2
### H3
**bold text**
*italicized text*
[title](https://www.example.com)

`code`
```
code block
```
> blockquote
- Item 1
- Item 2
1. First item
2. Second item
---