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".

Strategy for crossing SMA50

Aug 13, 2023 - 3:51 PM

Viewed 662 times

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

    0
  • 1

    0
  • 1

    0
  • 1

    0
  • 1

    0
  • 1

    0
  • 1

    0
  • 1

    0
  • 1

    0
  • 1

    0
  • 1

    0
  • 1

    0
  • 1

    0
  • 1

    0
  • 1

    0
  • Forgot to mention - the same happens to SL - it also get executed on the same day

    0
  • Hello,
    I have just started to learn pine script and can't resolve one issue.

    I wanted to test strategy meeting this condition: Open long position when USDJPY daily candle close above 50 SMA. Set up take profit at 500 pips above and stop loss 100 pips below

    MY CODE:

    ===

    //@version=5
    strategy(USDJPY SMA Strategy, overlay = true)

    // Parameters
    smaLength = input(50, title=SMA Length)
    takeProfitPips = input(500, title=Take Profit (pips))
    stopLossPips = input(100, title=Stop Loss (pips))

    // Calculate SMA
    smaValue = ta.sma(close, smaLength)

    // Custom function to check entry condition
    checkEntryCondition() =>
    prevDayClose = request.security(USDJPY, D, close[1], lookahead = barmerge.lookahead_on)
    crossoverCondition = close > smaValue and prevDayClose <= smaValue
    crossoverCondition

    // Entry condition
    longCondition = checkEntryCondition()

    if (longCondition)
    strategy.entry(Long, strategy.long)
    strategy.exit(Take Profit/Stop Loss, from_entry = Long, limit = close + takeProfitPips * syminfo.mintick, stop = close - stopLossPips * syminfo.mintick)

    // Plotting SMA
    plot(smaValue, title=50 SMA, color=color.blue, linewidth=2)

    ===

    Outcome - open position works well but it gets closed always on the same day and I wanted to set up TP at +500 pip above...

    Can you please help?

    Thanks
    Piotr

    0
CONTENTS