armybender
Active member
Hi. I have an "indicator" that uses a fold function to check if a bar would have achieved at least 1x risk:reward and colors bars green if they meet that condition for a long, and red if they meet that condition for a short. The bars tend to get out of synch as time goes, so you have to re-load the study or refresh the chart style to get it to correctly paint the bars.
I'm wondering if there's a way to "force" the indicator to refresh and recalculate the bar color.
What I'm saying is that even after the future bars close, it is not re-painting past bars. So, if a bar is an up bar and is 4 ticks tall, it needs to move 6 ticks above the bar's high at any point in the future to trigger a "green" condition.
When the next bar or two complete, it is not re-painting the original up bar even though the condition has been met. Once you re-load the study it paints it correctly.
I'm wondering how to make it re-paint as bars close without having to re-load the study.
I know it won't be green until that condition is met, but my concern is that because I'm using a fold function that it is only calculating the "front" bar and not actually recalculating all prior bars.
Lastly, in the past I have only used this for after-the-fact analysis. I'm trying to see IF I can use it as a real-time indicator to see which side is winning in a different way. That's why the live re-paint is now an issue I'm wondering about.
Any ideas?
Here's the code.
I'm wondering if there's a way to "force" the indicator to refresh and recalculate the bar color.
What I'm saying is that even after the future bars close, it is not re-painting past bars. So, if a bar is an up bar and is 4 ticks tall, it needs to move 6 ticks above the bar's high at any point in the future to trigger a "green" condition.
When the next bar or two complete, it is not re-painting the original up bar even though the condition has been met. Once you re-load the study it paints it correctly.
I'm wondering how to make it re-paint as bars close without having to re-load the study.
I know it won't be green until that condition is met, but my concern is that because I'm using a fold function that it is only calculating the "front" bar and not actually recalculating all prior bars.
Lastly, in the past I have only used this for after-the-fact analysis. I'm trying to see IF I can use it as a real-time indicator to see which side is winning in a different way. That's why the live re-paint is now an issue I'm wondering about.
Any ideas?
Here's the code.
Ruby:
### ACHIEVED_PROFIT_TARGET --- 2023-03-11 BY ARMYBENDER
### License: I don't care - do what you want with it.
### Determines whether a trade would have achieved a profit target before being stopped out.
### Risk is based on the bar height + 2 ticks (1 tick for a stop entry price above/below the bar, and
### 1 tick for a stop exit price below the bar. The study uses future bars, so it cannot be used in
### realtime to make trade decisions, however, it is helpful for quick analysis or measurement
### after the fact. This will work with all bar types.
### DECLARATIONS
declare upper;
#declare once_per_bar;
### GLOBAL COLOR DEFINITIONS
DefineGlobalColor("Green", CreateColor(0, 155, 0));
DefineGlobalColor("Red", CreateColor(225, 105, 105));
DefineGlobalColor("Gray", CreateColor(181, 181, 181));
### INPUTS
input requireSignalBar = no;
input colorBars = yes;
input trailColors = yes;
input showArrows = no;
input requireTickThrough = yes;
input rewardMode = {default "Risk Reward Ratio", "Number of Ticks"};
input riskTargetRatio = 1.0;
input rewardNumberOfTicks = 4;
input numberOfBarsForward = 30;
input offsetBarsPlot = 0;
input arrowTickOffset = 2;
input arrowSize = 1;
input showHammer2EBars = no;
### DEFINITIONS
def isLastBar = !IsNaN(close) and IsNaN(close[-1]);
def rtRatio = riskTargetRatio;
def barsFwd = numberOfBarsForward;
def longEntry = high + TickSize();
def shortEntry = low - TickSize();
def riskAmount = if requireTickThrough then high - low + (3 * TickSize()) else high - low + (2 * TickSize());
def longStop = low - TickSize();
def shortStop = high + TickSize();
def longTarget =
if rewardMode == rewardMode."Risk Reward Ratio" then
longEntry + (rtRatio * riskAmount)
else if rewardMode == rewardMode."Number of Ticks" then
longEntry + rewardNumberOfTicks * TickSize()
else
Double.NaN
;
def shortTarget =
if rewardMode == rewardMode."Risk Reward Ratio" then
shortEntry - (rtRatio * riskAmount)
else if rewardMode == rewardMode."Number of Ticks" then
shortEntry - rewardNumberOfTicks * TickSize()
else
Double.NaN
;
def isUp = close > open;
def isDown = close < open;
def isBullDoji = IsDoji() and high == close;
def isBearDoji = IsDoji() and low == close;
def isBullSignalBar = if (isUp or isBullDoji) and low < low[1] and high[-1] > high then yes else no;
def isBearSignalBar = if (isDown or isBearDoji) and high > high[1] and low[-1] < low then yes else no;
def longRiskTargetMet =
if !requireSignalBar or isBullSignalBar then
fold l = 1 to barsFwd
with long = Double.NaN
while IsNaN(long)
do
if GetValue(low, -l) <= longStop then
no
else if GetValue(high, -l) >= longTarget then
yes
else if GetValue(isLastBar, -l) then
no
else
Double.NaN
else
Double.NaN;
def shortRiskTargetMet =
if !requireSignalBar or isBearSignalBar then
fold s = 1 to barsFwd
with short = Double.NaN
while IsNaN(short)
do
if GetValue(high, -s) >= shortStop then
no
else if GetValue(low, -s) <= shortTarget then
yes
else if GetValue(isLastBar, -s) then
no
else
Double.NaN
else
Double.NaN;
def longPlotData =
if longRiskTargetMet then
low - (arrowTickOffset * TickSize())
else
Double.NaN;
def shortPlotData =
if shortRiskTargetMet then
high + (arrowTickOffset * TickSize())
else
Double.NaN;
def colorValue =
if longRiskTargetMet then 1 # Green
else if shortRiskTargetMet then -1 # Red
else if trailColors then colorValue[1] # Last Color
else 0; # Gray
### PLOTS
plot longPlot = if showArrows then if longPlotData[offsetBarsPlot] then low - (arrowTickOffset * TickSize()) else Double.NaN else Double.NaN;
plot shortPlot = if showArrows then if shortPlotData[offsetBarsPlot] then high + (arrowTickOffset * TickSize()) else Double.NaN else Double.NaN;
### BAR COLORING
AssignPriceColor(
if colorBars then
if colorValue == 1 then GlobalColor("Green")
else if colorValue == -1 then GlobalColor("Red")
else if isLastBar then Color.CURRENT
else GlobalColor("Gray")
else GlobalColor("Gray")
);
### FORMATTING
longPlot.DefineColor("Long", CreateColor(0, 205, 0)); ### Custom green color
longPlot.SetDefaultColor(longPlot.Color("Long"));
longPlot.HideBubble();
longPlot.HideTitle();
longPlot.SetLineWeight(arrowSize);
longPlot.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
shortPlot.DefineColor("Short", CreateColor(205, 0, 0)); ### Custom red color
shortPlot.SetDefaultColor(shortPlot.Color("Short"));
shortPlot.HideBubble();
shortPlot.HideTitle();
shortPlot.SetLineWeight(arrowSize);
shortPlot.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
Last edited by a moderator: