jfk
Download scriptThis automated trading script is designed to manage and execute trades based on moving average trends. It uses a systematic approach to identify market momentum and automatically handles trade entries, exits, and risk management.
Core Trading Logic
The script functions by monitoring two different Moving Average (MA) indicators on a 5-minute chart. By comparing a fast-moving average against a slower-moving average, the system determines the current direction of the market:
- Buy Signals: When the fast moving average crosses above the slow moving average, the system identifies an upward trend and prepares to enter a "Long" position.
- Sell Signals: When the fast moving average falls below the slow moving average, the system identifies a downward trend and prepares to enter a "Short" position.
Entry Mechanism
Unlike scripts that enter a trade immediately upon a signal, this tool utilizes Pending Orders. Once a trend is identified:
- It places a Buy Stop order at a specific distance (defined by pips) above the current price for upward trends.
- It places a Sell Stop order at a specific distance below the current price for downward trends. This ensures that a trade is only triggered if the price continues to move in the intended direction, providing an extra layer of confirmation.
Trade Management and Protection
The script includes several automated features to manage active trades:
- Position Tracking: It constantly monitors your open positions and pending orders to ensure it doesn't over-leverage by opening unnecessary duplicate trades.
- Stop Loss & Take Profit: Every trade is automatically assigned a safety net (Stop Loss) to limit potential losses and a target (Take Profit) to secure gains.
- Trailing Stop: This dynamic feature adjusts the Stop Loss of an active trade as the price moves in your favor. If the market moves significantly past your entry point, the script "trails" the stop loss behind the current price, locking in profits while allowing the trade room to continue running.
Execution Modes
The logic is flexible regarding how it processes data:
- Tick Mode: The script can be set to scan for changes every single time the market price moves (tick by tick).
- Bar Mode: Alternatively, it can be configured to check for new signals only when a new candle (bar) forms on the chart.
/*-----------------------------+
| |
| Shared by www.Aptrafx.com |
| |
+------------------------------*/
//+------------------------------------------------------------------+
//| This MQL is generated by Expert Advisor Builder |
//| http://sufx.core.t3-ism.net/ExpertAdvisorBuilder/ |
//| |
//| In no event will author be liable for any damages whatsoever. |
//| Use at your own risk. |
//| |
//| Please do not remove this header. |
//+------------------------------------------------------------------+
#property copyright "Expert Advisor Builder"
#property link "http://sufx.core.t3-ism.net/ExpertAdvisorBuilder/"
extern int MagicNumber = 0;
extern bool EachTickMode = True;
extern double Lots = 0.1;
extern int Slippage = 3;
extern bool StopLossMode = True;
extern int StopLoss = 30;
extern bool TakeProfitMode = True;
extern int TakeProfit = 100;
extern bool TrailingStopMode = True;
extern int TrailingStop = 30;
extern int PipsForEntry = 10;
extern bool Short = True;
extern bool Long = True;
#define SIGNAL_NONE 0
#define SIGNAL_BUY 1
#define SIGNAL_SELL 2
#define SIGNAL_CLOSEBUY 3
#define SIGNAL_CLOSESELL 4
int BarCount;
int Current;
int b,s;
bool TickCheck = False;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init() {
BarCount = Bars;
if (EachTickMode) Current = 0; else Current = 1;
return(0); }
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit() { return(0);}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start() {
PosCounter();
int Order = SIGNAL_NONE;
int Total, Ticket;
double StopLossLevel, TakeProfitLevel, Opentrades, PosCounter;
if (EachTickMode && Bars != BarCount) TickCheck = False;
Total = OrdersTotal();
Order = SIGNAL_NONE;
//+------------------------------------------------------------------+
//| Variable Begin |
//+------------------------------------------------------------------+
double Buy1_1 = iMA(NULL, PERIOD_M5, 10, 0, MODE_SMA, PRICE_CLOSE, Current + 0);
double Buy1_2 = iMA(NULL, PERIOD_M5, 25, 0, MODE_SMA, PRICE_CLOSE, Current + 0);
double Sell1_1 = iMA(NULL, PERIOD_M5, 10, 0, MODE_SMA, PRICE_CLOSE, Current + 0);
double Sell1_2 = iMA(NULL, PERIOD_M5, 25, 0, MODE_SMA, PRICE_CLOSE, Current + 0);
//+------------------------------------------------------------------+
//| Variable End |
//+------------------------------------------------------------------+
//Check position
for (int i = 0; i < Total; i ++) {
OrderSelect(0, SELECT_BY_POS, MODE_TRADES);
if(OrderType() <= OP_SELL && OrderSymbol() == Symbol()) {
if(OrderType() == OP_BUY) {
//Close
//+------------------------------------------------------------------+
//| Signal Begin(Exit Buy) |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Signal End(Exit Buy) |
//+------------------------------------------------------------------+
if (Order == SIGNAL_CLOSEBUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {
OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, MediumSeaGreen);
if (!EachTickMode) BarCount = Bars;
break;
}
//Trailing stop
if(TrailingStopMode && TrailingStop > 0) {
if(Bid - OrderOpenPrice() > Point * TrailingStop) {
if(OrderStopLoss() < Bid - Point * TrailingStop) {
OrderModify(OrderTicket(), OrderOpenPrice(), Bid - Point * TrailingStop, OrderTakeProfit(), 0, MediumSeaGreen);
if (!EachTickMode) BarCount = Bars;
break;
}
}
}
} else {
//Close
//+------------------------------------------------------------------+
//| Signal Begin(Exit Sell) |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Signal End(Exit Sell) |
//+------------------------------------------------------------------+
if (Order == SIGNAL_CLOSESELL && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {
OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, DarkOrange);
if (!EachTickMode) BarCount = Bars;
break;
}
//Trailing stop
if(TrailingStopMode && TrailingStop > 0) {
if((OrderOpenPrice() - Ask) > (Point * TrailingStop)) {
if((OrderStopLoss() > (Ask + Point * TrailingStop)) || (OrderStopLoss() == 0)) {
OrderModify(OrderTicket(), OrderOpenPrice(), Ask + Point * TrailingStop, OrderTakeProfit(), 0, DarkOrange);
if (!EachTickMode) BarCount = Bars;
break;
}
}
}
}
}
}
//used this code from another EA but changed it to suit me
if(Buy1_1 > Buy1_2 && b==0 && Long==True) {
OrderSend(Symbol(), OP_BUYSTOP, Lots, Ask+PipsForEntry*Point, Slippage, StopLossLevel, TakeProfitLevel, "Buy(#" + MagicNumber + ")", MagicNumber, 0, DodgerBlue);
return(0); }
if(Sell1_1 < Sell1_2 && s==0 && Short==True) {
OrderSend(Symbol(), OP_SELLSTOP, Lots, Bid-PipsForEntry*Point, Slippage, StopLossLevel, TakeProfitLevel, "Sell(#" + MagicNumber + ")", MagicNumber, 0, DeepPink);
return(0); }
//END OF MAIN SECTION
return(0);}
//+------------------------------------------------------------------+
void PosCounter() {
b=0;s=0;
for(int cnt=0;cnt<=OrdersTotal();cnt++) {
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol()==Symbol() && OrderMagicNumber()) {
if(OrderType() == OP_SELL) s++;
if(OrderType() == OP_SELLSTOP) s++;
if(OrderType() == OP_BUY) b++;
if(OrderType() == OP_BUYSTOP) b++;}}}
Profitability Reports
Expected payoff
Average expected payoff across the available backtests for each symbol.
Comments