Empty indicator / plot.window: need finite 'ylim' values

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Empty indicator / plot.window: need finite 'ylim' values

Mike-3
I like to plot several charts to files with an identical layout
according to indicators so the charts can be browsed quickly. It is
normal for the indicators in some charts to be completely NA at least
in the range to be plotted. Generating those charts throws

Error in plot.window(c(1, 31), c(NaN, NaN)) : need finite 'ylim' values

To substitute NA by 0, -1 or similar is no option since these are
legal values.

Can I tell R to ignore such errors and simply generate an empty
region instead?

Mike



Minimal reproducible example:

library(quantmod)

data (sample_matrix)
sample.xts <- as.xts (sample_matrix[1:50,'Close'], dateFormat="POSIXct")
sample.xts <- cbind (sample.xts, NA)
sample.xts['2007-02-05::',2] <- 0
sample.xts['2007-02-11',2] <- 1
colnames (sample.xts) <- c('Close', 'Stops')

ta <- list ("add_TA(sample.xts[,2])")

# In the range to be plotted ta is completely NA
subset <- '2007-01-10::2007-01-30'

plot (chart_Series (sample.xts[,1], subset=subset, TA=ta))

_______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Empty indicator / plot.window: need finite 'ylim' values

Rasmus Liland-3
Dear Mike,

if I change this line

        plot (chart_Series (sample.xts[,1], subset=subset, TA=ta))

to

        plot(chart_Series(sample.xts[subset,1], TA=ta))

A plot appears.  No errors.  Was this
what you were looking for?

/Rasmus

_______________________________________________
[hidden email] 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.

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Empty indicator / plot.window: need finite 'ylim' values

Daniel Cegiełka


> On 17 Aug 2020, at 04:55, Rasmus Liland <[hidden email]> wrote:
>
> Dear Mike,
>
> if I change this line
>
> plot (chart_Series (sample.xts[,1], subset=subset, TA=ta))
>
> to
>
> plot(chart_Series(sample.xts[subset,1], TA=ta))
>
> A plot appears.  No errors.  Was this
> what you were looking for?
>


Guys :)

chart_Series() is a plot env/func, so plot(chart_Series()) make no sense :)


Try:

chart_Series(sample.xts[subset,1], TA=ta)

?chart_Series

But your problem is here:

sample.xts[subset, 2]


Best,
Daniel




> /Rasmus
> _______________________________________________
> [hidden email] 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.

_______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Empty indicator / plot.window: need finite 'ylim' values

Joshua Ulrich
On Mon, Aug 17, 2020 at 12:33 PM Daniel Cegiełka
<[hidden email]> wrote:

>
>
>
> > On 17 Aug 2020, at 04:55, Rasmus Liland <[hidden email]> wrote:
> >
> > Dear Mike,
> >
> > if I change this line
> >
> >       plot (chart_Series (sample.xts[,1], subset=subset, TA=ta))
> >
> > to
> >
> >       plot(chart_Series(sample.xts[subset,1], TA=ta))
> >
> > A plot appears.  No errors.  Was this
> > what you were looking for?
> >
>
>
> Guys :)
>
> chart_Series() is a plot env/func, so plot(chart_Series()) make no sense :)
>
Except that it is necessary to wrap chart_Series() in a call to plot()
if you call it from within a function, for loop, or any other
situation where R's auto-printing is disabled.

Cheers,
Josh


>
> Try:
>
> chart_Series(sample.xts[subset,1], TA=ta)
>
> ?chart_Series
>
> But your problem is here:
>
> sample.xts[subset, 2]
>
>
> Best,
> Daniel
>
>
>
>
> > /Rasmus
> > _______________________________________________
> > [hidden email] 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.
>
> _______________________________________________
> [hidden email] 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

_______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Empty indicator / plot.window: need finite 'ylim' values

Daniel Cegiełka


> On 17 Aug 2020, at 19:37, Joshua Ulrich <[hidden email]> wrote:
>
> On Mon, Aug 17, 2020 at 12:33 PM Daniel Cegiełka
> <[hidden email] <mailto:[hidden email]>> wrote:
>>
>>
>>
>>> On 17 Aug 2020, at 04:55, Rasmus Liland <[hidden email]> wrote:
>>>
>>> Dear Mike,
>>>
>>> if I change this line
>>>
>>>      plot (chart_Series (sample.xts[,1], subset=subset, TA=ta))
>>>
>>> to
>>>
>>>      plot(chart_Series(sample.xts[subset,1], TA=ta))
>>>
>>> A plot appears.  No errors.  Was this
>>> what you were looking for?
>>>
>>
>>
>> Guys :)
>>
>> chart_Series() is a plot env/func, so plot(chart_Series()) make no sense :)
>>
> Except that it is necessary to wrap chart_Series() in a call to plot()
> if you call it from within a function, for loop, or any other
> situation where R's auto-printing is disabled.
>

I agree, but in this example earlier it was pointless.

https://github.com/joshuaulrich/quantmod/blob/master/R/replot.R#L202

> c <- quantmod::current.chob()
> str(c)
Classes 'replot', 'environment' <environment: 0x7f9cbdaa8fd8>

So print.replot() will call plot.replot() automatically.




> Cheers,
> Josh
>
>
>>
>> Try:
>>
>> chart_Series(sample.xts[subset,1], TA=ta)
>>
>> ?chart_Series
>>
>> But your problem is here:
>>
>> sample.xts[subset, 2]
>>
>>
>> Best,
>> Daniel
>>
>>
>>
>>
>>> /Rasmus
>>> _______________________________________________
>>> [hidden email] 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.
>>
>> _______________________________________________
>> [hidden email] <mailto:[hidden email]> mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-sig-finance <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 <http://about.me/joshuaulrich>
> FOSS Trading  |  www.fosstrading.com <http://www.fosstrading.com/>

        [[alternative HTML version deleted]]

_______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Empty indicator / plot.window: need finite 'ylim' values

Mike-3
In reply to this post by Daniel Cegiełka
> > On 17 Aug 2020, at 04:55, Rasmus Liland <[hidden email]> wrote:

> >     plot(chart_Series(sample.xts[subset,1], TA=ta))
> >
> > A plot appears.  No errors.

For now. But when you add an indicator like add_BBands() which is
calculated within chart_Series, this indicator will start at
observation 20, not from the beginning. This is because you call
chart_Series without lead time for calculation.


On 17.08. Daniel Cegiełka wrote:

> chart_Series() is a plot env/func, so plot(chart_Series()) make no sense :)

Like Joshua wrote, indeed, I use chart_Series within a function.

> But your problem is here:
>
> sample.xts[subset, 2]

Yes, not the main window is the issue, but the indicator is. So I
have to ask more specific:

Can I plot a subset of chart and indicator where the indicator's
subset is completely NA resulting in the indicator's plot region
being left blank?

Mike


The new minimal reproducible:

library(quantmod)

my_plot_function <- function () {
  data (sample_matrix)
  sample.xts <- as.xts (sample_matrix[1:50,'Close'], dateFormat="POSIXct")
  sample.xts <- cbind (sample.xts, NA)
  sample.xts[50,2] <- 0
  colnames (sample.xts) <- c('Close', 'Stops')

  ta <- list ("add_TA(sample.xts[,2])")

  # In the range to be plotted ta is completely NA
  subset <- '2007-01-10::2007-01-30'

  plot (chart_Series (sample.xts[,1], subset=subset, TA=ta))
}

my_plot_function ()

_______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Empty indicator / plot.window: need finite 'ylim' values

Daniel Cegiełka


> On 17 Aug 2020, at 22:42, Mike <[hidden email]> wrote:

> Like Joshua wrote, indeed, I use chart_Series within a function.
>
>> But your problem is here:
>>
>> sample.xts[subset, 2]
>
> Yes, not the main window is the issue, but the indicator is. So I
> have to ask more specific:
>
> Can I plot a subset of chart and indicator where the indicator's
> subset is completely NA resulting in the indicator's plot region
> being left blank?
>

you can try to force a value for ylim in the quantmod’s chart object:

x <- chart_Series (sample.xts[,1], subset=subset, TA=ta)

> x$Env$ylim
[[1]]
[1] 0 1
attr(,"fixed")
[1] FALSE

[[2]]
[1] 49.88096 50.67835
attr(,"fixed")
[1] FALSE

[[3]]
[1] 0 1
attr(,"fixed")
[1] FALSE

[[4]]
[1] -Inf  Inf
attr(,"fixed")
[1] FALSE


e.g.:

> x$Env$ylim[[4]] <- structure(c(0, 1), 'fixed' = FALSE)
>
> x$Env$ylim[[4]]
[1] 0 1
attr(,"fixed")
[1] FALSE

however, it is still overwritten:

> x  # or plot(x)
Error in plot.window(c(1, 21), c(NaN, NaN)) : need finite 'ylim' values
In addition: Warning messages:
1: In min(x) : no non-missing arguments to min; returning Inf
2: In max(x) : no non-missing arguments to max; returning -Inf
> x$Env$ylim[[4]]
[1] -Inf  Inf
attr(,"fixed")
[1] FALSE



> Mike
>
>
> The new minimal reproducible:
>
> library(quantmod)
>
> my_plot_function <- function () {
>  data (sample_matrix)
>  sample.xts <- as.xts (sample_matrix[1:50,'Close'], dateFormat="POSIXct")
>  sample.xts <- cbind (sample.xts, NA)
>  sample.xts[50,2] <- 0
>  colnames (sample.xts) <- c('Close', 'Stops')
>
>  ta <- list ("add_TA(sample.xts[,2])")
>
>  # In the range to be plotted ta is completely NA
>  subset <- '2007-01-10::2007-01-30'
>
>  plot (chart_Series (sample.xts[,1], subset=subset, TA=ta))
> }
>
> my_plot_function ()
>
> _______________________________________________
> [hidden email] 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.

_______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Empty indicator / plot.window: need finite 'ylim' values

Joshua Ulrich
I don't think this is possible with chart_Series() currently.

It looks similar to
https://github.com/joshuaulrich/quantmod/issues/81

Could you please file an issue on the GitHub tracker?  I'm not sure
when I'll have a chance to take a closer look, but the tracker issue
will make sure I don't lose track of it.

Best,
Josh

On Mon, Aug 17, 2020 at 4:46 PM Daniel Cegiełka
<[hidden email]> wrote:

>
>
>
> > On 17 Aug 2020, at 22:42, Mike <[hidden email]> wrote:
>
> > Like Joshua wrote, indeed, I use chart_Series within a function.
> >
> >> But your problem is here:
> >>
> >> sample.xts[subset, 2]
> >
> > Yes, not the main window is the issue, but the indicator is. So I
> > have to ask more specific:
> >
> > Can I plot a subset of chart and indicator where the indicator's
> > subset is completely NA resulting in the indicator's plot region
> > being left blank?
> >
>
> you can try to force a value for ylim in the quantmod’s chart object:
>
> x <- chart_Series (sample.xts[,1], subset=subset, TA=ta)
>
> > x$Env$ylim
> [[1]]
> [1] 0 1
> attr(,"fixed")
> [1] FALSE
>
> [[2]]
> [1] 49.88096 50.67835
> attr(,"fixed")
> [1] FALSE
>
> [[3]]
> [1] 0 1
> attr(,"fixed")
> [1] FALSE
>
> [[4]]
> [1] -Inf  Inf
> attr(,"fixed")
> [1] FALSE
>
>
> e.g.:
>
> > x$Env$ylim[[4]] <- structure(c(0, 1), 'fixed' = FALSE)
> >
> > x$Env$ylim[[4]]
> [1] 0 1
> attr(,"fixed")
> [1] FALSE
>
> however, it is still overwritten:
>
> > x  # or plot(x)
> Error in plot.window(c(1, 21), c(NaN, NaN)) : need finite 'ylim' values
> In addition: Warning messages:
> 1: In min(x) : no non-missing arguments to min; returning Inf
> 2: In max(x) : no non-missing arguments to max; returning -Inf
> > x$Env$ylim[[4]]
> [1] -Inf  Inf
> attr(,"fixed")
> [1] FALSE
>
>
>
> > Mike
> >
> >
> > The new minimal reproducible:
> >
> > library(quantmod)
> >
> > my_plot_function <- function () {
> >  data (sample_matrix)
> >  sample.xts <- as.xts (sample_matrix[1:50,'Close'], dateFormat="POSIXct")
> >  sample.xts <- cbind (sample.xts, NA)
> >  sample.xts[50,2] <- 0
> >  colnames (sample.xts) <- c('Close', 'Stops')
> >
> >  ta <- list ("add_TA(sample.xts[,2])")
> >
> >  # In the range to be plotted ta is completely NA
> >  subset <- '2007-01-10::2007-01-30'
> >
> >  plot (chart_Series (sample.xts[,1], subset=subset, TA=ta))
> > }
> >
> > my_plot_function ()
> >
> > _______________________________________________
> > [hidden email] 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.
>
> _______________________________________________
> [hidden email] 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

_______________________________________________
[hidden email] 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.