MBKAsctrend3times_v2
Download scriptThis script is designed to identify and visualize market trends by analyzing price momentum across three different timeframes simultaneously. Rather than relying on a single data point, it combines multiple perspectives to filter out "noise" and provide a more stable directional signal for traders.
Core Logic Overview
The tool operates by calculating a weighted momentum value based on the Williams Percent Range (WPR) indicator. It takes three specific lengths of history—representing short-term, medium-term, and long-term data—and processes them as follows:
1. Multi-Timeframe Synthesis The script calculates the momentum for three distinct periods (set by the user as 9, 33, and 77). It then applies a weighting system to these values. By giving different importance to each timeframe (for example, placing more weight on the medium-term data), the tool creates a "composite" momentum score. This allows the script to see not just what is happening right now, but how current price action aligns with broader market trends.
2. Trend Validation The logic uses a two-tier verification system to determine if a trend is truly active:
- Primary Signal: The script looks for the composite momentum score to cross specific "Buy" or "Sell" thresholds (adjusted by a user-defined risk factor).
- Secondary Filter: It simultaneously checks the long-term trend component. A trade signal is only confirmed if both the combined composite momentum and the long-term momentum are moving in the same direction. This prevents the script from signaling a "Buy" during a temporary price spike if the overarching long-term trend is still bearish (or vice versa).
3. Dynamic Range Measurement To ensure that visual signals are placed accurately on the chart, the script calculates the average volatility (the range between highs and lows) of the recent candles. This measurement helps determine exactly how far above or below the current price the entry arrows should appear, ensuring they are clearly visible and consistent with the asset's volatility.
4. Execution and Visualization The script tracks the "state" of the market (Up-trend vs. Down-trend). When a transition occurs—for instance, when the momentum flips from a bearish state to a bullish state—the script triggers a visual signal:
- Buy Signals: An arrow is placed below the candle price when the combined criteria confirm an upward trend.
- Sell Signals: An arrow is placed above the candle price when the combined criteria confirm a downward trend.
Summary for the User
In simple terms, this tool acts as a "trend filter." It looks at three different speeds of market movement at once, blends them together, and waits for both the immediate momentum and the long-term trend to agree before showing you a signal. This helps traders avoid entering positions based on minor price fluctuations that don't have enough "weight" behind them.
/*-----------------------------+
| |
| Shared by www.Aptrafx.com |
| |
+------------------------------*/
//+------------------------------------------------------------------+
//| MBKAsctrend3times.mq4
//+------------------------------------------------------------------+
#property copyright "MBKAsctrend3times matt kennel"
#property link "http://www.metatrader.org"
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Blue
#property indicator_color2 Red
//---- input parameters
//
//
// Modified by MBK: now have three different timescales (bar lengths)
// to combine for final WPR indicator, with further long-term trend indicator.
//
// try on H1 chart with these parameters.
//
extern int RISK=3;
//extern int CountBars=2000;
extern int NumberofAlerts=0;
extern int WPRLength1=9,WPRLength2=33, WPRLength3=77;
extern int LToffset = -5;
extern double w1=1.0, w2=3.0, w3 = 1.0; // relative weights
int counter=0;
//---- buffers
double val1[];
double val2[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
string short_name;
//---- indicator line
IndicatorBuffers(2);
SetIndexStyle(0,DRAW_ARROW);
SetIndexArrow(0,233);
SetIndexStyle(1,DRAW_ARROW);
SetIndexArrow(1,234);
SetIndexBuffer(0,val1);
SetIndexBuffer(1,val2);
//----
return(0);
}
//+------------------------------------------------------------------+
//| SilverTrend_Signal |
//+------------------------------------------------------------------+
int start() {
//if (CountBars>=Bars) CountBars=Bars;
//SetIndexDrawBegin(0,Bars-CountBars+SSP);
//SetIndexDrawBegin(1,Bars-CountBars+SSP);
int i1,i2,K;
double Range,AvgRange,smin,smax,SsMax,SsMin,price;
double wprvalue;
static bool uptrend,old;
bool flag1, flag2;
int i,shift,counted_bars=IndicatorCounted();
///----
//if(Bars<=SSP+1) return(0);
//---- initial zero
//if(counted_bars<SSP+1)
// {
// for(i=1;i<=SSP;i++) val2[CountBars-i]=0.0;
// }
////----
K=33-RISK;
// wprlength = 3+RISK*2;
uptrend=false;
old = false;
double sumweights = w1+w2+w3;
w1 /= sumweights;
w2 /= sumweights;
w3 /= sumweights;
int SSP = 10;
for (shift = Bars-SSP; shift>=0; shift--) {
Range=0;
AvgRange=0;
for (i1=shift; i1<=shift+SSP; i1++)
{AvgRange=AvgRange+MathAbs(High[i1]-Low[i1]);
}
Range=AvgRange/(SSP+1);
double wprval1 = (100-MathAbs(iWPR(Symbol(),Period(),WPRLength1,shift)));
double wprval2 = (100-MathAbs(iWPR(Symbol(),Period(),WPRLength2,shift)));
double wprval3 = (100-MathAbs(iWPR(Symbol(),Period(),WPRLength3,shift)));
wprvalue = w1*wprval1 + w2*wprval2 + w3*wprval3;
// MathPow(wprval1*wprval2*wprval3,1.0/3.0);
double wprlong = wprval3;
if ((wprvalue < 33-RISK) && (wprlong < 50-LToffset)) {
uptrend = false;
}
if ((wprvalue > 67+RISK) && (wprlong >= 50+LToffset)) {
uptrend = true;
}
if (uptrend!=old && uptrend==true) {
val1[shift]=Low[shift]-Range*0.8; // lower range
counter=0;
if (shift==0 && counter<=NumberofAlerts) {
// Alert("MBKAsctrend ",Period()," ",Symbol()," BUY");
counter=counter+1;
}
}
if (uptrend!=old && uptrend==false) {
counter=0;
val2[shift]=High[shift]+Range*0.8;
if (shift==0 && counter<=NumberofAlerts) {
// Alert("Silver Trend ",Period()," ",Symbol()," SELL");
counter=counter+1;
}
}
Comment(shift);
old=uptrend;
}
return(0);
}
//+------------------------------------------------------------------+
Comments