每个时间段/条均以开仓买入和平仓卖出

时间:2019-11-03 22:36:05

标签: r quantstrat

我正在尝试使用quantstrat建立(不良)策略。数据如下:

               score      open      high       low     close       prc
2016-10-19 0.7708707  68.72810  68.84724  68.20187  68.40045  64.12434
2016-10-20 0.8758272  68.48981  68.58910  68.08272  68.24159  63.97541
2016-10-21 0.8446031 122.50000 127.85000 122.31000 127.50000 127.50000
2016-10-24 0.8898701  12.56715  12.63091  12.51498  12.56715  11.71586
2016-10-25 0.9022833  68.51960  69.37349  68.50966  68.86711  64.56183
2016-10-26 0.7472578  34.77669  34.94542  34.47895  34.65759  32.25258

以下是该策略的外观:

enter image description here

该策略仅进行以下交易(我希望它每天进行一次交易):

[1] "2016-10-21 00:00:00 data 10 @ 122.5"
[1] "2016-10-31 00:00:00 data -10 @ 124.870003"
[1] "2016-10-31 00:00:00 data 10 @ 126.849998"
[1] "2016-11-16 00:00:00 data -10 @ 764.47998"
[1] "2016-11-16 00:00:00 data 10 @ 755.200012"
[1] "2016-12-05 00:00:00 data -10 @ 59.6361906377958"
[1] "2016-12-05 00:00:00 data 10 @ 59.7356194196591"
[1] "2016-12-15 00:00:00 data -10 @ 62.580002"
[1] "2016-12-15 00:00:00 data 10 @ 62.700001"
[1] "2016-12-20 00:00:00 data -10 @ 105.169998"
[1] "2016-12-20 00:00:00 data 10 @ 104.580002"

但是,我想要做的策略是每天在市场开盘时买入,在市场关闭时卖出。我试图通过创建信号来找到一种方法,当开盘时得分> 0.8时买入,当收盘时得分> 0.8时卖出(这每天都发生-几乎是每天发生),使用:

threshold = 0.8,
relationship = "gt",

signals中都做多和做空。然后我添加了:

prefer = "open", 

prefer = "close",

add.ruleEnterLONGExit2Short

如何在同一天/条的开市价买入和收盘价卖出?也就是说,我每天都想在开盘价买入并在收盘价卖出(我不关心score,只是这个策略每天都会进行交易并在一天结束时将其关闭,无论score

非常感谢您的帮助。

代码:

library(quantstrat)

Sys.setenv(TZ = "UTC")
currency('USD')

start_date <- "2016-01-01"
end_date <- "2017-01-01"

init_date <- as.Date(start_date) - 1  
init_equity <- 1e4 
adjustment <- TRUE

.orderqty <- 10
.txnfees <- -10


stock("data", currency = "USD", multiplier = 1)

portfolio.st <- "Port.data"
account.st <- "Acct.data"
strategy.st <- "Strat.data"

rm.strat(portfolio.st)
rm.strat(account.st)

initPortf(name = portfolio.st,
          symbols = "data",
          initDate = init_date)

initAcct(name = account.st,
         portfolios = portfolio.st,
         initDate = init_date,
         initEq = init_equity)

initOrders(portfolio = portfolio.st,
           symbols = "data",
           initDate = init_date)

strategy(strategy.st, store = TRUE)


add.signal(strategy = strategy.st,
           name = "sigThreshold",
           arguments = list(
             threshold = 0.8,
             column = "score",
             relationship = "gt",
             cross = TRUE),
           label = "longSig")


add.signal(strategy = strategy.st,
           name = "sigThreshold",
           arguments = list(
             threshold = 0.8,
             column = "score",
             relationship = "gt",
             cross = TRUE),
           label = "exitLongSig")



add.rule(strategy = strategy.st,
         name = "ruleSignal",
         arguments = list(sigcol = "longSig",
                          sigval = 1,
                          orderqty = .orderqty,
                          ordertype = "market",
                          orderside = "long",
                          #osFUN = osMaxPos,
                          prefer = "open",  
                          TxnFees = .txnfees, 
                          replace = FALSE),
         type = "enter",
         label = "EnterLONG")

add.rule(strategy.st, 
         name = "ruleSignal", 
         arguments = list(sigcol = "exitLongSig", 
                          sigval = 1, 
                          orderqty = "all",
                          ordertype = "market", 
                          orderside = "long",
                          prefer = "close",
                          TxnFees = .txnfees, 
                          replace = TRUE), 
         type = "exit", 
         label = "Exit2SHORT")


applyStrategy(strategy.st, portfolios = portfolio.st)

updatePortf(portfolio.st)
updateAcct(account.st)
updateEndEq(account.st)

chart.Posn(portfolio.st, Symbol = "data")

数据:

    data <- structure(c(0.770870685577393, 0.875827193260193, 0.844603061676025, 
0.88987010717392, 0.902283310890198, 0.747257828712463, 0.730861663818359, 
0.876745045185089, 0.879817068576813, 0.930711388587952, 0.866301774978638, 
0.888765215873718, 0.916693985462189, 0.914494633674622, 0.890864014625549, 
0.924996256828308, 0.840009570121765, 0.93012946844101, 0.766247272491455, 
0.924087762832642, 0.833678483963013, 0.910132944583893, 0.931937158107758, 
0.858937621116638, 0.846807181835175, 0.931219100952148, 0.879955530166626, 
0.920356154441833, 0.917216777801514, 0.882693707942963, 0.794553339481354, 
0.918832361698151, 0.905784606933594, 0.858742594718933, 0.862590491771698, 
0.821596741676331, 0.829982817173004, 0.886534750461578, 0.790071725845337, 
0.935553967952728, 0.748785376548767, 0.793714165687561, 0.881313323974609, 
0.85876339673996, 0.895134150981903, 0.920947432518005, 0.929289281368256, 
0.943412184715271, 0.919990122318268, 0.889334559440613, 0.870256721973419, 
68.728102193745, 68.489809694005, 122.5, 12.5671511950884, 68.5195955118023, 
34.776694618884, 69.1053988171489, 69.3039825195569, 126.849998, 
69.4827033837024, 68.7082403483763, 69.1351846349462, 69.2146181159093, 
771.640015, 783.400024, 779.940002, 70.743675390937, 735.72998, 
107.709999, 25.0588399969803, 755.200012, 159.220001, 60.779999, 
160.690002, 111.949997, 111.360001, 60.299999, 60.34, 60.650002, 
120.32, 60.7994861082626, 59.080002, 59.7356194196591, 69.5124892014996, 
69.93, 70.480003, 119.220001, 166.720001, 36.009998, 36.700001, 
62.700001, 36.720001, 758.890015, 104.580002, 105.639999, 106.82, 
106.379997, 112.059998, 119.599998, 36.509998, 36.790001, 68.8472444720402, 
68.5890980700811, 127.849998, 12.6309146392119, 69.3734850778357, 
34.9454151771917, 69.5025582788153, 69.711064960758, 126.900002, 
69.5621299144097, 69.472772461018, 69.5819917597784, 69.4429866432208, 
787.72998, 795.632996, 791.22699, 71.577702118708, 743.26001, 
107.809998, 25.3215106655513, 766.359985, 159.929993, 61.139999, 
163, 112.419998, 111.510002, 60.529999, 61.02, 61.41, 121.790001, 
61.9926036511201, 59.470001, 60.3818915877515, 69.8897858249035, 
70.650002, 70.900002, 119.940002, 166.789993, 36.889999, 36.889999, 
63.150002, 36.869999, 770.5, 106.129997, 105.949997, 108.870003, 
109.839996, 117.360001, 119.93, 36.720001, 36.799999, 68.2018695310991, 
68.082720302548, 122.309998, 12.5149819204539, 68.5096645891179, 
34.47894994676, 68.8968921352062, 69.234479961278, 124.400002, 
68.6486687127819, 68.7082403483763, 68.8571753947246, 68.6387377900975, 
770.940002, 780.190002, 771.669983, 70.4954519685126, 728.900024, 
104.080002, 24.8749693327756, 750.51001, 158.850006, 60.299999, 
160.369995, 111.400002, 110.330002, 60.130001, 60.209999, 60.52, 
117.949997, 60.5708005065378, 58.799999, 59.5566511916697, 69.0061114339666, 
69.900002, 70.32, 118.949997, 165.070007, 35.93, 36.459999, 62.299999, 
36.09, 756.159973, 104.120003, 103.709999, 106.529999, 106.269997, 
111.220001, 108.690002, 36.43, 36.200001, 68.4004452903575, 68.2415862715807, 
127.5, 12.5671511950884, 68.8671073103026, 34.6575933755835, 
69.3337683373541, 69.4926273561309, 124.870003, 68.8075356747082, 
68.9564637708006, 69.1351846349462, 68.6685315510442, 784.929993, 
790.51001, 785.309998, 70.8826795146009, 739.01001, 105.709999, 
25.1901753312658, 764.47998, 159.800003, 60.349998, 162.770004, 
111.800003, 111.230003, 60.529999, 60.610001, 61.09, 118.419998, 
61.7142106183392, 59.25, 59.6361906377958, 69.860001, 70.599998, 
70.339996, 119.68, 165.5, 36.799999, 36.549999, 62.580002, 36.310001, 
766, 105.169998, 105.830002, 107.110001, 109.779999, 117.32, 
109.25, 36.66, 36.27, 64.124344, 63.97541, 127.5, 11.715859, 
64.561829, 32.252579, 64.999336, 65.148254, 124.870003, 64.505997, 
64.645615, 64.813164, 64.375679, 784.929993, 790.51001, 785.309998, 
66.451408, 739.01001, 101.284096, 23.15066, 764.47998, 142.099106, 
57.376446, 144.740112, 107.119118, 106.572975, 57.547577, 57.623638, 
58.07999, 118.419998, 56.1194, 56.330643, 54.229771, 65.492661, 
66.186394, 65.94265, 119.68, 147.167709, 34.246315, 34.01366, 
59.496574, 33.790321, 766, 104.140709, 104.794243, 106.061722, 
108.705605, 116.171799, 108.180786, 34.116039, 33.753101), .Dim = c(51L, 
6L), .Dimnames = list(NULL, c("score", "open", "high", "low", 
"close", "prc")), index = structure(c(1476835200, 1476921600, 
1477008000, 1477267200, 1477353600, 1477440000, 1477526400, 1477612800, 
1477872000, 1477958400, 1478044800, 1478131200, 1478217600, 1478476800, 
1478563200, 1478649600, 1478736000, 1478822400, 1479081600, 1479168000, 
1479254400, 1479340800, 1479427200, 1479686400, 1479772800, 1479859200, 
1480032000, 1480291200, 1480377600, 1480464000, 1480550400, 1480636800, 
1480896000, 1480982400, 1481068800, 1481155200, 1481241600, 1481500800, 
1481587200, 1481673600, 1481760000, 1481846400, 1482105600, 1482192000, 
1482278400, 1482364800, 1482451200, 1482796800, 1482883200, 1482969600, 
1483056000), tzone = "UTC", tclass = "Date"), class = c("xts", 
"zoo"), .indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC", tzone = "UTC")

0 个答案:

没有答案