I have the following code:
//@version=4
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// ? Coded by dUdU
strategy("My Strategy", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100, initial_capital=15000, process_orders_on_close=true)
// Settings
candleLookback = input(5, minval=1)
atrExitMultiplier = input(0.0, step=0.1)
slowSMA = input(90, minval=0)
fastSMA = input(10, minval=1)
sSMA= sma(close, slowSMA)
fSMA= sma(close, fastSMA)
// Calculation
inMarket = strategy.opentrades != 0
opened = strategy.opentrades[0] > strategy.opentrades[1]
// Range check
FromDay = input(defval = 1, title = "From Day", minval = 1, maxval = 31)
FromMonth = input(defval = 10, title = "From Month", minval = 1, maxval = 12)
FromYear = input(defval = 2020, title = "From Year", minval = 1990)
ToDay = input(defval = 1, title = "To Day", minval = 1, maxval = 31)
ToMonth = input(defval = 1, title = "To Month", minval = 1, maxval = 12)
ToYear = input(defval = 2021, title = "To Year", minval = 1990)
Start = timestamp(FromYear, FromMonth, FromDay, 00, 00)
Finish = timestamp(ToYear, ToMonth, ToDay, 23, 59)
Timerange() =>
time >= Start and time <= Finish ? true : false
// Entry order
longCondition = close > fSMA and close > sSMA
if longCondition
strategy.entry("Long", true, comment="Entry",when=Timerange())
// Exit order
exitPrice = highest(candleLookback) + atr(candleLookback) * atrExitMultiplier
plot(longCondition or inMarket ? exitPrice : na, "Exit Price", color.white, style=plot.style_linebr, offset=1)
strategy.exit("Long", limit=exitPrice, comment="Exit")
plot(sSMA,"slow",#FFFFFF)
plot(fSMA,"fast",#EB4034)
I would like to be able to put the number "0" in slowSMA so that I can disable it and work only with fastSMA. However, when I put "0" it gives an error!
I tried to put an 'if' like this:
...
if slowSMA == 0
longCondition = close > fSMA
else
longCondition = close > fSMA and close > sSMA
...
Did not work! Would anyone have any other solutions?
PS.: Explaining the operation of the algorithm: it buys when the price closes above the slow SMA and fast SMA and sells at the maximum of the last 5 candles, with the possibility of turning off the slow SMA and working with only one SMA.