HI_Line_E_RSI_MA
Download scriptThis automated trading script is designed to manage market entries and exits by analyzing price momentum and long-term trends. It operates as an "Expert Advisor," meaning it monitors the live market to make decisions based on specific mathematical conditions without requiring constant manual intervention.
Core Trading Logic
The system relies on three primary components to determine when to enter or exit a trade:
1. Momentum Analysis (RSI) The script uses a Relative Strength Index (RSI) to gauge whether an asset is being overbought or oversold. It looks at the change between the current momentum and the previous candle's momentum.
- It seeks "crossover" points where the momentum shifts direction significantly, ensuring that it doesn't just enter a trade because the market is extreme, but rather because the trend is starting to turn.
2. Trend Filtering (Moving Averages) To ensure the system isn't trading against the major market direction, it uses two different Moving Averages calculated on a daily timeframe.
- By comparing these averages, the script identifies the "health" of the overall trend. This acts as a secondary filter to verify that the short-term momentum signals align with the long-term price movement.
3. Execution Rules The logic is divided into clear "Open" and "Close" triggers:
- Buy Signals: The script looks for a situation where the RSI momentum is rising above a specific threshold, combined with a confirmed upward trend in the daily moving averages.
- Sell Signals: Conversely, it looks for a downward shift in RSI momentum while the long-term moving averages indicate a bearish environment.
- Exit Strategy: The script also monitors "Close" conditions. If the RSI reaches opposite extremes (indicating the move might be exhausted), it will trigger an automatic exit to secure the position before the trend potentially reverses.
Operational Safeguards
To ensure smooth performance, the script includes several professional management features:
- Single Trade Prevention: The logic is built to prevent "over-trading." It checks for existing positions on the current candle to ensure it doesn't open multiple redundant trades for the same signal.
- Automated Profit Targets: Every trade opened by the script includes a pre-defined profit target (Take Profit). This ensures that once a certain price level is reached, the trade closes automatically.
- Spread Awareness: The system accounts for the "Spread" (the difference between the buy and sell price provided by the broker), ensuring that trades are executed at realistic prices to protect the account from unnecessary costs.
In summary, this script acts as an automated filter: it waits for a specific moment where short-term momentum aligns with long-term trends, executes the trade with a predefined profit goal, and monitors the market to exit the position when the momentum begins to fade.
//+------------------------------------------------------------------+
//| Helen_Iv_Line_Experts_RSI_MA.mq4 |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, Helen_Iv"
#property link "http://helenclubfinance.com"
//---- input parameters
extern double Lots=0.1;
extern int p=23;
extern int ma2=9;
extern int ma3=19;
extern int TakeProfit = 1000;
int BuyTicket,SellTicket;
int StLevel2;
double slts,tpts;
datetime prevtimecalc=0;
int OpenBuy=0, OpenSell=0, CloseBuy=0, CloseSell=0;
int Spread;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
double DeltaTakeStopMin, DeltaFreezeLevel;
Spread = MarketInfo (Symbol(), MODE_SPREAD);
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
CalcCloseOpen();
int total=OrdersTotal();
int NP=0;
for(int x=total; x>=0; x--)
{
if (!OrderSelect(x, SELECT_BY_POS, MODE_TRADES))continue;
// Çàêðûòèå îòêðûòûõ ïîçèöèé
if (OrderType()==OP_BUY && CloseBuy==1){Close_Order(); continue;}
if (OrderType()==OP_SELL && CloseSell==1) {Close_Order(); continue;}
// èñêëþ÷àåì îòêðûòèå íåñêîëüêèõ îðäåðîâ íà ñèãíàëå îäíîì áàðà
if (OrderType()==OP_BUY && OrderOpenPrice()>=Time[0]) NP++;
if (OrderType()==OP_SELL && OrderOpenPrice()>=Time[0]) NP++;
}
// åñëè åùå åñòü ñèãíàë íà îòêðûòèå, òî áóäåì îòêðûâàòüñÿ
if (NP == 0) Open_Buy_Sell ();
return(0);
}
//+------------------------------------------------------------------+
// âû÷èñëèì óñëîâèÿ íà çàêðûòèå è îòêðûòèå ïîçèöèé
//+------------------------------------------------------------------+
void CalcCloseOpen()
{
//Comment("000Buy - ", OpenBuy, " 000Sell - ", OpenSell);
if (prevtimecalc==Time[0])return; else prevtimecalc=Time[0];
OpenBuy=0; OpenSell=0; CloseBuy=0; CloseSell=0;
double t1=iRSI(NULL,0,p,PRICE_CLOSE,1);
double t11=iRSI(NULL,0,p,PRICE_CLOSE,2);
double t2=iMA(NULL,PERIOD_D1,ma2,0,MODE_EMA,0,1);
double t3=iMA(NULL,PERIOD_D1,ma3,0,MODE_EMA,0,1);
if (t1>30.0&&t11<30.0&&t1>t11&&t1>30.0&&t2>t3) {OpenBuy=1;}
if (t1<70.0&&t11>70.0&&t1<t11&&t1<70.0) {CloseBuy=1;}
if (t1<70.0&&t11>70.0&&t1<t11&&t1<70.0&&t2<t3) {OpenSell=1;}
if (t1>30.0&&t11<30.0&&t1>t11&&t1>30.0) {CloseSell=1;}
//Comment("Buy - ", OpenBuy, " Sell - ", OpenSell);
return;
}
//+------------------------------------------------------------------
//| Çàêðûòèå ïîçèöèé
//+------------------------------------------------------------------
void Close_Order ()
{
if(OrderType()==OP_BUY)
{
//Îáåñïå÷èâàåì ïàóçó ìåæäó îïåðàöèÿìè
//Pause();
OrderClose(OrderTicket(),OrderLots(),Bid,Spread,Yellow);
//TradeIsNotBusy();
}
if(OrderType()==OP_SELL)
{
//Îáåñïå÷èâàåì ïàóçó ìåæäó îïåðàöèÿìè
//Pause();
OrderClose(OrderTicket(),OrderLots(),Ask,Spread,Yellow);
//TradeIsNotBusy();
}
}
//+------------------------------------------------------------------
//| Îòêðûòèå ïîçèöèé ðûíî÷íûìè îðäåðàìè
//+------------------------------------------------------------------
void Open_Buy_Sell ()
{
// BUY
// Open=Ask, Take =Open+Profit, Stop = Open-Spred-stop
if(OpenBuy !=0)
{
// îáíîâëÿåì ðûíî÷íóþ èíôîðìàöèþ
if (!IsTesting() && !IsOptimization( )) RefreshRates();
OrderSend(Symbol(),OP_BUY,Lots,Ask,Spread,0,Ask + TakeProfit*Point,0,0,0,Blue);
if(GetLastError()==0){OpenBuy=0; return;}
}
// SELL
// Open = Bid, Take =Open-Profit, Stop=Open+Spred+Stop
if( OpenSell !=0)
{
if (!IsTesting() && !IsOptimization( )) {RefreshRates();}
OrderSend(Symbol(),OP_SELL,Lots,Bid,Spread,0,Bid - TakeProfit*Point,0,0,0,Red); // èñïîëíÿåì
if(GetLastError()==0) {OpenSell=0; return;}
}
return(0);
}
Profitability Reports
Expected payoff
Average expected payoff across the available backtests for each symbol.
Comments