I'm trying to build a strategy that buys when the First bar closes by at least 10.00 higher than its open. If the Second bar closes below the close of the First bar, then it sells. But otherwise it stays in. If the Third bar closes above the close of the Second bar, then it stays in. But otherwise it sells. What am I doing wrong that I keep getting the error on the chart of "Study Error Error on bar 18349: Loop takes too long to execute (> 500ms)"
//@version=5
strategy("SecretSauce",
overlay=true,
initial_capital=100000,
default_qty_type=strategy.percent_of_equity,
default_qty_value=100, // 100% of balance invested on each trade
commission_type=strategy.commission.cash_per_contract,
commission_value=0.000) // commissions rate)
// Get user input
//i_stopPercent = input.float(title="Stop Loss Percent", defval=0.10, step=0.1, group="Strategy Parameters", tooltip="Failsafe Stop Loss Percent Decline")
i_startTime = input.time(title="Start Filter", defval=timestamp("01 Jan 1999 13:30 +0000"), group="Time Filter", tooltip="Start date & time to begin searching for setups")
i_endTime = input.time(title="End Filter", defval=timestamp("01 Jan 2099 19:30 +0000"), group="Time Filter", tooltip="End date & time to stop searching for setups")
// Check filter(s)
f_dateFilter = (time >= i_startTime) and (time <= i_endTime)
// Check buy/sell conditions
var float buyPrice = 0.00
var float current_range = 10.00
buyCondition = (close > (open + current_range)) and (strategy.position_size == 0) and f_dateFilter
sellCondition = (close < buyPrice) and (strategy.position_size > 0)
//stopDistance = strategy.position_size > 0 ? ((buyPrice - close) / close) : na
//stopPrice = strategy.position_size > 0 ? buyPrice - (buyPrice * i_stopPercent) : na
//stopCondition = strategy.position_size > 0 and stopDistance > i_stopPercent
// Enter positions
if buyCondition
strategy.entry(id="Long", direction=strategy.long)
//atr := high - low
current_range := close - open
buyPrice := close
while strategy.position_size > 0
if close < buyPrice
strategy.close(id="Long", comment="Exit; SL=true")
else if close >= buyPrice
buyPrice := close
I've tried changing the date range ("i_startTime") so that it only processes a portion of the whole data set but that didn't work.
There are some problems and I trying to clear them for you:
1- var float current_range = 10.00 You just wanted a constant value so: current_range = 10.0 is enough
2- buyCondition = (close > (open + current_range)) this condition is related to the current live candle, not 2 & 3 candles before this condition passed for all green candles and strategy cannot trig for a million green candles
3- while strategy.position_size > 0 In Pine script, you don't need to add a while loop to build a backtest strategy you should write like this:
4- (strategy.position_size == 0) you can add Pyramiding_factor = 1 in the Strategy initial setting and remove this condition from your code
5- Finally, in terms of sell conditions:
6- Pine Script is not a programming language such as Python, there are many differences, It's important to know that: your Pine-Script code executes after any new candle, something like an internal hidden loop from the available first candle and current live candle.