Trading System Logic Overview: MA.S.R_Trading
This document provides a non-technical explanation of the logic contained within the MA.S.R_Trading.mq4 script. The script is designed to automate trading by identifying momentum shifts in price trends and managing active positions based on historical price extremes.
1. Market Analysis Logic
The script monitors the market using a Moving Average (MA), which is a calculation that smooths out price data to identify the general direction of the trend. It specifically looks at the relationship between three consecutive points of this moving average to determine "peaks" and "valleys":
- Bearish Reversal Detection: The script checks if the current moving average is lower than the previous one, while the previous one was higher than the one before it. This identifies a downward "hook" or peak in the trend.
- Bullish Reversal Detection: The script checks if the current moving average is higher than the previous one, while the previous one was lower than the one before it. This identifies an upward "hook" or valley in the trend.
2. Entry Rules (Execution)
The script automatically opens trades based on the signals identified above, provided that a position of the opposite type does not already exist:
- Sell Orders: If a bearish reversal is detected and there are no active Sell positions, the script opens a Sell trade.
- Buy Orders: If a bullish reversal is detected and there are no active Buy positions, the script opens a Buy trade.
3. Dynamic Price Tracking
Whenever a reversal signal occurs, the script captures specific price data:
- For Bearish signals, it identifies the highest price reached within the last 10 candles.
- For Bullish signals, it identifies the lowest price reached within the last 10 candles.
These values are stored in a memory list (an array) to serve as dynamic benchmarks for managing open trades.
4. Trade Management and Modification
Once a trade is open, the script continuously monitors the market to adjust the trade's "Stop Loss" or exit target based on the historical data it collected during the entry phase:
- For Sell Positions: The script looks at the most recent high price recorded. If the current market price stays below that high, it updates the trade's parameters to that high price.
- For Buy Positions: The script looks at the most recent low price recorded. If the current market price stays above that low, it updates the trade's parameters to that low price.
If the market price moves past these historical benchmarks in a way that violates the logic, the script discards the old benchmark and looks to the next available one in its memory list.
Summary of Behavior
The script acts as a momentum-reversal trader. It waits for the moving average to "turn around" to enter a trade. Once inside a trade, it uses the highest or lowest points from the immediate past to set a dynamic exit point, effectively attempting to lock in gains or limit losses based on recent price extremes.
//+------------------------------------------------------------------+
//| MA.S.R_Trading.mq4 |
//| FORTRADER.RU |
//| http://www.fortrader.ru |
//+------------------------------------------------------------------+
#property copyright "FORTRADER.RU"
#property link "http://www.fortrader.ru"
double maximum[100000];
double minimum[100000];
int l,m,flopen,b,s,total,cnt,flopens;
extern int perma=5;
int start()
{
total=OrdersTotal();
b=0;s=0;
for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()==OP_BUY)
{ b=1;}
if(OrderType()==OP_SELL)
{ s=1;}
}
/*--------------------------------------------------*/
double ma1=iMA(NULL,0,perma,0,MODE_SMA,PRICE_CLOSE,1);
double ma2=iMA(NULL,0,perma,0,MODE_SMA,PRICE_CLOSE,2);
double ma3=iMA(NULL,0,perma,0,MODE_SMA,PRICE_CLOSE,3);
if(ma1<ma2 && ma2>ma3)
{
maximum[m]=High[iHighest(NULL,0,MODE_HIGH,10,1)];
m++;
}
if(ma1>ma2 && ma2<ma3)
{
minimum[l]=Low[iLowest(NULL,0,MODE_LOW,10,1)];
l++;
}
if(ma1<ma2 && ma2>ma3 && s==0)
{
OrderSend(Symbol(),OP_SELL,0.1,Bid,3,0,0,"",Green);
}
if(ma1>ma2 && ma2<ma3 && b==0)
{
OrderSend(Symbol(),OP_BUY,0.1,Bid,3,0,0,"",Red);
}
/*------------------------------------------*/
for(int cnt=0;cnt<OrdersTotal();cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()==OP_SELL)
{
if(maximum[m-1]>Close[1])
{
OrderModify(OrderTicket(),OrderOpenPrice(),maximum[m-1],0,0,Yellow);
}
else{m--;}
}
if(OrderType()==OP_BUY)
{
if(minimum[l-1]<Close[1])
{
OrderModify(OrderTicket(),OrderOpenPrice(),minimum[l-1],0,0,Yellow);
}else{l--;}
}
}
return(0);
}
Comments