MTFPI-sub3

This script is a technical analysis tool designed to help traders identify potential entry points and, more importantly, dynamically manage their "Stop Loss" levels based on momentum shifts in the market. It operates by monitoring the relationship between two specific types of price data: the general trend (Stochastic oscillators) and actual price action (Highs and Lows).

Core Logic Overview

The script functions through a three-step observation process to determine where a trade might be safe or where it might become risky.

1. Identifying Momentum Shifts The tool constantly watches a "Stochastic Oscillator," which is a momentum indicator that tells you if an asset is overbought or oversold. It specifically looks for "crosses"—moments when the main trend line crosses the signal line.

  • When the trend turns upward, the script prepares for potential "Buy" opportunities.
  • When the trend turns downward, it prepares for potential "Sell" opportunities.

2. Locating Historical Price Anchors Once a momentum shift is detected (for example, an upward cross), the script doesn't just look at the current price. It looks back at the recent past (up to 50 bars) to find the most recent significant price "swings."

  • In a bullish trend, it identifies the lowest point reached during that specific momentum shift to establish a structural floor.
  • In a bearish trend, it identifies the highest point reached during that shift to establish a structural ceiling.

3. Dynamic Stop-Loss and Entry Execution The script then uses these historical floors and ceilings to plot visual markers on your chart:

  • Buy Signals: If the current price closes above a recently identified "floor," the script marks a potential Buy signal and sets a Stop Loss at that floor level.
  • Sell Signals: If the current price closes below a recently identified "ceiling," the script marks a potential Sell signal and sets a Stop Loss at that ceiling level.

Key Behavioral Features

  • Automatic Adjustment: The Stop Loss lines are not static. As the market moves and new momentum shifts occur, the script updates these levels to reflect the most recent structural highs and lows.
  • Mutual Exclusivity: The logic includes a safety feature where it prevents Buy and Sell markers from overlapping or conflicting with one another simultaneously. If a Bullish signal is active, it clears out opposing Sell signals, and vice versa, ensuring the visual display remains clean and logically consistent.
  • Price Protection: The script continuously checks to ensure that the calculated Stop Loss levels are not being "violated" by immediate price fluctuations, attempting to keep the stop level at a structurally sound point relative to recent candle activity.
Indicators Used
Stochastic oscillator
13 Views
0 Downloads
0 Favorites
MTFPI-sub3
//KurlFX 2009.01.31
//+------------------------------------------------------------------+
//|	*** MTFPI-sub3 ***		SL-line based on stochastic cross		|
//+------------------------------------------------------------------+
#property copyright "Copyright (c) 2009,Kurl FX"//*rev.2/2/09
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Green 
#property indicator_color2 OrangeRed

extern int K_Period = 5;
extern int D_Period = 3;
extern int Slowing = 3;

double BS[];//StopLoss;Buy
double SS[];//StopLoss;Sell
double BG[];//sign;Buy/No 
double SG[];//sign;Sell/No
double Bx[];//price;at stochas up cross
double Sx[];//price;at stochas dn cross
double Sto[];//stochastic;main
double Sig[];//stochastic;signal
//+------------------------------------------------------------------+
//|  Custom indicator initialization function                        |
//+------------------------------------------------------------------+
int init()
{
	IndicatorBuffers(8);
	SetIndexBuffer(0, BS);
	SetIndexBuffer(1, SS);
	SetIndexBuffer(2, BG);
	SetIndexBuffer(3, SG);
	SetIndexBuffer(4, Bx);
	SetIndexBuffer(5, Sx);
	SetIndexBuffer(6, Sto);
	SetIndexBuffer(7, Sig);
	SetIndexLabel(0,"BuySL");
	SetIndexLabel(1,"SellSL");
	return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
	return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
	static int Pos;//*must be static
	int counted_bar=IndicatorCounted();
	int limit=Bars-counted_bar;
	if(counted_bar==0)limit-=K_Period+D_Period+Slowing;
	for(int i=limit-1;i>=0;i--) 
	{
		Sto[i]=iStochastic(NULL,0,K_Period,D_Period,Slowing,MODE_SMA,0,MODE_MAIN,i);
		Sig[i]=iStochastic(NULL,0,K_Period,D_Period,Slowing,MODE_SMA,0,MODE_SIGNAL,i);
	} 
	if(counted_bar==0){limit-=50;BS[limit]=EMPTY_VALUE;SS[limit]=EMPTY_VALUE;}
	for(i=limit-1;i>=0;i--)
	{ 
		BS[i]=BS[i+1];SS[i]=SS[i+1];
		if (Sto[i+1]<=Sig[i+1] && Sto[i]>Sig[i])Pos=1;
		if (Sto[i+1]>=Sig[i+1] && Sto[i]<Sig[i])Pos=-1;
		if (Pos>0)
		{
			for(int j=2;j<=50;j++)
			{     
				if(Sto[i+j+1]<=Sig[i+j+1]&&Sto[i+j]>Sig[i+j]){Bx[i]=MathMin(Low[i+j+1],Low[i+j]);break;}
			}
		}
		else Bx[i]=Bx[i+1];
		if (Pos<0)
		{
			for(j=2;j<=50;j++)
			{
				if(Sto[i+j+1]>=Sig[i+j+1]&&Sto[i+j]<Sig[i+j]){Sx[i]=MathMax(High[i+j+1],High[i+j]);break;}
			}
		}
		else Sx[i]=Sx[i+1];

		int bx=0;
		if(Close[i+1]<=Bx[i+1]&&Close[i]>Bx[i])bx=1;
		else{if(Close[i+1]>=Bx[i+1]&&Close[i]<Bx[i])bx=-1;}
		int sx=0;
		if (Close[i+1]<=Sx[i+1]&&Close[i]>Sx[i])sx=1;
		else{if(Close[i+1]>=Sx[i+1]&&Close[i]<Sx[i])sx=-1;}

		BG[i]=EMPTY_VALUE;SG[i]=EMPTY_VALUE;
		double sxh=MathMax(Low[i+1],Sx[i]);double sxl=MathMin(High[i+1],Sx[i]);
		double bxh=MathMax(Low[i+1],Bx[i]);double bxl=MathMin(High[i+1],Bx[i]);
		if(bx==1)
		{
			BG[i]=Close[i];
			BS[i]=bxh;
		}
		if(sx==1)
		{  
			BS[i]=sxh;
			if(BS[i+1]==EMPTY_VALUE)BG[i]=Close[i];
		}
		if(sx==-1)
		{
			SG[i]=Close[i];
			SS[i]=sxl;
		}
		if(bx==-1)
		{
			if(SS[i+1]==EMPTY_VALUE)SG[i]=Close[i];
			SS[i] = bxl;
		}
		if (BG[i+1]!=EMPTY_VALUE&&SG[i]==EMPTY_VALUE)SS[i]=EMPTY_VALUE;
		if (SG[i+1]!=EMPTY_VALUE&&BG[i]==EMPTY_VALUE)BS[i]=EMPTY_VALUE;

		if (BS[i]!=EMPTY_VALUE){double d=MathMin(Low[i+2],Low[i+1]);BS[i]=MathMax(d,BS[i]);}
		if (SS[i]!=EMPTY_VALUE){d=MathMax(High[i+2],High[i+1]);SS[i]=MathMin(d,SS[i]);}
	}
	return(0);
}
//+------------------------------------------------------------------+

Comments