everyone!
I am testing a strategy simply based on 25 moving average lines, and using some boolean variables to contrl open, SL or TP. But these bool variables seems not work.
And in my vision, it can not open another long or short position, before it exit or close current long or short position. But unfortunately, it also failed.
Who can tell why? Thanks!
Here is my code…
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//@version=5
strategy("25 average", overlay=true, margin_long=100, margin_short=100)
myma = ta.sma(close, 25)
plot(myma)
// ctrl to open position
var canLong = false
var canShort = true
// determine whether in a long or short position
var isLong = false
var isShort = false
// SL position
var longLoss = 0.0
var shortLoss = 0.0
// calc when to TP
longDelta = math.abs(high - ta.sma(close, 25))
longTP = longDelta < longDelta[1] ? true : false
shortDelta = math.abs(low - ta.sma(close, 25))
shortTP = shortDelta < shortDelta[1] ? true : false
// long open
if canLong and ta.crossover(close, ta.sma(close, 25))
strategy.entry("BUY", strategy.long)
isLong := true
canLong := false
canShort := false
longLoss := low
// long SL
if isLong and close < longLoss
strategy.close("BUY", "Long SL")
isLong := false
canShort := true
// long TP
if isLong and longTP
strategy.close("BUY", "Long TP")
isLong := false
canShort := true
// short open
if canShort and ta.crossunder(close, ta.sma(close, 25))
strategy.entry("SELL", strategy.short)
isShort := true
canShort := false
canLong :=false
shortLoss := high
// short SL
if isShort and close > shortLoss
strategy.close("SELL", "Short SL")
isShort := false
canLong := true
// short TP
if isShort and shortTP
strategy.close("SELL", "Short TP")
isShort := false
canLong := true
enter image description here
enter image description here
That is what i have done, please help me, thanks!
jiaxing bai is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.