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.

  1. Change the top line from strategy(...) to study(...) (or the other way around, depending on which way you’re converting)
  2. If you’re converting from strategy to study
    1. Comment out (prepend lines with //) any of the following: strategy.entry, strategy.exit, etc
    2. Add at least one plot if you didn’t have one before.
  3. If you’re converting from study to strategy
    1. Comment out (prepend lines with //) any of the following: alertcondition, etc. (Note that this isn’t necessary, but alertcondition doesn’t work in strategy pine scripts.)
    2. Add strategy.entry when your script signals buy and strategy.exit when your script signals sell.

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.

Pine Script strategy showing entries and exits.
Pine Script strategy converted to a study.

Need some help converting a Pine Script strategy to a study? Reach out to us in Discord.