Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

pine script - Pinescript not plotting higher resolution calculations on lower timeframe charts. How to solve

I have created a plots based on higher timeframe to be viewed on lower timeframe. however, no plots are done when i shift to lower timeframe, what could be error in the code

study(title = "Trading system", shorttitle="TS", overlay =true)
//______________User inputs_________________
_1              = input(title = "═════ Bias Settings ═════",    type = input.bool,          defval = true)
i_resolution    = input(title = "Select Bias Resolution",       type = input.resolution,    defval ="75")


//____________ variables ____________________
var float highValue     = na
var float lowValue      = na
var float bias          = na
string barType          = na


//_____________Calculations
//Assign high low for bias calculations

tfhigh  = security(syminfo.tickerid,expression = high, resolution = i_resolution, gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_on )
tflow   = security(syminfo.tickerid,expression = low, resolution = i_resolution, gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_on )

highValue       := tfhigh < highValue[1] and tflow > lowValue[1] ? highValue[1] : tfhigh
lowValue        := tfhigh < highValue[1] and tflow > lowValue[1] ? lowValue[1] : tflow

// Define Bar Types
barType         := highValue > highValue[1] and lowValue > lowValue[1] ? "Up" : highValue < highValue[1] and lowValue < lowValue[1] ? "Down" : highValue > highValue[1] and lowValue < lowValue[1] ? "Out" : "In"

// Generate bias
bias            := barType == "Up" ? highValue : barType == "Down" ? lowValue : barType == "Out" and barType[1] == "Up" ? highValue : barType == "Out" and barType[1] == "Down" ? lowValue : bias[1]

// Bias shift to bullish or bearish
bullishcondition = bias > bias[1] and bias[1] < bias[2]
bearishcondition = bias < bias[1] and bias[1] > bias[2]

// Values to plot
upBias = valuewhen(bullishcondition, bias[1],0)
dnBias = valuewhen(bearishcondition, bias[1],0)
plot(upBias, color = color.green)
plot(dnBias, color = color.red)
question from:https://stackoverflow.com/questions/65839230/pinescript-not-plotting-higher-resolution-calculations-on-lower-timeframe-charts

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Because these conditions are always false

bullishcondition = bias > bias[1] and bias[1] < bias[2]
bearishcondition = bias < bias[1] and bias[1] > bias[2]

To make sure of this, put it on the chart plot(bias, color = color.blue)

[added]

If I understand your comment correctly, all calculations should be combined into a function and called in security

//@version=4
study(title = "Help (Trading system)", shorttitle="TS", overlay =true)
//______________User inputs_________________
_1              = input(title = "═════ Bias Settings ═════",    type = input.bool,          defval = true)
i_resolution    = input(title = "Select Bias Resolution",       type = input.resolution,    defval ="75")


//____________ variables ____________________
// var float highValue     = na
// var float lowValue      = na
// var float bias          = na
// string barType          = na


//_____________Calculations
//Assign high low for bias calculations

bias() =>
    float highValue     = na
    float lowValue      = na
    float bias          = na
    string barType      = na
    // tfhigh  = security(syminfo.tickerid, expression = high, resolution = i_resolution, gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_on )
    // tflow   = security(syminfo.tickerid, expression = low, resolution = i_resolution, gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_on )
    
    tfhigh  = high
    tflow   = low

    highValue       := tfhigh < highValue[1] and tflow > lowValue[1] ? highValue[1] : tfhigh
    lowValue        := tfhigh < highValue[1] and tflow > lowValue[1] ? lowValue[1] : tflow
    
    // Define Bar Types
    barType         := highValue > highValue[1] and lowValue > lowValue[1] ? "Up" : highValue < highValue[1] and lowValue < lowValue[1] ? "Down" : highValue > highValue[1] and lowValue < lowValue[1] ? "Out" : "In"
    
    // Generate bias
    bias            := barType == "Up" ? highValue : barType == "Down" ? lowValue : barType == "Out" and barType[1] == "Up" ? highValue : barType == "Out" and barType[1] == "Down" ? lowValue : bias[1]
    
    // Bias shift to bullish or bearish
    bullishcondition = bias > bias[1] and bias[1] < bias[2]
    bearishcondition = bias < bias[1] and bias[1] > bias[2]
    
    // Values to plot
    upBias = valuewhen(bullishcondition, bias[1], 0)
    dnBias = valuewhen(bearishcondition, bias[1], 0)
    [upBias, dnBias]

[up, dn] = security(syminfo.tickerid, expression = bias(), resolution = i_resolution, gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_on )
plot(up, color = color.green)
plot(dn, color = color.red)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...