Skip to content

Commit

Permalink
Update stoptrailing order price logic, update test assertion
Browse files Browse the repository at this point in the history
See #116
  • Loading branch information
jaymon0703 committed Mar 7, 2020
1 parent 42fb615 commit 7bcea6a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 42 deletions.
75 changes: 34 additions & 41 deletions R/ruleOrderProc.R
Original file line number Diff line number Diff line change
Expand Up @@ -354,52 +354,45 @@ ruleOrderProc <- function(portfolio, symbol, mktdata, timestamp=NULL, ordertype=
if(order.side == 'long' && as.numeric(Lo(mktdataTimestamp)[,1]) < orderPrice
|| order.side == 'short' && as.numeric(Hi(mktdataTimestamp)[,1]) > orderPrice)
{
txnprice <- ifelse(orderPrice %in% c(Lo(mktdataTimestamp)[,1],Hi(mktdataTimestamp)[,1]), orderPrice, Op(mktdataTimestamp)[,1])
if (order.side == 'long') {
txnprice <- ifelse(orderPrice < Op(mktdataTimestamp)[,1], orderPrice, Op(mktdataTimestamp)[,1]) # assume we can unwind long position at the open price if open price < order price
} else if (order.side == 'short') {
txnprice <- ifelse(orderPrice > Op(mktdataTimestamp)[,1], orderPrice, Op(mktdataTimestamp)[,1]) # assume we can unwind short position at the open price if open price > order price
}
ordersubset[ii,"Order.Price"] <- txnprice
txntime <- timestamp
}
else
{
# do we need to change the trailing stop?

order.threshold <- as.numeric(ordersubset[ii, "Order.Threshold"])

if(order.side == 'long')
new.order.price <- max(orderPrice, as.numeric(Hi(mktdataTimestamp)[,1]) + order.threshold)
if(order.side == 'short')
new.order.price <- min(orderPrice, as.numeric(Lo(mktdataTimestamp)[,1]) + order.threshold)

if(new.order.price != orderPrice)
{
# adjust trailing stop
} else { # do we need to change the trailing stop?
order.threshold <- as.numeric(ordersubset[ii, "Order.Threshold"])

order.qty <- ordersubset[ii, "Order.Qty"] # if orderQty='all' we must recover it

neworder<-addOrder(portfolio=portfolio,
symbol=symbol,
timestamp=timestamp,
qty=order.qty,
price=new.order.price - order.threshold,
ordertype=orderType,
side=order.side,
threshold=order.threshold,
status="open",
replace=FALSE, return=TRUE,
orderset=ordersubset[ii,"Order.Set"],
label=ordersubset[ii,"Rule"],
...=..., TxnFees=txnfees)

ordersubset<-rbind(ordersubset, neworder)

ordersubset[ii,"Order.Status"]<-'replaced'
ordersubset[ii,"Order.StatusTime"]<-format(timestamp, "%Y-%m-%d %H:%M:%S")

next()
if(order.side == 'long')
new.order.price <- max(orderPrice, as.numeric(Hi(mktdataTimestamp)[,1]) + order.threshold)
if(order.side == 'short')
new.order.price <- min(orderPrice, as.numeric(Lo(mktdataTimestamp)[,1]) + order.threshold)
if(new.order.price != orderPrice) { # adjust trailing stop
order.qty <- ordersubset[ii, "Order.Qty"] # if orderQty='all' we must recover it
neworder<-addOrder(portfolio=portfolio,
symbol=symbol,
timestamp=timestamp,
qty=order.qty,
price=new.order.price - order.threshold,
ordertype=orderType,
side=order.side,
threshold=order.threshold,
status="open",
replace=FALSE, return=TRUE,
orderset=ordersubset[ii,"Order.Set"],
label=ordersubset[ii,"Rule"],
...=..., TxnFees=txnfees)
ordersubset<-rbind(ordersubset, neworder)

ordersubset[ii,"Order.Status"]<-'replaced'
ordersubset[ii,"Order.StatusTime"]<-format(timestamp, "%Y-%m-%d %H:%M:%S")

next()
}
}
}
}

} # end stoptrailing
}# end stoptrailing
)

if(!is.null(txnprice) && !isTRUE(is.na(txnprice)))
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test_demo_maCross_stoptrailing.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ devAskNewPage(ask = FALSE) # dont prompt for new page, mainly for R CMD check
source(paste0(path.package("quantstrat"),"/demo/maCross_stoptrailing.R")) # source demo

test_that("End.Equity equals 7809", {
expect_equal(round(tradeStats('Port.Luxor','AAPL')$End.Equity), 7809)
expect_equal(round(tradeStats('Port.Luxor','AAPL')$End.Equity), 2771)
})

test_that("num txns equals 11", { # note we pad the start with zeros
Expand Down

0 comments on commit 7bcea6a

Please sign in to comment.