2021年1月3日星期日

Return result if indicator triggers during a time period or at the end of the period

Right now, I use a custom indicator to generate a line whose value is 1 every 4 hours and 0 at all other times. I use that indicator to generate a buy alert to my bot every time the line crosses above 0.5.

study("Buy every 4 hours")  is_newbar(res) =>      t = time(res)      not na(t) and (na(t[1]) or t > t[1])  plot(is_newbar("240") ? 1 : 0)  

This divides up the day into 6 periods and 6 buys. Is there a way I can return a 1 prior to the period ending if say RSI crossed below 30, and if RSI doesn't cross below 30 to generate the 1 at the end of the period instead?

I essentially want a DCA strategy that would slightly accelerate the buys if another indicator shows an opportunity.

Update

Thanks to PineCoders I was able to incorporate the second indicator into the signal, but I can't figure out how to get new_period to be equal to 0 if zscoreSignal had a 1 in the last period.

//@version=4  study("DCA buys")    buys_per_day = input(6) //number of buys to make per day  minutes_between_buys = 24 * 60 / buys_per_day    z_score_period = input(30) //period to calculate zscore from  xStdDev = stdev(close, z_score_period)  xMA = sma(close, z_score_period)  zscore = (close - xMA) / xStdDev    is_newbar(res) =>      t = time(res)      not na(t) and (na(t[1]) or t > t[1])  new_period = is_newbar(tostring(minutes_between_buys))    x = crossunder(zscore, -3.1)  var zscoreXdn = false  if new_period      zscoreXdn := false  else if not zscoreXdn and x      zscoreXdn := true  zscoreSignal = zscoreXdn and not zscoreXdn[1]    signal = new_period or zscoreSignal ? 1 : 0  plot(signal, "signal")  

The red X's shows the signals that should not happen because of the preceding Z-Score signal during the period. enter image description here

https://stackoverflow.com/questions/65526042/return-result-if-indicator-triggers-during-a-time-period-or-at-the-end-of-the-pe January 01, 2021 at 06:37AM

没有评论:

发表评论