How to draw a line between two given points in R with plotly?

Alejandro Carrera

I'm trying to draw some kind of trend line using highs and lows from cryptocurrencies (CC) prices. First libraries I'm using:

library(tidyverse)
library(reshape2)
library(lubridate)
library(TTR)
library(binancer)
library(plotly)

Since I choose among many CC, I'm using next scheme to achieve some kind of automation. For this example, I use Bitcoin (BTC). The following code will retrieve data for Bitcoin for 4 hours timeframe. I set "horas" to 900 in order to get 225 observations:

nombre <- "BTC"
tiempo <- "4h"
horas <- 900

data <- binance_klines(paste0(nombre,"USDT"), interval = paste0(tiempo), 
    start_time=Sys.time()-hours(paste0(as.numeric(horas))), end_time=Sys.time())%>%
    select(open_time, open, high, low, close, volume, trades)%>%
    rename(time=1)

Next I get lows and highs to use this data for drawing the lines I want. As you can see, I choose exactly two points for either highs and lows:

lows <- arrange(data, low)%>%
    slice(c(which.min(low), which.max(low)))%>%
    arrange(time)

highs <- arrange(data, high)%>%
    slice(c(which.max(high), which.min(high)))%>%
    arrange(time)

And I also add some simple moving averages (SMA). My sma database is the source for my plot:

data%>%
    mutate(SMA_5= SMA(close, 5), SMA_10= SMA(close,10), SMA_20= SMA(close,20)) -> sma

I'm trying to use add_segments to draw the line I want for lows (if this works I'll use same code for highs) but I got some error:

sma %>% plot_ly(x = ~time, type="candlestick",
                       open = ~open, close = ~close,
                       high = ~high, low = ~low) %>%
    add_lines(x = ~time, y= ~SMA_5,  line = list(color = "gold", width = 2), inherit = F,
                name = "SMA 5", showlegend=T)%>%
    add_lines(x = ~time, y= ~SMA_10,  line = list(color = "deeppink", width = 2), inherit = F,
                name = "SMA 10", showlegend=T)%>%
    add_lines(x = ~time, y= ~SMA_20,  line = list(color = "purple", width = 2), inherit = F,
                name = "SMA 20", showlegend=T)%>%
    add_segments(
                x = ~lows[1,1], xend = ~lows[2,1], 
                y = ~lows[1,4], yend = ~lows[2,4], color="black")%>%
    plotly::layout(title = paste0(nombre, " Simple Moving Average, ", tiempo),
        xaxis= list(title="Time", rangeslider = list(visible = F)), yaxis = list(title = "Price"),
        sliders=list(visible=F)) -> sma_plot

sma_plot

Error in `*tmp*`[[jj]] : subscript out of bounds 

Any idea on what I'm doing wrong? Any feedback will be highly appreciated.

Kat

If you are trying to draw a line between the two coordinates, just use the same pattern you used with the other lines.

add_lines(inherit = F, data = lows, x = ~time, y = ~low, 
          name = "Lows, line = list(color = "black")) 

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

R: draw a line between two points in ggplot

Javascript + svg, draw sinus (wave) line between two given points

Draw line between two given points (OpenCV, Python)

Draw arrow between on line between two points

Matplotlib how to draw vertical line between two Y points

How to draw a line between two points over an image in swift 3?

How to draw a line between two points objective-C

How to draw a line between two points with js and CSS

Draw line between two distinct points

Draw line between two points in JavaScript?

Draw curved line between two points

How to draw a line which cross two points but spans across the X direction using Plotly Python?

Draw a line between points

How to find whole distance between two points in a curved line in R?

How to draw a line between two points when holding one axis fixed (time series)

How to draw a line / link between two points on a D3 map based on latitude / longitude?

How to draw line between two points in JavaScript - Google Maps Api Route

ChartJS: Draw line between two data points on hover

Is there a way to draw a line between two points on a HTML page without Canvas?

How to draw a triangle given two points using an html canvas and typescript

Find the vertices of a line between two points with a given stroke width

plotly in r 3d arrows between two points

Given two points (x1,y1) (x2,y2), how can I compute N different points evenly lying on the line between the given points

Draw an arc between two points

Draw an arrow between two specific points in a scatter plot with plotly graph objs

How can draw a line using the x and y coordinates of two points?

How to draw a line beet wen two points in loop?

How to drag and draw line between 2 points in Java Swing

how to draw line between to points using mapKit in xcode 6

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  3. 3

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  4. 4

    pump.io port in URL

  5. 5

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    Do Idle Snowflake Connections Use Cloud Services Credits?

  9. 9

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  10. 10

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  11. 11

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  12. 12

    Generate random UUIDv4 with Elm

  13. 13

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  14. 14

    Is it possible to Redo commits removed by GitHub Desktop's Undo on a Mac?

  15. 15

    flutter: dropdown item programmatically unselect problem

  16. 16

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  17. 17

    EXCEL: Find sum of values in one column with criteria from other column

  18. 18

    Pandas - check if dataframe has negative value in any column

  19. 19

    How to use merge windows unallocated space into Ubuntu using GParted?

  20. 20

    Make a B+ Tree concurrent thread safe

  21. 21

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

HotTag

Archive