1. 首页
  2. 易语言 - EasyLanguage
  3. 如何将指标转化为策略 How To Convert An Indicator to a Strategy

如何将指标转化为策略 How To Convert An Indicator to a Strategy

Why Strategies and Indicators Are Not Interchangeable

You cannot verify (compile) EasyLanguage code used for Analysis Techniques as a Strategy, and vice-versa. Analysis Techniques cannot verify EasyLanguage order statements, like Buy or Sell. Likewise, Strategies cannot verify EasyLanguage plot statements used in Analysis Techniques, like Plot or PlotPaintBar. However, calculation statements can be used in either. So if you need to use Strategy calculations in an Analysis Technique this can be done by copying the calculation statements into the Analysis Technique, or turning the calculations into a function that either a Strategy or Analysis Technique can call.

How Indicators Can Obtain Strategy Position and Performance Information

Verification errors will occur if an indicator references strategy performance / position Reserved Words like MarketPosition or TotalTrades. Analysis Techniques can obtain select strategy position and performance information when inserted on the same chart as a strategy is inserted. Information such as the market position or average entry price, can be obtained using the “I_” keywords, such as I_MarketPosition.

How To Convert an Indicator to a Strategy

To convert an indicator to strategy you would need to remove the plot statements and add code to generate buy / sell orders based on what you define as the indicator’s trading signals.

First, here’s some links, most from the EasyLanguage Hot Links For All FAQ page HOW TO CONVERT AN INDICATOR INTO A STRATEGY that discuss converting indicators to strategies.

Here is an example where an Indicator is converted into a strategy. 

The basic technique is to convert issuing Alerts to issuing entry and exit orders. Plots and related commands ( SetPlotColor ) and Alerts must all be commented out as these do not work in strategies.

The converted strategy is posted below (in a code box ). Comments beginning with *** were added to the code to explain changes. One minor modification of the indicator logic was required as explained in comments in the code. The strategy is a swing strategy, which is always in a position, either long or short. Profit target or stop loss exits could be added or built-in Proftit Target and Stop Loss exit strategies could be inserted on the same chart to change this. You will have to test this strategy to confirm its operates as required, and you will likely have to customize this code to meet your requirements.

[LegacyColorValue = true];  
inputs:   
	NumShares( 100 ),  // *** Added this input 
	Length(3),  
	Length1(1), 
	Lengthstp(14), 
	Price(Close) ; 
 
Vars:    
	//  *** Type all variables.  Not required, but good practice. 
	Double Trend(1), 
	Double PerR( 0 ), 
	Double StDev( 0 ), 
	Int LastBar( 0 ), 
	Double TrStop( 0 ); 
 
var: 
    alertTextID(-1),Avgstp(0); 
 
if barnumber = 1 then begin 
   alertTextID = Text_New(date,time,0,"RSI"); 
end; 
 
Avgstp = AverageFC( TrStop, Price) ; 
StDev  = StdDev(C,length1); 
PerR   = PercentR(length); 
 
//  *** Comment out all occurrences of Plot, SetPlotColor and Alert as these statements 
//      do not work in stratgies. 
If PerR >= 70 and PerR[1] < 70 and Trend < 1 and Close > TrStop then Begin 
 
	//SetPlotColor(1,green); 
	//Plot1(Low - tick,"Signal"); 
	TrStop = Low - StDev*1.382; 
	Trend = 1; 
	LastBar = BarNumber; 
	 
	if Text_GetTime(alertTextID) <> time then begin 
        text_setLocation(alertTextID, date, time, 0); 
        //alert("PercentRLong"); 
    end; 
     
End; 
 
//  *** Leave this next section of code in place as it can update TrStop, which is 
//      used for determining entries/exits elsewhere in the code. 
If PerR <= 30 and PerR[1] > 30 and Trend > -1 and Close < TrStop then Begin 
 
	//SetPlotColor(1,Red); 
	//Plot1(High + tick,"Signal"); 
	TrStop = High + StDev*1.382; 
	Trend = -1; 
	LastBar = BarNumber; 
	 
	if Text_GetTime(alertTextID) <> time then begin 
        text_setLocation(alertTextID, date, time, 0); 
        //alert("PercentRShort"); 
    end; 
 
End; 
 
If Trend = 1 then Begin 
 
	If LastBar < BarNumber then Begin 
	 
		If TrStop = 0 then  
			TrStop = Close; 
			 
		If Close > Close[1] then  
			TrStop = Low - StDev * 1.382; 
			 
		If Low - High[1] > StDev then  
			TrStop = Low - StDev * 0.618; 
			 
		If TrStop < TrStop[1] then  
			TrStop = TrStop[1]; 
			 
	End; 
	 
	// *** Convert this Alert to a Long entry. 
	If High crosses over TrStop then  
		Buy NumShares shares this bar at close; // alert("Hoverstop"); 
		 
	//Plot2(TrStop,"Stop"); 
	//SetPlotColor(2,green); 
	 
End; 
 
If Trend = -1 then Begin 
 
	If LastBar < BarNumber then Begin 
	 
		If TrStop = 0 then  
			TrStop = Close; 
			 
		If Close < Close[1] then  
			TrStop = High + StDev * 1.382; 
			 
		If Low[1] - High > StDev then  
			TrStop = High + StDev * 1.382; 
			 
		If TrStop > TrStop[1] then  
			TrStop = TrStop[1]; 
			 
	End; 
 
    // *** Convert this Alert to a Short Entry.  Move this test and entry to this section 
    //     of code as it would never be true in the section of code above which 
    //     sets Trend = 1, because TrStop is set to a value below Low, which 
    //     means Low could not cross below TrStop. 
	If Low crosses under TrStop then  
		Sellshort NumShares shares this bar at close;  // alert("Lunderstop"); 
 
	//Plot2(TrStop,"Stop"); 
	//SetPlotColor(2,Yellow); 
		 
End; 
 
//  *** Remove all the plot commands below. 
{ 
If PercentR(14) > 70 then 
	Begin 
		SetPlotColor(3,Magenta); 
		SetPlotColor(3,Magenta); 
		Plot3(High,"TREND"); 
		Plot4(Low,"TREND"); 
 
 
	End 
Else If PercentR(14) < 30 then 
	Begin 
		SetPlotColor(3,Red); 
		SetPlotColor(4,Red); 
		Plot3(High,"TREND"); 
		Plot4(Low,"TREND"); 
{Plot5(Avgstp,"Avgstp"); 
SetPlotColor(5,magenta);} 
	End 
Else If PercentR(14) < 70 and PercentR(9) > 30 then 
	Begin 
		SetPlotColor(3,green); 
		SetPlotColor(4,green); 
		Plot3(High,"TREND"); 
		Plot4(Low,"TREND"); 
 
 
{Plot5(Avgstp,"Avgstp"); 
		SetPlotColor(5,magenta);} 
	end;  
	 
} 

Another example to convert an indicator with an alert into a signal, i.e. Copy and paste the indicator code into a new strategy window. Remove all references to plots and alerts and change those into buy and sellshorts where needed. Verify to ensure accurate coding and to save the code set.

Here is one more example to illustrate how you create the actual command that generates an actual order to buy/sell and create the stop order when the stop is hit.

The first step is fairly straight-forward. The ‘indicator’ code must be copied into an EasyLanguage ‘strategy’ document.

Second, all ‘plot statements’ must be removed. They will not work within a strategy. Generally, the plot statements will need define variables used in buy/sell/sellshort/buytocover logic.

To produce a trading event, one of the four keywords needs to be added:
Buy
Sell
SellShort
BuyToCover

There are 4 methods:
Buy next bar at market ;
Buy next bar ‘myprice’ stop ;
Buy next bar ‘myprice’ limit ;
Buy this bar at close ;

If you were plotting stop points, the variable defining the stop point can be used in the sell statement. For example:

Sell next bar at StopPoint stop ;

A challenge in converting an indicator to a strategy is defining the conditions when to take an indicator “signal” and actually issue an entry / exit order. There are many approaches one could take.

For example, this indicator plots a blue dot as soon as it detects “bullish” MACD divergence (defined as lower low in prices, but higher low in MACD histogram). You could code the strategy, for example, to take a long entry immediately on the first bar that shows the divergence – i.e., when the first blue dot plots, you go long. An alternative to this might be to wait until pivot lows or pivot highs form (i.e., the conditions which generate the drawing of the trendlines) and take entries on this event. How you want to interpret the indicator and determine which “signals” to take is really up to you.

更新于 2020年6月5日

这篇文章对您有帮助吗?

相关的话题

留言评论