Author: Copyright � 2005, Forex-Experts
AFStar_v1

This script is designed as a technical analysis tool that identifies potential buying and selling opportunities on a trading chart by combining two distinct layers of market logic: Trend Analysis and Momentum Validation.

1. The Core Trend Detection (Layer 1)

The first stage of the logic acts as a "filter" to determine the general direction of the market. It performs an exhaustive search across a range of Moving Averages (EMAs). Instead of looking at just one fixed average, it checks multiple periods simultaneously:

  • Fast Movement: It scans for short-term price reactions.
  • Slow Movement: It scans for longer-term price trends.

The script looks for specific "crosses" where the fast moving average intersects with the slow moving average. A crossover from below to above signals potential upward momentum, while a crossover from above to below signals downward momentum. By iterating through many combinations of these averages, the script attempts to find the most stable trend confirmation available at any given moment.

2. Momentum and Volatility Validation (Layer 2)

The second stage serves as a "confirmation" layer. Even if a trend cross is detected, the script requires additional evidence before generating a signal. This stage involves three specific checks:

  • Volatility Measurement: It calculates the average price range over the most recent bars to understand how much the market is moving.
  • Price Gap Detection: It analyzes "true" price movements—specifically looking for significant gaps or jumps in price relative to that calculated volatility. This helps filter out "noisy" markets where prices are fluctuating without actual direction.
  • Momentum Oscillators: It utilizes a momentum indicator (WPR) to check if the market is currently overbought, oversold, or moving with sufficient strength.

3. The Signal Execution Logic

The final step of the script is the "decision module." A trade signal is only triggered when both Layer 1 and Layer 2 agree.

  • Buy Signal: Triggered if a trend-based "Buy" cross is detected and the momentum/volatility check confirms it is a valid entry point (specifically looking for instances where the price is moving into a bullish zone).
  • Sell Signal: Triggered if a trend-based "Sell" cross is detected and the momentum/volatility check confirms it is a valid exit or shorting point.

Safety Mechanisms: To ensure clarity on the chart, the script includes a conflict resolution rule: if the logic simultaneously generates both a Buy and a Sell signal at the exact same moment, it cancels both out. This prevents "conflicting signals" from appearing on the screen, ensuring that the user only sees a clear direction when the data is consistent.

Indicators Used
Moving average indicatorLarry William percent range indicator
Miscellaneous
Implements a curve of type %1
10 Views
0 Downloads
0 Favorites
AFStar_v1
/*-----------------------------+
|			       |
| Shared by www.Aptrafx.com    |
|			       |
+------------------------------*/

//+------------------------------------------------------------------+
//|                                                       AFStar.mq4 |
//|                                  Copyright © 2005, Forex-Experts |
//|                                     http://www.forex-experts.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, Forex-Experts"
#property link      "http://www.forex-experts.com"

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Magenta
#property indicator_color2 Aqua

//---- input parameters
 
extern double StartFast=3;
extern double EndFast=3.5;
extern double StartSlow=8;
extern double EndSlow=9;
extern double StepPeriod=0.2;
extern double StartRisk=1;
extern double EndRisk=2.8;
extern double StepRisk=0.5; 
extern int    AllBars=500;


int      shift=0,RiskCnt=0, EndScan1=0, EndScan2=0;
double   iMaSlowPrevious=0, iMaSlowCurrent=0, iMaFastPrevious=0, iMaFastCurrent=0;
double   FastCnt=0, SlowCnt=0;
double   Buy1[1000], Sell1[1000];
double   Buy2[1000], Sell2[1000];
double   Buy=0, Sell=0;
//---- buffers

double val1[];
double val2[];


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicator line
   IndicatorBuffers(2);
   SetIndexStyle(0,DRAW_ARROW);
   SetIndexArrow(0,226);
   SetIndexStyle(1,DRAW_ARROW);
   SetIndexArrow(1,225);
   SetIndexBuffer(0,val1);
   SetIndexBuffer(1,val2);

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- TODO: add your code here
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   if (AllBars>=1000) AllBars=1000;

   SetIndexDrawBegin(0,Bars-AllBars+11+1);
   SetIndexDrawBegin(1,Bars-AllBars+11+1);
   int i,shift,counted_bars=IndicatorCounted();
   int Counter,i1,value10,value11;
   double value1,x1,x2;
   double value2,value3;
   double TrueCount,Range,AvgRange,MRO1,MRO2;
   double Table_value2[1000];
//----
   if(Bars<=11+1) return(0);
//---- initial zero
   if(counted_bars<11+1)
   {
      for(i=1;i<=0;i++) val1[AllBars-i]=0.0;
      for(i=1;i<=0;i++) val2[AllBars-i]=0.0;
   }

//---- TODO: add your code here
//for (shift=(AllBars-11-1); shift<=0; shift--)
//{
   shift=AllBars-11-1;
   while(shift>=0)
{

	Sell1[shift]=0;
	Buy1[shift]=0;
	EndScan1=0;
	SlowCnt=StartSlow;
	while (SlowCnt<=EndSlow)
	{	
	   if (EndScan1==1) break;
		FastCnt=StartFast;
		while (FastCnt<=EndFast)
		{
		 if (EndScan1==1) break;
		 //Scan Parameters		
		 iMaSlowPrevious = iMA(NULL,0,SlowCnt,0,MODE_EMA, PRICE_CLOSE, shift-1);
		 iMaSlowCurrent = iMA(NULL,0,SlowCnt,0,MODE_EMA, PRICE_CLOSE, shift);
		 iMaFastPrevious = iMA(NULL,0,FastCnt,0, MODE_EMA, PRICE_CLOSE, shift-1);
		 iMaFastCurrent = iMA(NULL,0,FastCnt,0, MODE_EMA, PRICE_CLOSE, shift);
		 if (iMaFastPrevious<iMaSlowPrevious && iMaFastCurrent>iMaSlowCurrent) { EndScan1=1; Sell1[shift]=1;}
		 if (iMaFastPrevious>iMaSlowPrevious && iMaFastCurrent<iMaSlowCurrent) { EndScan1=1; Buy1[shift]=1;}
		 FastCnt=FastCnt+StepPeriod;
		}	
	SlowCnt=SlowCnt+StepPeriod;
	}

	EndScan2=0;
	Sell2[shift]=0;
   Buy2[shift]=0;
	RiskCnt=StartRisk;
//	while (RiskCnt<=EndRisk)
	if (RiskCnt<=EndRisk)
	{ 

	if (EndScan2!=1) {
 
   value10=3+RiskCnt*2;
   x1=67+RiskCnt;
   x2=33-RiskCnt;
   value11=value10;

//----
//   shift=AllBars-11-1;
//   while(shift>=0)
//     {
     
   Counter=shift;
	Range=0.0;
	AvgRange=0.0;
	for (Counter=shift; Counter<=shift+9; Counter++) AvgRange=AvgRange+MathAbs(High[Counter]-Low[Counter]);
		
	Range=AvgRange/10;
	Counter=shift;
	TrueCount=0;
	while (Counter<shift+9 && TrueCount<1)
		{if (MathAbs(Open[Counter]-Close[Counter+1])>=Range*2.0) TrueCount=TrueCount+1;
		Counter=Counter+1;
		}
	if (TrueCount>=1) {MRO1=Counter;} else {MRO1=-1;}
	Counter=shift;
	TrueCount=0;
	while (Counter<shift+6 && TrueCount<1)
		{if (MathAbs(Close[Counter+3]-Close[Counter])>=Range*4.6) TrueCount=TrueCount+1;
		Counter=Counter+1;
		}
	if (TrueCount>=1) {MRO2=Counter;} else {MRO2=-1;}
	if (MRO1>-1) {value11=3;} else {value11=value10;}
	if (MRO2>-1) {value11=4;} else {value11=value10;}
	value2=100-MathAbs(iWPR(NULL,0,value11,shift)); // PercentR(value11=9)
	Table_value2[shift]=value2;
	val1[shift]=0;
	val2[shift]=0;
	value3=0;
	if (value2<x2)
		{i1=1;
		while (Table_value2[shift+i1]>=x2 && Table_value2[shift+i1]<=x1){i1++;}
		if (Table_value2[shift+i1]>x1) 
			{
			value3=High[shift]+Range*0.5;
         Sell2[shift]=value3;
//			val1[shift]=value3;
			} 
		}
	if (value2>x1)
		{i1=1;
		while (Table_value2[shift+i1]>=x2 && Table_value2[shift+i1]<=x1){i1++;}
		if (Table_value2[shift+i1]<x2) 
			{
			value3=Low[shift]-Range*0.5;
         Buy2[shift]=value3;
//			val2[shift]=value3;
			}
		}

  }
	if (Buy2[shift]>0 || Sell2[shift]>0) {EndScan2=1;}

	RiskCnt=RiskCnt+StepRisk;      
}

//Main decision module

Buy=0; Sell=0;

if ((Buy1[shift]>0 && Buy2[shift]>0) || (Buy1[shift]>0 && Buy2[shift+1]>0) || (Buy1[shift+1]>0 && Buy2[shift]>0)) Buy=Low[shift]-1*Point;   	
if ((Sell1[shift]>0 && Sell2[shift]>0) || (Sell1[shift]>0 && Sell2[shift+1]>0) || (Sell1[shift+1]>0 && Sell2[shift]>0)) Sell=High[shift]+1*Point;   	


//Ignore if we have two signals
if (Buy!=0 && Sell!=0)
{
	Buy=0; Sell=0;
}


val1[shift]=Sell;
val2[shift]=Buy;

   shift--;
}
  
//----
   return(0);
  }
//+------------------------------------------------------------------+

Comments