简体   繁体   中英

How to force the TradingView strategy-tester to open/exit Long positions only ? (Remove short) + set TP/SL not giving any result

I've been trying to get backtest results for a Long-only strategy without success. Also, I can't seem to set a "Take Profit" / Stop loss for some reason. I'm trading second-based intervals so I want a take profit of 0.01% and am using:

strategy.exit("Exit Long", from_entry="Long", stop=close * 1.00001)"

(I've also tried limit=close instead of "stop") But playing up and down with the value doesn't seem to do anything to the.net profit results. It did work in 15mn candles for some reasons. I can't seem to set a stop-loss whatever I try tho.

If you know of a good "input" style TP and SL that I could change directly in the indicator's settings, instead of having to type it manually in the Pine editor, that would be awesome too.

Here's my code. Bear with me as I'm completely beginner level in any kind of programing. I tried removing everything that could mention short, adding the "strategy.direction.long" line that I found in the doc. The parts with // are the ones I've tried but didn't solve anything, but kept for later tests.

// Only trade from the long side
strategy.risk.allow_entry_in(strategy.direction.long)


// Submit orders
//if buySignal
    //strategy.entry(id="EL", long=true)

//if sellSignal
    //strategy.entry(id="ES", long=false)

strategy.entry('Long', strategy.long, 1, when=buySignal)
strategy.close('Long', when=sellSignal)
strategy.cancel('Short', when=sellSignal)


//strategy.exit("exit", "long", profit = 50, loss = 20)

strategy.exit("Exit Long", from_entry="Long", stop=close * 1.000015)

//strategy.exit("Exit Long", from_entry="Long", stop=low * 1.007)

strategy.risk.allow_entry_in(strategy.direction.long)

If anything is outdated for Pine V5, do tell.

If you call this line each time without any condition, your old exit order will be deleted and a new one will be created with the new value of close.

strategy.exit("Exit Long", from_entry="Long", stop=close * 1.000015)

That's certainly the reason why your stop didn't execute in the 1 second timeframe.
You should execute this line only one time after your strategy.entry.
Try:

if strategy.opentrades == 0 // No open order
    strategy.entry('Long', strategy.long, 1, when=buySignal)
    strategy.exit("Exit Long", from_entry="Long", stop=close * 1.000015)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM