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

Pinescript Noob

Jul 30, 2020 - 4:13 AM

Viewed 954 times

https://best-trading-indicator.com/community/bti/forums/4180/topics/18130 COPY
  • I am new to trading view and have very little programming background. I am trying to write a simple strategy to buy when RSI hits 30 and sell when the price rises by 5% after my entry.

    I am not sure how to go about exiting the trade. See code below, can you help me out please?

    ```fromYear = year >= 2010
    toYear = year <= 2020

    longCondition = rsi(close, 21) < 30
    if (longCondition and fromYear and toYear)
    strategy.entry(Long 1, strategy.long)

    entry_price = valuewhen(longCondition, open, 0)
    exit_price = entry_price * 1.05

    closeCondition = exit_price
    if (closeCondition)
    strategy.close(Long 1)```

    0
  • Seems like the first line of code is missing from above,

    strategy(Simple RSI, overlay=true, initial_capital=10000, currency='USD')
    fromYear = year >= 2010
    toYear = year <= 2020

    longCondition = rsi(close, 21) < 30
    if (longCondition and fromYear and toYear)
    strategy.entry(Long 1, strategy.long)

    entry_price = valuewhen(longCondition, open, 0)
    exit_price = entry_price * 1.05

    closeCondition = exit_price
    if (closeCondition)
    strategy.close(Long 1)

    0
  • Hi

    I would do

    entry_price = valuewhen(longCondition, close, 0)
    exit_price = crossover(entry_price, entry_price * 1.05)
    
    if (exit_price)
      strategy.close("Long")
    
    0

    Dave - Helping traders becoming the best version of themselves
CONTENTS