From 5b20e0938f42fb070ea1d92689709a68af0bf864 Mon Sep 17 00:00:00 2001 From: bosma Date: Tue, 29 Nov 2016 16:41:21 -0800 Subject: [PATCH 1/2] Fixed bug where if trailstop order is triggered it would sell [buy] at the high [low] of a bar. Previously: at line 376 a new order replaces the previous trailstop order with order price new.order.price - order.threshold since the order is being replaced, we can say new.order.price != orderPrice, and in the long case, this means new.order.price = previous_bar_high + order.threshold therefore, the new order price on line 380 is: price = new.order.price - order.threshold = previous_bar_high + order.threshold - order.threshold = previous_bar_high Since, in the long case, the next bar is most likely under the high of the previous bar, the order is fired and you sell the top of the previous bar. analogolous for the short case. So, the order.threshold subtraction on line 380 was removed, since the threshold is already considered in line 366 and 368. --- R/ruleOrderProc.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/ruleOrderProc.R b/R/ruleOrderProc.R index b4e2dcd..92801f5 100644 --- a/R/ruleOrderProc.R +++ b/R/ruleOrderProc.R @@ -377,7 +377,7 @@ ruleOrderProc <- function(portfolio, symbol, mktdata, timestamp=NULL, ordertype= symbol=symbol, timestamp=timestamp, qty=order.qty, - price=new.order.price - order.threshold, + price=new.order.price, ordertype=orderType, side=order.side, threshold=order.threshold, From 2d9da4a4fa65cc3c43de052fafe900e6f904fdf3 Mon Sep 17 00:00:00 2001 From: Bosma Date: Sun, 18 Dec 2016 11:58:08 -0800 Subject: [PATCH 2/2] same argument applies to BBO case --- R/ruleOrderProc.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/ruleOrderProc.R b/R/ruleOrderProc.R index 92801f5..6fc6a67 100644 --- a/R/ruleOrderProc.R +++ b/R/ruleOrderProc.R @@ -325,7 +325,7 @@ ruleOrderProc <- function(portfolio, symbol, mktdata, timestamp=NULL, ordertype= symbol=symbol, timestamp=timestamp, qty=new.order.qty, - price=new.order.price-orderThreshold, + price=new.order.price, ordertype=orderType, prefer=prefer, side=order.side,