1. 首页
  2. 易语言 - EasyLanguage
  3. 随机震荡背离指标策略应用Stochastic divergence indicator, into a strategy

随机震荡背离指标策略应用Stochastic divergence indicator, into a strategy

本文讲述一个的将随机震荡背离指标在策略中的具体应用实例。This code was found on markplex.com and coppied. Its a divergence indicator of stochastics and price. It displays a green line between the first and second pivots in stochastics (bottoms) when there is divergence from price.

Let’s try and put this into a strategy to:

buy at the close of the bar where the green line ends.
and
hold until the next bar and sell at close.

Here is the code which is all pretty much the code from the original indicator, accept for the “cts” and “mp” and the last bit that says to buy and sell.

inputs: Stochlength(12),
SmoothingLength1(3),
SmoothingLength2(3),
SmoothingType(1),
Length (20),
LeftStrength(3),
RightStrength(1),
cts (1);



Variables: oFastK(0),
oFastD(0),
oSlowk(0),
oSlowd(0),
oPivotPrice1(0),
oPivotBar1(0),
oPivotPrice2(0),
oPivotBar2(0),
beginningofline (0),
mp (0);

mp = MarketPosition;

{calculate stochastics value}
Value1= Stochastic( H, L, C, Stochlength, SmoothingLength1, SmoothingLength2,
SmoothingType, oFastK, oFastD, oSlowk, oSlowD) ;

{Determine if pivots in the sotchastic have occured}
Condition1= Pivot(oSlowk, Length, LeftStrength, RightStrength, 1, -1, oPivotPrice1,oPivotBar1) <>-1
And (oPivotBar1-RightStrength) = 0;
{COnfirms bottom pivot, RightStrength bars before current bar}
Condition2 = Pivot (oSlowD, Length, LeftStrength, RIghtstrength, 2, -1, oPivotPRice2, oPivotBar2) <>-1;

If Condition1 And Condition2 And L[oPivotBar2] >= L[oPivotBar1] AND oSlowD[oPivotBar2] < oSlowD[oPivotBar1] then
Begin
Value2 = TL_new (D[oPivotBar2], T[oPivotBar2], L[oPivotBar2], D[oPivotBar1], T[oPivotBar1], L[oPIvotBar1]);
TL_SetColor (value2, Green) ;
TL_SetSize (value2, 3);
End;


If Condition2 then begin
Buy ("buy") cts contracts next bar at Market;
If mp = 1 Then sell this bar cts contracts at close;
end;

The above posted code was updated to help jump start and is posted below. You will likely have to customize this code to meet your requirements.

Comments were added to the code to explain changes. Please read these carefully to begin to learn EasyLanguage. Even if you are an experienced programmer, learning EasyLanguage will require you invest time in reading/reviewing reference material. 

inputs:  
	Stochlength(12),  
	SmoothingLength1(3),  
	SmoothingLength2(3),  
	SmoothingType(1),  
	Length (20),  
	LeftStrength(3),  
	RightStrength(1),  
	cts (1);  
 
Variables: oFastK(0),  
	oFastD(0),  
	oSlowK(0),  
	oSlowD(0),  
	oPivotPrice(0),  
	oPivotBar(0),  
	OldPivotBar( 0 ), 
	NewPivotBar( 0 ), 
	OldPivotPrice( 0 ), 
	NewPivotPrice( 0 ) ; 
 
{calculate stochastics value}  
Value1= Stochastic( H, L, C, Stochlength, SmoothingLength1, SmoothingLength2, SmoothingType, oFastK, oFastD, oSlowK, oSlowD) ;  
 
 
//  If a low pivot has occurred, then capture the pivot bar and the Low price at the pivot bar 
If Pivot( oSlowK, Length, LeftStrength, RightStrength, 1, -1, oPivotPrice, oPivotBar ) <> - 1 Then 
	Begin 
	 
	//  Update the old pivot bar and price with the new pivot bar and price, and then 
	//  update the new pivot bar and price with the latest pivot. 
	OldPivotBar = NewPivotBar ; 
	NewPivotBar = CurrentBar - oPivotBar ; 
	 
	OldPivotPrice = NewPivotPrice ; 
	NewPivotPrice = oPivotPrice ; 
	 
	End ; 
 
//  Test for the condition that we have both an old and new pivot bar, and the new Stochastics pivot price is greater 
//  than the old Stochastics pivot price (of the stochastics) while the bar Low prices are decreasing.  In other words, 
//  consecutive Stochastics Low pivot prices are increasing while bar Low prices are decreasing indicating price divergence. 
Condition1 = OldPivotBar > 0 And  
             NewPivotBar > 0 And  
             Low[ CurrentBar - OldPivotBar ] >= Low[ Currentbar - NewPivotBar ] AND  
             OldPivotPrice <  NewPivotPrice ; 
 
 
//  If Condition1 is true, then create the trend line on the price chart between the old pivot and the new pivot  
//  at the bar Lows corresponding to the pivot locations.  Also, enter long. 
If Condition1 Then Begin  
 
	Value2 = TL_New( Date[ CurrentBar - OldPivotBar ], Time[ CurrentBar - OldPivotBar ], Low[ CurrentBar - OldPivotBar ],  
	                 Date[ CurrentBar - NewPivotBar ], Time[ CurrentBar - NewPivotBar ], Low[ CurrentBar - NewPivotBar ] ) ;  
	                  
	TL_SetColor( Value2, Magenta ) ;  
	TL_SetSize( Value2, 2 ) ;  
	 
	Buy ("buy") cts contracts next bar at Market;  
	 
End ; 
	 
 
//  If it has been 1 bar since entry then exit at this bar at the closing price	 
If BarsSinceEntry = 1 Then  
	Sell this bar at close;  

An example of inserting this strategy on a 1-minute @ES chart is shown below. Note the trend lines are drawn on the price chart between the bar Lows corresponding to the Low pivots on the Stochastics chart, shown in the lower sub-graph.

更新于 2020年5月15日

这篇文章对您有帮助吗?

相关的话题

留言评论