True average price of a bar?

Len20

Member
It doesn't seem possible, but does anyone know if there's a way to find the true average price of a 1 minute bar? By that, I mean the average of every transaction that occurred in that minute, or as close as possible.

I've tried code like (high + low) / 2, but that only finds the midpoint on the candle. It's not the true average price because, for example, 90% of the trading could have happened near the low of the candle, and only a few trades near the mid and top end. Then the true average would be below the midpoint. Similar issue with (high + low + open + close) / 4.
Any ideas?
 
It doesn't seem possible, but does anyone know if there's a way to find the true average price of a 1 minute bar? By that, I mean the average of every transaction that occurred in that minute, or as close as possible.

I've tried code like (high + low) / 2, but that only finds the midpoint on the candle. It's not the true average price because, for example, 90% of the trading could have happened near the low of the candle, and only a few trades near the mid and top end. Then the true average would be below the midpoint. Similar issue with (high + low + open + close) / 4.
Any ideas?
The ATR?
 

Len20

Member
Thanks for the reply. I think the ATR would give the range of prices but unfortunately not the actual average of all transactions within a bar. I now think thinkscript just doesn't have access to any data that would make it possible to determine. Thanks again for the idea though
 
Thanks for the reply. I think the ATR would give the range of prices but unfortunately not the actual average of all transactions within a bar. I now think thinkscript just doesn't have access to any data that would make it possible to determine. Thanks again for the idea though
Gotcha.

I'm not sure if this is what you want but here is code for time weighted average price (TWAP). You might be able to tweak or pull some of the code out of it for what you're looking for.

09:46 Mobius: Jaycon -

Code:
# Time Weighted Average Price
# Mobius
# Mobius at MyTrade
# TWAP in algorithmic trading is the even distribution of a large order over an even amount of time using the (V)olume (W)eighted (A)verage (P)rice. This is NOT that. This is an attempt to weight an average price using the values for the largest areas of distribution by time.

input n = 20;
input ValueAreaPercent = 70;
input SDmult = 2;
input ATRmult = 1.5;
input Time = {default chart, day};

def c = close;
def agg = getAggregationPeriod();
def TPOperiod = agg * n;
def allchart = 0;
def yyyymmdd = getYyyyMmDd();
def seconds = secondsFromTime(0);
def month = getYear() * 12 + getMonth();
def day_number = daysFromDate(first(yyyymmdd)) + getDayOfWeek(first(yyyymmdd));
def dom = getDayOfMonth(yyyymmdd);
def dow = getDayOfWeek(yyyymmdd - dom + 1);
def expthismonth = (if dow > 5
                    then 27
                    else 20) - dow;
def exp_opt = month + (dom > expthismonth);
def period = countTradingDays(Min(first(yyyymmdd), yyyymmdd), yyyymmdd) - 1;
def count = CompoundValue(1, if period != period[1]
                             then (count[1] + period - period[1]) % 1
                             else count[1], 0);
def cond = count < count[1] + period - period[1];
def timeSwitch = if Time  then cond else allchart;
profile tpo = timeProfile("startNewProfile" = timeswitch, "onExpansion" = yes, "numberOfProfiles" = 1000, "pricePerRow" = ticksize(), "value area percent" = valueAreaPercent);
tpo.Show("color"=Color.DARK_GRAY, "poc color" = Color.LIGHT_GRAY, "va color" = Color.GRAY, "open color" = Color.WHITE, "close color" = Color.YELLOW, "ib color" = Color.BLUE, "volume va color" = Color.CYAN, "volume poc color" = Color.DARK_ORANGE);
def PPR = PriceperRow.ticksize;

def hVA = if IsNaN(tpo.getHighestValueArea())
          then hVA[1]
          else tpo.getHighestValueArea();
def lVA = if IsNaN(tpo.getLowestValueArea())
          then lVA[1]
          else tpo.getLowestValueArea();
def pc = if IsNaN(tpo.getPointOfControl())
         then pc[1]
         else tpo.getPointOfControl();
def Wt = (1-(pc / ((hva - lva) + pc)));
def SD = StDev(c, n);
def ATR = Average(TrueRange(high, close, low), n);

plot TWAP = ExpAverage((sum(c, n) + sum(wt, n)) / n, n);
     TWAP.SetStyle(Curve.Firm);
     TWAP.SetLineWeight(1);
     TWAP.AssignValueColor(if TWAP < TWAP[1]
                           then Color.Red
                           else Color.Green);

plot TWAPupper = TWAP + (SDmult * SD);
     TWAPupper.SetDefaultColor(Color.Yellow);

plot TWAPatr = TWAP + (ATRmult * ATR);
     TWAPatr.SetDefaultColor(Color.Red);

plot TWAPlower = TWAP + (-SDmult * SD);
     TWAPlower.SetDefaultColor(Color.Yellow);

plot TWAPatrDn = TWAP - (ATRmult * ATR);
     TWAPatrDn.SetDefaultColor(Color.Red);

AddCloud(TWAPatr, TWAPupper, CreateColor(50,50,50), Color.Current);
AddCloud(TWAPatrDn, TWAPlower, Color.Current, CreateColor(50,50,50));

# End Code TWAP
 
Last edited:

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