2013-04-19 37 views
0

研究在以下兩個鏈接中使用的量化方法後,我試圖創建一個簡單的交易策略模板(如下所示代碼)可以在R上矢量更好的速度Vs的環型結構。我無法進行矢量化,因爲可變狀態必須維持並建立在如下基礎之上:R中矢量化簡單循環的貿易體系?

1)我使用的信號在長短期並不互相排斥(如在簡單的MA交叉系統中)。

2)一旦被觸發,該信號可以漫步,直到它到達一個相對的指示(諸如RSI去短高於80,去長低於20型系統)。

3)位置保持多個週期,因此它不是在每個信號輸入或信號錯誤後輸入的情況(我希望只能在停止和反向或SAR系統中輸入一次)。

我認爲這是一個簡單的例子系統,但它比這裏列出的例子更復雜一點:

http://blog.fosstrading.com/2011/03/how-to-backtest-strategy-in-r.html

Cumulative Return in Trading Strategy Test

系統邏輯總結:系統啓動平板然後進入長(短)在詢問(BID)的價格時zscore低於(高於)-2(2)。該系統跟蹤的性能統計數據,如「交易」,「勝」,關閉P & L(其他爲簡單起見省略)。該系統在系統運行後還可以保持運行的「平等」。

# assume vectors bid, ask, and zscore containing those price series respectively 
# pos = current position where 1 == long, -1 == short, 0 == flat 
# entryP = entry price, pnl = open pnl, cpnl = closed pnl 
pos = 0; entryP = 0.0; pnl = 0; cpnl = 0; trades = 0; wins = 0 
ub = length(bid) 
equity = rep(0, ub) 
for (i in 10000:ub) { 
pnl = 0 
if (pos > 0) pnl = bid[i] - entryP 
if (pos < 0) pnl = entryP - ask[i] 
if (zscore[i] > 2.0 && pos != -1) { # go short 
    if (pos > 0) { # exit long and record pnl 
     cpnl = cpnl + pnl 
     if (pnl > 0) wins = wins + 1 
     trades = trades + 1 
     pnl = 0 
    } 
    pos = -1 
    entryP = bid[i] 
} else if (zscore[i] < -2.0 && pos != 1) { # go long 
    if (pos < 0) { # exit short and record pnl 
     cpnl = cpnl + pnl 
     if (pnl > 0) wins = wins + 1 
     trades = trades + 1 
     pnl = 0 
    } 
    pos = 1 
     entryP = ask[i] 
    } 

    equity[i] = cpnl + pnl 
} 
# assume close-out of final position 
cpnl = cpnl + pnl 
if (pnl > 0) wins = wins + 1 
if (pos != 0) trades = trades + 1 
# plot equity chart and report performance stats 
plot(equity, t='l', lwd=3) 
cpnl;trades; cpnl/trades; wins/trades 

是否有可能向量化這種簡單的基於循環均值迴歸交易的R系統?

回答

3

「我無法進行矢量化,因爲必須保持可變狀態」

總結一下。如果任何迭代中的結果取決於先前的迭代,則無法避免循環。

+0

這聽起來當你說出這樣說,這非常明顯。感謝您的回答,並感謝您不要投票! –