Plot renders into the future

Chillionaire

New member
VIP
I have the following script. Not really much to it to be honest, just 2 lines. Define a moving average variable and then plot that variable indexed to the prior bar. Moves the plot forward in time instead of actually rendering the plot on the current candle.

Code:
def avg = SimpleMovingAvg(close, 20);
def avgHist = avg[1];

plot Data = avg;
plot Data2 = avgHist;

This plot shifts forward one candle into the future instead of stopping on the current candle. Bizarre.
Any ideas why that is happening?

screen shot of the result shows the purple plot has jumped ahead of the current candle.

Here is a TOS link to a chart with the code:
http://tos.mx/f3iZRUZ
 
Last edited by a moderator:
Solution
Thank you so much. That explanation makes perfect sense. So at least now I know that the data in my calculations for the current candle is ok, even though the next candle's values are already plotting.

I'm guessing there is no way to disable this behavior, so I'll just ignore it.

Thanks again,

This will help keep the plot within the display of active candles.

Screenshot-2023-04-15-102255.png
Code:
def avg = SimpleMovingAvg(close, 20);
def avgHist = avg[1];

plot Data = avg;
plot Data2 = if isnan(close) then double.nan else avgHist;
I have the following script. Not really much to it to be honest, just 2 lines. Define a moving average variable and then plot that variable indexed to the prior bar. Moves the plot forward in time instead of actually rendering the plot on the current candle.

Code:
def avg = SimpleMovingAvg(close, 20);
def avgHist = avg[1];

plot Data = avg;
plot Data2 = avgHist;

This plot shifts forward one candle into the future instead of stopping on the current candle. Bizarre.
Any ideas why that is happening?

screen shot of the result shows the purple plot has jumped ahead of the current candle.

Here is a TOS link to a chart with the code:
http://tos.mx/f3iZRUZ

it plots because the variable has valid data at that time location.
there are placeholders for candles after the last visible candle.

after the last bar, 2 of the plots are visible because they are looking back at previous bars, so they have valid data.
plot D4 = round(avg[1],2);
plot D5 = round(avg[2],2);


here is a test study that draws 5 lines, each with a different offset.

Code:
# test_lines_offset1

def avg = SimpleMovingAvg(close, 20);
#def avgHist = avg[1];
#plot Data2 = avgHist;


plot D1 = round(avg[-2],2);
plot D2 = round(avg[-1],2);
plot D3 = round(avg[0],2);
plot D4 = round(avg[1],2);
plot D5 = round(avg[2],2);

addchartbubble(1, low,
d1 + "  [-2]\n" +
d2 + "  [-1]\n" +
d3 + "  [0]\n" +
d4 + "  [1]\n" +
d5 + "  [2]" 
, color.yellow, no);
#
 

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

it plots because the variable has valid data at that time location.
there are placeholders for candles after the last visible candle.

after the last bar, 2 of the plots are visible because they are looking back at previous bars, so they have valid data.
plot D4 = round(avg[1],2);
plot D5 = round(avg[2],2);


here is a test study that draws 5 lines, each with a different offset.

Code:
# test_lines_offset1

def avg = SimpleMovingAvg(close, 20);
#def avgHist = avg[1];
#plot Data2 = avgHist;


plot D1 = round(avg[-2],2);
plot D2 = round(avg[-1],2);
plot D3 = round(avg[0],2);
plot D4 = round(avg[1],2);
plot D5 = round(avg[2],2);

addchartbubble(1, low,
d1 + "  [-2]\n" +
d2 + "  [-1]\n" +
d3 + "  [0]\n" +
d4 + "  [1]\n" +
d5 + "  [2]"
, color.yellow, no);
#
Thank you so much. That explanation makes perfect sense. So at least now I know that the data in my calculations for the current candle is ok, even though the next candle's values are already plotting.

I'm guessing there is no way to disable this behavior, so I'll just ignore it.

Thanks again,
 
Thank you so much. That explanation makes perfect sense. So at least now I know that the data in my calculations for the current candle is ok, even though the next candle's values are already plotting.

I'm guessing there is no way to disable this behavior, so I'll just ignore it.

Thanks again,

This will help keep the plot within the display of active candles.

Screenshot-2023-04-15-102255.png
Code:
def avg = SimpleMovingAvg(close, 20);
def avgHist = avg[1];

plot Data = avg;
plot Data2 = if isnan(close) then double.nan else avgHist;
 
Solution

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
479 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