Welcome to the BEST forum for traders

Did you know we're sharing tons of exclusive content here?
Our community is growing quickly. Take a look around and say "Hello".

200SMA strategy - please help

Aug 15, 2023 - 8:04 PM

Viewed 621 times

https://best-trading-indicator.com/community/bti/forums/3953/topics/1780599 COPY
  • 1

    0
  • 1

    0
  • 1

    0
  • 1

    0
  • 1

    0
  • 1

    0
  • 1

    0
  • 1

    0
  • 1

    0
  • 1

    0
  • 1

    0
  • You can add it at the end

    var bool new_trade_long_condition = false
    var int dayMonth = 0
    
    // Long
    if strategy.closedtrades.profit(strategy.closedtrades - 1) > 0 and strategy.closedtrades.size(strategy.closedtrades - 1) > 0
         dayMonth := dayofmonth(time)
         new_trade_long_condition := true
    
    var bool new_trade_short_condition = false
    // Short
    if strategy.closedtrades.profit(strategy.closedtrades - 1) > 0 and strategy.closedtrades.size(strategy.closedtrades - 1) <  0
         dayMonth := dayofmonth(time)
         new_trade_short_condition := true
    
    if new_trade_long_condition and dayMonth != dayMonth[1]
         new_trade_long_condition := false
         strategy.entry("Long ReEntry", strategy.long)
    
    if new_trade_short_condition and dayMonth != dayMonth[1]
         new_trade_short_condition := false
         strategy.entry("Short ReEntry", strategy.short)
    
    
    0
  • Hi,

    got this pine script code for crossing SMA200.
    I want to add one more condition - if position is closed with take profit - open new position the next day (if long was closed on TP - open new long, if short was closed TP - open new short)

    Thank you in advance

    ==

    //@version=5
    strategy(2 Daily Candles Above/Below 200SMA, pyramiding=2, overlay = true)

    smaLength = input.int(200, title = SMA Length)
    smaValue = ta.sma(close, smaLength)

    prevClose = request.security(syminfo.tickerid, D, close[1])

    var consecutiveAboveSMA = 0
    if (close > smaValue and prevClose > smaValue)
    consecutiveAboveSMA := consecutiveAboveSMA + 1
    else
    consecutiveAboveSMA := 0

    var consecutiveBelowSMA = 0
    if (close < smaValue and prevClose < smaValue)
    consecutiveBelowSMA := consecutiveBelowSMA + 1
    else
    consecutiveBelowSMA := 0

    openLong = consecutiveAboveSMA == 2
    openShort = consecutiveBelowSMA == 2

    if (openLong)
    strategy.entry(Long, strategy.long)
    if (openShort)
    strategy.entry(Short, strategy.short)

    // Calculate take profit and stop loss levels
    entryPrice = strategy.position_avg_price
    LongtakeProfitPrice = entryPrice * (1 + 0.30)
    LongstopLossPrice = entryPrice * (1 - 0.07)
    ShorttakeProfitPrice = entryPrice * (1 - 0.30)
    ShortstopLossPrice = entryPrice * (1 + 0.07)

    // Exit conditions for long positions
    strategy.exit(Take Profit/Stop Loss, from_entry = Long, limit = LongtakeProfitPrice, stop = LongstopLossPrice)

    // Exit conditions for short positions
    strategy.exit(Take Profit/Stop Loss, from_entry = Short, limit = ShorttakeProfitPrice, stop = ShortstopLossPrice)

    plot(smaValue, color = color.blue, title = 220 SMA)
    plot(close, color = color.black, title = Close)

    0
CONTENTS