0
votes

I am new to this, I have developed a trading strategy and I am unable to code the following idea:

I want to use to indicators. The first one is my main signal (RSI) and I want to close the trade if a second signal (cross under) is not reached in the following x bars (SMA) Otherwise te position keeps open.

I dont know hoy to code the second part

Anyone can help me?

1

1 Answers

0
votes

This shows how you can force an exit using forceLongExit when your normal exitCondition has not occurred:

//@version=4
strategy("S", "", true)
int barsLimit = input(10)
bool enterLong = rising(rsi(close, 20), 3)
bool exitCondition = crossunder(close, sma(close, 20))
bool forceLongExit = barssince(change(strategy.opentrades)) >= barsLimit
bool exitLong = exitCondition or forceLongExit

strategy.entry("Long", strategy.long, when = enterLong)
strategy.close("Long", when = exitLong)

//Debugging.
plotchar(enterLong, "enterLong", "▲", location.bottom, size = size.tiny)
plotchar(forceLongExit, "forceLongExit", "◄", location.top, size = size.tiny)
plotchar(exitCondition, "exitCondition", "▼", location.top, size = size.tiny)

It includes debugging plots that show on the chart when your conditions are met. enter image description here