Getting maximum value of bid in the current bar

epete

Member
VIP
I'm trying to save the bid price after entering a trade (on a 1 minute chart) and try to save the maximum of this value to establish a trailing stop value. I'm using the following code so far. It calculates a TS value, but that value goes up and DOWN in the current bar (I assume the script gets evaluated for every tick in the 1 minute bar). I'm puzzled why it isn't retaining a MAXimum value, but instead rises and lowers during the bar.
Code:
input TrailStop   = 2.5;#hint TrailStop: Trailing stop
input offsetType  = {default value, percent, tick};
def qty   = GetQuantity();
def entry = GetAveragePrice();
def bid   = if qty==0 then 0 else close(GetSymbol(), AggregationPeriod.MIN, PriceType.BID);

#---------------------------------
def MaxBid = if qty<>0 then
 if qty>0 then
  if IsNan(entry[1]) then entry else Max(bid, MaxBid[1])
 else
  if IsNan(entry[1]) then entry else Min(bid, MaxBid[1])
else 0;

def mult;
switch (offsetType) {
case percent:
    mult = MaxBid / 100;
case value:
    mult = 1;
case tick:
    mult = TickSize();
}

def TSP = if qty>0 then MaxBid - TrailStop * mult else if qty<0 then MaxBid + TrailStop * mult else 0;
 
Solution
ThinkScript can be a little weird at first if you're used to traditional programming. ThinkScript is iterated on a bar by bar basis, from left to right. You're basically always working within a for/each loop that runs the entire chart. Every variable is essentially an array, with offsets being related to the current position within the iteration as it makes its pass.

However, in open markets, the right-most bar is live and fluctuating. It is not possible to retrieve a prior state from among those fluctuations, or in other words, intra-bar values. Once a bar closes, and a new bar opens, the closed bar becomes a historic bar, which basically just represents the final resting place of that previously fluctuating bar.

On the right-most...

Joshua

Well-known member
VIP
MaxBid[1] references the final value from the prior bar, not the previous intra-bar value. Bid can decrease and still be the greater of the two values.
 

epete

Member
VIP
Thanks, I guess I'm not following. Perhaps I don't understand the notion of "intra-bar value" vs "final value from the prior bar". My thought is that, if the final value from the prior bar is say 12; and the current bid is say 14; then wouldn't I get the maximum value of 14? From another post "no, you can't save a value during a bar." https://usethinkscript.com/threads/intra-bar-trailing-stop-code.8300/ So, am I stuck not being able to get this? Is there some code the gets me at least closer? Many thanks.
 

Joshua

Well-known member
VIP
ThinkScript can be a little weird at first if you're used to traditional programming. ThinkScript is iterated on a bar by bar basis, from left to right. You're basically always working within a for/each loop that runs the entire chart. Every variable is essentially an array, with offsets being related to the current position within the iteration as it makes its pass.

However, in open markets, the right-most bar is live and fluctuating. It is not possible to retrieve a prior state from among those fluctuations, or in other words, intra-bar values. Once a bar closes, and a new bar opens, the closed bar becomes a historic bar, which basically just represents the final resting place of that previously fluctuating bar.

On the right-most active bar, it would be 14 in that exact moment, but if it drops down to anything 12 or greater, it will drop to that value. You're comparing the live fluctuating value to the prior bar's closed value. 14 is greater than 12, but so is 13. Therefore, it can drop from 14 to 13 with no way of determining that it was at 14 previously.

However, you can also use High() rather than Close() along with PriceType.BID, and High, by its very nature, will not recede intra-bar.
 
Last edited:
Solution

epete

Member
VIP
Thanks, that helps explain it better. I think I'll just change the Close() to High() which seems to help, although I do occasionally receive a N/A for the TS.
 

Joshua

Well-known member
VIP
If I remember correctly, I once wrote a script to visualize the bid ask spread on the chart. It seemed to randomly fail and then resume working again, for no apparent reason, without any changes to the code. I believe that references to PriceType.BID and ASK can simply be unreliable at times. I didn't put too much thought into it before I got distracted by something else though. Perhaps I'll take another look at it.
 

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.
Top