rquantmodquantstrat

osFixedDollar Producing Wrong Time Stamps Quantstrat R


I have a strategy where I sell VXX when F1.F2 Contango in The vix futures is greater than 9 and close the position when F1.F2 Contango is less than 2.

I would like to apply the Fixed Dollar Function. After the code runs and I check my order book vs mktdata I see the strategy taking positions for unwanted time stamps. To make sure it wasn't other code messing it up, I switched osFixedDollar for osMaxPos. The code worked as expected! So the problem lies within the osFixedDollar function.

Before I produce the code, here is a quick glimpse of the problem.

The order book trades are taken at different times. For example when we get a signal to go long on 2009-10-14 (highRoll == 1). The order book does not end up taking it until 2010-09-17! In fact none of the dates are accurate.

I tried customizing the osFixedDollar function but did not find a solution

enter image description here enter image description here Data will be provided at the end of the page. library(DT) library(quantstrat) library(tidyverse)

Sys.setenv(TZ = "UTC")
rm.strat(strat)
currency("USD")
ticker = "VXX"
stock(ticker, currency = "USD", multiplier = 1)

osFixedDollar <- function(timestamp, orderqty, portfolio, symbol, ruletype, ...) {
      pos<-getPosQty(portfolio,symbol,timestamp)
      if(is.character(orderqty)) {
            if(ruletype == "risk" && orderqty == "all") {
                  orderqty <- pos * -1
                  return(as.numeric(orderqty))
            } else {
                  stop("orderqty ", orderqty, " is not supported for non-risk ruletypes")
            }
      }else{
      ClosePrice <- as.numeric(Cl(mktdata[timestamp,]))
      orderqty <- round(trade_size/ClosePrice,-2)*sign(orderqty)
      return(as.numeric(orderqty))}

}
trade_size = 1000000
strat = "learn"
initPortf(strat, ticker)
initOrders(strat,ticker)
initAcct(strat, strat, initEq = 1000000)
strategy(strat, store = TRUE)

add.signal(strat, "sigThreshold", arguments = list(threshold = 9, column = "F1.F2.Contango",
                                                  relationship = "gte", cross = T), 
           label = "highRoll")
add.signal(strat, "sigThreshold", arguments = list(threshold = 2, column = "F1.F2.Contango",
                                                   relationship = "lt", cross = T),
           label = "lowRoll")


add.rule(strat, "ruleSignal", arguments = list(sigcol = "highRoll", 
                                               sigval = TRUE, 
                                               orderqty  = -100, 
                                              ordertype = "market",
                                               orderside = "short", 

                                               osFUN = osFixedDollar
                                              ),
         type = "enter",
         label = "enter2short")

add.rule(strat, "ruleSignal", arguments = list(sigcol = "lowRoll",
                                               sigval = TRUE, 
                                               orderqty = "all", 
                                               orderside = 'short', 
                                               ordertype = "market"
                                               ),
         type = "exit",
         label = "exit2long")


out = applyStrategy(strat, strat)
updatePortf(strat)
chart.Posn(strat, ticker)
View(mktdata)
z = getOrderBook(strat)
DT::datatable(z[[1]][[1]])

Here is a sample of the data.

    VXX = structure(c(32.439999, 32.82, 32.43, 32.080002, 31.98, 32.139999, 
31.271999, 31.620001, 31.249001, 32.839001, 32.09, 34.209999, 
33.32, 38.099998, 36.540001, 38.5, 39.200001, 36.919998, 37.740002, 
37.740002, 36.16, 34.365002, 32.48, 31.790001, 32.84, 32, 31.729, 
31.24, 30.719999, 30.559999, 31.200001, 31.267, 31.559999, 30.74, 
30.459999, 30.540001, 31.540001, 32.130001, 31.379999, 31, 30.68, 
30.139999, 29.268, 28.559999, 28.032, 28.530001, 29.959999, 31.700001, 
30.1, 31.98, 30.23, 29.49, 28.879999, 29.459999, 28.950001, 29.030001, 
28.860001, 28.9, 28.93, 28.65, 29.51, 29.690001, 29.280001, 29.459999, 
30.360001, 30.809999, 29.98, 29.1, 28.719999, 27.940001, 27.35, 
28.200001, 28.049999, 27.08, 26.639999, 26.566, 26.67, 26.92, 
27, 26.74, 26.700001, 26.4, 26.4, 26.200001, 27.799999, 28.24, 
28.639999, 29.08, 33.950001, 36.860001, 34.07, 34.27, 32.080002, 
32.459999, 34.849998, 34.27, 34.580002, 35.889999, 38.66, 37.860001, 
39.990002, 40.34, 38.810001, 37.580002, 36.369999, 36.66, 36.310001, 
35.080002, 32.650002, 32.5, 33.233002, 36.060001, 36.330002, 
37.27, 36.880001, 35.189999, 37.25, 39.330002, 38.41, 38.740002, 
36.549999, 35.900002, 34.84, 35.470001, 34.470001, 32.599998, 
36.84, 37.790001, 40.32, 39.93, 39.937, 39.619999, 39.009998, 
40.599998, 42.59, 42.790001, 42.639999, 44.810001, 47.07, 49.43, 
46.810001, 48.790001, 48.709999, 46.990002, 45.740002, 47.639999, 
43.860001, 42.880001, 42.009998, 41.02, 40.59, 39.27, 39.380001, 
37.669998, 38.43, 38, 37.060001, 40.32, 40.099998, 38.68, 37.23, 
38.77, 38.66, 36.959999, 35.43, 35.09, 34, 33.709999, 33.529999, 
34.650002, 34.23, 33.860001, 33.25, 33.169998, 33.77, 32.59, 
32.689999, 31.379999, 31.790001, 30.59, 31.139999, 31.4, 31.43, 
31.41, 29.98, 30.709999, 30.889999, 31.799999, 33.009998, 33.23, 
30.809999, 29.940001, 29.620001, 29.129999, 28.59, 28.66, 28.85, 
29.16, 28.780001, 32.080002, 32.240002, 30.42, 30.879999, 30.190001, 
29.26, 28.74, 28.66, 28.860001, 28.57, 28, 27.870001, 28.860001, 
28.049999, 27.5, 26.26, 25.92, 25.709999, 25.889999, 25.48, 25.25, 
25.049999, 25.700001, 26.200001, 25.290001, 25.68, 25.68, 26.65, 
26.690001, 25.42, 26.860001, 31.33, 30.709999, 30.549999, 28.190001, 
32.450001, 30.65, 29.360001, 28.1, 28.52, 28.940001, 27.48, 27.219999, 
29.02, 28.42, 29.27, 29.84, 29.309999, 30.42, 30.73, 29.02, 28.459999, 
27.85, 28.059999, 27.860001, 27.91, 27.780001, 27.610001, 27.32, 
27.030001, 26.82, 25.93, 26.01, 26.620001, 26.379999, 27.049999, 
26.84, 26.42, 26, 24.559999, 23.65, 23.51, 23.58, 24.23, 24.299999, 
23.559999, 23.17, 22.799999, 22.709999, 22.780001, 23.360001, 
23.115, 23.379999, 22.91, 22.1, 21.540001, 22.200001, 21.639999, 
21.83, 22.34, 23.59, 25.450001, 25.66, 29.299999, 27.48, 27.620001, 
25.98, 26.93, 28.889999, 26.76, 30.465, 29.67, 28.030001, 25.98, 
26.6, 25.299999, 7.65, 7.59, 6.75, 8.54, 9, 7.18, 7.89, 9.15, 
7.89, 4.79, 5.03, 2.69, 4.51, -0.31, 1.57, -0.3, 0.3, 2.18, 1.87, 
2.14, 3.88, 6.52, 6.17, 7.45, 4.94, 8.63, 9.63, 10.02, 15.26, 
6.82, 6, 6.3, 6.75, 6.39, 7.59, 7.76, 5.93, 5.04, 6.75, 5.95, 
7.54, 9.43, 10.36, 10.22, 11.13, 11.44, 7.26, 4.03, 7.29, 2.35, 
7.65, 9.56, 10.96, 13.59, 6.35, 6.68, 7.43, 7.79, 8.53, 9.34, 
7.92, 8.2, 7.03, 6.52, 5.6, 5.53, 6.09, 6.33, 7.13, 9.35, 11.65, 
10.31, 15.32, 5.95, 6.42, 6.73, 6.42, 6.39, 5.89, 6.37, 7.16, 
6.82, 6.8, 6.9, 2.35, 1.64, 0.32, -0.31, -7.95, -12.51, -9.03, 
-7.95, -2.26, 0.29, -1.9, -1.36, -2.15, -2.12, -4.85, -5.26, 
-4.74, -4.41, -3.35, -2.47, -1.3, -2.55, -3.09, -2.15, 1.78, 
1.18, 1.14, -4.34, -2.57, -3.72, -1.29, 0.27, -2.76, -7.89, 1.24, 
0.24, 2.38, 1.86, 1.92, 2.45, 2.83, 3.58, -0.77, -2.26, -3.5, 
-3.75, -3.76, -3.08, -2.41, -3.91, -7.67, -10.94, -1.79, -2.86, 
-5.66, -9.94, -8.31, -8.39, -9.28, -7.86, -5.41, -6.56, -3.19, 
-2.33, 0, 0.75, 0.75, 2.37, 1.3, 0, 0.26, 1.35, 1.64, -2.23, 
-0.76, 0.26, 1.37, 0, -0.26, 2.23, 3.8, 2.94, 3.33, 2.72, 2.75, 
1.16, 2.71, 4.62, 7.04, 0.9, 1.18, 1.84, 1.84, 4.17, 3.78, 4.94, 
3.53, 4.13, 4.15, 4.17, 5.4, 3.93, 3.88, 2.49, 0.9, 1.8, 5.99, 
8.29, 9.95, 10.05, 10.39, 16.25, 5.29, 5.22, 5.64, 2.39, 2.38, 
4.71, 4.62, 6.4, 7.22, 7.39, 8.07, 7.73, 7.78, 9.42, 8.29, 7.63, 
9.36, 12.3, 15.66, 16.7, 16.3, 6.84, 7.97, 8.47, 8.5, 7.2, 7.76, 
8.38, 8.26, 9.36, 7.22, 6.92, 9.11, 5.15, -0.84, -1.38, -0.56, 
4.34, -2.82, -0.28, 2.66, 6.68, 7.24, 6.51, 8.32, 4.01, 1.73, 
3.59, 2.01, 1.41, 2.3, 1.1, 0.27, 2.61, 4.21, 3.95, 2.99, 3.97, 
3.95, 4.29, 5.58, 6.34, 8.84, 10.28, 4.41, 3.72, 3.63, 4.3, 3.9, 
4.54, 5.6, 6.76, 8.04, 9.49, 10.7, 9.42, 7.73, 8.43, 11.05, 13.21, 
16, 18.25, 19.96, 5.41, 6.85, 5.35, 7.91, 9.14, 10.09, 9.88, 
10.09, 10.02, 8.96, 6.84, 1.72, 1.14, -6.47, -1.27, -1.29, 1.97, 
0.54, -3.85, 1.93, -4.67, -3.31, 2.93, 7.43, 7.26, 3.74, 0.820295983086681, 
0.840609840609841, 0.834246575342466, 0.848797250859107, 0.845784784098698, 
0.867292225201072, 0.841666666666667, 0.830790568654646, 0.850138121546961, 
0.880026367831246, 0.864773495605139, 0.922495274102079, 0.888387096774193, 
0.989155251141552, 0.94368696243705, 0.992243822284976, 0.96175799086758, 
0.938739844733947, 0.920353928002718, 0.930259308357349, 0.905626079514454, 
0.851050286441757, 0.853396099529254, 0.859279401767505, 0.886215864759428, 
0.85230352303523, 0.837689133425034, 0.863973063973064, 0.8375, 
0.8408617095205, 0.859719438877755, 0.852785145888594, 0.849259757738896, 
0.84421768707483, 0.8446735395189, 0.843055555555556, 0.868666666666667, 
0.910600255427842, 0.862811028917283, 0.863994743758213, 0.825880758807588, 
0.803312629399586, 0.791988756148981, 0.784637473079684, 0.777220630372493, 
0.797593772116065, 0.864651773981603, 0.91178290634071, 0.867100977198697, 
0.904261834202481, 0.867182462927144, 0.833223467369809, 0.832666666666667, 
0.84660961158657, 0.812334217506631, 0.824037184594953, 0.809041835357625, 
0.822177146720757, 0.827266710787558, 0.819397993311037, 0.870656370656371, 
0.848844884488449, 0.851779935275081, 0.873743718592965, 0.896572827417381, 
0.896925858951175, 0.87569573283859, 0.850707850707851, 0.857702349869452, 
0.83750846310088, 0.839944328462074, 0.895287958115183, 0.850398936170213, 
0.802047781569966, 0.813232253618194, 0.801097393689986, 0.825998645903859, 
0.832439678284182, 0.850825082508251, 0.833445265278711, 0.820582261340555, 
0.814111261872456, 0.811994609164421, 0.790871934604905, 0.897727272727273, 
0.912, 0.950333131435493, 0.959110099766091, 1.10917864206866, 
1.15862714093818, 1.07409269153226, 1.04977811484583, 0.981069152620777, 
0.961325966850829, 1.01007039224218, 1.0025201108871, 0.990918213925328, 
1.0212030561537, 1.11538456607495, 1.07932254548474, 1.07473309608541, 
1.07111891028269, 1.05038236617184, 1.00378250591017, 0.963627304434479, 
0.961083791186394, 0.98811871345947, 1.01685393258427, 0.928490355931308, 
0.945701300904978, 0.94041180608948, 1.01087498764212, 0.98766650222003, 
1.00141380779518, 0.983751894817917, 0.937467648578811, 0.973837256484363, 
1.00942972608891, 0.975609709193246, 0.989425287356322, 0.939830883151125, 
0.953861536917676, 0.959024846523608, 0.959162889229199, 0.956590839417196, 
0.915877442012399, 0.999518072289157, 1.00617293476605, 1.03520494495522, 
1.02862330758746, 1.00184166675146, 0.993978647522001, 0.967213114754098, 
0.984972676911324, 1.0474156343443, 1.07029288702929, 1.06494587843464, 
1.12440566561014, 1.14792226458254, 1.25069348127601, 1.18372903138462, 
1.15720355956754, 1.11927330173776, 1.07121786767804, 1.0206592967033, 
1.05557859578687, 0.978041995515005, 0.989824191034959, 0.968306526409959, 
0.965683858594304, 0.950292397660819, 0.92147928064843, 0.9568489713999, 
0.966735966735967, 0.974910395549903, 0.942096974439228, 0.944798248407643, 
1.00531648113502, 0.972111553784861, 0.960345653279105, 0.925611052072264, 
0.963738559754852, 0.976020357142857, 0.945396145610278, 0.926733832591378, 
0.917566742467311, 0.915066846127583, 0.912126537785589, 0.910059171597633, 
0.934360787671233, 0.910776361529548, 0.927410042242163, 0.917360285374554, 
0.922759488370253, 0.93648954965358, 0.904733009708738, 0.904559215528315, 
0.88621997471555, 0.89480198019802, 0.873868046571798, 0.928125, 
0.930674903722387, 0.919324577861163, 0.918012422360248, 0.882314694408322, 
0.912663698523787, 0.912693498452013, 0.94026284348865, 0.958959593003445, 
0.938047811920058, 0.895065584009994, 0.879872204472843, 0.874755381604697, 
0.882352941176471, 0.85981308411215, 0.859580052493438, 0.87146529562982, 
0.88093730208993, 0.878787878787879, 0.948762179115591, 0.935816565282718, 
0.89240116155616, 0.902860494466002, 0.885276127931051, 0.867721518987342, 
0.866752910737387, 0.85971685971686, 0.879078694817658, 0.877260981912145, 
0.851261620185923, 0.862565445026178, 0.893058161350844, 0.864759427828348, 
0.853770491803279, 0.830567081604426, 0.847900894700619, 0.841741534208708, 
0.850202429149797, 0.829787234042553, 0.84375, 0.844566712517194, 
0.873670212765958, 0.871137409598948, 0.854936198791135, 0.860801050558109, 
0.861457649376231, 0.914144477199229, 0.899563262659809, 0.854014598540146, 
0.941463414634146, 1.02875393883099, 1.03466666666667, 1.03579170087898, 
0.949112485207101, 1.03840313095487, 0.98742471364545, 0.939965751858205, 
0.913381123058542, 0.935521742996585, 0.943319725661092, 0.91047503045067, 
0.898294762484775, 0.956472529311898, 0.927442949093037, 0.977653631284916, 
0.981359649122807, 0.963251668332498, 0.993627137546468, 1.00479493873202, 
0.96475264354747, 0.939287854015636, 0.941489361702128, 0.950437259475219, 
0.944312852151235, 0.941141848145968, 0.933137884641518, 0.938315484085677, 
0.920481927710843, 0.924142027444791, 0.918181818181818, 0.890062111801242, 
0.913878619199419, 0.923815236952609, 0.926533146723324, 0.950934582305223, 
0.95073313494036, 0.943914081145585, 0.938978887856718, 0.911211924821776, 
0.878396739130435, 0.865106675843083, 0.886515353805073, 0.901808785529716, 
0.904945407835581, 0.857801184990125, 0.863150867823765, 0.837162162162162, 
0.843085106382979, 0.844940867279895, 0.883059418457649, 0.870656370656371, 
0.907093534212178, 0.874595992243051, 0.844608171466845, 0.828983516483516, 
0.846511627906977, 0.827773995915589, 0.855333333333333, 0.887898089171975, 
0.953282140745121, 0.99332968315731, 0.984348909130738, 1.12283105022831, 
1.02437780475481, 0.999487179487179, 0.946808457625506, 0.968211205183793, 
1.02878048780488, 0.945493742822788, 1.03755873415769, 1.00522069292833, 
0.93756335342318, 0.919389873671576, 0.927397937074831, 0.891647905849205, 
12.33, 8.62, 9.4, 4.25, 3.53, 2.2, 4.58, 4.97, 2.97, 1.69, 8.87, 
1.61, 4.76, -5.8, -0.28, -5.5, -1.93, -0.4, 2.72, 1.46, 3.37, 
9.01, 8.55, 6.21, 4, 5.92, 6.53, 1.13, 0.54, 15.08, 10.14, 11, 
11.53, 13.42, 12.49, 14.09, 9.94, 4.31, 9.7, 8.56, 14.23, 16.19, 
15.57, 16.42, 15.9, 12.47, 4.67, 0.64, 3.12, 1.61, 2.04, 3.44, 
2.28, 0.12, 15.71, 14.63, 17.81, 16.16, 12.6, 13.67, 7.35, 9.06, 
8.09, 4.78, 3.58, 3.33, 4.34, 7.6, 6.74, 8.12, 6.67, -0.77, -0.51, 
21.49, 18.86, 20.93, 14.96, 13.33, 11.91, 13.82, 15.31, 16.04, 
15.98, 18.65, 4.61, 2.73, -0.1, 0.16, -12.35, -15.23, -6.5, -5.52, 
0.31, -1.58, -8.15, -7.37, -5.17, -9.1, -18.25, -13.6, -12.77, 
-12.85, -10.39, -4.5, -0.34, 0.33, -2.68, -6.45, 2.84, 1.53, 
0.66, -4.28, -2.97, -5.06, -3.03, 0.74, -0.87, -1.36, -3.25, 
-4.86, 0.13, -1.29, -1.43, -2.21, -2.19, 2.04, -6.34, -5.97, 
-7.77, -5.81, -2.23, -1.79, 0.36, 0.44, -4.26, -3.54, -12.72, 
-19.93, -19.3, -28.2, -18.86, -16.47, -10.64, -4.9, -0.41, -4.22, 
2.55, 0.12, 0.76, 0.73, 1.92, 4.59, 0.81, 1.21, -0.08, 2.57, 
2.95, -3, 1.05, 1.51, 4.91, 1.35, -1.07, 1.78, 3.35, 5.48, 5.05, 
6.13, 6.47, 4.92, 5.76, 1.6, 1.26, 6.55, 4.35, 9.49, 9.71, 11.09, 
9.79, 12.32, 4.88, 3.66, 6.63, 5.38, 9.25, 4.41, 4.99, 2.13, 
0.81, 3.89, 4.85, 5.12, 4.96, 3.15, 4.62, -1.34, 11.54, 10.17, 
10.6, 1.49, 3.03, 8.48, 7.1, 8.28, 11.05, 11.01, 11.34, 8.26, 
8.8, 11.74, 12.91, 5.57, 8.46, 6.18, 6.37, 2.07, 3.24, 16.07, 
19.31, 14.13, 15.02, 10.92, 11.89, 12.53, 10.79, 9.95, 2.87, 
5.24, 10.92, 0.55, -7.48, -6.31, -5.89, 0.53, -4.99, 0.36, 2.95, 
2.84, -0.53, -1.13, 0.5, 10, 2.39, 5.52, -0.71, -0.7, 0.72, -2.86, 
-3.1, 1.5, 3.33, 3.42, 2.61, 2.73, 3.03, 2.61, 1.93, 3.24, -0.49, 
-0.5, 10.78, 9.32, 7.31, 6.65, 2.43, 1.94, 1.61, 2.95, 6.15, 
10.02, 11.58, 7.87, 6.55, 5.22, 7.64, 5.38, 5.93, 1.54, -0.66, 
12.56, 13.27, 9.86, 12.16, 17.17, 19.1, 15.19, 18.22, 12.82, 
8.14, -0.28, -2.21, -0.77, -12.06, -2.45, -0.59, 4.82, 2.81, 
-1.49, 3.45, -3.05, -0.02, 1.65, -0.33, -1.57, 9.97), class = c("xts", 
"zoo"), .indexCLASS = "Date", .indexTZ = "UTC", tclass = "Date", tzone = "UTC", src = "yahoo", updated = structure(1566586240.46543, class = c("POSIXct", 
"POSIXt")), na.action = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 
21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 
34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 
47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 
60L, 61L, 62L, 63L, 64L, 65L, 66L, 67L, 68L, 69L, 70L, 71L, 72L, 
73L, 74L, 75L, 76L, 77L, 78L, 79L, 80L, 81L, 82L, 83L, 84L, 85L, 
86L, 87L, 88L, 89L, 90L, 91L, 92L, 93L, 94L, 95L, 96L, 97L, 98L, 
99L, 100L, 101L, 102L, 103L, 104L, 105L, 106L, 107L, 108L, 109L, 
110L, 111L, 112L, 113L, 114L, 115L, 116L, 117L, 118L, 119L, 120L, 
121L, 122L, 123L, 124L, 125L, 126L, 127L, 128L, 129L, 130L, 131L, 
132L, 133L, 134L, 135L, 136L, 137L, 138L, 139L, 140L, 141L, 142L, 
143L, 144L, 145L, 146L, 147L, 148L, 149L, 150L, 151L, 152L, 153L, 
154L, 155L, 156L, 157L, 158L, 159L, 160L, 161L, 162L, 163L, 164L, 
165L, 166L, 167L, 168L, 169L, 170L, 171L, 172L, 173L, 174L, 175L, 
176L, 177L, 1290L, 1334L, 1360L, 1389L, 1430L, 1493L, 1513L, 
1518L, 1530L, 1550L, 1584L, 1620L, 1649L, 1695L, 1753L, 1774L, 
1779L, 1790L, 1810L, 1839L, 1885L, 1910L, 1955L, 2013L, 2035L, 
2040L, 2050L, 2075L, 2114L, 2145L, 2171L, 2215L, 2273L, 2295L, 
2300L, 2310L, 2335L, 2364L, 2405L, 2432L, 2475L, 2533L, 2542L, 
2556L, 2561L, 2575L, 2595L, 2639L, 2665L, 2693L, 2728L), class = "omit", index = c(1230854400, 
1231113600, 1231200000, 1231286400, 1231372800, 1231459200, 1231718400, 
1231804800, 1231891200, 1231977600, 1232064000, 1232409600, 1232496000, 
1232582400, 1232668800, 1232928000, 1233014400, 1233100800, 1233187200, 
1233273600, 1233532800, 1233619200, 1233705600, 1233792000, 1233878400, 
1234137600, 1234224000, 1234310400, 1234396800, 1234483200, 1234828800, 
1234915200, 1235001600, 1235088000, 1235347200, 1235433600, 1235520000, 
1235606400, 1235692800, 1235952000, 1236038400, 1236124800, 1236211200, 
1236297600, 1236556800, 1236643200, 1236729600, 1236816000, 1236902400, 
1237161600, 1237248000, 1237334400, 1237420800, 1237507200, 1237766400, 
1237852800, 1237939200, 1238025600, 1238112000, 1238371200, 1238457600, 
1238544000, 1238630400, 1238716800, 1238976000, 1239062400, 1239148800, 
1239235200, 1239580800, 1239667200, 1239753600, 1239840000, 1239926400, 
1240185600, 1240272000, 1240358400, 1240444800, 1240531200, 1240790400, 
1240876800, 1240963200, 1241049600, 1241136000, 1241395200, 1241481600, 
1241568000, 1241654400, 1241740800, 1.242e+09, 1242086400, 1242172800, 
1242259200, 1242345600, 1242604800, 1242691200, 1242777600, 1242864000, 
1242950400, 1243296000, 1243382400, 1243468800, 1243555200, 1243814400, 
1243900800, 1243987200, 1244073600, 1244160000, 1244419200, 1244505600, 
1244592000, 1244678400, 1244764800, 1245024000, 1245110400, 1245196800, 
1245283200, 1245369600, 1245628800, 1245715200, 1245801600, 1245888000, 
1245974400, 1246233600, 1246320000, 1246406400, 1246492800, 1246838400, 
1246924800, 1247011200, 1247097600, 1247184000, 1247443200, 1247529600, 
1247616000, 1247702400, 1247788800, 1248048000, 1248134400, 1248220800, 
1248307200, 1248393600, 1248652800, 1248739200, 1248825600, 1248912000, 
1248998400, 1249257600, 1249344000, 1249430400, 1249516800, 1249603200, 
1249862400, 1249948800, 1250035200, 1250121600, 1250208000, 1250467200, 
1250553600, 1250640000, 1250726400, 1250812800, 1251072000, 1251158400, 
1251244800, 1251331200, 1251417600, 1251676800, 1251763200, 1251849600, 
1251936000, 1252022400, 1252368000, 1252454400, 1252540800, 1252627200, 
1252886400, 1252972800, 1392595200, 1397779200, 1401062400, 1404432000, 
1409529600, 1417046400, 1419465600, 1420070400, 1421625600, 1424044800, 
1428019200, 1432512000, 1435881600, 1441584000, 1448496000, 1451001600, 
1451606400, 1453075200, 1455494400, 1458864000, 1464566400, 1467590400, 
1473033600, 1479945600, 1482710400, 1483315200, 1484524800, 1487548800, 
1492128000, 1496016000, 1499126400, 1504483200, 1511395200, 1514160000, 
1514764800, 1515974400, 1518998400, 1522368000, 1527465600, 1530662400, 
1535932800, 1542844800, 1543968000, 1545696000, 1546300800, 1548028800, 
1550448000, 1555632000, 1558915200, 1562198400, 1566432000)), index = structure(c(1528243200, 
1528329600, 1528416000, 1528675200, 1528761600, 1528848000, 1528934400, 
1529020800, 1529280000, 1529366400, 1529452800, 1529539200, 1529625600, 
1529884800, 1529971200, 1530057600, 1530144000, 1530230400, 1530489600, 
1530576000, 1530748800, 1530835200, 1531094400, 1531180800, 1531267200, 
1531353600, 1531440000, 1531699200, 1531785600, 1531872000, 1531958400, 
1532044800, 1532304000, 1532390400, 1532476800, 1532563200, 1532649600, 
1532908800, 1532995200, 1533081600, 1533168000, 1533254400, 1533513600, 
1533600000, 1533686400, 1533772800, 1533859200, 1534118400, 1534204800, 
1534291200, 1534377600, 1534464000, 1534723200, 1534809600, 1534896000, 
1534982400, 1535068800, 1535328000, 1535414400, 1535500800, 1535587200, 
1535673600, 1536019200, 1536105600, 1536192000, 1536278400, 1536537600, 
1536624000, 1536710400, 1536796800, 1536883200, 1537142400, 1537228800, 
1537315200, 1537401600, 1537488000, 1537747200, 1537833600, 1537920000, 
1538006400, 1538092800, 1538352000, 1538438400, 1538524800, 1538611200, 
1538697600, 1538956800, 1539043200, 1539129600, 1539216000, 1539302400, 
1539561600, 1539648000, 1539734400, 1539820800, 1539907200, 1540166400, 
1540252800, 1540339200, 1540425600, 1540512000, 1540771200, 1540857600, 
1540944000, 1541030400, 1541116800, 1541376000, 1541462400, 1541548800, 
1541635200, 1541721600, 1541980800, 1542067200, 1542153600, 1542240000, 
1542326400, 1542585600, 1542672000, 1542758400, 1542931200, 1543190400, 
1543276800, 1543363200, 1543449600, 1543536000, 1543795200, 1543881600, 
1544054400, 1544140800, 1544400000, 1544486400, 1544572800, 1544659200, 
1544745600, 1545004800, 1545091200, 1545177600, 1545264000, 1545350400, 
1545609600, 1545782400, 1545868800, 1545955200, 1546214400, 1546387200, 
1546473600, 1546560000, 1546819200, 1546905600, 1546992000, 1547078400, 
1547164800, 1547424000, 1547510400, 1547596800, 1547683200, 1547769600, 
1548115200, 1548201600, 1548288000, 1548374400, 1548633600, 1548720000, 
1548806400, 1548892800, 1548979200, 1549238400, 1549324800, 1549411200, 
1549497600, 1549584000, 1549843200, 1549929600, 1550016000, 1550102400, 
1550188800, 1550534400, 1550620800, 1550707200, 1550793600, 1551052800, 
1551139200, 1551225600, 1551312000, 1551398400, 1551657600, 1551744000, 
1551830400, 1551916800, 1552003200, 1552262400, 1552348800, 1552435200, 
1552521600, 1552608000, 1552867200, 1552953600, 1553040000, 1553126400, 
1553212800, 1553472000, 1553558400, 1553644800, 1553731200, 1553817600, 
1554076800, 1554163200, 1554249600, 1554336000, 1554422400, 1554681600, 
1554768000, 1554854400, 1554940800, 1555027200, 1555286400, 1555372800, 
1555459200, 1555545600, 1555891200, 1555977600, 1556064000, 1556150400, 
1556236800, 1556496000, 1556582400, 1556668800, 1556755200, 1556841600, 
1557100800, 1557187200, 1557273600, 1557360000, 1557446400, 1557705600, 
1557792000, 1557878400, 1557964800, 1558051200, 1558310400, 1558396800, 
1558483200, 1558569600, 1558656000, 1559001600, 1559088000, 1559174400, 
1559260800, 1559520000, 1559606400, 1559692800, 1559779200, 1559865600, 
1560124800, 1560211200, 1560297600, 1560384000, 1560470400, 1560729600, 
1560816000, 1560902400, 1560988800, 1561075200, 1561334400, 1561420800, 
1561507200, 1561593600, 1561680000, 1561939200, 1562025600, 1562112000, 
1562284800, 1562544000, 1562630400, 1562716800, 1562803200, 1562889600, 
1563148800, 1563235200, 1563321600, 1563408000, 1563494400, 1563753600, 
1563840000, 1563926400, 1564012800, 1564099200, 1564358400, 1564444800, 
1564531200, 1564617600, 1564704000, 1564963200, 1565049600, 1565136000, 
1565222400, 1565308800, 1565568000, 1565654400, 1565740800, 1565827200, 
1565913600, 1566172800, 1566259200, 1566345600), tzone = "UTC", tclass = "Date"), .Dim = c(305L, 
4L), .Dimnames = list(NULL, c("VXX.Close", "F1.F2.Contango", 
"InContango", "Spot.F1.Roll.Yield")))

Solution

  • This will be my first contribution to the Quanstrat community so your criticism would be greatly appreciated!

    The problem was I was rounding to the closest hundred. So some positions that were less than 50 were being rounded down to 0. The other problem was I kept adding a position every time "highRoll" was true.

    I have created a function called osFixedDollarMaxPos which gives you an "in or out" scenario. Sort of like combining osMaxPos with osFixedDollar.

    I got my desired result after replacing osFixedDollar with osFixedDollarMaxPos. Here it is!

    osFixedDollarMaxPos <- function(timestamp, orderqty, portfolio, symbol, ruletype,trade_size, ...) {
          pos<-getPosQty(portfolio,symbol, timestamp)
    
    
          if(is.character(orderqty)) {
                if(ruletype == "risk" && orderqty == "all") {
                      orderqty <- pos * -1
                } else {
                      stop("orderqty ", orderqty, " is not supported for non-risk ruletypes")
                }
          }
    
    
          if(pos > 0 || pos < 0){
                orderqty = 0
          }else if (orderqty > 0 && pos == 0){#go long
                ClosePrice <- as.numeric(Cl(mktdata[timestamp,]))
                orderqty <- round(trade_size/ClosePrice)*sign(orderqty)
          }else if(orderqty < 0 && pos == 0){#go short
                ClosePrice <- as.numeric(Cl(mktdata[timestamp,]))
                orderqty <- round(trade_size/ClosePrice)*sign(orderqty)
           }
    
         return(orderqty)
    
    
    }
    

    Here is how it should be called. Add an arbitrary "orderqty" to show if you are long or short. ie. -100 = short, 100 = long

    add.rule(strat, "ruleSignal", arguments = list(sigcol = "highRoll", 
                                                   sigval = TRUE, 
                                                   orderqty  = -100, 
                                                   orderside = "short",
                                                   ordertype = "market",
                                                   osFUN = osFixedDollarMaxPos, 
                                                   prefer = "Close"
                                                  ),
             type = "enter",
             label = "enter2short")