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:
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="::"))