TrendManagerNT
Download scriptThis tool is designed to identify market trends and provide specific visual signals for entering and exiting trades. It functions by analyzing price movement patterns, momentum strength, and trend shifts to help filter out "noise" in the market.
Here is a breakdown of how the logic works:
Core Trend Tracking
The script establishes a baseline by looking at the historical high and low prices over a set period. It calculates two main components:
- Primary Trend Line: A calculation based on the average price of recent candles.
- Secondary Trend Line: A comparison point that is slightly shifted backward in time to identify when the current trend is pulling away or crossing specific thresholds.
By comparing these two lines, the script determines the "direction" of the market. When the primary line crosses above the secondary line, it signals a potential upward movement; when it crosses below, it signals a potential downward movement.
Entry Signal Logic (Buy & Sell)
To prevent "false starts," the script does not trigger a signal every time the lines cross. It uses a multi-layered filtering system to ensure the trend has actual strength:
- Trend Confirmation: The script verifies that the trend shift is significant enough to be meaningful, using a specific percentage gap to filter out minor price fluctuations.
- Momentum Filtering (ADX): It measures the "speed" or strength of the trend using an ADX indicator.
- If the trend is actively gaining strength (rising), the requirement for entry is slightly different than if the trend is steady or cooling off.
- It checks if the momentum meets a minimum threshold to ensure you aren't entering a "dying" move.
- Visual Cues: Once all conditions are met, the script places arrows on the chart (Buy/Sell) and triggers an audible alert for real-time monitoring.
Exit Signal Logic (Close Trades)
The tool also includes logic to help determine when it might be time to exit a position. It monitors specific trend components (referred to in the logic as "Silver" and "Orange" trends).
- For Buy Positions: It looks for signs that the trend structure is flipping or losing its dominant upward momentum.
- For Sell Positions: It looks for signs that the downward pressure is weakening.
- Execution: If these specific structural changes occur, the script places a "Close" arrow on the chart to suggest taking profit or mitigating risk.
Summary of User Controls
The script allows for customization of several key parameters:
- Period & Shift: Adjusts how far back the script looks to calculate price averages and trend lines.
- ADX Thresholds: Defines how "strong" the market must be before a signal is validated.
- Gap Settings: Determines how much of a price discrepancy is needed to consider a trend change "valid," helping users switch between aggressive and conservative trading styles.
//+------------------------------------------------------------------+
//| |
//| Copyright © 1999-2008, MetaQuotes Software Corp. |
//| http://www.metaquotes.ru |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 6
#property indicator_color1 Red
#property indicator_color2 Blue
#property indicator_color3 Blue
#property indicator_color4 Red
#property indicator_color5 Red
#property indicator_color6 Blue
//----
#property indicator_width3 2
#property indicator_width4 2
#property indicator_width5 1
#property indicator_width6 1
//----
extern int TM_Period=7;
extern int TM_Shift=2;
extern int MinADX=22;
extern int MinRisingADX=15;
extern int MoveArrowFromCandleInPoints=10;
extern double NTMinGap=5;
extern bool risingADXoverrides=true;
//----
double SpanA_Buffer[];
double SpanB_Buffer[];
double BufferBuySignals[];
double BufferSellSignals[];
double BufferExitBuySignals[];
double BufferExitSellSignals[];
int a_begin;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int init()
{
a_begin=TM_Shift;
SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID);
SetIndexBuffer(0,SpanB_Buffer);
SetIndexDrawBegin(0,TM_Period+a_begin-1);
SetIndexLabel(0,"TM_Period+");
SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID);
SetIndexBuffer(1,SpanA_Buffer);
SetIndexDrawBegin(1,TM_Period+a_begin-1);
SetIndexLabel(1,"TM_Period");
//
SetIndexStyle(2,DRAW_ARROW);
SetIndexBuffer(2,BufferBuySignals);
SetIndexArrow(2,233);
SetIndexLabel(2,"BUY");
//
SetIndexStyle(3,DRAW_ARROW);
SetIndexBuffer(3,BufferSellSignals);
SetIndexArrow(3,234);
SetIndexLabel(3,"SELL");
//
SetIndexStyle(4,DRAW_ARROW);
SetIndexBuffer(4,BufferExitBuySignals);
SetIndexArrow(4,217);
SetIndexLabel(4,"CLOSE");
//
SetIndexStyle(5,DRAW_ARROW);
SetIndexBuffer(5,BufferExitSellSignals);
SetIndexArrow(5,218);
SetIndexLabel(5,"CLOSE");
IndicatorShortName("TRENDMGRNT");
//----
string SymbolText=Symbol();
if (StringLen(SymbolText)==7){
SymbolText=StringSubstr(SymbolText, 0,6);
}
return(0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start()
{
int i,k;
int counted_bars=IndicatorCounted();
double high,low,price;
//----
if(Bars<=TM_Period) return(0);
if(counted_bars<1)
{
for(i=1;i<=TM_Period;i++)
{
SpanA_Buffer[Bars-i]=0;
SpanB_Buffer[Bars-i]=0;
BufferBuySignals[Bars-i]=0;
BufferSellSignals[Bars-i]=0;
BufferExitBuySignals[Bars-i]=0;
BufferExitSellSignals[Bars-i]=0;
}
}
i=Bars-TM_Period;
if(counted_bars>TM_Period) i=Bars-counted_bars-1;
while(i>=0)
{
high=High[i]; low=Low[i]; k=i-1+TM_Period;
while(k>=i)
{
price=High[k];
if(high<price) high=price;
price=Low[k];
if(low>price) low=price;
k--;
}
SpanA_Buffer[i] =(high+low)/2.0;
SpanB_Buffer[i] =SpanA_Buffer[i+TM_Shift];
BufferBuySignals[i]=0;
BufferSellSignals[i]=0;
BufferExitBuySignals[i]=0;
BufferExitSellSignals[i]=0;
//----
if (CheckClose(i, OP_BUY)==true)
{
BufferExitBuySignals[i]=Low[i]-Point*MoveArrowFromCandleInPoints;
}
if (CheckClose(i, OP_SELL)==true)
{
BufferExitSellSignals[i]=High[i]+Point*MoveArrowFromCandleInPoints;
}
if (SpanA_Buffer[i]!= SpanB_Buffer[i])
{
if ((SpanA_Buffer[i] > SpanB_Buffer[i]) && (SpanA_Buffer[i+1]<= SpanB_Buffer[i+1]))
{
if (CheckAdx(i, OP_BUY)==true)
{
BufferBuySignals[i]=Low[i]-Point*MoveArrowFromCandleInPoints;
if (i==1)
{
Alert("Buy on TM");
}
}
else
{
BufferBuySignals[i]=0;
}
}
else if ((SpanA_Buffer[i] < SpanB_Buffer[i]) && (SpanA_Buffer[i+1]>= SpanB_Buffer[i+1]))
{
if (CheckAdx(i, OP_SELL)==true)
{
BufferSellSignals[i]=High[i]+Point*MoveArrowFromCandleInPoints;
if (i==1)
{
Alert("Sell on TM");
}
}
else
{
BufferSellSignals[i]=0;
}
}
}
i--;
}
//----
return(0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CheckClose(int i, int OrderTyp)
{
bool retval=false;
// get the WindowFind
//double NewTrend = iCustom(Symbol(),Period(), 9,9,PRICE_CLOSE,i);
double NewTrendSilver=iCustom(Symbol(),Period(), "NewTrend",14,9,0, i);
double NewTrendOrange=iCustom(Symbol(),Period(), "NewTrend",14,9,1, i);
double OldNewTrendSilver=iCustom(Symbol(),Period(), "NewTrend",14,9,0, i+1);
double OldNewTrendOrange=iCustom(Symbol(),Period(), "NewTrend",14,9,1, i+1);
double Perc=MathAbs(1-(NewTrendSilver/NewTrendOrange))*100;
//----
if (Perc > NTMinGap)
{
if (OrderTyp==OP_BUY)
{
if ((OldNewTrendSilver < OldNewTrendOrange) && (NewTrendSilver > NewTrendOrange) )
{retval=true;}
}
if (OrderTyp==OP_SELL)
{
if ((OldNewTrendSilver > OldNewTrendOrange) && (NewTrendSilver < NewTrendOrange) )
{retval=true;}
}
}
//----
return(retval);
}
// check all filters here, not just adx
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CheckAdx(int i, int OrderTyp)
{
bool retval=false;
// get the WindowFind
//double NewTrend = iCustom(Symbol(),Period(), 9,9,PRICE_CLOSE,i);
double NewTrendSilver=iCustom(Symbol(),Period(), "NewTrend",14,9,0, i);
double NewTrendOrange=iCustom(Symbol(),Period(), "NewTrend",14,9,1, i);
// silver coming back over zero line might be good exits?
double Perc=MathAbs(1-(NewTrendSilver/NewTrendOrange))*100;
// silver > orange is buy
//double Juice = iCustom(Symbol(),Period(), "Juice",0, i);
//double redflattrend = iCustom(Symbol(),Period(), "flat trend",Period(),0, i);
//double greenflattrend = iCustom(Symbol(),Period(), "flat trend",Period(),1, i);
//double yellowflattrend = iCustom(Symbol(),Period(), "flat trend",2, i);
double adx=iADX(Symbol(),Period(), 14, PRICE_CLOSE, MODE_MAIN,i);
double adxold=iADX(Symbol(),Period(), 14, PRICE_CLOSE, MODE_MAIN,i+1);
bool adxvalid=false;
//----
if (risingADXoverrides ==true)
{
if (adxold < adx) // rising
{
if (adx > MinRisingADX)
{ adxvalid=true;}
}
else //falling
{
if (adx > MinADX)
{ adxvalid=true;}
}
}
else
{
if (adx>=MinADX)
{
adxvalid=true;
}
}
if (OrderTyp==OP_BUY)
{
if ((NewTrendSilver > NewTrendOrange) && (adxvalid==true) && (Perc > NTMinGap) )
//if ((Juice >= minJuice) && (adx >= MinADX) )(SolarWind >= minSolarWind) && (Juice >= minJuice) &&&& (greenflattrend>0)
{
retval=true;
}
else
{
retval=false;
}
}
else if (OrderTyp==OP_SELL)
{
if ((NewTrendSilver < (NewTrendOrange - NTMinGap)) && (adxvalid==true)&& (Perc > NTMinGap) )
//if ((Osma <= -minOsma) && (SolarWind <= minSolarWind) && (Juice >= minJuice) && (adxvalid == true) && (redflattrend>0) )
// if ((Juice >= minJuice) && (adx >= MinADX) )
{
retval=true;
}
else
{
retval=false;
}
}
//----
return(retval);
}
//+------------------------------------------------------------------+
Comments