Pine Script during Back testing higher time frame indicator isn't drawn on screen - plot

While doing back testing on daily time frame I wish to refer Weekly (HTF) macd line value. The default MACD indicator in tradingview doesn't show value nor does it plot macdline of higher HTF.
So i wrote following code which shows the value but doesn't plot the line while week is running. It plots the line only after week gets over. During back testing, how do I plot the running value of weekly MACD while week is still on?
macdlineH = request.security(syminfo.tickerid, "W", macdline[0])[0]
plot(macdlineH, title='MACD', color=color.new(color.blue, 0), linewidth=2)

Related

How to plot three barplots over time on the same axis

I have a data frame (df) that contains time (five min intervals) and number of animals that passes by at three different locations. Countings starts and ends at the same time for all three places. They are passing by the three places at different times; first place1, second place2 and third place3 due to their alignment in space.
Today I get:
Number of animals on the y-axis and time on the x-axis (where there are three separate groupings like I want). However, time starts over again for each of the three groupings (thus the x-axis looks like this (start-end, start-end, start-end).
What I want:
A plot with number of counts on the y-axis, and time on the x-axis (from start to end of counting). Thus there will be a "displacement" of number of animals (possible fewer as well) as time goes by.
My code today is:
barplot(df$nr.animals, names.arg = df$time, xlab="Time", ylab="Number of animals counted")
How can I get this data into a barplot with time on the x-axis (without the time starting at the beginning again for each new place)?
EDITED:
see picture for some of my dataset:
time=time, pos has three different values: mmys1, mmys_c1 and mmys_c2, which stands for the three different places, nr.bats.dir1 = number of animals counted in that specific time interval
see picture of the structure of my dataset:
time has factor format, nr,bats.dir1 has integer format
thus my code is actually like this
barplot(df$nr.bats.dir1, names.arg = df$time, xlab="Time", ylab="Number of bats counted")
Thanks!

How to make a one dimensional scatterplot of points in time?

I want to create a one dimensional scatterplot of points in time (range of about 5 hours), to visualise e.g. the time when I get up in the morning.
I tried
time=rep(Sys.time(),100)+round(3600*rnorm(100),1)
stripchart(as.numeric(time), main="stripchart", method="jitter", jitter = 2)
but that gives me
where I believe time is given in seconds since epoch. I'm interested in times of the day(8:02, 7:50,...) so time in seconds does not work for me. I need as.numeric however as I get 'invalid first argument' for leaving it out.
Plot the chart without the x-axis labels with xaxt="n" and add it afterwards with hours and minutes using axis. I'm using pretty to get the beginning of the hour.
time=rep(Sys.time(),100)+round(3600*rnorm(100),1)
stripchart(as.numeric(time), main="stripchart", method="jitter", jitter = 2, xaxt="n")
axis(side=1,pretty(time), format(pretty(time),"%H:%M"))
You could do this:
time=rep(Sys.time(),100)+round(3600*rnorm(100),1)
minutes <- (time - min(time))/60
stripchart(as.numeric(minutes), main="stripchart", method="jitter", jitter = 2)
Which yields this (x-axis in minutes):

Plot boxplots and line of time series data in R

I want to combine a time series of in situ values (line) with boxplots of estimated values of special dates. I tried to understand this "Add a line from different result to boxplot graph in ggplot2" question, but my dates make me drive crazy. Sometimes I only have in situ values of a date, sometimes only estimated values and sometimes both together.
I uploaded a sample of my data here:
http://www.file-upload.net/download-9942494/estimated.txt.html
http://www.file-upload.net/download-9942495/insitu.txt.html
How can I create a plot with both data sets that looks like this http://www.file-upload.net/download-9942496/desired_outputplot.png.html
in the end?
I got help and have a solution now:
insitu <- read.table("insitu.txt",header=TRUE,colClasses=c("Date","numeric"))
est <- read.table("estimated.txt",header=TRUE,colClasses=c("Date","numeric"))
insitu.plot <- xyplot(insitu~date_fname,data=insitu,type="l",
panel=function(x,y,...){panel.grid(); panel.xyplot(x,y,...)},xlab=list(label="Date",cex=2))
est.plot <- xyplot(estimated~date,data=est,panel=panel.bwplot,horizontal=FALSE)
both <- insitu.plot+est.plot
update(both,xlim=range(c(est$date,insitu$date_fname))+c(-1,1),ylim=range(c(est$estimated,insitu$insitu)))

Plotting Multiple Time Series in R

I am trying to plot the Operating Cash flow of BBBY (Bed Bath & Beyond) and the stock price.
2x Problem! 1) The price variable is daily, and operating cash flow variable I have is annual. When I plot both charts the graph never lines up correctly. How do I plot two time series, of different frequencies and have the annual operating cash flow data points line up to the daily variable on the chart. 2) How do I get the axis for operating cash flow only on the right of the chart and the stock price only on the left. Everytime I try to graph both variables the operating cash flow gets pasted on top of the existing stock price and you end up with a jumbled left axis. Help!!! (Thank you )
setInternet2(TRUE)
con = gzcon(url('http://www.systematicportfolio.com/sit.gz', 'rb'))
source(con)
close(con)
library("quantmod")
getSymbol("BBBY",from="1-1-2000")
data1=fund.data("BBBY",30,"annual",1)
operatingcash=as.numeric(gsub(",","",data[169,]))
date.year=seq(as.Date("1994-01-01"),length=22,by="years")
plot(BBBY)
par(new=T)
axis(4)
plot(date.year,operatingcash,type="l",axis=4)
First, plot the main series:
plot(BBBY)
Then reset the Y limits by changing values 3 and 4 in par()$usr:
par(usr="[<-"(par()$usr,3:4,range(operatingcash)))
Now add the operating cash. Note conversion to as.POSIXct since that's what the time
series plot sets the x-axis with:
lines(as.POSIXct(date.year),operatingcash,col="red")
And now add the right axis and label:
axis(4,col="red",col.axis="red")
mtext(4,col="red",text="Operating Cash",line=3)
Giving:
The operating cash line is now chopped because its data goes back to 1994. You can also see that the data points line up with the year starts.
The only change to your example I did was to call the data data and not data1, and use getSymbols and not getSymbol.

Is there a method to omit time intervals in plot.zoo function of R?

I have a time series of zoo and POSIXct class using second intervals. However, as is usually the case in financial time series, there are often long time gaps without information (ex. overnight). When using plot.zoo, the useful (populated) intervals appear scrunched relative to the unused intervals. Is there some way to simply omit time intervals in the plotting function of plot.zoo(). P.S. It's possible to just simply plot the continuous time series (plot(ts(obj)), but then the time information is lost on the x axis.
#AndresT Thanks for the idea, but the data is already filtered. It is the plot.zoo() function that appears to be padding unused sample intervals to keep the total x range
active. Pls see example below.
library(zoo)
st<-Sys.time()
t<-c(st, st+500, st+1000, st+1500, st+2000, st+90000, st+95000, st+100000)
rn<-runif(7)
zr<-zoo(rn, order.by=t)
plot.zoo(zr,type='o')
compare to the uniform spacing from plot(ts(data))
layout(1:2)
plot.zoo(zr, type='o')
plot(ts(zr),type='o')
So I would like to basically have the 2nd ts plot, but labeled with the corresponding x-axis labels from the 1st plot.zoo() function.
Try this:
library(quantmod)
chartSeries(zr)

Resources