Steve
Steve

Reputation: 55

How to create an input template/preset for a custom pine script strategy?

Is it possible hard code input templates into my pine script strategy?

I need to switch between input presets like this: e.g. The built in Trend Line Tool allows you to save and load input templates (Template1, Template2, Trendline):

Input template example

I wasn't able to find anything about this in the documentation.

But I'd like to be able to hard code (or even just save) my own input templates, then switch between them at run time and tweak the values.


What I have already tried

Creating a drop down, then using the selection in if statements to:

  1. Update the variables which I use as the defVal for my inputs. [Fail: defVal only accepts const variables]
// Defaults
defaultInput1 = 1

If (template1)
    defaultInput1 := 999
    
If (template2)
    defaultInput1 := 123

// Throws exception, defaultInput1 must be const
Input1 = input(defVal=defaultInput1, ...)
  1. Create seperate sets of duplicate inputs inside if statements. [Sort of fail: I hoped that only the selected inputs would be rendered, but it turns out they're all rendered at once. It works and I can edit the values, but I end up with a lot of duplicated inputs to scroll through).
// Defaults
Input1 = 0
Input2 = 0

If (template1)
    Input1 := input(defVal=1, ...)
    Input2 := input(defVal=2, ...)

If (template2)
    Input1 := input(defVal=500, ...)
    Input2 := input(defVal=123, ...)
        
// ^ Ends up displaying all of the inputs on screen, regardless of which drop down option is selected. 

Upvotes: 1

Views: 789

Answers (2)

Girish Kuvalekar
Girish Kuvalekar

Reputation: 11

indicators don't have templates only drawings have

Upvotes: 1

Workaround for option 1 Try using a bool like

    temp = input(type = input.bool, text = "Use template 1", defval = true)

int default_input = na //declaring a global variable
if(temp == true)
    default_input := 999
else
    default_input := 123

or if you have multiple options to select from use this

check = (Title = "Select one", defval = "Template1", options["Template1","template2","template3"]

//and then use above method with nested ifs or if and elseif

Workaround for option 2 What I think the mistake is that you are giving it space to fulfil both conditions, change it to be something like this:

// Defaults
Input1 = 0
Input2 = 0

If (template1)
    Input1 := input(defVal=1, ...)
    Input2 := input(defVal=2, ...)

If (template2 == true and template1 == false)  //if(template2 and ! template1) will also work just fine
    Input1 := input(defVal=500, ...)
    Input2 := input(defVal=123, ...)

Upvotes: 0

Related Questions