podolkerod
Pine Script Rookie
Pine Script Rookie
Posts: 6
Joined: February 1st, 2022

[ASK] Grab Heikin Ashi value for calculation

I try to get value from HA for used in n1 , n2 and n3 calculation. Do I need to make another security request for each of it?

Code: Select all

//--------------

// Sources:
src0 = open
src1 = high
src2 = low
src3 = close
src4 = hl2
src5 = hlc3
src6 = ohlc4
src7 = ta.tr
vol = volume

// Input Settings
PHX_TF               = input.timeframe  ('',    'PHX TimeFrame',inline='phx_L1',    group = groupPHX)
PHX_Repaint          = input            (false, 'Repainting',   inline='phx_L1',    group = groupPHX)
PHX_Use_HA           = input            (true, 'Heikin Ashi',  inline='phx_L1',    group = groupPHX)
PHX_src              = input            (close, "Source",       inline='phx_L1',    group = groupPHX)
n1   = input(9, 'Phx master',   group = groupPHX)
n2   = input(6, 'Phx time 1',   group = groupPHX)
n3   = input(3, 'Phx time 2',   group = groupPHX)
PHX_security        = request.security (PHX_Use_HA?ticker.heikinashi(syminfo.tickerid):syminfo.tickerid, PHX_TF, PHX_src[PHX_Repaint?0:barstate.isrealtime?1:0])[PHX_Repaint?0:barstate.isrealtime?0:1]

// Calculate n1 , n2, n3 below from HA data above 
tci(src) =>
    ta.ema((src - ta.ema(src, n1)) / (0.025 * ta.ema(math.abs(src - ta.ema(src, n1)), n1)), n2) + 50

csi(src) =>
    math.avg(ta.rsi(src, n3), ta.tsi(src0, n1, n2) * 50 + 50)
    
    
// END OF SECTION
//--------------------------

processingclouds
Pine Script Master
Pine Script Master
Posts: 115
Joined: January 30th, 2022

Re: [ASK] Grab Heikin Ashi value for calculation

So you want to use heikinashi for TCI and CSI calculations. Please find the edited code below which pulls both TCI and CSI directly on single request.security.

I have cleaned the code also separating your repeated patterns into variables as :
useHA = PHX_Use_HA ? ticker.heikinashi(syminfo.tickerid) : syminfo.tickerid
index = PHX_Repaint ? 0 : (barstate.isrealtime ? 1 : 0)

I than plotted them just to make it visual.

Code: Select all

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © processingclouds

//@version=5
indicator("PHX CSI and TCI")
plot(na)
//--------------

// Sources:
src0 = open
src1 = high
src2 = low
src3 = close
src4 = hl2
src5 = hlc3
src6 = ohlc4
src7 = ta.tr
vol = volume

groupPHX = "Group PHX"
// Input Settings
PHX_TF               = input.timeframe  ('',    'PHX TimeFrame',inline='phx_L1',    group = groupPHX)
PHX_Repaint          = input            (false, 'Repainting',   inline='phx_L1',    group = groupPHX)
PHX_Use_HA           = input            (true, 'Heikin Ashi',  inline='phx_L1',    group = groupPHX)
PHX_src              = input            (close, "Source",       inline='phx_L1',    group = groupPHX)
n1   = input(9, 'Phx master',   group = groupPHX)
n2   = input(6, 'Phx time 1',   group = groupPHX)
n3   = input(3, 'Phx time 2',   group = groupPHX)


// Calculate n1 , n2, n3 below from HA data above 
tci(src) =>
    ta.ema((src - ta.ema(src, n1)) / (0.025 * ta.ema(math.abs(src - ta.ema(src, n1)), n1)), n2) + 50

csi(src) =>
    math.avg(ta.rsi(src, n3), ta.tsi(src0, n1, n2) * 50 + 50)
    
useHA  = PHX_Use_HA ? ticker.heikinashi(syminfo.tickerid) : syminfo.tickerid
index = PHX_Repaint ? 0 : (barstate.isrealtime ? 1 : 0)

[PHX_tci, PHX_csi] = request.security (useHA , PHX_TF, [tci(PHX_src[index]), csi(PHX_src[index])])

plot (PHX_tci, "TCI", color.blue)
plot (PHX_csi, "CSI", color.green)

// END OF SECTION
//--------------------------

podolkerod
Pine Script Rookie
Pine Script Rookie
Posts: 6
Joined: February 1st, 2022

Re: [ASK] Grab Heikin Ashi value for calculation

processingclouds wrote:
Thu Feb 10, 2022 4:16 am
So you want to use heikinashi for TCI and CSI calculations. Please find the edited code below which pulls both TCI and CSI directly on single request.security.

I have cleaned the code also separating your repeated patterns into variables as :
useHA = PHX_Use_HA ? ticker.heikinashi(syminfo.tickerid) : syminfo.tickerid
index = PHX_Repaint ? 0 : (barstate.isrealtime ? 1 : 0)

I than plotted them just to make it visual.

Code: Select all

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © processingclouds

//@version=5
indicator("PHX CSI and TCI")
plot(na)
//--------------

// Sources:
src0 = open
src1 = high
src2 = low
src3 = close
src4 = hl2
src5 = hlc3
src6 = ohlc4
src7 = ta.tr
vol = volume

groupPHX = "Group PHX"
// Input Settings
PHX_TF               = input.timeframe  ('',    'PHX TimeFrame',inline='phx_L1',    group = groupPHX)
PHX_Repaint          = input            (false, 'Repainting',   inline='phx_L1',    group = groupPHX)
PHX_Use_HA           = input            (true, 'Heikin Ashi',  inline='phx_L1',    group = groupPHX)
PHX_src              = input            (close, "Source",       inline='phx_L1',    group = groupPHX)
n1   = input(9, 'Phx master',   group = groupPHX)
n2   = input(6, 'Phx time 1',   group = groupPHX)
n3   = input(3, 'Phx time 2',   group = groupPHX)


// Calculate n1 , n2, n3 below from HA data above 
tci(src) =>
    ta.ema((src - ta.ema(src, n1)) / (0.025 * ta.ema(math.abs(src - ta.ema(src, n1)), n1)), n2) + 50

csi(src) =>
    math.avg(ta.rsi(src, n3), ta.tsi(src0, n1, n2) * 50 + 50)
    
useHA  = PHX_Use_HA ? ticker.heikinashi(syminfo.tickerid) : syminfo.tickerid
index = PHX_Repaint ? 0 : (barstate.isrealtime ? 1 : 0)

[PHX_tci, PHX_csi] = request.security (useHA , PHX_TF, [tci(PHX_src[index]), csi(PHX_src[index])])

plot (PHX_tci, "TCI", color.blue)
plot (PHX_csi, "CSI", color.green)

// END OF SECTION
//--------------------------
@processingclouds Wow! it's works. Thank you very much :zen: :zen:

podolkerod
Pine Script Rookie
Pine Script Rookie
Posts: 6
Joined: February 1st, 2022

Re: [ASK] Grab Heikin Ashi value for calculation

Continue from study case above I try to use tuple for request security. But it's correct or efficient enough the way I write the code?

Code: Select all

// Security Calls
varip security_syminfo_indics = i_HA_data ? ticker.heikinashi ( syminfo.ticker ) : syminfo.ticker

[src0_phx_HA,
 src1_phx_HA, 
 src2_phx_HA, 
 src3_phx_HA, 
 src4_phx_HA,
 src5_phx_HA,
 src6_phx_HA,
 src7_phx_HA,
 vol_phx_HA,
 i_n1_phx_HA,
 i_n2_phx_HA,
 i_n3_phx_HA,
 i_n4_phx_HA,
 i_n5_phx_HA,
 wt1_phx_HA,
 wt2_phx_HA,
 wt3_phx_HA,
 wt4_phx_HA,
 ext1_phx_HA
 ]
 = request.security ( security_syminfo_indics, rt_timeframe,
 [ src0_phx,
  src1_phx,
  src2_phx,
  src3_phx,
  src4_phx,
  src5_phx,
  src6_phx,
  src7_phx,
  vol_phx,
  i_n1_phx,
  i_n2_phx,
  i_n3_phx,
  i_n4_phx,
  i_n5_phx,
  wt1_phx,
  wt2_phx,
  wt3_phx,
  wt4_phx,
  ext1_phx
  ] )
Here is the full study script:
Full Script: https://www.tradingview.com/script/HCdw ... Grab-Data/
Chart: https://www.tradingview.com/chart/pky8c40y/

processingclouds
Pine Script Master
Pine Script Master
Posts: 115
Joined: January 30th, 2022

Re: [ASK] Grab Heikin Ashi value for calculation

Hey podolkerod,

I hope you are doing great. Yes you can make it more efficient. You do not need to pass in the src values, as you are already passing functions that define using src values themselves. So your above part of code can be rewritten only as:

Code: Select all

[wt1_phx_HA,
 wt2_phx_HA,
 wt3_phx_HA,
 wt4_phx_HA,
 ext1_phx_HA
 ]
 = request.security ( security_syminfo_indics, rt_timeframe,
 [wt1_phx,
  wt2_phx,
  wt3_phx,
  wt4_phx,
  ext1_phx
  ] )

Pinescript takes care of the rest :happy1:

Return to “Pine Script Q&A”