TradingView

A clever way of exporting data with alerts from TradingView

A clever way of exporting data with alerts from TradingView

TradingView is not very friendly when it’s coming to exporting/importing custom data. But still, there are ways you can use to do that to some extent. In this article, I’ll show you an export method that will allow you to export custom-calculated data with email alerts.

In this article, I’ll work with a simple SMA indicator, here is a code for it:

//@version=5
indicator("SMA", overlay = true)

ma_fast = ta.sma(close, 50)
ma_slow = ta.sma(close, 200)

plot(ma_fast, color = color.red,  linewidth = 2)
plot(ma_slow, color = color.blue, linewidth = 2)

For example, for all SMA crossover, I want to export:

  • Date of the Crossover
  • Type: Bull or Bear
  • And values for fast and slow moving average at the time of the cross.

First of all, I’ll create arrays to store all these values:

var dates = array.new_float()
var type  = array.new_string()
var fast  = array.new_float()
var slow  = array.new_float()

Next, I have to fill these arrays with values on every cross:

if (ta.crossover(ma_fast, ma_slow))
    array.push(dates, time)
    array.push(type,  'Bull')
    array.push(fast,  ma_fast)
    array.push(slow,  ma_slow)
    

if (ta.crossunder(ma_fast, ma_slow))
    array.push(dates, time)
    array.push(type,  'Bear')
    array.push(fast,  ma_fast)
    array.push(slow,  ma_slow)
    

Lastly, I have to gather the data from all arrays and compose a CSV file inside Pine Script. After that, it will be quite easy to get this data in spreadsheet format. I’m using barstate.islast to send an alert only for the last bar when all the data will be gathered. I put every cross on separate lines and separate values with commas. Alert is designed this way so it will be fired immediately after creation.

if barstate.islast
    
    alert_text = "date,type,fast,slow\n"
    
    for i = 0 to array.size(dates) - 1
        alert_text += str.tostring(array.get(dates, i)) + "," +
                      str.tostring(array.get(type, i)) + "," + 
                      str.tostring(array.get(fast, i)) + "," + 
                      str.tostring(array.get(slow, i)) + "\n"
                      
    alert(alert_text, freq = alert.freq_once_per_bar)

Create an alert with “Any alert() function call’ and select “Send Email”. It will be easier to copy it to excel from an email message.

Immediately you’ll see a popup with your data:

Go to an email, copy entire message and paste it to Excel or Google Spreadsheet. If all data end up in a single row try to copy it to the text editor first and then to the spreadsheet. At first, you might see data not separated into columns:

To fix that you can select the first column and click Data -> Split Text to Columns.

After that, you’ll see a very nice spreadsheet with your data:

This approach is not ideal, it’s limited by the size of the string (max 4k character). But still it can be useful for certain use-cases.

Here is the entire code used in this example:

//@version=5
indicator("SMA", overlay = true)

ma_fast = ta.sma(close, 50)
ma_slow = ta.sma(close, 200)

plot(ma_fast, color = color.red,  linewidth = 2)
plot(ma_slow, color = color.blue, linewidth = 2)

var dates = array.new_float()
var type  = array.new_string()
var fast  = array.new_float()
var slow  = array.new_float()

if (ta.crossover(ma_fast, ma_slow))
    array.push(dates, time)
    array.push(type,  'Bull')
    array.push(fast,  ma_fast)
    array.push(slow,  ma_slow)
    

if (ta.crossunder(ma_fast, ma_slow))
    array.push(dates, time)
    array.push(type,  'Bear')
    array.push(fast,  ma_fast)
    array.push(slow,  ma_slow)
    
if barstate.islast
    
    alert_text = "date,type,fast,slow\n"
    
    for i = 0 to array.size(dates) - 1
        alert_text += str.tostring(array.get(dates, i)) + "," +
                      str.tostring(array.get(type, i)) + "," + 
                      str.tostring(array.get(fast, i)) + "," + 
                      str.tostring(array.get(slow, i)) + "\n"
                      
    alert(alert_text, freq = alert.freq_once_per_bar)

Leave a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Pine Script Programming Courses
Pine Script Programming Courses
Learn to build your own TradingView Indicators and Strategies
Sidebar Signup Form
If you want to be the first in this business, subscribe to the latest news