2

I am just getting to grips with PineScript and more specifically writing strategies to place orders/trades however, have a quick question.

I want to create a basic template strategy that will enter a trade, set its stop loss and take profit, and that's all to begin with. I am not interested in using any indicators to set any of these values as yet, so I just wish to simply set:

  • Entry price = current price + 10pips
  • Stop loss = entry_price - 20pips
  • Take profit = entry_price + 60pips

I appreciate this isn't a useful strategy to trade with, I am just using this exercise to better understand PineScript and once I have a script that can execute the above I plan to then build on that, so this will be a huge help!

Thanks all

1 Answers1

3

You can use the following to get the pip size:

pip_size = syminfo.mintick * (syminfo.type == "forex" ? 10 : 1)

And use this to get the entry price:

entry_price = strategy.opentrades.entry_price(strategy.opentrades - 1)

Then:

tp = entry_price + (60 * pip_size)
sl = entry_price - (20 * pip_size)

Then to exit a position:

if (strategy.position_size > 0)
    strategy.exit("exit", "long", stop=sl, limit=tp)
vitruvius
  • 15,740
  • 3
  • 16
  • 26
  • That's brilliant, thank you @vitruvius. So then from what I've read, to enter and exit a trade using the above variables would be something like: `strategy.entry("long", strategy.long, 100, when = close == entry_price)` And to exit: `strategy.exit("exit", "long", stop=sl, limit=tp)` Just a note on the exit, most examples I've seen are within an if statement, however would that be needed, as I thought the `stop` and `limit` parameters dictate when this `strategy.exit()` line should be executed? – user15286105 May 07 '22 at 12:01
  • You might want to add an if check before the `strategy.exit()` to see if you are in a long position. Otherwise don't execute the exit order. See my edit please. – vitruvius May 07 '22 at 12:05