0

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?

PineCoders-LucF
  • 8,288
  • 2
  • 12
  • 21
peferu
  • 1
  • 1

1 Answers1

0

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

PineCoders-LucF
  • 8,288
  • 2
  • 12
  • 21
  • Thanks !! Do you know any website where I can learn more of Pine Editor? – peferu May 10 '21 at 14:18
  • Welcome to Pine! This is the best place to start your journey: https://www.tradingview.com/?solution=43000561836 If you mean learning about the Editor itself, there are no good TV docs yet specifically on the Editor, but there are a few good introductory videos. You'll find a few here: https://www.youtube.com/results?search_query=pine+editor+tutorial – PineCoders-LucF May 10 '21 at 14:36
  • Hi Lucf I tried the functions that you mentioned but my problem is now that in the price source . I want to force the short exit if the ema ( t+"x bars") is below other ema. In other word insted of using only close that means at time t, I want to use se source of t+x Do you think is possible? strategy("R", overlay=true) int barsoffset = input(10) Fast_ema = ema(close,7) **# I want to be close +barsoffset** Slow_ema = ema(close,21)** # I want to be close +barsoffset** bool forceShortExit = Fast_ema>Slow_ema – peferu May 11 '21 at 18:14