Currency Correlation indicator in Pinescript

Post Reply
SamiFX
Expert Trader
Posts: 7
Joined: Thu Nov 30, 2023 9:44 am

Currency Correlation indicator in Pinescript

Post by SamiFX »

In the Forex market, you can use the currency correlation to find the resemblance in the movement of the market between different currency pairs.

I have made this currency correlation indicator in pine script which will help you find the relation between current p[air and the other 6 pairs.

Below is the full code of the indicator.


Code: Select all

//@version=5
indicator("Currency Pair Correlation", overlay=true)

// User input for 6 currency pairs
pair1 = input.symbol("EURUSD", "Pair 1")
pair2 = input.symbol("GBPUSD", "Pair 2")
pair3 = input.symbol("USDJPY", "Pair 3")
pair4 = input.symbol("AUDUSD", "Pair 4")
pair5 = input.symbol("USDCAD", "Pair 5")
pair6 = input.symbol("NZDUSD", "Pair 6")

// Get the close prices of the selected pairs
pair1_close = request.security(pair1, timeframe.period, close)
pair2_close = request.security(pair2, timeframe.period, close)
pair3_close = request.security(pair3, timeframe.period, close)
pair4_close = request.security(pair4, timeframe.period, close)
pair5_close = request.security(pair5, timeframe.period, close)
pair6_close = request.security(pair6, timeframe.period, close)

// Calculate the correlation between the current chart (pair1 in this case) and the other pairs
correlation1 = ta.correlation(close, pair1_close, 20)  // 20-period correlation
correlation2 = ta.correlation(close, pair2_close, 20)
correlation3 = ta.correlation(close, pair3_close, 20)
correlation4 = ta.correlation(close, pair4_close, 20)
correlation5 = ta.correlation(close, pair5_close, 20)
correlation6 = ta.correlation(close, pair6_close, 20)

// Create a table to display the correlation values
var table corr_table = table.new(position.top_right, 3, 7, border_width = 1)

if (bar_index % 10 == 0)  // Update every 10 bars
    // Table headers
    table.cell(corr_table, 0, 0, "Currency Pair", bgcolor=color.gray)
    table.cell(corr_table, 1, 0, "Correlation", bgcolor=color.gray)
    
    // Table data
    table.cell(corr_table, 0, 1, pair1, bgcolor=color.blue)
    table.cell(corr_table, 1, 1, str.tostring(correlation1, "#.##"), bgcolor=color.blue)
    
    table.cell(corr_table, 0, 2, pair2, bgcolor=color.green)
    table.cell(corr_table, 1, 2, str.tostring(correlation2, "#.##"), bgcolor=color.green)
    
    table.cell(corr_table, 0, 3, pair3, bgcolor=color.red)
    table.cell(corr_table, 1, 3, str.tostring(correlation3, "#.##"), bgcolor=color.red)
    
    table.cell(corr_table, 0, 4, pair4, bgcolor=color.orange)
    table.cell(corr_table, 1, 4, str.tostring(correlation4, "#.##"), bgcolor=color.orange)
    
    table.cell(corr_table, 0, 5, pair5, bgcolor=color.purple)
    table.cell(corr_table, 1, 5, str.tostring(correlation5, "#.##"), bgcolor=color.purple)
    
    table.cell(corr_table, 0, 6, pair6, bgcolor=color.yellow)
    table.cell(corr_table, 1, 6, str.tostring(correlation6, "#.##"), bgcolor=color.yellow)
 
Post Reply