Quantcast

Quantmod graphing issue

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Quantmod graphing issue

Jon Golenbock
Hi, having an issue and I'm running into a wall. What I'm trying to do is
set up some alerts given a list of products, terms, and rules. For example,
look at the prompt 3 months for natural gas and crude, and if there is RSI
divergence on any of those charts, graph them with the relevant technical
indicators) and email them out (haven't written this part yet but it should
be pretty simple I think).

The issue I'm having might be a graphics issue (my suspicion) or I might be
using a purrr function incorrectly, as I'm still figuring out how to best
use it's mapping functions.

Code below -- should be spitting out two graphs, but it only creates one
and then throws the error "Error: is.list(res) is not TRUE". This
particular (vanilla) example is looking to flag natgas pricing as it breaks
bollinger bands.

Worth noting, I am using the lmap() function instead of the map() function
because I want the name passed as well. i.e if I pass df[[1]] to
graph_send(), it will pass an unnamed dataframe. if i pass df[1], it passes
a (named) list containing the dataframe, which I can still access using
mylist[[1]].

Any help is appreciated, thank you in advance!!

library(TTR)
library(dplyr)
library(purrr)
library(magrittr)
library(quantmod)

graph_send <- function(mylist){
  #print(df %>% str())
  df2 <- mylist[[1]] %<>% dplyr::select(COB_DT, PRICE,QUOTE_DEF_CD) %>%
    dplyr::rename (Date = COB_DT, Close = PRICE) %>%
    dplyr::mutate (Open = lag(Close))


  df2$Open[1] <- df2$Close[1]
  df2$High <- pmax(df2$Close, df2$Open)
  df2$Low <- pmin(df2$Close, df2$Open)
  #print(df2 %>% str())


  df3 <- df2 %>% select(Date, Open, High, Low, Close)
  ts <- xts(df3[,-1], df3[,1]) %>% zoo::na.locf()
  #print(ts)



  candleChart(ts, theme = "white", name = names(mylist))
  plot(addBBands())
}

df <- structure(list(`Henry Hub xh19` = structure(list(COB_DT =
structure(c(17151,

17152, 17153, 17154, 17155, 17156, 17157, 17158, 17159, 17160,

17161, 17162, 17163, 17164, 17165, 17166, 17167, 17168, 17169,

17170, 17171, 17172, 17173, 17174, 17175, 17176, 17177, 17178,

17179, 17180, 17181, 17182, 17183, 17184, 17185, 17186, 17187,

17188, 17189, 17190, 17191, 17192, 17193, 17194, 17195, 17196,

17197, 17198, 17199, 17200, 17201, 17202, 17203, 17204, 17205,

17206, 17207, 17208, 17209, 17210), class = "Date"), PRICE = c(-0.86,

                                                               -0.86,
-0.86, -0.86, -0.83, -0.87, -0.87, -0.83, -0.83, -0.83,

                                                               -0.83,
-0.81, -0.81, -0.78, -0.77, -0.77, -0.77, -0.77, -0.75,

                                                               -0.775,
-0.755, -0.755, -0.755, -0.755, -0.73, -0.74, -0.72,

                                                               -0.72,
-0.72, -0.72, -0.72, -0.72, -0.72, -0.71, -0.71, -0.7,

                                                               -0.7, -0.7,
-0.7, -0.735, -0.7, -0.7, -0.72, -0.72, -0.72, -0.72,

                                                               -0.72,
-0.72, -0.72, -0.72, -0.72, -0.72, -0.665, -0.68, -0.635,

                                                               -0.635,
-0.635, -0.635, -0.635, -0.585), CURVE_START_DT = c("",


                                               "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "",


                                               "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "",


                                               "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "",


                                               "", "", "", "", "", "", "",
"", "", "", ""), QUOTE_DEF_CD = c("Dominion South Basis",



                                 "Dominion South Basis", "Dominion South
Basis", "Dominion South Basis",



                                 "Dominion South Basis", "Dominion South
Basis", "Dominion South Basis",



                                 "Dominion South Basis", "Dominion South
Basis", "Dominion South Basis",



                                 "Dominion South Basis", "Dominion South
Basis", "Dominion South Basis",



                                 "Dominion South Basis", "Dominion South
Basis", "Dominion South Basis",



                                 "Dominion South Basis", "Dominion South
Basis", "Dominion South Basis",



                                 "Dominion South Basis", "Dominion South
Basis", "Dominion South Basis",



                                 "Dominion South Basis", "Dominion South
Basis", "Dominion South Basis",



                                 "Dominion South Basis", "Dominion South
Basis", "Dominion South Basis",



                                 "Dominion South Basis", "Dominion South
Basis", "Dominion South Basis",



                                 "Dominion South Basis", "Dominion South
Basis", "Dominion South Basis",



                                 "Dominion South Basis", "Dominion South
Basis", "Dominion South Basis",



                                 "Dominion South Basis", "Dominion South
Basis", "Dominion South Basis",



                                 "Dominion South Basis", "Dominion South
Basis", "Dominion South Basis",



                                 "Dominion South Basis", "Dominion South
Basis", "Dominion South Basis",



                                 "Dominion South Basis", "Dominion South
Basis", "Dominion South Basis",



                                 "Dominion South Basis", "Dominion South
Basis", "Dominion South Basis",



                                 "Dominion South Basis", "Dominion South
Basis", "Dominion South Basis",



                                 "Dominion South Basis", "Dominion South
Basis", "Dominion South Basis",



                                 "Dominion South Basis", "Dominion South
Basis"), dn = c(NA, NA,




             NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA,




             NA, -0.894741986767873, -0.891255877526362, -0.88636710179481,




             -0.880094879733141, -0.872383869796585, -0.870767185654083,
-0.858363089262289,




             -0.844740808202286, -0.83891646141184, -0.831269081190139,
-0.821607558621575,




             -0.809474309040864, -0.800695515162401, -0.789713052669259,
-0.785893441660804,




             -0.783099173310338, -0.780390228494039, -0.77657550056119,
-0.771630648315995,




             -0.769709751494918, -0.761836566749306, -0.757707733980463,
-0.752643214660164,




             -0.746563490998189, -0.739223944189474, -0.737874662159694,
-0.733967566619156,




             -0.733967566619156, -0.733967566619156, -0.733967566619156,
-0.733967566619156,




             -0.733967566619156, -0.733967566619156, -0.740086079144498,
-0.741578029864691,




             -0.75138715591489, -0.757590834083357, -0.761721993725188,
-0.764454364360499,




             -0.766130855814034, -0.769658234195486), mavg = c(NA, NA, NA,




                                                               NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,




                                                               -0.81675,
-0.8115, -0.80625, -0.801, -0.79575, -0.79075, -0.78425,




                                                               -0.77675,
-0.77125, -0.76575, -0.76025, -0.75475, -0.75025, -0.74575,




                                                               -0.74225,
-0.73925, -0.73575, -0.73225, -0.72875, -0.72625, -0.72425,




                                                               -0.7215,
-0.71875, -0.717, -0.71525, -0.71475, -0.71375, -0.71375,




                                                               -0.71375,
-0.71375, -0.71375, -0.71375, -0.71375, -0.711, -0.7095,




                                                               -0.70575,
-0.7025, -0.69925, -0.696, -0.69275, -0.68525), up = c(NA,





                                                    NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,





                                                    NA, NA,
-0.738758013232128, -0.731744122473638, -0.72613289820519,





                                                    -0.721905120266859,
-0.719116130203415, -0.710732814345917, -0.710136910737711,





                                                    -0.708759191797714,
-0.70358353858816, -0.700230918809861, -0.698892441378425,





                                                    -0.700025690959136,
-0.699804484837599, -0.701786947330742, -0.698606558339196,





                                                    -0.695400826689663,
-0.691109771505961, -0.687924499438811, -0.685869351684006,





                                                    -0.682790248505082,
-0.686663433250695, -0.685292266019537, -0.684856785339835,





                                                    -0.687436509001811,
-0.691276055810526, -0.691625337840306, -0.693532433380844,





                                                    -0.693532433380844,
-0.693532433380844, -0.693532433380844, -0.693532433380844,





                                                    -0.693532433380844,
-0.693532433380844, -0.681913920855502, -0.677421970135309,





                                                    -0.66011284408511,
-0.647409165916643, -0.636778006274811, -0.627545635639501,





                                                    -0.619369144185966,
-0.600841765804514), pctB = c(NA, NA, NA,






                          NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA,






                          0.767655702400944, 0.854205870165024,
0.819844320699827, 0.790789999018899,






                          0.765874606803529, 0.879605952792593,
0.798530262605824, 0.917335824507027,






                          0.878695729987092, 0.849134932671232,
0.827995449169055, 0.817500582547799,






                          0.799828437697737, 0.792859554063748,
0.869471320005496, 0.833529663067839,






                          0.900423577634397, 0.863786077897535,
0.835232804645777, 0.8020035676351,






                          0.356996781966012, 0.796897894958033,
0.77660403694367, 0.449261743138118,






                          0.400934114919529, 0.38648482810809,
0.345431447865779, 0.345431447865779,






                          0.345431447865779, 0.345431447865779,
0.345431447865779, 0.345431447865779,






                          0.345431447865779, 1.29075628879841,
0.959816268711555, 1.27513594549958,






                          1.11262459647885, 1.01423042685842,
0.945552307510719, 0.893495001927707,






                          1.09384016829345), cross_up = c(NA, NA, NA, NA,
NA, NA, NA, NA,






                                                          NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, FALSE, FALSE, FALSE,






                                                          FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,






                                                          FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,






                                                          FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,






                                                          FALSE, FALSE,
FALSE, TRUE, FALSE, TRUE, TRUE, TRUE, FALSE, FALSE,






                                                          TRUE), cross_dn =
c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,







  NA, NA, NA, NA, NA, NA, NA, NA, FALSE, FALSE, FALSE, FALSE, FALSE,







  FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,







  FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,







  FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,







  FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE






                                                          )), class =
"data.frame", .Names = c("COB_DT", "PRICE", "CURVE_START_DT",







                   "QUOTE_DEF_CD", "dn", "mavg", "up", "pctB", "cross_up",
"cross_dn"






                                                          ), row.names =
c(NA, -60L)), `Dominion South Basis xh19` = structure(list(






                                                            COB_DT =
structure(c(17151, 17152, 17153, 17154, 17155, 17156,







     17157, 17158, 17159, 17160, 17161, 17162, 17163, 17164, 17165,







     17166, 17167, 17168, 17169, 17170, 17171, 17172, 17173, 17174,







     17175, 17176, 17177, 17178, 17179, 17180, 17181, 17182, 17183,







     17184, 17185, 17186, 17187, 17188, 17189, 17190, 17191, 17192,







     17193, 17194, 17195, 17196, 17197, 17198, 17199, 17200, 17201,







     17202, 17203, 17204, 17205, 17206, 17207, 17208, 17209, 17210






                                                            ), class =
"Date"), PRICE = c(2.95, 2.95, 2.95, 2.975, 2.905,







              2.96, 2.955, 2.95, 2.95, 2.95, 2.95, 2.96, 2.955, 2.96, 2.97,







              2.97, 2.97, 2.97, 2.94, 2.92, 2.925, 2.925, 2.925, 2.925,







              2.895, 2.94, 2.95, 2.97, 2.97, 2.97, 2.97, 2.97, 2.975,
2.975,







              2.975, 2.95, 2.95, 2.95, 2.965, 2.955, 2.945, 2.955, 2.955,







              2.955, 2.955, 2.95, 2.945, 2.96, 2.98, 2.935, 2.935, 2.935,







              2.95, 2.98, 2.97, 2.975, 2.96, 2.96, 2.96, 2.94),
CURVE_START_DT = c("",








       "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",








       "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",








       "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",








       "", "", "", "", "", "", "", "", "", "", "", "", "", ""),






                                                            QUOTE_DEF_CD =
c("Henry Hub", "Henry Hub", "Henry Hub", "Henry Hub",







 "Henry Hub", "Henry Hub", "Henry Hub", "Henry Hub", "Henry Hub",







 "Henry Hub", "Henry Hub", "Henry Hub", "Henry Hub", "Henry Hub",







 "Henry Hub", "Henry Hub", "Henry Hub", "Henry Hub", "Henry Hub",







 "Henry Hub", "Henry Hub", "Henry Hub", "Henry Hub", "Henry Hub",







 "Henry Hub", "Henry Hub", "Henry Hub", "Henry Hub", "Henry Hub",







 "Henry Hub", "Henry Hub", "Henry Hub", "Henry Hub", "Henry Hub",







 "Henry Hub", "Henry Hub", "Henry Hub", "Henry Hub", "Henry Hub",







 "Henry Hub", "Henry Hub", "Henry Hub", "Henry Hub", "Henry Hub",







 "Henry Hub", "Henry Hub", "Henry Hub", "Henry Hub", "Henry Hub",







 "Henry Hub", "Henry Hub", "Henry Hub", "Henry Hub", "Henry Hub",







 "Henry Hub", "Henry Hub", "Henry Hub", "Henry Hub", "Henry Hub",







 "Henry Hub"), dn = c(NA, NA, NA, NA, NA, NA, NA, NA, NA,







                      NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
2.92007584473369,







                      2.91663946027188, 2.9135, 2.9106138770061,
2.9086350423062,







                      2.90576852275423, 2.90519066900209, 2.90512519592525,
2.90469624714387,







                      2.90440892112191, 2.90425071429154, 2.90421160864688,
2.90401373833782,







                      2.90371324973884, 2.90340911790111, 2.90321956918171,
2.90297106144673,







                      2.90282023993206, 2.9027725465393, 2.90365498942402,
2.90744321845618,







                      2.9100147084275, 2.9134290744498, 2.91717246420653,
2.92130686340485,







                      2.93832051661132, 2.94027625158384, 2.93939934053801,
2.93938458814558,







                      2.9384612414305, 2.9346043937093, 2.93139748533183,
2.92873214892687,







                      2.92907714041705, 2.92838750305027, 2.92887531407233,
2.92854248688935,







                      2.9290613918672, 2.92961818808345, 2.92962051473909,
2.92798148782779







 ), mavg = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,







             NA, NA, NA, NA, NA, NA, NA, 2.953, 2.95175, 2.9505, 2.94925,







             2.94675, 2.94625, 2.94525, 2.945, 2.946, 2.947, 2.948, 2.949,







             2.9495, 2.9505, 2.95125, 2.9515, 2.9505, 2.9495, 2.9485,







             2.94975, 2.9515, 2.9525, 2.954, 2.9555, 2.957, 2.96, 2.9605,







             2.96025, 2.95975, 2.96025, 2.9585, 2.95675, 2.955, 2.95375,







             2.954, 2.95375, 2.955, 2.9555, 2.956, 2.95575, 2.955), up =
c(NA,








 NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,








 NA, NA, NA, 2.98592415526631, 2.98686053972812, 2.9875, 2.9878861229939,








 2.9848649576938, 2.98673147724577, 2.98530933099791, 2.98487480407475,








 2.98730375285613, 2.98959107887809, 2.99174928570845, 2.99378839135312,








 2.99498626166217, 2.99728675026116, 2.99909088209889, 2.99978043081829,








 2.99802893855326, 2.99617976006793, 2.99422745346069, 2.99584501057598,








 2.99555678154382, 2.9949852915725, 2.9945709255502, 2.99382753579347,








 2.99269313659515, 2.98167948338868, 2.98072374841616, 2.98110065946199,








 2.98011541185442, 2.9820387585695, 2.9823956062907, 2.98210251466817,








 2.98126785107313, 2.97842285958295, 2.97961249694973, 2.97862468592767,








 2.98145751311065, 2.9819386081328, 2.98238181191655, 2.98187948526091,








 2.98201851217221), pctB = c(NA, NA, NA, NA, NA, NA, NA, NA,








                           NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
-0.00115180986540484,








                           0.119060256448094, 0.155405405405416,
0.186174515959769,








                           0.214678943438289, -0.133005555711918,
0.434472195750436,








                           0.56269623282195, 0.790530500746511,
0.770009595974702, 0.75143267648539,








                           0.734435747361799, 0.725342765605294,
0.761826263453277,








                           0.748218667361821, 0.743369825017154,
0.494740046640864,








                           0.505355640209733, 0.516401525631536,
0.665419204914405,








                           0.539721467131223, 0.411734158782905,
0.512324096461183,








                           0.493477274371434, 0.471983409265977,
0.384683598996404,








                           0.240404207372084, 0.134304132495249,
0.50613785770176, 0.953215357290844,








                           0.00827780400054887, 0.071048468274765,
0.119306506186558,








                           0.424005565966269, 1.00756472613798,
0.826637289959252, 0.877964473009239,








                           0.585102815878129, 0.575809804357899,
0.581325750537434,








                           0.222412546175904), cross_up = c(NA, NA, NA, NA,
NA, NA,








                                                            NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, FALSE,








                                                            FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,








                                                            FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,








                                                            FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,








                                                            FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE,








                                                            FALSE, FALSE,
FALSE, FALSE), cross_dn = c(NA, NA, NA, NA,









                          NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA,









                          TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE,
FALSE, FALSE,









                          FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE,









                          FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE,









                          FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE,









                          FALSE, FALSE, FALSE, FALSE, FALSE)), class =
"data.frame", .Names = c("COB_DT",










                    "PRICE", "CURVE_START_DT", "QUOTE_DEF_CD", "dn",
"mavg", "up",










                    "pctB", "cross_up", "cross_dn"), row.names = c(NA,
-60L)), `Henry Hub jv18` = structure(list(










                      COB_DT = structure(c(17151, 17152, 17153, 17154,
17155, 17156,










                                           17157, 17158, 17159, 17160,
17161, 17162, 17163, 17164, 17165,










                                           17166, 17167, 17168, 17169,
17170, 17171, 17172, 17173, 17174,










                                           17175, 17176, 17177, 17178,
17179, 17180, 17181, 17182, 17183,










                                           17184, 17185, 17186, 17187,
17188, 17189, 17190, 17191, 17192,










                                           17193, 17194, 17195, 17196,
17197, 17198, 17199, 17200, 17201,










                                           17202, 17203, 17204, 17205,
17206, 17207, 17208, 17209, 17210










                      ), class = "Date"), PRICE = c(-0.58, -0.58, -0.58,
-0.645,










                                                    -0.61, -0.675, -0.685,
-0.685, -0.685, -0.685, -0.685, -0.63,










                                                    -0.62, -0.58, -0.59,
-0.59, -0.59, -0.59, -0.555, -0.545,










                                                    -0.51, -0.49, -0.49,
-0.49, -0.465, -0.48, -0.46, -0.46,










                                                    -0.43, -0.43, -0.43,
-0.43, -0.43, -0.41, -0.38, -0.37, -0.37,










                                                    -0.37, -0.37, -0.385,
-0.37, -0.35, -0.465, -0.465, -0.465,










                                                    -0.5, -0.49, -0.475,
-0.475, -0.375, -0.375, -0.375, -0.295,










                                                    -0.295, -0.265, -0.285,
-0.26, -0.26, -0.26, -0.27), CURVE_START_DT = c("",











                                                "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "",











                                                "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "",











                                                "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "",











                                                "", "", "", "", "", "", "",
"", "", "", "", "", "", ""),










                      QUOTE_DEF_CD = c("Dominion South Basis", "Dominion
South Basis",










                                       "Dominion South Basis", "Dominion
South Basis", "Dominion South Basis",










                                       "Dominion South Basis", "Dominion
South Basis", "Dominion South Basis",










                                       "Dominion South Basis", "Dominion
South Basis", "Dominion South Basis",










                                       "Dominion South Basis", "Dominion
South Basis", "Dominion South Basis",










                                       "Dominion South Basis", "Dominion
South Basis", "Dominion South Basis",










                                       "Dominion South Basis", "Dominion
South Basis", "Dominion South Basis",










                                       "Dominion South Basis", "Dominion
South Basis", "Dominion South Basis",










                                       "Dominion South Basis", "Dominion
South Basis", "Dominion South Basis",










                                       "Dominion South Basis", "Dominion
South Basis", "Dominion South Basis",










                                       "Dominion South Basis", "Dominion
South Basis", "Dominion South Basis",










                                       "Dominion South Basis", "Dominion
South Basis", "Dominion South Basis",










                                       "Dominion South Basis", "Dominion
South Basis", "Dominion South Basis",










                                       "Dominion South Basis", "Dominion
South Basis", "Dominion South Basis",










                                       "Dominion South Basis", "Dominion
South Basis", "Dominion South Basis",










                                       "Dominion South Basis", "Dominion
South Basis", "Dominion South Basis",










                                       "Dominion South Basis", "Dominion
South Basis", "Dominion South Basis",










                                       "Dominion South Basis", "Dominion
South Basis", "Dominion South Basis",










                                       "Dominion South Basis", "Dominion
South Basis", "Dominion South Basis",










                                       "Dominion South Basis", "Dominion
South Basis", "Dominion South Basis",










                                       "Dominion South Basis"), dn = c(NA,
NA, NA, NA, NA, NA, NA,










                                                                       NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, -0.713921801503932,











 -0.720596316101235, -0.728803179455088, -0.735135162694137,











 -0.735660162446852, -0.740184328913496, -0.732877433700339,











 -0.722778780170072, -0.709329903557334, -0.695083239700345,











 -0.676323574997258, -0.652432125306766, -0.638170250551281,











 -0.623004896808335, -0.615203884726974, -0.605732722363753,











 -0.593744876941616, -0.577680534934208, -0.556963571902068,











 -0.541539596198718, -0.524069645625611, -0.513641930997681,











 -0.507274996479865, -0.50260074692116, -0.497706792863262,











 -0.497706792863262, -0.50222910301065, -0.507668380045843,











 -0.510039591914492, -0.515420178875468, -0.514712232154113,











 -0.513652444925869, -0.512258837989343, -0.517680034178804,











 -0.522046125247264, -0.529833230325465, -0.533588195172019,











 -0.53892091461952, -0.542798305725634, -0.545459666783158,











 -0.545505649627349), mavg = c(NA, NA, NA, NA, NA, NA, NA,











                         NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
-0.61925,











                         -0.61575, -0.61125, -0.60675, -0.599, -0.59175,
-0.582, -0.57075,











                         -0.5595, -0.54675, -0.534, -0.52125, -0.51125,
-0.50175,











                         -0.49325, -0.48275, -0.47175, -0.46075, -0.44975,
-0.4405,











                         -0.4325, -0.4255, -0.4185, -0.41725, -0.416,
-0.416, -0.417,











                         -0.4185, -0.41925, -0.4215, -0.41875, -0.416,
-0.41325, -0.4065,











                         -0.40075, -0.395, -0.39075, -0.38525, -0.37975,
-0.37425,











                         -0.3685), up = c(NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA,











                                          NA, NA, NA, NA, NA, NA, NA, NA,
NA, -0.524578198496067, -0.510903683898765,











                                          -0.493696820544912,
-0.478364837305863, -0.462339837553148,











                                          -0.443315671086504,
-0.431122566299662, -0.418721219829928,











                                          -0.409670096442666,
-0.398416760299655, -0.391676425002742,











                                          -0.390067874693234,
-0.384329749448719, -0.380495103191665,











                                          -0.371296115273026,
-0.359767277636247, -0.349755123058384,











                                          -0.343819465065792,
-0.342536428097931, -0.339460403801282,











                                          -0.340930354374389,
-0.337358069002319, -0.329725003520135,











                                          -0.331899253078839,
-0.334293207136738, -0.334293207136738,











                                          -0.33177089698935,
-0.329331619954156, -0.328460408085508,











                                          -0.327579821124531,
-0.322787767845886, -0.31834755507413,











                                          -0.314241162010656,
-0.295319965821196, -0.279453874752735,











                                          -0.260166769674534,
-0.24791180482798, -0.231579085380479,











                                          -0.216701694274365,
-0.203040333216842, -0.19149435037265











                         ), pctB = c(NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA,











                                     NA, NA, NA, NA, NA, NA, NA,
0.892144222569356, 1.00430956438132,











                                     1.01572403469667, 0.954686497839878,
0.898799467410229, 0.92695648953911,











                                     0.838022716513673, 0.86424024410413,
0.832043195776087, 0.893539574258111,











                                     0.865364627757573, 0.847798908527417,
0.820082885304309,











                                     0.795864339868325, 0.841317540586662,
0.917741606402606,











                                     0.917025708582399, 0.888050905826529,
0.871921197032991,











                                     0.848873128220668, 0.7593654244017,
0.814833129770324, 0.885806830279832,











                                     0.220271926594225, 0.200147329965559,
0.200147329965559,











                                     0.0130771234936691,
0.0990731245580526, 0.192971414319677,











                                     0.215183677029951, 0.72795426397407,
0.709928179633002, 0.693164573874287,











                                     1.00143895360151, 0.935916645253204,
0.982077006114149, 0.870174097595693,











                                     0.907526695308968, 0.867222460445268,
0.83365522562666, 0.778239706493471











                         ), cross_up = c(NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA,











                                         NA, NA, NA, NA, NA, NA, NA, NA,
FALSE, TRUE, TRUE, FALSE,











                                         FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE,











                                         FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE,











                                         FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE,











                                         FALSE, FALSE, TRUE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE,











                                         FALSE), cross_dn = c(NA, NA, NA,
NA, NA, NA, NA, NA, NA,











                                                              NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, FALSE, FALSE, FALSE,











                                                              FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,











                                                              FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,











                                                              FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,











                                                              FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,











                                                              FALSE,
FALSE)), class = "data.frame", .Names = c("COB_DT",












                                   "PRICE", "CURVE_START_DT",
"QUOTE_DEF_CD", "dn", "mavg", "up",












                                   "pctB", "cross_up", "cross_dn"),
row.names = c(NA, -60L)), `Dominion South Basis jv18` = structure(list(












                                     COB_DT = structure(c(17151, 17152,
17153, 17154, 17155, 17156,












                                                          17157, 17158,
17159, 17160, 17161, 17162, 17163, 17164, 17165,












                                                          17166, 17167,
17168, 17169, 17170, 17171, 17172, 17173, 17174,












                                                          17175, 17176,
17177, 17178, 17179, 17180, 17181, 17182, 17183,












                                                          17184, 17185,
17186, 17187, 17188, 17189, 17190, 17191, 17192,












                                                          17193, 17194,
17195, 17196, 17197, 17198, 17199, 17200, 17201,












                                                          17202, 17203,
17204, 17205, 17206, 17207, 17208, 17209, 17210












                                     ), class = "Date"), PRICE = c(2.92,
2.92, 2.92, 2.94, 2.86,












                                                                   2.93,
2.93, 2.94, 2.94, 2.94, 2.94, 2.965, 2.97, 2.975, 2.98,












                                                                   2.98,
2.98, 2.98, 2.92, 2.89, 2.9, 2.91, 2.91, 2.91, 2.85,












                                                                   2.905,
2.91, 2.96, 2.97, 2.97, 2.97, 2.97, 2.985, 2.965,












                                                                   2.975,
2.92, 2.92, 2.92, 2.94, 2.94, 2.95, 2.96, 2.96, 2.96,












                                                                   2.96,
2.945, 2.925, 2.945, 2.97, 2.92, 2.92, 2.92, 2.93,












                                                                   2.965,
2.955, 2.96, 2.935, 2.935, 2.935, 2.905), CURVE_START_DT = c("",













                                                           "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "",













                                                           "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "",













                                                           "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "",













                                                           "", "", "", "",
"", "", "", "", "", "", "", "", "", ""),












                                     QUOTE_DEF_CD = c("Henry Hub", "Henry
Hub", "Henry Hub", "Henry Hub",












                                                      "Henry Hub", "Henry
Hub", "Henry Hub", "Henry Hub", "Henry Hub",












                                                      "Henry Hub", "Henry
Hub", "Henry Hub", "Henry Hub", "Henry Hub",












                                                      "Henry Hub", "Henry
Hub", "Henry Hub", "Henry Hub", "Henry Hub",












                                                      "Henry Hub", "Henry
Hub", "Henry Hub", "Henry Hub", "Henry Hub",












                                                      "Henry Hub", "Henry
Hub", "Henry Hub", "Henry Hub", "Henry Hub",












                                                      "Henry Hub", "Henry
Hub", "Henry Hub", "Henry Hub", "Henry Hub",












                                                      "Henry Hub", "Henry
Hub", "Henry Hub", "Henry Hub", "Henry Hub",












                                                      "Henry Hub", "Henry
Hub", "Henry Hub", "Henry Hub", "Henry Hub",












                                                      "Henry Hub", "Henry
Hub", "Henry Hub", "Henry Hub", "Henry Hub",












                                                      "Henry Hub", "Henry
Hub", "Henry Hub", "Henry Hub", "Henry Hub",












                                                      "Henry Hub", "Henry
Hub", "Henry Hub", "Henry Hub", "Henry Hub",












                                                      "Henry Hub"), dn =
c(NA, NA, NA, NA, NA, NA, NA, NA, NA,













 NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 2.87802381402466,













 2.87511548720997, 2.87385702627089, 2.87262229289889, 2.86993521627356,













 2.86704287027043, 2.8644565921701, 2.86260604668442, 2.86279282077821,













 2.86278692190085, 2.8629294882634, 2.86321267138588, 2.86311137034422,













 2.86245437829422, 2.86269393040897, 2.86291778554333, 2.86163828369988,













 2.86088614260888, 2.86071158286838, 2.86182468606366, 2.86702941812813,













 2.87120520382413, 2.87427329309938, 2.87773118034116, 2.88159653006341,













 2.90425067568061, 2.9110874415183, 2.9144846804476, 2.91370567051746,













 2.91370567051746, 2.90962674152363, 2.90620835435588, 2.90340673131383,













 2.90450413929979, 2.90450413929979, 2.90605411955924, 2.90856931373228,













 2.91074342170337, 2.91305220827881, 2.91257822060098, 2.90649051848137












                                                      ), mavg = c(NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,












                                                                  NA, NA,
NA, NA, NA, NA, NA, 2.941, 2.94, 2.9395, 2.939, 2.9375,












                                                                  2.937,
2.93575, 2.93475, 2.93575, 2.93725, 2.93875, 2.94025,












                                                                  2.9405,
2.94125, 2.94075, 2.9405, 2.9375, 2.9345, 2.9315,












                                                                  2.9325,
2.935, 2.9375, 2.94, 2.9425, 2.945, 2.9505, 2.9525,












                                                                  2.95325,
2.9525, 2.9525, 2.95, 2.9475, 2.945, 2.94225, 2.94225,












                                                                  2.94125,
2.94325, 2.944, 2.94475, 2.9445, 2.94275), up = c(NA,













                                                 NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA,













                                                 NA, NA, NA,
3.00397618597533, 3.00488451279003, 3.00514297372911,













                                                 3.0053777071011,
3.00506478372643, 3.00695712972957, 3.0070434078299,













                                                 3.00689395331558,
3.00870717922179, 3.01171307809915, 3.0145705117366,













                                                 3.01728732861412,
3.01788862965578, 3.02004562170578, 3.01880606959103,













                                                 3.01808221445667,
3.01336171630012, 3.00811385739112, 3.00228841713162,













                                                 3.00317531393634,
3.00297058187187, 3.00379479617587, 3.00572670690062,













                                                 3.00726881965885,
3.00840346993659, 2.99674932431939, 2.9939125584817,













                                                 2.9920153195524,
2.99129432948254, 2.99129432948254, 2.99037325847637,













                                                 2.98879164564413,
2.98659326868617, 2.97999586070021, 2.97999586070021,













                                                 2.97644588044076,
2.97793068626772, 2.97725657829663, 2.97644779172119,













                                                 2.97642177939902,
2.97900948151863), pctB = c(NA, NA, NA,














                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA,














                   NA, 0.095085037223641, 0.191760034251622,
0.275299637385869,














                   0.281553165463918, 0.296491615281828,
-0.121809387665777,














                   0.284341912274921, 0.328467952873774, 0.666193377119756,














                   0.719907643063004, 0.706078799023163, 0.69308821148912,
0.690596474774226,














                   0.777616947825876, 0.655337055318415, 0.722344774776124,














                   0.384658554713109, 0.401513108306767, 0.418772021850563,














                   0.55305954499725, 0.53678061789603, 0.594275876245545,
0.652145154862543,














                   0.635095869371843, 0.618290055851844, 0.602704203140293,














                   0.409447758421942, 0.135627923022663, 0.40333638833254,
0.725548427224084,














                   0.128466946536447, 0.167002857708663, 0.19947060197853,
0.337730551473016,














                   0.80135754726439, 0.695335360670196, 0.741488877565705,
0.364688424652042,














                   0.346203797321917, 0.351198771201796, -0.0205534996494938













                                                 ), cross_up = c(NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA,













                                                                 NA, NA,
NA, NA, NA, NA, NA, NA, FALSE, FALSE, FALSE, FALSE,













                                                                 FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,













                                                                 FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,













                                                                 FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,













                                                                 FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,













                                                                 FALSE),
cross_dn = c(NA, NA, NA, NA, NA, NA, NA, NA, NA,














          NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, FALSE, FALSE, FALSE,














          FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,














          FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,














          FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,














          FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,














          FALSE, TRUE)), class = "data.frame", .Names = c("COB_DT",














                                                          "PRICE",
"CURVE_START_DT", "QUOTE_DEF_CD", "dn", "mavg", "up",














                                                          "pctB",
"cross_up", "cross_dn"), row.names = c(NA, -60L))), .Names = c("Henry Hub
xh19",















                                                     "Dominion South Basis
xh19", "Henry Hub jv18", "Dominion South Basis jv18"














                                                          ))
process_lgl <- df %>%  map_lgl( ~ .$cross_up[nrow(.)] == TRUE |
.$cross_dn[nrow(.)] == TRUE)
process_df <-  data.frame(Names = names(process_lgl), Process =
process_lgl, row.names = NULL)
process_df
df[process_df$Process == TRUE] %>% lmap(graph_send)

        [[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.
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Quantmod graphing issue

Joshua Ulrich
On Tue, Feb 14, 2017 at 8:58 AM, Jon Golenbock <[hidden email]> wrote:

> Hi, having an issue and I'm running into a wall. What I'm trying to do is
> set up some alerts given a list of products, terms, and rules. For example,
> look at the prompt 3 months for natural gas and crude, and if there is RSI
> divergence on any of those charts, graph them with the relevant technical
> indicators) and email them out (haven't written this part yet but it should
> be pretty simple I think).
>
> The issue I'm having might be a graphics issue (my suspicion) or I might be
> using a purrr function incorrectly, as I'm still figuring out how to best
> use it's mapping functions.
>
> Code below -- should be spitting out two graphs, but it only creates one
> and then throws the error "Error: is.list(res) is not TRUE". This
> particular (vanilla) example is looking to flag natgas pricing as it breaks
> bollinger bands.
>
> Worth noting, I am using the lmap() function instead of the map() function
> because I want the name passed as well. i.e if I pass df[[1]] to
> graph_send(), it will pass an unnamed dataframe. if i pass df[1], it passes
> a (named) list containing the dataframe, which I can still access using
> mylist[[1]].
>

I have trouble following your code, because my brain does not parse
"%>%" very well.  I've also never used purrr, so I can't help if
that's the issue.  If you truly suspect the issue is with purr, you
should ask your question on a more general forum, because purrr
doesn't really have anything to do with finance.

Based on your description, it seems like a simple for loop works fine:
for (n in names(df)) {
  d <- df[[n]]
  lastobs <- last(d)
  if (lastobs$cross_up || lastobs$cross_dn) {
    x <- na.locf(xts(d$PRICE, d$COB_DT))
    dev.new()  # open new plot device for each chart
    candleChart(x, theme="white", name=n, TA="addBBands()")
  }
}

>         [[alternative %>% version deleted]]

I would also note that creating OHLC data from close prices of the
same periodicity is not a good idea.  The day's open is usually not
the prior day's close.

> Any help is appreciated, thank you in advance!!
>
> library(TTR)
> library(dplyr)
> library(purrr)
> library(magrittr)
> library(quantmod)
>
> graph_send <- function(mylist){
>   #print(df %>% str())
>   df2 <- mylist[[1]] %<>% dplyr::select(COB_DT, PRICE,QUOTE_DEF_CD) %>%
>     dplyr::rename (Date = COB_DT, Close = PRICE) %>%
>     dplyr::mutate (Open = lag(Close))
>
>
>   df2$Open[1] <- df2$Close[1]
>   df2$High <- pmax(df2$Close, df2$Open)
>   df2$Low <- pmin(df2$Close, df2$Open)
>   #print(df2 %>% str())
>
>
>   df3 <- df2 %>% select(Date, Open, High, Low, Close)
>   ts <- xts(df3[,-1], df3[,1]) %>% zoo::na.locf()
>   #print(ts)
>
>
>
>   candleChart(ts, theme = "white", name = names(mylist))
>   plot(addBBands())
> }
>
> df <- structure(list(`Henry Hub xh19` = structure(list(COB_DT =

<snip thousands blank lines... and some data>

>                                                           ))
> process_lgl <- df %>%  map_lgl( ~ .$cross_up[nrow(.)] == TRUE |
> .$cross_dn[nrow(.)] == TRUE)
> process_df <-  data.frame(Names = names(process_lgl), Process =
> process_lgl, row.names = NULL)
> process_df
> df[process_df$Process == TRUE] %>% lmap(graph_send)
>
>         [[alternative HTML version deleted]]
>
Please follow the posting guide and send plain text. The list server
doesn't like HTML.

> _______________________________________________
> [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
R/Finance 2017 | www.rinfinance.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.
Loading...