ChatGPT gave me wrong code, was: VWAP Giving all Errors

Please help me with the below code. Its giving me error in every thing.

What i am looking is a VWAP with upper and lower bands. (Exactly as it shows in Trading View).

_SECTION_BEGIN("VWAP");


// VWAP Indicator with Bands using HLC3, anchored to a specific period (session), and considering gaps

// Define the length of the period (in this case, it's the NYSE session)
SessionStart = 0930;
SessionEnd = 1600;

// Define the number of standard deviations for the upper and lower bands
NumStdDevs = 2;

// Define the time frame for the chart
TimeFrame = Interval(TimeframeGetBuiltIn);

// Check if current bar falls within the defined period
if (TimeNum() >= SessionStart AND TimeNum() <= SessionEnd) {

  // Calculate VWAP for the current bar
  Vwap = (H+L+C)/3 * Volume;

  // Sum up the volume and VWAP values
  SumVolume = SumVolume + Volume;
  SumVwap = SumVwap + Vwap;

  // Check for gaps in the price data
  if (C > Ref(C, -1)) {

    // Update the sum of the closing prices for up days
    SumUpClose = SumUpClose + C;
    NumUpClose = NumUpClose + 1;

  } else if (C < Ref(C, -1)) {

    // Update the sum of the closing prices for down days
    SumDownClose = SumDownClose + C;
    NumDownClose = NumDownClose + 1;

  }

} else {

  // Reset the sum of volume and VWAP values if bar is outside the defined period
  SumVolume = 0;
  SumVwap = 0;
  SumUpClose = 0;
  NumUpClose = 0;
  SumDownClose = 0;
  NumDownClose = 0;

}

// Calculate the final VWAP value
Vwap = SumVwap / SumVolume;

// Calculate the average closing price for up days
AvgUpClose = SumUpClose / NumUpClose;

// Calculate the average closing price for down days
AvgDownClose = SumDownClose / NumDownClose;

// Calculate the standard deviation of the closing prices for up and down days
StdDevUpClose = StdDev(C, NumUpClose, SumUpClose/NumUpClose);
StdDevDownClose = StdDev(C, NumDownClose, SumDownClose/NumDownClose);

// Plot the VWAP on the chart
Plot(Vwap, "VWAP", colorGreen);

// Plot the upper bands
Plot(Vwap + NumStdDevs * StdDevUpClose, "Upper Band 1", colorRed);
Plot(Vwap + 2 * NumStdDevs * StdDevUpClose, "Upper Band 2", colorRed);
Plot(Vwap + 3 * NumStdDevs * StdDevUpClose, "Upper Band 3", colorRed);
Plot(Vwap + 4 * NumStdDevs * StdDevUpClose, "Upper Band 4", colorRed);


// Plot the lower bands
Plot(Vwap - NumStdDevs * StdDevClose, "Lower Band 1", colorRed);
Plot(Vwap - 2 * NumStdDevs * StdDevClose, "Lower Band 2", colorRed);
Plot(Vwap - 3 * NumStdDevs * StdDevClose, "Lower Band 3", colorRed);
Plot(Vwap - 4 * NumStdDevs * StdDevClose, "Lower Band 4", colorRed);



_SECTION_END();

Read below as it has been discussed millions of times already.

And to sum up array since period start rather use SumSince function.

even when i am using iif or == its still giving other errors on the code...
also, i am not at all good at coding. very very very new to coding..

Where did you get that code from?

Chat GPT. i used that website to get the code.

Despite all hype ChatGPT is NOT capable of producing working code. It makes huge mistakes. On the surface and with trivial examples it may look like it is working, but beyond few lines it gets lost and produces nonsense. It is not ready to be used in any serious way. It can write short essays where it can hallucinate freely, but code is more complex and needs more precision than average homework for 10-year-old. Read entire thread below.

For what it is worth, StackOverflow banned ChatGPT generated submissions because they were vastly wrong Temporary policy: ChatGPT is banned - Meta Stack Overflow

4 Likes

One thing is for sure that chat gpt is hallucinating. :slightly_smiling_face:

Please help me with VWAP which works correctly. In all other threads and VWAP which was given by other users is either not working or not complete.

I am looking for VWAP which works exactly like the one in trading view.

My Belief is that it can be achieved in amibroker as it has huge options.
But i am not understanding how to.

Please help me with it.. Pleaseee.

I dint find VWAP as pre existing indicator in amibroker. Would be nice to have it like other (rsi, macd, etc)

If you chose to find out what VWAP is first:

you would know that

VWAP = Cumulative Typical Price x Volume/Cumulative Volume

Where Typical Price = High price + Low price + Closing Price/3

Cumulative = total since the trading session opened.

This is easily coded by hand.

What is typical price in AFL?
You could write this:

TypicalPrice = ( High + Low + Close ) /3;

but you don't need to because if you checked Users guide AFL Reference Manual

you would find that typical price in AmiBroker is built-in variable Avg

So you just need to calculate "Cumulative" since day start. How to detect day start?

DayStart = DateNum() != Ref( DateNum(), -1 ); // change in date

How to calculate cumulative sum since Day start? @fxshrat wrote you already - use SumSince.

So all in all VWAP is 2 lines of AFL CODE !

DayStart = DateNum() != Ref( DateNum(), -1 );
VWAP = SumSince( Avg * Volume, DayStart ) / SumSince( Volume, DayStart );

It is super-easy and super short. Way shorter than code for any other platform.

5 Likes

That is the answer to most questions.

DayStart = DateNum() != Ref( DateNum(), -1 );
VWAP = SumSince( DayStart, Avg * Volume ) / SumSince( DayStart, Volume );

This is giving empty results. :frowning_face:

Read documentation if something is unclear.
https://www.amibroker.com/guide/afl/sumsince.html

DayStart = DateNum() != Ref( DateNum(), -1 );
VWAP = SumSince( DayStart, Avg * Volume) / SumSince( DayStart, Volume );

No it doesn’t. Really you need to read the manual. It produces results but you don’t understand how to display them. For example if you want a chart you need Plot call.

@Tomasz I think you made a typo (corrected by @fxshrat )

VWAP = SumSince( DayStart, Avg * Volume) / SumSince( DayStart, Volume );

I also have one additional question/comment. I know nothing about VWAP as I do not trade intraday. But your calculation appears to not count the 1st bar? This example is using 5 minute bar intraday data.
image

Yes the order of arguments is reverse, I was typing that on my iPad from memory. And obviously my memory is not as good as it used to be. I corrected it in the previous post. Anyway you learn more when you not just copy-pasting the code.
VWAP is intraday indicator. For EOD it makes no sense (if it never got reset, its values would depend on zoom factor or how much data you have and would differ from other people if they happen to have more/less bars). You might however use say month boundary to reset it, if you really want to use VWAP for EOD.

And yes, as docs say AFL Function Reference - SUMSINCE the value gets reset to zero on day when "condition" is met (see AFL equivalent code with ValueWhen)

1 Like

Add Valuewhen(daystart, your_array) to each Sumsince via + operator.
Has been posted several times in the past already (as usual).
There is almost nothing that has not been posted already.

I Understand that everything is discussed already. And i really appreciate for the help.
I have written the code. and its working now.
But, I have few unresolved issues. Pleaseeee help me with those.
The Standard deviation for VWAP i suppose should be taken from VWAP Value. But i am not finding a way to take it from VWAP Value. Also, Not understanding how to include the 1st candle also as now its not calculating the 1st candle.
Pleaseeee help me with the complete code. Please. I am looking for a complete code for VWAP with Upper and Lower Bands for intraday.
Or is there a prebuilt indicator already existing in Amibroker which i am unable to find ? Please help me with that too.

Thank you soooo much in advance..

// VWAP calculation

DayStart = DateNum() != Ref( DateNum(), -1 );
Vwap = SumSince(DayStart, (Avg * Volume) ) / SumSince(DayStart, Volume);



// Standard deviation calculation
StdDev = StDev(Close, 10);

// Multiplier
Mult1 = 1;
Mult2 = 2;
Mult3 = 3;
Mult4 = 4;


// Upper band
UpperBand1 = Vwap + Mult1 * StdDev;
UpperBand2 = Vwap + Mult2 * StdDev;
UpperBand3 = Vwap + Mult3 * StdDev;
UpperBand4 = Vwap + Mult4 * StdDev;



// Lower band
LowerBand1 = Vwap - Mult1 * StdDev;
LowerBand2 = Vwap - Mult2 * StdDev;
LowerBand3 = Vwap - Mult3 * StdDev;
LowerBand4 = Vwap - Mult4 * StdDev;


Plot(Vwap, "VWAP", colorBlue, styleLine);
Plot(UpperBand4, "Upper Band 4", colorGreen, styleLine);
Plot(UpperBand3, "Upper Band 3", colorGreen, styleLine);
Plot(UpperBand2, "Upper Band 2", colorGreen, styleLine);
Plot(UpperBand1, "Upper Band 1", colorGreen, styleLine);
Plot(LowerBand1, "Lower Band 1", colorRed, styleLine);
Plot(LowerBand2, "Lower Band 2", colorRed, styleLine);
Plot(LowerBand3, "Lower Band 3", colorRed, styleLine);
Plot(LowerBand4, "Lower Band 4", colorRed, styleLine);

You are not able to do what? Pass VWAP instead of Close? What is difficult with that?

Really? Can't you write this:

sd = StDev( vwap, 10 ); // standard deviation from VWAP instead of Close
1 Like

i have written that. when i wrote that, it started giving me same number for all the bands.
Example, if Vwap is 20, then all 8 bands are showing 20.

Do you have Intraday data? Post the formula you are using NOW and screenshot how it looks NOW. We don’t see your computer screen, remember? A picture is worth thousands of words.

I have used standard deviation as close before and the results are like this.

If you see on the left hand side in the data window, the bands have seperate results.

now i have used the same code but only changed deviation from close to Vwap as mentioned below.

_SECTION_BEGIN("VWAP");

SetChartOptions( 0, chartShowArrows | chartShowDates );
Plot( C, "Close", ParamColor( "Color", colorDefault ), styleCandle | ParamStyle( "Style" ) | GetPriceStyle() );



// VWAP calculation

//Bars_so_far_today = 1 + BarsSince( Day() != Ref(Day(), -1));
//StartBar = ValueWhen(TimeNum() == 090000, BarIndex());
//TodayVolume = Sum(V,Bars_so_far_today);
//IIf (BarIndex() >= StartBar, Vwap = Sum (C * V, Bars_so_far_today  ) / TodayVolume,0);



DayStart = DateNum() != Ref( DateNum(), -1 );
Vwap = SumSince(DayStart, (Avg * Volume) ) / SumSince(DayStart, Volume);



// Standard deviation calculation
StdDev = StDev(Vwap, 10);



// Multiplier
Mult1 = 1;
Mult2 = 2;
Mult3 = 3;
Mult4 = 4;


// Upper band
UpperBand1 = Vwap + Mult1 * StdDev;
UpperBand2 = Vwap + Mult2 * StdDev;
UpperBand3 = Vwap + Mult3 * StdDev;
UpperBand4 = Vwap + Mult4 * StdDev;



// Lower band
LowerBand1 = Vwap - Mult1 * StdDev;
LowerBand2 = Vwap - Mult2 * StdDev;
LowerBand3 = Vwap - Mult3 * StdDev;
LowerBand4 = Vwap - Mult4 * StdDev;


Plot(VW=Vwap, "VWAP", colorBlue, styleLine);
Plot(UB4=UpperBand4, "Upper Band 4", colorGreen, styleLine);
Plot(UB3=UpperBand3, "Upper Band 3", colorGreen, styleLine);
Plot(UB2=UpperBand2, "Upper Band 2", colorGreen, styleLine);
Plot(UB1=UpperBand1, "Upper Band 1", colorGreen, styleLine);
Plot(LB1=LowerBand1, "Lower Band 1", colorRed, styleLine);
Plot(LB2=LowerBand2, "Lower Band 2", colorRed, styleLine);
Plot(LB3=LowerBand3, "Lower Band 3", colorRed, styleLine);
Plot(LB4=LowerBand4, "Lower Band 4", colorRed, styleLine);

_SECTION_END();

And the results are as this picture.

I See a single Vwap line and all the bands are also showing same number as vwap.

Kindly let me know what wrong am i doing.

Thank you so much in advance for the replies.