简体   繁体   中英

PineScript order entry, stop loss and take profit

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

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.closedtrades.entry_price(strategy.closedtrades - 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)

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