i have this script im creating that is using a channel breakout to determine when i go long on condition based on if the current price is greater than the highest 50 day cloeses, and i sell when the close is less than the 50 days lows.
The problem im having is when im in a trade and it keeps going up, i would like to buy more shares, however that condition to buy more is never met.
i noticed when i was troubleshooting this in a different different that the long_additonal entry to buy more was only met if the strategy_position_size == 0 and when commented out the update for the next_purchase price after i bought in
in the picture the green plus signs are suppose to be the next_purchase
here is my script
lookback = input.int(title="Length", minval=1, maxval=1000, defval=100)
atr_length = input.int(21, title="ATR Length")
atrMultiplier = input.int(3, title="ATR Multiplier")
risk_per_trade = input.float(5, title="risk percentage")
// Variables
float atr = ta.atr(atr_length)
float atr_value = atr * atrMultiplier
// Calculate stop loss values
var float trailing_stop = na
var float next_purchase = na
longCondition = close > ta.highest(close, lookback)[1]
sellCondition = close < ta.lowest(close, lookback)[1]
if(longCondition and strategy.position_size == 0 and time_cond)
// caclculate risk per trade
float risk_amount = strategy.initial_capital * (risk_per_trade / 100)
// calculate amount of shares to purchase
float amount_of_shares = risk_amount / atr_value
// buy
strategy.entry("long", strategy.long, qty=amount_of_shares)
if (na(trailing_stop))
trailing_stop := close - atr_value
if (na(next_purchase))
next_purchase = close + atr_value
// executes condition if close hits next_purchase
if ( strategy.position_size >= 1 and close > next_purchase)
float risk_amount = strategy.initial_capital * (risk_per_trade / 100)
float amount_of_shares = risk_amount / atr_value
strategy.entry("Long_additonal", strategy.long, qty=amount_of_shares)
// Update next_purchase after buying additional shares
next_purchase := close + atr_value
- tried changing the lookback period to something smaller to see if it would buy when the trend is going up
2) i tried to change the condition to only when the strategy.position_size is >=1 and close > next_purchase, however the long_Additional is never met and it only works if the strategy_position_size == 0 . i also noticed it always buy after the sell, and not in between the long and sell, which is weird because unless the next_purchase price calculation is wrong, it should’ve bought at the green plus signs