简体   繁体   中英

Pine script - Enter position when multiple conditions are true

I have been trying to figure this out for days now and I know the answer is going to be so simple...

I want to enter a position when multiple conditions are true. No errors are created when compiling script but no buys and sells appear on the chart as they should (perhaps something else is wrong in the code??). I have tried many things but here is my latest attempt.

//@version=3
strategy("Easy BTC Trading", overlay=true,pyramiding = 0, default_qty_type = strategy.percent_of_equity, default_qty_value = 10)

Multiple = input(2, minval=0,step=0.01)
ATR1 = atr(21)*Multiple
volumeavg = (volume[2]+volume[3]+volume[4])/3

highest = highest(high,5)
lowest = lowest(low,5)


long1 = high > highest
long2 = volume[1] >= volumeavg*2

go_long = high > highest and volume[1] > volumeavg*2
exit_long = low < lowest

strategy.entry("Long",strategy.long, when = go_long)
strategy.exit("Exit long","Long", profit = 5, stop = strategy.position_avg_price - ATR1, when = exit_long)

I made your script work, I copied / pasted the code below, with comments to explain :

//@version=3
strategy("Easy BTC Trading", overlay=true,pyramiding = 0, default_qty_type = strategy.percent_of_equity, default_qty_value = 10)

Multiple = input(2, minval=0,step=0.01)
ATR1 = atr(21)*Multiple
volumeavg = (volume[2]+volume[3]+volume[4])/3
//I guess that you want to know the highest value for the last 5 bars
//if yes, in the highest & lowest functions, you need to use close[1]
//which is the previous close before the actual one
//if you just use close, your actual close can be the highest one
//so your long order would never trigger as your close would never
//be higher than the highest
highest = highest(close[1],5)
lowest = lowest(close[1],5)

long1 = close > highest
long2 = volume[1] >= (volumeavg*2)


//go_long = high > highest and volume[1] > volumeavg*2
//there was a problem with that condition above, so I wrote it a clearer way,
// with an if and it works better
go_long = 0
if (close > highest and volume[1] > volumeavg)
    go_long := 1
exit_long = close < lowest

strategy.entry("Long",strategy.long, when = go_long)
strategy.exit("Exit long","Long", profit = 5, stop = strategy.position_avg_price - ATR1, when = exit_long)

Vsoler is right. High will never be higher then highest. When a new high is reach high equals highest, so you need to take the current candle out of the highest checking.

Change this:
highest = highest(high,5)
To this:
highest = highest(high[1],5)

This way you are passing to 'highest()' the last 5 candles not including the current one.

Do you think that "high" can be greater than "highest"? Perhahs it can be greater than highest[1]

Regards Vicente Soler

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