[R-SIG-Finance] GetOrders with Quantstrat
Joshua Ulrich
josh.m.ulrich at gmail.com
Thu Apr 7 01:48:51 CEST 2016
Please keep messages on-list, so others may benefit.
On Wed, Apr 6, 2016 at 2:46 AM, Ryan Crawford <suchislife27 at gmail.com> wrote:
> Joshua thank you for your time - all your work is greatly appreciated.
>
> I'm not sure why the example strategy wouldn't run and my apologies, perhaps
> a Mac/PC issue, however I think I pinpointed the problem.
>
> Would it be likely that getOrders only works for a single symbol?
>
Yes, that's it exactly. Thanks for this example, which made the issue clear.
I've committed a change (r1746) that throws a warning if
length(symbol) > 1, but proceeds with just the first element of the
symbol vector.
> When I try it using multiple symbols I get an error, however with only one
> symbol it works fine.
>
> I've attached a better example below to demonstrate.
>
> The error is:
>
> Error in getOrders(portfolio.st, symbols, status = "open") :
> symbol STW.AX does not exist in portfolio RSI_Test having symbols
> BHP.AXsymbol BHP.AX does not exist in portfolio RSI_Test having symbols
> STW.AX
>
> Thanks again.
>
>
> require(quantstrat)
>
> # Suppresses warnings
> options("getSymbols.warning4.0" = FALSE)
>
> # Set the currency and the timezone
> currency('USD')
> Sys.setenv(TZ = "UTC")
>
> # Define symbols of interest - this leads to the error
> symbols <- c("STW.AX", "BHP.AX")
>
> # Using a single, getOrders works fine
> #symbols <- "STW.AX"
>
>
> #Get Symbols
> getSymbols(Symbols=symbols, from="2014-01-01", to="2016-04-04")
>
>
>
> ### Start ###
>
>
> # Define the instrument type
> stock(symbols, currency = "USD", multiplier = 1)
>
> #Boilerplate
> initDate = "1990-01-01"
> from = "2005-01-01"
> to = "2015-12-07"
>
>
> #trade sizing and initial equity settings
> tradeSize <- 100
> initEq <- 100000
>
> strategy.st <- portfolio.st <- account.st <- "RSI_Test"
> rm.strat(portfolio.st)
> rm.strat(strategy.st)
> initPortf(portfolio.st, symbols=symbols, initDate=initDate, currency='USD')
> initAcct(account.st, portfolios=portfolio.st, initDate=initDate,
> currency='USD',initEq=initEq)
> initOrders(portfolio.st, initDate=initDate)
> strategy(strategy.st, store=TRUE)
>
> #parameters
> nRSI=2
> RSIentry=30
> RSIexit=70
>
> #Add Indicators
> add.indicator(strategy.st, name="RSI",
> arguments=list(price=quote(Cl(mktdata)), n=nRSI),
> label="rsi")
>
> #Add Entry and Exit Signals
> add.signal(strategy.st, name="sigThreshold",
> arguments=list(column="rsi", threshold=RSIentry,
> relationship="lt", cross=FALSE),
> label="longentry")
>
> add.signal(strategy.st, name="sigThreshold",
> arguments=list(column="rsi", threshold=RSIexit,
> relationship="gt", cross=FALSE),
> label="longexit")
>
> #enter signal rule
> add.rule(strategy.st, name="ruleSignal",
> arguments=list(sigcol="longentry", sigval=TRUE, ordertype="market",
> orderside="long", replace=FALSE, prefer="Open",
> orderqty=tradeSize),
> type="enter", path.dep=TRUE, label="enterlong")
>
> add.rule(strategy.st, name="ruleSignal",
> arguments=list(sigcol="longexit", sigval=TRUE, ordertype="market",
> orderside="long", replace=FALSE, prefer="Open",
> orderqty="all"),
> type="exit", path.dep=TRUE, label="exitlong")
>
> #take profit - I changed the parent to longentry from long
> add.rule(strategy.st, name="ruleSignal", arguments=list(sigcol="longentry",
> sigval=TRUE,
> ordertype="limit",
> orderside="long",
> replace=FALSE,
> orderqty="all",
> threshold=quote(.5),
> orderset="ocolong"),
> type="chain",
> parent="enterlong",
> label="takeProfitLong",
> path.dep=TRUE,
> enable=TRUE)
>
>
>
> #stop loss
> add.rule(strategy.st, name="ruleSignal", arguments=list(sigcol="longentry",
> sigval=TRUE,
>
> ordertype="stoplimit",
> orderside="long",
> replace=FALSE,
> orderqty="all",
> threshold=quote(.1),
> orderset="ocolong"),
> type="chain",
> parent="enterlong",
> label="stopLossLong",
> path.dep=TRUE,
> enable=TRUE)
>
> #apply strategy
> t1 <- Sys.time()
> out2 <- applyStrategy(strategy=strategy.st,portfolios=portfolio.st )
> t2 <- Sys.time()
> print(t2-t1)
>
> #getOrders
> oB <- getOrders(portfolio.st, symbols, status="open")
> View(oB)
>
> On Tue, Apr 5, 2016 at 7:48 PM, Joshua Ulrich <josh.m.ulrich at gmail.com>
> wrote:
>>
>> On Tue, Apr 5, 2016 at 4:26 AM, Ryan Crawford <suchislife27 at gmail.com>
>> wrote:
>> > Hello
>> >
>> > I have been trying to view "open" orders from the orderbook in
>> > Quantstrat
>> > and keep running into the same error.
>> >
>> > It seems a reasonably simple process however the error I get is:
>> >
>> > oB <- getOrders(portfolio.st, symbols, status="open")
>> >
>> > Error in orderbook[[portfolio]][[symbol]] :
>> > recursive indexing failed at level 2
>> >
>> > I've searched extensively and I can't seem to pinpoint the issue.
>> >
>> > Below is a reproducible example that generates the error.
>> >
>> > Thank you in advance for any help.
>> >
>> While reproducible, a strategy with 22 symbols isn't minimal.
>> Regardless, I ran your code, but no orders are created and lots of
>> warnings are generated by IKTrading function(s).
>>
>> I also get a different error when calling getOrders():
>> "Error in orderbook[[portfolio]][[symbol]] : no such index at level 2".
>> ...which is reasonable, given there are no orders.
>>
>> >
>> > require(quantstrat)
>> > require(IKTrading)
>> >
>> > # Set the currency and the timezone
>> > currency('USD')
>> > Sys.setenv(TZ = "UTC")
>> >
>> > # Define symbols of interest
>> > symbols <- c("AMP.AX", "BHP.AX", "ANZ.AX",
>> > "CBA.AX", "BXB.AX", "CSL.AX", "IAG.AX",
>> > "MQG.AX", "NAB.AX", "ORG.AX", "QBE.AX",
>> > "RIO.AX", "SCG.AX", "SUN.AX", "TLS.AX",
>> > "WBC.AX", "WES.AX", "WOW.AX", "WPL.AX",
>> > "TCL.AX", "WFD.AX", "AMC.AX")
>> >
>> > #Get Symbols
>> > getSymbols(Symbols=symbols, from="2010-01-01", to="2016-04-04")
>> >
>> > # Define the instrument type
>> > stock(symbols, currency = "USD", multiplier = 1)
>> >
>> > #Boilerplate
>> > from = "2010-01-01"
>> > to = "2016-04-04"
>> >
>> > #trade sizing and initial equity settings
>> > tradeSize <- 2500
>> > initEq <- 100000
>> >
>> > strategy.st <- portfolio.st <- account.st <- "GetOrderBook"
>> > rm.strat(portfolio.st)
>> > rm.strat(strategy.st)
>> > initAcct(account.st, portfolio.st, currency='USD', initEq=initEq)
>> > initPortf(portfolio.st, symbols, currency='USD')
>> > initOrders(portfolio.st)
>> > strategy(strategy.st, store=TRUE)
>> >
>> > #Add Indicators
>> > add.indicator(strategy.st, name="SMA",
>> > arguments=list(x=quote(Cl(mktdata)), n=20),
>> > label="sma")
>> >
>> > #Add Entry and Exit Signals
>> > add.signal(strategy.st, name="sigComparison",
>> > arguments=list(columns=c("Close", "SMA.20.sma"),
>> > relationship="gt"),
>> > label="longentry")
>> >
>> > #enter signal rule
>> > add.rule(strategy.st, name="ruleSignal",
>> > arguments=list(sigcol="longentry",
>> > sigval=TRUE,
>> > ordertype="limit",
>> > orderside="long",
>> > replace=TRUE,
>> > prefer="Close",
>> > tmult = TRUE,
>> > threshold = 0.05,
>> > time.in.force=172800,
>> > orderqty=tradeSize,
>> > osFUN=osMaxDollar,
>> > tradeSize=tradeSize,
>> > maxSize=tradeSize),
>> > type="enter",
>> > path.dep=TRUE,
>> > label="enterlong")
>> >
>> > #stop loss.
>> > add.rule(strategy.st, name="ruleSignal",
>> > arguments=list(sigcol="longentry",
>> > sigval=TRUE,
>> > ordertype="stoptrailing",
>> > orderside="long",
>> > replace=FALSE,
>> > orderqty="all",
>> > threshold=0.05,
>> > tmult=TRUE,
>> > orderset="ocolong"),
>> > type="chain",
>> > parent="enterlong",
>> > label="stopLossLong",
>> > path.dep=TRUE,
>> > enable=TRUE)
>> >
>> > #apply strategy
>> > out2 <- applyStrategy(strategy=strategy.st,portfolios=portfolio.st )
>> >
>> > #get open orders
>> > oB <- getOrders(portfolio.st, symbols, status="open")
>> >
>> > [[alternative HTML version deleted]]
>> >
>> > _______________________________________________
>> > R-SIG-Finance at r-project.org mailing list
>> > https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>> > -- Subscriber-posting only. If you want to post, subscribe first.
>> > -- Also note that this is not the r-help list where general R questions
>> > should go.
>>
>>
>>
>> --
>> Joshua Ulrich | about.me/joshuaulrich
>> FOSS Trading | www.fosstrading.com
>> R/Finance 2016 | www.rinfinance.com
>
>
--
Joshua Ulrich | about.me/joshuaulrich
FOSS Trading | www.fosstrading.com
R/Finance 2016 | www.rinfinance.com
More information about the R-SIG-Finance
mailing list