[Amibroker新手日誌][函式]停損ApplyStop-實作範例

[Amibroker新手日誌][函式]停損ApplyStop-實作範例

測試檔:台積電日線20021203-20121218

測試原則: 先測簡單的策略,再測難的策略

[注意]: 如果你是用收盤價交易,且交易Zero Deplays,請打開Settings中的Activate Stops Immediately(停損利單優先)

            If you are trading at the Close with zero delays be sure to unmark "Activate Stops Immediately" in Settings.

 

 

容易混淆概念
ActivateStopsImmediately & AllowSameBarExit & ReEntryDelay

ActivateStopsImmediately = True : 停損單優先權 > 一般訊號

AllowSameBarExit(Entry) = True : 同棒進出

ReEntryDelay =0 : 不延遲進場; ReEntryDelay =n :延遲n棒後進場

 

 

 

不停損stopModeDisable:


SetPositionSize( 1000, spsShares );
SetOption("ActivateStopsImmediately", False ); 
SetOption("AllowSameBarExit", False ); 

Buy=1;   //Never Sell , to check when dose the Stop will be triggered
Sell=0;

ApplyStop(stopTypeLoss, stopModeDisable, amount=0, ExitAtStop =0,Volatile =False, ReEntryDelay =0);

01

圖一

買進之後就沒出場了

 

 

5%停損出場


SetPositionSize( 1000, spsShares );
SetOption("ActivateStopsImmediately", True); //Stop單優先權高於一般訊號 
SetOption("AllowSameBarExit", False ); //不可同棒進出     

Buy=1;   //Never Sell , to check when dose the Stop Order will be triggered
Sell=0;

ApplyStop(stopTypeLoss,stopModePercent, amount=5, ExitAtStop =0,Volatile =False, ReEntryDelay =0);

02 

圖二

PS:我以為我眼瞎了,怎麼只有5筆。的確是的,因為跌破5%時下一個棒子又撿,等於撿在相對低點,就更難跌破。

 

NBarStop: 持有0Bar便出場=>結果都最少持有1棒

SetPositionSize( 1000, spsShares );
SetOption("ActivateStopsImmediately", True ); //Stop單優先權高於一般訊號
SetOption("AllowSameBarExit", True );         //同棒進出   

Buy=1;   //Never Sell , to check when dose the Stop will be triggered
Sell=0;

ApplyStop(stopTypeNBar ,stopModeBars, amount=0, ExitAtStop =0,Volatile =False, ReEntryDelay =0);

03

圖三

明明設定0Bar,結果都持有1Bar出場? 因為買了馬上出,也是持有1Bar

那設1 Bar呢? 結果就會是持有2Bar了。因為12/3日進,12/4日出,算是2支Bar

 

 

ActivateStopsImmediately= False, ReEntryDelay =0 ,AllowSameBarExit= True 的奇怪現象(呼應[注意]中的第1點)


SetPositionSize( 1000, spsShares );
SetOption("ActivateStopsImmediately", False ); 
SetOption("AllowSameBarExit", True ); 

Buy=1;   //Never Sell , to check when dose the Stop will be triggered
Sell=0;

ApplyStop(stopTypeNBar ,stopModeBars, amount=0, ExitAtStop =0,Volatile =False, ReEntryDelay =0);
04
圖四
程式上有衝突,ActivateStopsImmediately = False,但是ReEntryDelay =0,結果Run出來怪怪的,有的持有1Bar有的持有2Bar。
 
將ReEntryDelay 改成1,結果如下,就正常許多了。
05
圖五

或是ActivateStopsImmediately改成True,結果如[NBarStop]範例圖三相同

 

ActivateStopsImmediately= True, ReEntryDelay =0 ,AllowSameBarExit= True


SetPositionSize( 1000, spsShares );
SetOption("ActivateStopsImmediately", True ); //Stop單優先於一般訊號 
SetOption("AllowSameBarExit", False ); //不可同棒進出

 
Buy=1;   //Never Sell , to check when dose the Stop will be triggered
Sell=0;

ApplyStop(stopTypeNBar ,stopModeBars, amount=0, ExitAtStop =0,Volatile =False, ReEntryDelay =0);

06

圖六

與圖三比較後發現,在這些條件之下,設AllowSameBarExit是沒有影響的。

 

ExitAtStop

ExitAtStop = 0 - 交易價格停損 (如果你固定用收盤價交易,且設定ExitAtStop = 0,只有在收盤價時,才會檢查是否需要停損,若需要也會在收盤時執行)

                          uses SellPrice/CoverPrice variables in backtestRegular mode only, in other modes it uses trade prices from the Settings dialog (not overridable via SellPrice/CoverPrice)

ExitAtStop = 1 - 檢查最高與最低價格,若價格觸及停損價,就會用停損價格在當天(同棒)出場。

ExitAtStop = 2 - 檢查最高與最低價格,若價格觸及停損價,就會用交易價格在隔天(下一棒)出場。

(ps: 通常訊號分析的價格,就是你的交易價格)

 

volatile

False:  買入時就決定了停損的價位,且停損價位不會改變

TRUE: 停損價格會改變(支援Chandelier exit) ,而且此功能只在 backtestRegular mode中有用

 

 

 

 

 

http://www.plurk.com/SophieQ