Awesome! Could also help me combine an additional indicator? It is the built in ROC indicator from TOS.
Basically I would like arrows to plot only if the OBV UpArrow is true and a confirmed ROC is greater than ZeroLine. And the opposite for the DnArrow so then plot the OBV DnArrow when combined with the ROC when its smaller than ZeroLine and confirmed.
input length = 14;
input signalLength = 26;
input averageType = AverageType.EXPONENTIAL;
input showarrows = yes;
def OBV = TotalSum(Sign(close - close[1]) * volume);
def OBVM = MovingAverage(averageType, OBV, length);
def Signal = MovingAverage(averageType, OBVM, signalLength);
def price = close ;
def up = OBVM > Signal and Signal < OBVM ;
def dn = OBVM < Signal and Signal > OBVM ;
def neutral = between(close,min(OBVM,SIGNAL),max(SIGNAL,OBVM));
def x = CompoundValue(1,
if x[1]>=0 and (dn[1] or up[1]==0) and up
then 1
else if x[1]>=1 and (up or !dn)
then x[1]+1
else 0, 1);
def y = CompoundValue(1,
if y[1]<=0 and (up[1] or dn[1]==0) and dn
then 1
else if y[1]>=1 and (dn or !up)
then y[1]+1
else 0, 1);
def uparrow = if showarrows and x == 1 and up then 1 else 0;
#uparrow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
#uparrow.SetLineWeight(1);
#uparrow.SetDefaultColor(Color.yellow);
def dnarrow = if showarrows and y == 1 and dn then 1 else 0;
#dnarrow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
#dnarrow.SetLineWeight(1);
#dnarrow.SetDefaultColor(Color.white);
###
input length2 = 14;
input colorNormLength = 14;
assert(length2 > 0, "'length' must be positive: " + length2);
def ROC = if price[length2] != 0 then (price / price[length2] - 1) * 100 else 0;
def ZeroLine = 0;
#ROC.DefineColor("Highest", Color.YELLOW);
#ROC.DefineColor("Lowest", Color.LIGHT_RED);
#ROC.AssignNormGradientColor(colorNormLength, ROC.color("Lowest"), ROC.color("Highest"));
#ZeroLine.SetDefaultColor(GetColor(5));
def highestROC = HighestAll(ROC);
def lowestROC = LowestAll(ROC);
def midPtHighestROC = highestROC/2;
def midPtLowestROC = lowestROC/2;
#highestROC.setLineWeight(3);
#lowestROC.setLineWeight(3);
#highestROC.setDefaultColor(Color.Red);
#lowestROC.setDefaultColor(Color.Green);
#midPtHighestROC.setLineWeight(1);
#midPtLowestROC.setLineWeight(1);
#midPtHighestROC.setStyle(Curve.Short_Dash);
#midPtLowestROC.setStyle(Curve.Short_Dash);
#midPtHighestROC.setDefaultColor(Color.light_red);
#midPtLowestROC.setDefaultColor(Color.light_green);
def extremeHigh = ROC < HighestROC and ROC >= midPtHighestROC;
def extremeLow = ROC > LowestROC and ROC <= midPtLowestROC;
def ROCUpSignal = extremeLow and (ROC > ROC[1]);
def ROCDownSignal = extremeHigh and (ROC < ROC[1]);
plot bullSignal = if UpArrow and ROC > ZeroLine and ZeroLine < ROC then UpArrow else double.nan;
plot bearSignal = if DnArrow and ROC < ZeroLine and ZeroLine > ROC then DnArrow else double.nan;
bullSignal.setPaintingStrategy(PaintingStrategy.Arrow_Up);
bullSignal.setDefaultColor(Color.Green);
bullSignal.setLineWeight(3);
bearSignal.setPaintingStrategy(PaintingStrategy.Arrow_Down);
bearSignal.setDefaultColor(Color.Red);
bearSignal.setLineWeight(3);
Thanks for providing your code. You were very close to what you requested! See if the following helps.
To have and confirmed ROC, I am assuming that you want the ROC on the bar before and during the arrow is the same, either above zero or below zero for bullsignal/bearsignal.
Code:
plot bullSignal = if UpArrow and ROC > ZeroLine and ROC[1] > ZeroLine[1] then UpArrow else double.nan;
plot bearSignal = if DnArrow and ROC < ZeroLine and ROC[1] < ZeroLine[1] then DnArrow else double.nan;
The [1] in the above is the ROC bar before the Uparrow/Downarrow.
Also your code needed the Arrows to be boolean as Uparrow/Downarrow are defined as 1/0 (true/false) rather than a price.
Here is the combined code with the above adjustments
Ruby:input length = 14; input signalLength = 26; input averageType = AverageType.EXPONENTIAL; input showarrows = yes; def OBV = TotalSum(Sign(close - close[1]) * volume); def OBVM = MovingAverage(averageType, OBV, length); def Signal = MovingAverage(averageType, OBVM, signalLength); def price = close ; def up = OBVM > Signal and Signal < OBVM ; def dn = OBVM < Signal and Signal > OBVM ; def neutral = between(close,min(OBVM,SIGNAL),max(SIGNAL,OBVM)); def x = CompoundValue(1, if x[1]>=0 and (dn[1] or up[1]==0) and up then 1 else if x[1]>=1 and (up or !dn) then x[1]+1 else 0, 1); def y = CompoundValue(1, if y[1]<=0 and (up[1] or dn[1]==0) and dn then 1 else if y[1]>=1 and (dn or !up) then y[1]+1 else 0, 1); def uparrow = if showarrows and x == 1 and up then 1 else 0; #uparrow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP); #uparrow.SetLineWeight(1); #uparrow.SetDefaultColor(Color.yellow); def dnarrow = if showarrows and y == 1 and dn then 1 else 0; #dnarrow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN); #dnarrow.SetLineWeight(1); #dnarrow.SetDefaultColor(Color.white); ### input length2 = 14; input colorNormLength = 14; assert(length2 > 0, "'length' must be positive: " + length2); def ROC = if price[length2] != 0 then (price / price[length2] - 1) * 100 else 0; def ZeroLine = 0; #ROC.DefineColor("Highest", Color.YELLOW); #ROC.DefineColor("Lowest", Color.LIGHT_RED); #ROC.AssignNormGradientColor(colorNormLength, ROC.color("Lowest"), ROC.color("Highest")); #ZeroLine.SetDefaultColor(GetColor(5)); def highestROC = HighestAll(ROC); def lowestROC = LowestAll(ROC); def midPtHighestROC = highestROC/2; def midPtLowestROC = lowestROC/2; #highestROC.setLineWeight(3); #lowestROC.setLineWeight(3); #highestROC.setDefaultColor(Color.Red); #lowestROC.setDefaultColor(Color.Green); #midPtHighestROC.setLineWeight(1); #midPtLowestROC.setLineWeight(1); #midPtHighestROC.setStyle(Curve.Short_Dash); #midPtLowestROC.setStyle(Curve.Short_Dash); #midPtHighestROC.setDefaultColor(Color.light_red); #midPtLowestROC.setDefaultColor(Color.light_green); def extremeHigh = ROC < HighestROC and ROC >= midPtHighestROC; def extremeLow = ROC > LowestROC and ROC <= midPtLowestROC; def ROCUpSignal = extremeLow and (ROC > ROC[1]); def ROCDownSignal = extremeHigh and (ROC < ROC[1]); plot bullSignal = if UpArrow and ROC > ZeroLine and ROC[1] > ZeroLine[1] then UpArrow else double.nan; plot bearSignal = if DnArrow and ROC < ZeroLine and ROC[1] < ZeroLine[1] then DnArrow else double.nan; bullSignal.setPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP); bullSignal.setDefaultColor(Color.Green); bullSignal.setLineWeight(3); bearSignal.setPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN); bearSignal.setDefaultColor(Color.Red); bearSignal.setLineWeight(3);