'If statements not working - select input for tp and sl
This is my code:
// This source code is subject to the terms of the Mozilla Public License 2.0 at
https://mozilla.org/MPL/2.0/
// © VSPuzzler
//@version=5
strategy("My script", overlay = true)
pt = 0
sl = 0
pip_PT = input.int(0,minval=0)
percent_PT = input.int(7, minval=0)
if (percent_PT != 0)
pt = percent_PT * 100
else
pt = pip_PT
pip_SL = input.int(0,minval=0)
percent_SL = input.int(7, minval=0)
if (percent_SL != 0)
sl = percent_SL * 100
else
sl = pip_SL
buy_signal = (((ta.rsi(close,7)[1]+ta.rsi(close,7)[2]+ta.rsi(close,7)[3])/3) > ta.rsi(close,7))
if bar_index < 100
strategy.entry("buy", strategy.long, when = strategy.position_size <= 0)
strategy.exit("bracket", "buy", profit = pt, stop = sl)
When I run this script the if statements skip and the pt and sl variables remain at 0 and don't change with the input.
Solution 1:[1]
Variables can be declared with =, but when they are to be re-assigned later in the script you need to use :=. You will see this warning when you add your script to the chart. An example of what I mean here:
pt = 0 // we use equal sign here
if (percent_PT != 0)
pt := percent_PT * 100 // we use assignment operator here
Cheers and have a great day
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Bjorgum |
