Re: “but it would be nice if we could improve the trades made inside blue rectangle” (when price was dumping)
One method that you could look into is to use the 3 candles rule. When there is 3 red candles, pause the buy condition until a green candle closes.
Also using the percentage away from an EMA with the 3 candle rule and looking at what volume is doing. Is it increasing or decreasing. If volume is decreasing over the last 3 candles then it’s better to wait to DCA.
This typically stops buys occurring then the waterfall appears or typically drops the DCA level down. I have coded this in pine on TradingView but not in Javascript.