Pine Script

Add a parameter to allow Pine Script Strategy to be long or short.

Add a parameter to allow Pine Script Strategy to be long or short.

Trading strategies can vary greatly when applied to long and short positions. So traders might want to use different parameters for both sides. Furthermore, traders may develop different strategies for managing risk when taking a long or short position. By understanding the differences in strategies for long and short sides, traders can make informed decisions and maximize their profits. In this article, I’ll show you a straightforward way to add an input to Pine Script Parameters to control if the strategy can go Long / Short or Both sides.

Let’s start with a code of build-in MACD Strategy as an example:

//@version=5
strategy("MACD Strategy", overlay=true)

fastLength = input(12)
slowlength = input(26)
MACDLength = input(9)

MACD = ta.ema(close, fastLength) - ta.ema(close, slowlength)
aMACD = ta.ema(MACD, MACDLength)
delta = MACD - aMACD

if (ta.crossover(delta, 0))
	strategy.entry("MacdLE", strategy.long, comment="MacdLE")

if (ta.crossunder(delta, 0))
	strategy.entry("MacdSE", strategy.short, comment="MacdSE")

First, we have to create our input with three options: Long, Short, and Both:

side = input.string("Both", title = "Side", options = ['Long', 'Short', 'Both'])

Next, we have to adjust the code for strategy entries. So long “if” should be “true” only if Both or Long are selected. You can use the following condition: side != ‘Short’. Here is how these conditions will look after that:

if (ta.crossover(delta, 0) and side != 'Short')
	strategy.entry("MacdLE", strategy.long, comment="MacdLE")

if (ta.crossunder(delta, 0) and side != 'Long')
	strategy.entry("MacdSE", strategy.short, comment="MacdSE")

The issue with this strategy is that it’s always in the market, and positions aren’t closed. If we leave it with these conditions, the strategy won’t work because short entry closes long positions. To fix this, we have to add an explicit strategy.close calls for both sides:

if (ta.crossover(delta, 0))
	strategy.close("MacdSE")

if (ta.crossunder(delta, 0))
	strategy.close("MacdLE")

That’s it. Now we have a parameter that can control which sides the strategy can go, and you can change this without changing your code. Here is the complete code of this example:

//@version=5
strategy("MACD Strategy", overlay=true)

fastLength = input(12)
slowlength = input(26)
MACDLength = input(9)

side = input.string("Both", title = "Side", options = ['Long', 'Short', 'Both'])


MACD = ta.ema(close, fastLength) - ta.ema(close, slowlength)
aMACD = ta.ema(MACD, MACDLength)
delta = MACD - aMACD

if (ta.crossover(delta, 0) and side != 'Short')
	strategy.entry("MacdLE", strategy.long, comment="MacdLE")

if (ta.crossunder(delta, 0) and side != 'Long')
	strategy.entry("MacdSE", strategy.short, comment="MacdSE")

if (ta.crossover(delta, 0))
	strategy.close("MacdSE")

if (ta.crossunder(delta, 0))
	strategy.close("MacdLE")

Follow me on TradingView and YouTube.

This image has an empty alt attribute; its file name is wide.png

Leave a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Pine Script Programming Courses
Pine Script Programming Courses
Learn to build your own TradingView Indicators and Strategies
Sidebar Signup Form
If you want to be the first in this business, subscribe to the latest news