How to record value for something before it repaints

greco26

Active member
Hi All,

I've ran into an issue with my code. By design, my code repaints however I need to hold the value of the prior occurrence. In particular, the code below is what I need help with. Is there a way to record the values of AP1_ReversingDown1ago and AP1_ReversingDown before they repaint?

Thanks in advance as always!

def AP1_ReversingDown = if H_AP1 < HighAfterWeakHighBreak and H_AP1 < AP1_weakhigh and H_AP1 < AP1_stronghigh and H_AP1 < H_AP1[1] and H_AP1[1] < H_AP1[2] and H_AP1[3] < H_AP1[2] then H_AP1[2] else AP1_ReversingDown[1];
def AP1_ReversingDown1ago = if AP1_ReversingDown != AP1_ReversingDown[1] then AP1_ReversingDown[1] else AP1_ReversingDown1ago[1];

* Put the code below on a 1 minute chart.*

Code:
##Strat_Market_Structure_Dashboard

declare lower;
input LBPeriod = 6;


input Show1m = yes;
input Show3m = yes;
input Show5m = yes;
input Show15m = yes;
input Show30m = yes;
input Show60m = yes;
input ShowDay = yes;
input ShowPriceLabels = yes; #Hint labels show for the current chart's TF


def AP1 = AggregationPeriod.MIN;
def AP2 = AggregationPeriod.THREE_MIN;
def AP3 = AggregationPeriod.FIVE_MIN;
def AP4 = AggregationPeriod.FIFTEEN_MIN;
def AP5 = AggregationPeriod.THIRTY_MIN;
def AP6 = AggregationPeriod.HOUR;
def AP7 = AggregationPeriod.DAY;

def bn = BarNumber();
def dn = Double.NaN;
def isRollover = GetYYYYMMDD() != GetYYYYMMDD()[1];
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def firstBarOfDay = if (beforeStart[1] == 1 and beforeStart == 0) or (isRollover and beforeStart == 0) then bn else firstBarOfDay[1];
def dayopen = if bn == firstBarOfDay then open else dayopen[1];
def DayTrendLong = close > dayopen;
def DayTrendShort = close < dayopen;


#&&&&&&&&&&&&&&&

### 1m timeframe ###
def tf1m = AggregationPeriod.MIN;
def valid1m = GetAggregationPeriod() <= AggregationPeriod.MIN;
def H_AP1;
def L_AP1;
def O_AP1;
def C_AP1;


if valid1m {
    H_AP1 = high (period = tf1m);
    L_AP1 = low  (period = tf1m);
    O_AP1 = open (period = tf1m);
    C_AP1 = close(period = tf1m);


} else {
    H_AP1 = 0;
    L_AP1 = 0;
    O_AP1 = 0;
    C_AP1 = 0;

}




#&&&&&&&&&&&&&&&


#--------------------------------------------------------------
def HH_AP1 = Highest(H_AP1, LBPeriod);
def LL_AP1 = Lowest(L_AP1, LBPeriod);
#--------------------------------------------------------------
def AP1_marketLow1 = if LL_AP1 < LL_AP1[-LBPeriod] then LL_AP1 else LL_AP1[-LBPeriod];
def AP1_markedLow1 = L_AP1 == AP1_marketLow1;

def AP1_lastMarkedLow1 = CompoundValue(1, if IsNaN(AP1_markedLow1) then AP1_lastMarkedLow1[1] else if AP1_markedLow1 then L_AP1 else AP1_lastMarkedLow1[1], L_AP1);
#--------------------------------------------------------------
def AP1_marketHigh1 = if HH_AP1 > HH_AP1[-LBPeriod] then HH_AP1 else HH_AP1[-LBPeriod];
def AP1_markedHigh1 = H_AP1 == AP1_marketHigh1;

def AP1_lastMarkedHigh1 = CompoundValue(1, if IsNaN( AP1_markedHigh1) then AP1_lastMarkedHigh1[1] else if AP1_markedHigh1 then H_AP1 else AP1_lastMarkedHigh1[1], H_AP1);
#--------------------------------------------------------------

def AP1_Support1 = AP1_lastMarkedLow1 ;
def AP1_stronglow = if AP1_Support1 < AP1_Support1[1] then L_AP1 else AP1_stronglow[1];
def AP1_weaklow = if L_AP1 > AP1_stronglow and AP1_Support1 != AP1_stronglow then AP1_Support1 else AP1_weaklow[1];
def AP1_BrkoMStructureBullish = DayTrendLong and low < AP1_stronglow;
def AP1_PullbackBullish = DayTrendLong and low < AP1_weaklow and low > AP1_stronglow;


def AP1_Resistance1 = AP1_lastMarkedHigh1;
def AP1_strongHigh = if AP1_Resistance1 > AP1_Resistance1[1] then H_AP1 else AP1_strongHigh[1];
def AP1_weakHigh = if H_AP1 < AP1_strongHigh and AP1_Resistance1 != AP1_strongHigh then AP1_Resistance1 else AP1_weakHigh[1];
def AP1_BrkoMStructureBearish = DayTrendShort and high > AP1_strongHigh;
def AP1_PullbackBearish = DayTrendShort and high > AP1_weakHigh and high < AP1_strongHigh;

def AP1_weakhighBN = if H_AP1 == AP1_weakHigh  then bn else AP1_weakhighBN[1];
def AP1_TTOlowbreak = if bn > AP1_weakhighBN then Lowest(L_AP1) else AP1_TTOlowbreak[1];
def AP1_NearTTOBreakShort = if close < AP1_TTOlowbreak then Double.NaN else DayTrendShort and close < AP1_TTOlowbreak * 1.0015;

def CrossAbove = Crosses(H_AP1,  AP1_weakHigh, CrossingDirection.ABOVE);
def HighAfterWeakHighBreak = if bn > AP1_weakhighBN and CrossAbove then Highest(H_AP1) else HighAfterWeakHighBreak[1];

def AP1_ReversingDown =  if H_AP1 < HighAfterWeakHighBreak and H_AP1 < AP1_weakhigh and H_AP1 < AP1_stronghigh and H_AP1 < H_AP1[1] and H_AP1[1] < H_AP1[2] and H_AP1[3] < H_AP1[2] then H_AP1[2] else AP1_ReversingDown[1];
def AP1_ReversingDown1ago = if AP1_ReversingDown != AP1_ReversingDown[1] then AP1_ReversingDown[1] else AP1_ReversingDown1ago[1];

def AP1_RevDownOK = DayTrendShort and C_AP1 < AP1_ReversingDown;

###TROUBLESHOOTING LABLES###
addlabel(yes, "In My Favor? :  " + AP1_RevDownOK , COLOR.magenta);
addlabel(yes, "Last Peak:  " + AP1_ReversingDown, COLOR.cyan);
addlabel(yes, "Previous Peak:  " + AP1_ReversingDown1ago, COLOR.green);

#*********************
 
The short answer is that you can't do that. Repainting is what happens (for ToS) when the value while a bar is the current bar indicating that some condition is true, but that something changes before the bar closes and the condition becomes untrue. Since ToS does not keep track of the inter-bar play of values (other than the usual High, Low, Open, Close and Volume) it can not maintain a value based on anything but those after the bar has closed.

hope that helps, though it isn't what you were hoping to hear. Of course, there may be tricks I am unaware of. :cool:
mashume
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

The short answer is that you can't do that. Repainting is what happens (for ToS) when the value while a bar is the current bar indicating that some condition is true, but that something changes before the bar closes and the condition becomes untrue. Since ToS does not keep track of the inter-bar play of values (other than the usual High, Low, Open, Close and Volume) it can not maintain a value based on anything but those after the bar has closed.

hope that helps, though it isn't what you were hoping to hear. Of course, there may be tricks I am unaware of. :cool:
mashume
Thanks for your note! I believe it’s possible to do based on using counters. I’ve seen it before but I’m having an issue putting it together. Hopefully someone is familiar and can help. Thx!
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
344 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top