'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
Solution 1:[1]
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)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |