简体   繁体   中英

Strategy won't sell despite condition being met

The simple strategy should open a long position when SMA9 is greater than SMA50, and it opens this position. However, it never closes the position despite the conditions being met and I don't know why. Any help would be greatly appreciated.

//@version=5
strategy('Coinrule template - RSI and SMA',
         overlay=true,
         initial_capital=1000,
         process_orders_on_close=true,
         default_qty_type=strategy.percent_of_equity,
         default_qty_value=30,
         commission_type=strategy.commission.percent,
         commission_value=0.1)

showDate = input(defval=true, title='Show Date Range')
timePeriod = time >= timestamp(syminfo.timezone, 2022, 5, 1, 0, 0)
notInTrade = strategy.position_size <= 0

//==================================Buy Conditions============================================

//RSI
length = input(14)
rsi = ta.rsi(close, length)

//SMA
fastSMA = ta.sma(close, 9)
slowSMA = ta.sma(close, 50)
plot(fastSMA, color = color.green)
plot(slowSMA, color = color.blue)


bullish = ta.crossover(fastSMA, slowSMA) and rsi > 50
bearish = ta.crossover(slowSMA, fastSMA) and rsi < 50

//if(bullish and timePeriod)
//    strategy.entry(id="Long", direction = strategy.long)//, when=bullish and timePeriod)

//strategy.close(id="Exit", when = bearish)

strategy.entry(id='Long', direction = strategy.long, when = bullish and timePeriod)
strategy.close(id='Exit', when=bearish)

strategy.close doesn't accept a when parameter https://www.tradingview.com/pine-script-reference/v5/#fun_strategy{dot}close

Do

if bearish
    strategy.close("Long")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM