自訂函數

  •   968 
  • 最後發表   帥哥元  2016 八月 01
帥哥元 發文於   2016/07/28

自訂函數 tset,程式碼如下

input: i(numericref),j(numericref),n(numericref);

variable: EMAPeriod(8);

i=0;

if close < EMA(Close, EMAPeriod) then i=1;

j = EMA(Close, EMAPeriod);

n = close;

==========================

自訂指標,程式碼如下

variable: EMAPeriod(8),i(0),j(0),n(0);

 

test(i,j,n);

plot1(i,"close<EMA8"); 

plot2(j,"EMA8");

plot3(n,"收盤價");

=======================

變數j的值沒錯,如下圖

 

但是另一個自訂函數及指標,卻出現錯誤,不知原因為何?

自訂函數AAA,程式碼如下

input: TV(numericref),_BuyOPValue(numericref),EMA8(numericref); 

variable: EMAPeriod(8); 

EMA8 = EMA(Close, EMAPeriod);

if TV = 2 then

   begin

     if Close < EMA8 then

   begin

 _BuyOPValue = 1;

end

else _BuyOPValue = -1;

   end;   

====================================================

自訂指標,程式碼如下

variable: TV(0), _BuyOPValue(0), EMA8(0);

variable: TowerLength(4), TowerValue(0);

variable: i(0);

variable: EMAPeriod(8);

Tower(Close, TowerLength, TowerValue); 

if TowerValue = 1 then 

   begin

TV = 2;

AAA(TV, _BuyOPValue, EMA8);  

     plot1(_BuyOPValue);

plot2(EMA8,"EMA8");

   end;

===========================================

自訂函數Tower,程式碼如下

input: price(numericseries), TowerLength(numericsimple);

input: TowerValue(numericref);

variable: _Index(0), HV(0), LV(0), i(0);

TowerValue = 0;

 HV = High[1]; 

LV = Low[1];   

for _Index = 2 to TowerLength

begin

  if HV <= High[_Index] then HV = High[_Index];

  if LV >= Low[_Index]  then LV = Low[_Index];

end;   

 

if Close >= HV then TowerValue = 1

else if Close <= LV then TowerValue = -1;

 

if TowerValue = 0 then  

   begin

       i = 1;

       while i < 100000

         Begin

           if TowerValue[i] <> 0 then Break;  

           i = i + 1;   

         End;

  TowerValue = TowerValue[i]; 

   end;   

===================================================

結果EMA8的值有錯誤

排序方式: 標準 | 最新
XQ小幫手 發文於   2016/07/28

Hi 帥哥元:

 

小編稍微看了一下,

發現會出現此問題的原因是因為,

您的自訂函數 Tower 有問題,

請您確認一下此函數腳本跑出來的結果是否符合您的預期,謝謝您,

 

小編將您的第二個指標腳本,註解掉使用到 Tower 函數的部分,跑出來的結果就正常了

variable: TV(0), _BuyOPValue(0), EMA8(0);

variable: TowerLength(4), TowerValue(0);

variable: i(0);

variable: EMAPeriod(8);

{
Tower(Close, TowerLength, TowerValue); 

if TowerValue = 1 then 

   begin
}

TV = 2;

AAA(TV, _BuyOPValue, EMA8);  

     plot1(_BuyOPValue);

plot2(EMA8,"EMA8");

//   end;

 

帥哥元 發文於   2016/07/29

可是,我如果引用EMA的部分,不要寫成函數,而是將所有程式碼都寫在指標腳本內,執行狀況就正常

自訂指標CCC,程式碼如下,而自訂函數Tower不做任何異動

variable: _BuyOPValue(0);

variable: TowerLength(4), TowerValue(0);

variable: EMAPeriod(8),EMA8(0);

 

_BuyOPValue=0;

TowerValue=0;

EMA8=0;

 

EMA8 = EMA(Close, EMAPeriod);

Tower(Close, TowerLength, TowerValue); 

 

if TowerValue = 1 then 

   begin

      if Close < EMA(Close, EMAPeriod) then

         begin

            _BuyOPValue = -2500;

         end

      else _BuyOPValue = 2500;

   end;

 

plot1(_BuyOPValue,"_BuyOPValue");

plot2(EMA8,"EMA8");

plot3(Close,"Close");

plot4(TowerValue,"TowerValue");

 

XQ小幫手 發文於   2016/08/01

不好意思,小編仔細查看您的腳本後,發現

您文章中的自訂指標 ( 不是CCC指標腳本 ) 

 

variable: TV(0), _BuyOPValue(0), EMA8(0);

variable: TowerLength(4), TowerValue(0);

variable: i(0);

variable: EMAPeriod(8);

Tower(Close, TowerLength, TowerValue); 

 

if TowerValue = 1 then 

begin

   TV = 2;

   AAA(TV, _BuyOPValue, EMA8);

   plot1(_BuyOPValue);

   plot2(EMA8,"EMA8");

end;

 

在此腳本程式碼中,

如果要計算 EMA8 的數值,必須要在條件為 TowerValue = 1 的情況下,

才能運算 EMA8,

 

而您的 Tower 函數腳本中,有以下程式碼

if Close >= HV then TowerValue = 1
else if Close <= LV then TowerValue = -1;

if TowerValue = 0 then  
begin
  i = 1;
  while i < 100000
  Begin
     if TowerValue[i] <> 0 then Break; 
     i = i + 1;   
  End;
  TowerValue = TowerValue[i]; 
end; 

因此 TowerValue 可能不為 1 ( 有可能是 -1 )

導致在 TowerValue = -1 的狀況下,不會計算 EMA8 的數值,

此時 EMA8 為 前期 TowerValue = 1 的數值,

 

而這個結果,將造成 EMA 計算發生偏誤,

EMA 計算公式如連結網址,擷取部分資訊如下,

當期均價 = (2/(n+1))當期價格 + (n-1)/(n+1)前期均價

 

由於前期均價,在 TowerValue = -1 的狀況下,會發生偏誤,

因此用您的文章腳本計算 EMA 的數值,會與內建的EMA指標不一樣,

 

以上報告,謝謝您。

 

 

 

發表回覆
Close