Strategy vs Study
A strategy
is used when you want to backtest a pine script you’ve written by telling it when to enter and exit positions. Note that if you want to receive alerts from your code (for text alerts or hooking up to bots or whatever) you can’t do this with a pine script strategy
.
A study
is used when you want to create an indicator for TradingView, draw lines on the chart, but not actually test entering and exiting positions. Also use a study
if you want to set custom alerts based on your code.
How to convert from a Pine Script strategy
to study
or vice versa
Before we get into the steps, a couple things to know: some functions in strategy
mode won’t work in study
mode, and vice versa. Also, you may want to copy your code into a new script in case you want to keep your existing one as-is.
- Change the top line from
strategy(...)
tostudy(...)
(or the other way around, depending on which way you’re converting) - If you’re converting from
strategy
tostudy
- Comment out (prepend lines with
//
) any of the following:strategy.entry
,strategy.exit
, etc - Add at least one
plot
if you didn’t have one before.
- Comment out (prepend lines with
- If you’re converting from
study
tostrategy
- Comment out (prepend lines with
//
) any of the following:alertcondition
, etc. (Note that this isn’t necessary, butalertcondition
doesn’t work instrategy
pine scripts.) - Add
strategy.entry
when your script signals buy andstrategy.exit
when your script signals sell.
- Comment out (prepend lines with
Handling positioning in a study
Since a study
doesn’t know anything about positions like a strategy
can, you’ll have to do this manually.
For example, you could keep an isLong
and isShort
booleans to keep track of if you’re in any position and in which direction. However it’ll be up to you to set these accordingly…most likely based on where you used to call strategy.entry
or strategy.exit
and also taking into account any stop losses, take profits, etc.
Stop Losses in a study
When converting a strategy
to a study
, as mentioned above you’ll have to manage your own positions. That means entries, exits, shorts, longs, stops, everything.
Logic for a normal stop loss is relatively simple. Assuming you’re already keeping track of if you are long or short, you can check the current candle’s low
price against your stop price (if you’re long), and adjust your isLong
variable as necessary and maybe print a label on the chart to show this is where you exited.
Same for shorting stop losses. If you’re short, check the current candle’s high
price against your stop price, and adjust isShort
as necessary and do whatever other logic you need or display something on the chart to show the stop was hit.
A trailing stop will be a bit more complicated since you’ll have to update your stop price every candle. Again you’ll want to check your isLong
or isShort
variable every candle, then increase or decrease your stopPrice
var based on the high
(for long) or low
(for short), but only when price moves in your favor.
I recommend you display a plot line on your chart that shows your stopPrice
at every candle so you can check your logic is doing what it should.
When you’re done converting to a study
, you should be able to compare your two scripts side by side, and see all the entries and exits get printed on the chart at the same places.
Need some help converting a Pine Script strategy
to a study
? Reach out to us in Discord.