Jimmy
Jimmy

Reputation: 17

How to plot arrows to show my buy and sell data points on TradingView using Pine Editor

I have a list of datapoints (epoch time, trade price executed) generated from python e.g. buy_orders = [[1626377856, 31004.32], [1626394565, 33041.95], [1626575616, 32218.59]], and I would like to display this data on Trading View as small green arrows. Is there a way to do it? Using Pine Editor I can't seem to find a way to input custom data points.

Upvotes: 1

Views: 1707

Answers (2)

lacabra
lacabra

Reputation: 500

While the solution proposed by Rohit works, it is very inefficient because it has to loop through the array of timestamps on every bar. Given that TradingView has a 20 second limit on the execution of any indicator on their free tier, you will quickly hit that limit (it happened to me while trying out that answer). Instead, in your Python script code to output the following, with one plotshape() per datapoint:

//@version=5
indicator(title="Plot Trades",overlay=true)

plotshape(1626377856>=time and 1626377856<time_close ? 31004.32 : na,location=location.absolute, style=shape.xcross, size=size.normal)
plotshape(1626394565>=time and 1626394565<time_close ? 33041.95 : na,location=location.absolute, style=shape.xcross, size=size.normal)
plotshape(1626575616>=time and 1626575616<time_close ? 32218.59 : na,location=location.absolute, style=shape.xcross, size=size.normal)

What the first argument in plotshape() does is that it generates a time series filled with all empty (na) values except where the timestamp falls within the opening (time) and closing (time_close) of any given bar, which effectively plots the shape at the right time.

Reference: Pine Script documentation on time variables

Upvotes: 0

Rohit Agarwal
Rohit Agarwal

Reputation: 1368

You can save all timestamps and prices in an array. Then you can compare the time of each bar with array timestamps and whichever falls in between can be plotted there. Example below

//@version=5
indicator(title="Plot Trades",overlay=true)
var timestamps=array.new<int>()
var prices=array.new<float>()
array.push(timestamps,1626377856)
array.push(prices,31004.32)
array.push(timestamps,1626394565)
array.push(prices,33041.95)
array.push(timestamps,1626575616)
array.push(prices,32218.59)
plotIndex=-1
var prvtime=0
for i=0 to array.size(timestamps)-1
    if time/1000>array.get(timestamps,i) and prvtime<array.get(timestamps,i)
        plotIndex:=i
prvtime:=time/1000
plotshape(plotIndex>-1?array.get(prices,plotIndex):na,location=location.absolute,style=shape.xcross,size=size.normal)

enter image description here

Upvotes: 3

Related Questions