-1

I have these variables that I want to be able to choose from in a drop down input under option for Line1 and Line2. These should be selectable using yet another drop down input with options = ['none', '==', '<', '>', '<=', '>=', '!=' , 'crossover', 'crossunder']) named Operator1.

sm1res = input.timeframe(title='Time for open', defval='30')
sm1 = request.security(syminfo.tickerid, sm1res, open, lookahead=barmerge.lookahead_on)
sm2res = input.timeframe(title='Time for open', defval='60')
sm2 = request.security(syminfo.tickerid, sm2res, open, lookahead=barmerge.lookahead_on)
sm3res = input.timeframe(title='Time for open', defval='240')
sm3 = request.security(syminfo.tickerid, sm3res, open, lookahead=barmerge.lookahead_on)

to1res = input.timeframe(title='Time for previous open 1', defval='30')  
to1 = request.security(syminfo.tickerid, to1res, open[1], lookahead=barmerge.lookahead_on) 
to2res = input.timeframe(title='Time for previous open 2', defval='60')  
to2 = request.security(syminfo.tickerid, to2res, open[1], lookahead=barmerge.lookahead_on)
to3res = input.timeframe(title='Time for previous open 3', defval='240') 
to3 = request.security(syminfo.tickerid, to3res, open[1], lookahead=barmerge.lookahead_on)  

th1res = input.timeframe(title='Time for previous high 1', defval='30') 
th1 = request.security(syminfo.tickerid, th1res, high[1], lookahead=barmerge.lookahead_on)  
th2res = input.timeframe(title='Time for previous high 2', defval='60') 
th2 = request.security(syminfo.tickerid, th2res, high[1], lookahead=barmerge.lookahead_on) 
th3res = input.timeframe(title='Time for previous high 3', defval='240')  
th3 = request.security(syminfo.tickerid, th3res, high[1], lookahead=barmerge.lookahead_on) 

tl1res = input.timeframe(title='Time for previous low 1', defval='30')
tl1 = request.security(syminfo.tickerid, tl1res, low[1], lookahead=barmerge.lookahead_on)
tl2res = input.timeframe(title='Time for previous low 2', defval='60')
tl2 = request.security(syminfo.tickerid, tl2res, low[1], lookahead=barmerge.lookahead_on)
tl3res = input.timeframe(title='Time for previous low 3', defval='240')
tl3 = request.security(syminfo.tickerid, tl3res, low[1], lookahead=barmerge.lookahead_on)


Line1 = input.string(defval = "sm1", title = 'Line1', inline = 'Operator1', group = 'Strategy1', options=["sm1", "sm2", "sm3", "to1", "to2", "to3", "th1", "th2", "th3", "tl1", "tl2", "tl3"])
Operator1 = input.string(defval = 'none', title = 'Operator1', inline = 'Operator1', group = 'Strategy1', options = ['none', '==', '<', '>', '<=', '>=', '!=', 'crossover', 'crossunder'])
Line2 = input.string(defval = none, title = 'Line2', inline = 'Operator1', group = 'Strategy1', options=["sm1", "sm2", "sm3", "to1", "to2", "to3", "th1", "th2", "th3", "tl1", "tl2", "tl3"])

Then I have a buy variable but can't get it to work with different attempts of code, sometimes it says it should be series string, bool, const string, Syntax error at input ':' and a lot of other errors.

BUY = line1 : Operator1 : line2 ? : na

Then comes my order placement:

if BUY
    if Type == _S_
        strategy.close('S')
    else
        strategy.entry('K', strategy.long)

if SELL
    if Type == _L_
        strategy.close('K')
    else
        strategy.entry('S', strategy.short)

Grateful for help so that parts of the errors are with correct code instead and suggestions for improvements welcome. I'm not very good at programming, so please explain in a simple way if you have time.

I've tried reading the manual and looking for different allowed types that go together. I have also tried Pinecoders and the chat. I'm afraid request.security doesn't allow options.

1 Answers1

0

Please edit your question so it contains only one question at a time.

Regarding your first question, you can use a switch:

Line1_input = input.string(defval = "sm1", title = 'Line1', inline = 'Operator1', group = 'Strategy1', options=["sm1", "sm2", "sm3", "to1", "to2", "to3", "th1", "th2", "th3", "tl1", "tl2", "tl3"])
Line2_input = input.string(defval = "sm1", title = 'Line2', inline = 'Operator1', group = 'Strategy1', options=["sm1", "sm2", "sm3", "to1", "to2", "to3", "th1", "th2", "th3", "tl1", "tl2", "tl3"])

Operator1 = input.string(defval = 'none', title = 'Operator1', inline = 'Operator1', group = 'Strategy1', options = ['none', '==', '<', '>', '<=', '>=', '!=', 'crossover', 'crossunder'])

line1 = switch Line1_input
    "sm1" => ta.sma(close, 10)
    "sm2" => ta.ema(close, 10)
    => ta.hma(close, 10)

line2 = switch Line2_input
    "sm1" => ta.sma(close, 20)
    "sm2" => ta.ema(close, 20)
    => ta.hma(close, 20)

BUY = switch Operator1
    '==' => line1 == line2
    '<' => line1 < line2
    '>' => line1 > line2
    '<=' => line1 <= line2
    '>=' => line1 >= line2
    '!=' => line1 != line2
    'crossover' => ta.crossover(line1, line2)
    'crossunder' => ta.crossunder(line1, line2)
mr_statler
  • 1,913
  • 2
  • 3
  • 14
  • Hey! @mr_statler You probably misunderstood the technical solution, but with your code it was at least possible to compile the code. I have tried to insert a link with graph in this comment. To illustrate the technical solution. I absolutely do not want to include, for example => ta.ema(close, 10). Because then I don't compare sm1 with sm2. sm1 is barmerge 30 minutes and sm2 is barmerge 1H in this example. I want to be able to get a buy signal when sm1 is above sm2 and a sell signal when it's the other way around. More text... next comment [link](https://www.tradingview.com/x/2VmJTJ9s/) – Inboxmover Dec 12 '22 at 22:07
  • If you click on the Link, a graph will appear! It is important that there is a dropdown menu to be able to select sm1, sm2 etc. ... in the second you should be able to choose <, > etc... and in the last you should be able to choose sm1, sm2 etc... An example of a result is sm1 > sm2. I want to build a large logical strategy, so I need to be able to add many comparisons. – Inboxmover Dec 12 '22 at 22:10
  • I have programmed a price variable that could perhaps be used instead of ta.ema(close, 10) but for example I have to use something similar to ta.ema(close, 10) but then my price variable must be used instead. The price variable is two pieces of hma 2 that are used to on a price as close to the truth as possible. – Inboxmover Dec 12 '22 at 22:10
  • Just change my code example so that `"sm1" => sm1` , `"sm2" => sm2` and so on. The rest should work the same way – mr_statler Dec 13 '22 at 05:47
  • Thanks for the help, still working and maybe not learning! :-) – Inboxmover Jan 04 '23 at 00:27