COCO研究院

 找回密碼
 註冊
搜索
樓主: jacklcl

關於移動停損

[複製鏈接]
發表於 14-8-7 23:59 | 顯示全部樓層
jack, 我想問下, 這段時間你測試, 於實測中觀察的hsi滑價大概是多少? 3點是否還合理?
發表於 14-8-8 00:11 | 顯示全部樓層
orangelam 發表於 15-9-27 18:38
Thanks for telling me this! I am much more cleared now!
但想想下, 以下兩個情況又感到混亂
Assume my s ...

我雖然未能很理解你的問題, 但我試試分享一些看法, in backtest, "unless you change your buy/sell/short/cover price," you are actually testing your strategy in end of every bar, either using the closeprice of this bar or openprice of next bar.

however, in realtime, you can enter the market in anytime you want. in order to simulate it, one of the way(if not the only one) is to "reset" your buy/sell/short/cover price in backtest mode and simulate the realtime mode.

so, in realtime mode, apart from the openprice, all the other 3 price(H/L/C) might keep changing during the bar is being built in the time interval(1min, 5min...etc). so any strategy base on the H/L/C will be triggered once the condition is fullfill, no matter when you are at during the bar is being built - unless you tell the system not to do so until the bar is finished(that's why there is so so so much discussion about "barcomplete", google it and you will have the information.)

that's all I have learned and understood so far, hope it is useful to you.
 樓主| 發表於 14-8-8 09:48 | 顯示全部樓層
orangelam 發表於 14-8-7 11:38
Thanks for telling me this! I am much more cleared now!
但想想下, 以下兩個情況又感到混亂
Assume my s ...

如果你的條件是用O/H/L去trigger但你想用close price去成交
則你在live trading中你需要用語法去限制當根BAR走完才入市
例如K大之前提過的barcomplete語法
barcomplete = BarIndex() < LastValue(BarIndex());
如果你沒有以上語法
那你Case1如用Open去trigger的話
則它一開就會掉市價單出去, 沒滑價則buyprice會是Openprice, 你那個N+0.2就沒意義的
因為它在N+0.2之前已掉單
如果你用Close來trigger, 則以上barcomplete的語法必須加上, 因為live trading里C會不停變動直至那根BAR跑完
限制了當根BAR跑完才入市的話, 則buyprice會是close price

你case 2是用H/L trigger, 如果你想在當根BAR跑完才入市
上面那句barcomplete需加上你的buy/short條件, 否則它會在live trading中只要一符合條件則會掉單出去

以上是我的理解

 樓主| 發表於 14-8-8 09:52 | 顯示全部樓層
osdak 發表於 14-8-7 23:59
jack, 我想問下, 這段時間你測試, 於實測中觀察的hsi滑價大概是多少? 3點是否還合理? ...

3點應該合理吧
我一般在3點以內
我還在修改策略
之前我出場那裡有些不合理的地方
現在有IB Paper account真的很方便
有時真的要放在live trading中跑一下才看到一些陷阱

發表於 14-8-8 19:07 來自手機 | 顯示全部樓層
jacklcl 發表於 14-8-8 09:52
3點應該合理吧
我一般在3點以內
我還在修改策略

介不介意說那裡不合理?i am gonna use it for back test lei....
 樓主| 發表於 14-8-8 19:53 來自手機 | 顯示全部樓層
本帖最後由 jacklcl 於 14-8-8 19:55 編輯
osdak 發表於 14-8-8 19:07
介不介意說那裡不合理?i am gonna use it for back test lei....


之前我在那個出場條件加多一樣
L [ i ] <= trailstopB and  H [ i ] < upper [ i ]
那就會有問題了
發表於 14-8-8 20:51 | 顯示全部樓層
jacklcl 發表於 14-8-8 19:53
之前我在那個出場條件加多一樣
L [ i ]

那原本的移動停損的code還是ok? 我只想有一個可以移動停損的就可以.
 樓主| 發表於 14-8-8 21:05 | 顯示全部樓層
osdak 發表於 14-8-8 20:51
那原本的移動停損的code還是ok? 我只想有一個可以移動停損的就可以.
...

原本那個沒問題的
我現在用這個跑live trading出場點基本上是一樣
發表於 14-8-8 21:49 | 顯示全部樓層
jacklcl 發表於 14-8-8 21:05
原本那個沒問題的
我現在用這個跑live trading出場點基本上是一樣

我還沒試, 但我想請教, 用loop於 live trading 中, 是可以原封不動的放於code中就可以? 是否放在Ib contoller 那段之前就ok?
 樓主| 發表於 14-8-8 23:12 | 顯示全部樓層
osdak 發表於 14-8-8 21:49
我還沒試, 但我想請教, 用loop於 live trading 中, 是可以原封不動的放於code中就可以? 是否放在Ib conto ...

是, 直接放進code中
我習慣將IB那段放在最下面
發表於 14-8-8 23:52 | 顯示全部樓層
J大, 小弟愚昧不太懂你說甚麼, 可能我連程序流程也未攪懂, 我試試用trail stop 那個例子說說吧,
Buy =          sentence(1)
Sell = 0;        sentence(2)
trailARRAY = Null;
trailstop = 0;

for( i = 1; i < BarCount; i++ )
{
   if( trailstop == 0 AND Buy[ i ] )                  (sentence 3)
   {
      trailstop = High[ i ] * stoplevel;
   }
   else Buy[ i ] = 0;
   if( trailstop > 0 AND Low[ i ] < trailstop )      (sentence 4)

我想肯定一下自己的understanding. 因為我只試諣做back test, 以我的理解(假設在loop 1mins data), 在做back test 時, sentence(1) and sentence(2) 會先loop through 整個back test period (我估計是looping embed在buy, sell command 中), 然後便造出了一條buy, 一條sell 的array, 數值為1 or 0. 而我在setting-->trades set buyprice 及sell price = close 的話, 除了這兩條array 外, 還有另外兩條buyprice 及sellprice會整了出來, 裡面全都是該分鍾的close price. 走到for loop 時, buy arrary 便重新loop 一次來去檢查應否trail stop. 我理解對嗎?
但我攪不懂的是,

Case 1
假設sentence(1)是: BUY=(Triggered by Close price only)
當t=n 時, 因為是整數時間, 由t=n-1 to t=n 的close price 會出現. 所以buy(n)有機會等於1. 如果假設現在buy(n)=1(假設現在trigger 了), 當程序走到 sentence3時, sentence3的buy(n) 便是 1.
但在t=n+0.2 時, 而現在程序執行到sentence1, 因為close price 未生成, 那sentence 1中的buy(n+1,is this subscript “n+1”?)會是甚麼? 而sentence 3 中的buy(n+1) 又會是甚麼?

Case 2:
假設sentence(1)是: BUY=(Triggered by H and L price only)
在t=n+0.2 時, 而現在程序執行到sentence1, 因為H/L 巳經生成了, 所以BUY可以trigger, 假苦成功trigger了, 那這一刻的buyprice在那裡設定?
J 大, 有冇一個example 可以令我明白live trading中程序是怎麼走呢?
我知識還停留在back test的層次, 很想把這個弄明白. 真的不好意思
 樓主| 發表於 14-8-9 01:12 | 顯示全部樓層
本帖最後由 jacklcl 於 14-8-9 01:20 編輯
orangelam 發表於 14-8-8 23:52
J大, 小弟愚昧不太懂你說甚麼, 可能我連程序流程也未攪懂, 我試試用trail stop 那個例子說說吧,
Buy =      ...


你第一段的意思, 我自己也是這樣理解

至於你case 1 and case 2,

你case 1里的 n + 0.2 是否指buy = 1 (buy was triggered at N bar) 後過了0.2分鐘?
如果是的話, 它不會有buyprice的, 最後那句else Buy[ i ] = 0已filter了之後的buy signal
而當你在N BAR時buy = 1, 這時那個trailstop會開始計算直至你的L < trailstop而trigger sell signal

你case 2如果用H/L去trigger signals的話
那個buyprice/shortprice會是你那個條件的價件
例如breakout strategy, 突破前3條BAR高位則BUY = 1
Upper = HHV(Ref(H, -1), 3);
Buy = H >= Upper;
那你模擬的buyprice則會是這個breakout point
BuyPrice = Upper;


其實以我理解那個trailstop的loop是這樣的


trailARRAY = Null; // 這個是for之後用來plot的
trailstop = 0; //set做0有2個用途, 第1, 如是0, 即表示現在沒position, 第2, 如不等於0則它會用來store stoplevel

for( i = 1; i < BarCount; i++ )
{
    if( trailstop == 0 AND Buy[ i ] )  // 如果trailstop是0的話則代表沒position, 而此時Buy signal = 1則assign value to trailstop        
        
    {
       trailstop = High[ i ] * stoplevel; // assign value to trailstop and will keep calculating the trailstop     }

    else Buy[ i ] = 0; // 如trailstop不等於0或Buy = 1, 即代表已有position, then filter out subsequent signals

    if( trailstop > 0 AND Low[ i ] < trailstop ) // trailstop > 0 即表示有position及trailstop已store stop level, 而 當L < trailstop 時則trigger sell  


其實你可以用bar replay去run 1次
不過我都是有了IB paper account後才更明白live trading的運作

發表於 14-8-9 03:33 | 顯示全部樓層
對於trail stop的code, 我跟你的理解是一樣的, 在回測時, 假設我用1min data, 因為是做回測, 明確地i=1時指第一條bar即第一分鍾, n=2時指第二條bar...
所以那個for loop 也十分明確地在loop 每條bar, 但back test 是失去了一分鍾內的data的(前題是我只有1min data), 所以在live trading 時, 如果每秒refresh, 我便想到case 1 and case 2的問題,

你case 1里的 n + 0.2 是否指buy = 1 (buy was triggered at N bar) 後過了0.2分鐘?
對, 我假設在分鍾n 時, 這個buy 叫做buy[n]=1, 如果現在到了時間n+0.2(就當作是上一下refresh 在t=n, 下一下就refresh 在t=n+0.2 吧),在這個t=n+0.2 時, 我想知道如果BUY=(Triggered by Close price only)
因為如果close 只在整數分鍾才生成, 那這個buy[n+0.2]會是甚麼? 例如buy trigger by MA(10)>23000, 而這個MA 是根據過去十分鍾的close 來生成, 但在t=n+0.2 時, 因為close 還未出現, 何來MA(10)?

你case 2如果用H/L去trigger signals的話
那個buyprice/shortprice會是你那個條件的價件
例如breakout strategy, 突破前3條BAR高位則BUY = 1
Upper = HHV(Ref(H, -1), 3);
Buy = H >= Upper;
那你模擬的buyprice則會是這個breakout point
BuyPrice = Upper;
j 大意思是不是指, 因為如果buy/short trigger 是根據H/L, 而H,L在每秒也會生成的, 所以唯有手動設定一個Buyprice? (我混亂的是, 在setting--> trades set 了buy at close, 但你用buyprice=upper, 不會起衝突嗎?)

如果是的話, 它不會有buyprice的, 最後那句else Buy[ i ] = 0已filter了之後的buy signal
對, 當我們做back test 時, 開頭的buy, sell 巳經loop through 了所有bar一次, 例如buy arrary 是
(0,0,1,0,0,1) 當走到下面的for loop 時, 再由第一粒buy loop到第六粒buy, 如果第三粒的buy=1 在for loop走到第六粒時還未被settle position, 第六粒的buy 便會強制變成0了. 應該是這樣吧...

IB paper account 應該是在live trading 時可以實時import 數據入amibroker, 然後再用自己的strategies 模擬trade. 但有免費試用版嗎? 應該都冇的...

感謝j大, 現在清晰了很多, 也肯定了一些understanding  




 樓主| 發表於 14-8-9 10:21 | 顯示全部樓層
本帖最後由 jacklcl 於 14-8-9 10:31 編輯
orangelam 發表於 14-8-9 03:33
對於trail stop的code, 我跟你的理解是一樣的, 在回測時, 假設我用1min data, 因為是做回測, 明確地i=1時指 ...


我明白你擔心什麼了
沒錯, C在整數分鐘才出現, 不過這是在backtest時才會這樣
但在live trading時, Close時每秒都出現而且在那分鐘會不停變動
而你的AFL只要有data進來它則會run一次的
所以如果你的策略用C的話, 它會在這一秒符合條件買入
但在下一秒又會不符合
那你之後就會發現backtest時沒這個trade
但live trading會出現, 所以才說要加barcomplete去限制當根BAR跑完才入場

而在live trading中, 由於那些variable會每秒去run一次
所以在K大那條thread才會有這麼多討論
real time裡需用到static variable這些functions
再配合newbar去限制那分鐘的BAR只下單一次
以下這篇應該會讓你更明白

http://www.amibroker.org/userkb/ ... bles-in-rt-systems/

Buyprice/shortprice我習慣在AFL裡set的
那麼它會override你在setting裡的設定
 樓主| 發表於 14-8-9 10:24 | 顯示全部樓層
本帖最後由 jacklcl 於 14-8-9 10:40 編輯
orangelam 發表於 14-8-9 03:33
對於trail stop的code, 我跟你的理解是一樣的, 在回測時, 假設我用1min data, 因為是做回測, 明確地i=1時指 ...


IB是有實時數據, 而它是snapshot報價的, 好像廿幾元一個月
paper account是免費的, 只要你去IB開戶口然後去申請就可以了
開戶口亦很快, 我只用了3天就已開了
你上網申請完後, 香港IB會聯絡你去交些身份証明文件
然後很快會開通的
之後你存USD10000去activate個account就可以
您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則

手機版|Archiver|站長信箱|廣告洽詢|COCO研究院

GMT+8, 24-6-6 12:33

Powered by Discuz! X3.4

Copyright © 2001-2023, Tencent Cloud.

快速回復 返回頂部 返回列表
理財討論網站 | AI繪圖AI超擬真美女AI beauty AI Stable DiffusionAI正妹AI Lookbook