Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

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

pine script - Turn on/off an SMA in TradingView

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.


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

1 Answer

0 votes
by (71.8m points)

I added an input to use the slowMA at the top:

useSlow = input(title="Use Slow MA", type=input.bool, defval = false)

And then changed the longcondition statement as such:

// Entry order
longCondition = iff(useSlow, close > fSMA and close > sSMA, close > fSMA)
if longCondition
    strategy.entry("Long", true, comment="Entry",when=Timerange())

I think that's what you're looking for if I understand your question. Alternatively set the value with a simple if, or a ternary.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.7k users

...