Add text label near the position arrows in chart.Posn () function of blotter

87 Views Asked by At

Is it possible to add texts like "BUY" and "SELL" near the green and red arrows in chart.Posn() function. (not with manual trial of coordinates, I need an automatic solution) I have changed the source code of function a bit and increased the size of arrows:

enter image description here

If a minimum reproducible example is needed, I can provide but this is a basic question for graphical parameters of R.

Your help is appreciated.

Edit for a minimum reproducible example:

library(quantstrat)

start_date <- as.Date("2018-02-02")
end_date <- as.Date("2018-09-24")
init_date <- as.Date("2018-01-01")
init_equity <- "50000"
adjustment <- TRUE
symbol <- "AAPL"


getSymbols(symbol, src = "yahoo",
       from = start_date, to=end_date,
       adjust = adjustment)


portfolio.st <- "basic_port"
account.st <- "basic_account"
strategy.st <- "basic_strategy"


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

stock(symbol, currency = currency("USD"), multiplier = 1)
initPortf(name = portfolio.st, symbols = symbol, initDate =init_date)

initAcct(name = account.st, portfolios = portfolio.st, 
         initDate = init_date, initEq =init_equity)
initOrders(portfolio.st, symbol, init_date)
strategy(strategy.st, store = TRUE)



add.indicator(strategy = strategy.st, name = "SMA",
              arguments = list(x = quote(Cl(mktdata)), n=10),
              label ="nFast")


add.indicator(strategy = strategy.st, name = "SMA",
              arguments = list(x = quote(Cl(mktdata)), n=30),
              label = "nSlow")

add.signal(strategy = strategy.st, 
           name= "sigCrossover",
           arguments =  list(columns = c("nFast", "nSlow"),
                             relationship = "gte"),
           label = "longenter")

add.signal(strategy = strategy.st,
           name= "sigCrossover", 
           arguments =  list(columns = c("nFast",
                                         "nSlow"), 
                             relationship = "lt"),
           label = "longexit")

#Add rules for entering positions
#enter long position
add.rule(strategy.st, 
         name = "ruleSignal", 
         arguments = list(sigcol = "longenter",
                          sigval = TRUE,
                          orderqty = 100,
                          ordertype = "market",
                          orderside = "long",
                          orderset= "ocolong",
                          prefer = "Close",
                          TxnFees = -.8,
                          replace = FALSE),
         type = "enter",
         label = "EnterLong")



#stoploss long
add.rule(strategy = strategy.st, 
         name = "ruleSignal",
         arguments = list(sigcol = "longenter",
                          sigval = TRUE,
                          TxnFees=-.8,
                          replace = FALSE, 
                          orderside = "long", 
                          ordertype = "stoplimit",
                          orderqty = "all", 
                          tmult = TRUE,
                          prefer = "Close",
         order.price=quote(as.numeric(mktdata$AAPL.Low[timestamp])),
                          orderset="ocolong"), 
         type = "chain", parent = "EnterLong", 
         path.dep=TRUE, 
         label = "stop_loss_long", 
         enabled=TRUE)


#Apply strategy
applyStrategy(strategy.st, portfolios = portfolio.st,debug = TRUE)
updatePortf(portfolio.st)
updateAcct(account.st)
updateEndEq(account.st)

st<- "2018-05-01 00:00:00"
fn<-"2018-06-20 00:00:00"
chart.Posn(portfolio.st, Symbol = symbol, Dates = paste(st, fn, sep="::"))

enter image description here

1

There are 1 best solutions below

5
Quinten On

You could use the base R function text where you can specify the position and the label like this:

library(quantstrat)

start_date <- as.Date("2018-02-02")
end_date <- as.Date("2018-09-24")
init_date <- as.Date("2018-01-01")
init_equity <- "50000"
adjustment <- TRUE
symbol <- "AAPL"


getSymbols(symbol, src = "yahoo",
           from = start_date, to=end_date,
           adjust = adjustment)
#> [1] "AAPL"


portfolio.st <- "basic_port"
account.st <- "basic_account"
strategy.st <- "basic_strategy"


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

stock(symbol, currency = currency("USD"), multiplier = 1)
#> [1] "AAPL"
initPortf(name = portfolio.st, symbols = symbol, initDate =init_date)
#> [1] "basic_port"

initAcct(name = account.st, portfolios = portfolio.st, 
         initDate = init_date, initEq =init_equity)
#> [1] "basic_account"
initOrders(portfolio.st, symbol, init_date)
strategy(strategy.st, store = TRUE)



add.indicator(strategy = strategy.st, name = "SMA",
              arguments = list(x = quote(Cl(mktdata)), n=10),
              label ="nFast")
#> [1] "basic_strategy"


add.indicator(strategy = strategy.st, name = "SMA",
              arguments = list(x = quote(Cl(mktdata)), n=30),
              label = "nSlow")
#> [1] "basic_strategy"

add.signal(strategy = strategy.st, 
           name= "sigCrossover",
           arguments =  list(columns = c("nFast", "nSlow"),
                             relationship = "gte"),
           label = "longenter")
#> [1] "basic_strategy"

add.signal(strategy = strategy.st,
           name= "sigCrossover", 
           arguments =  list(columns = c("nFast",
                                         "nSlow"), 
                             relationship = "lt"),
           label = "longexit")
#> [1] "basic_strategy"

#Add rules for entering positions
#enter long position
add.rule(strategy.st, 
         name = "ruleSignal", 
         arguments = list(sigcol = "longenter",
                          sigval = TRUE,
                          orderqty = 100,
                          ordertype = "market",
                          orderside = "long",
                          orderset= "ocolong",
                          prefer = "Close",
                          TxnFees = -.8,
                          replace = FALSE),
         type = "enter",
         label = "EnterLong")
#> [1] "basic_strategy"



#stoploss long
add.rule(strategy = strategy.st, 
         name = "ruleSignal",
         arguments = list(sigcol = "longenter",
                          sigval = TRUE,
                          TxnFees=-.8,
                          replace = FALSE, 
                          orderside = "long", 
                          ordertype = "stoplimit",
                          orderqty = "all", 
                          tmult = TRUE,
                          prefer = "Close",
                          order.price=quote(as.numeric(mktdata$AAPL.Low[timestamp])),
                          orderset="ocolong"), 
         type = "chain", parent = "EnterLong", 
         path.dep=TRUE, 
         label = "stop_loss_long", 
         enabled=TRUE)
#> [1] "basic_strategy"


#Apply strategy
applyStrategy(strategy.st, portfolios = portfolio.st,debug = TRUE)
#> [1] "2018-04-19 02:00:00 AAPL 100 @ 42.8836602855443"
#> [1] "2018-04-20 02:00:00 AAPL -100 @ 42.3376887640763"
#> [1] "2018-05-09 02:00:00 AAPL 100 @ 46.4970046592104"
#> [1] "2018-06-19 02:00:00 AAPL -100 @ 45.9659223045418"
#> [1] "2018-07-17 02:00:00 AAPL 100 @ 47.6952277265372"
#> [1] "2018-07-30 02:00:00 AAPL -100 @ 47.1346935681311"
#> $basic_port
#> $basic_port$AAPL
#> <environment: 0x7fa52528db88>
updatePortf(portfolio.st)
#> [1] "basic_port"
updateAcct(account.st)
#> Warning in rbind(deparse.level, ...): mismatched types: converting objects to
#> numeric
#> [1] "basic_account"
updateEndEq(account.st)
#> [1] "basic_account"

st<- "2018-05-01 00:00:00"
fn<-"2018-06-20 00:00:00"
chart.Posn(portfolio.st, Symbol = symbol, Dates = paste(st, fn, sep="::"))
text(8, 1000, "BUY")
text(35, 1000, "SELL")

Created on 2022-07-22 by the reprex package (v2.0.1)