How to pass VWAP array to my custom function?

Hello to everybody,
I have created some function and I wanted to pass the created VWAP array to that function but nothing is happening, My code is not giving any error but nothing is plotting
this is my piece of code, Can anybody help me with this.

Bars_so_far_today = 1 + BarsSince( DateNum() != Ref(DateNum(), -1));
StartBar = ValueWhen(TimeNum() == 093000, BarIndex());
TodayVolume = Sum(V,Bars_so_far_today);
IIf (BarIndex() >= StartBar, VWAP = Sum (C * V, Bars_so_far_today  ) / TodayVolume,0);
//Plot (VWAP,"VWAP",colorOrange, styleThick);
  function myEMA(array)
{
  k=0.5;
  result[0]=(1-k)*array[0];
  for (i = 1; i < BarCount; i++)
  { 
  result[0] = array[0];
  
  result[i] = k* result[i - 1] + (1-k)*array[i];
 
  }
  return result;
}

a=myEMA(VWAP);  //passing VWAP value to my function
Plot(a,"value",colorRed,styleLine);

Because you do not read official material carefully.

This one

is incorrect line

Have you read IIf() function AFL reference?

There are clear examples on how to construct IIf() line

result = IIf( MACD() < Signal(), Volume, -Volume );

So your IIf() line should be

VWAP = IIf (BarIndex() >= StartBar, Sum (C * V, Bars_so_far_today ) / TodayVolume,0);

Also your function myEMA is not correct. Besides you do not need to create such function because AMA()/AMA2() exist.


Also this one is pointless IMO.

StartBar = ValueWhen(TimeNum() == 093000, BarIndex());
... BarIndex() >= StartBar ...

You rather mean this one

TimeNum() >= 093000

Bars_so_far_today = 1 + BarsSince( DateNum() != Ref(DateNum(), -1));
StartBar = TimeNum() >= 093000;
TodayVolume = Sum(V,Bars_so_far_today);
VWAP = IIf(StartBar, Sum (C * V, Bars_so_far_today  ) / TodayVolume,0);
//Plot (VWAP,"VWAP",colorOrange, styleThick);

a = AMA(VWAP, 0.5);  //passing VWAP value to my function
Plot(a,"value",colorRed,styleLine);

But if your day session starts at 09:30 then you do not need it anyway.. you do not need such Iif() line

Bars_so_far_today = 1 + BarsSince( DateNum() != Ref(DateNum(), -1));
TodayVolume = Sum(V,Bars_so_far_today);
VWAP = Sum (C * V, Bars_so_far_today) / TodayVolume;
//Plot (VWAP,"VWAP",colorOrange, styleThick);

a = AMA(VWAP, 0.5);  //passing VWAP value to my function
Plot(a,"value",colorRed,styleLine);
1 Like

Thanks for the reply.
But I wanted to pass the VWAP value to my custom function , how it can be done??
what is wrong with the above myEMA function?? if I pass the close value to the myEMA function it is plotting correctly but when it comes to passing VWAP value it is plotting nothing...................Please help me to correct it.

Do you have reading difficulties or do you just rush over posts someone has taken time to create??

First I said there are native AMA/AMA2.

Secondly I have even posted two links.
Do you even bother to read them carefully and completely:

14

function myEMA(array)
{
  k=0.5;
  result=0;
  for( i = 1; i < BarCount; i++ )
	result[ i ] = k * array[ i ] + (1 - k) * result[ i - 1 ];
  return result;
}

a=myEMA(C);
Plot(a,"value",colorRed,styleLine);

a=AMA(C, 0.5); 
Plot(a,"value",colorOrange,styleLine);

Yes i have studied all the things very carefully, Let me explain to you in detail why I wanted to use my own custom function ,as i wanted to multiply some complex coefficient in the loop for example volume and some other, which can not be done in AMA.

after passing VWAP values to myEMA (which you have created ) , it plots nothing.
i don't understand why it is not plotting the value, Kindly solve the issue also i do not want to use AMA function ,rather i would like to use my custom function on which i can research on different complex weighing and other thoughts.

It plots nothing on your end because you have NULL elements at start of your source array. Nothing being calculated with something returns nothing (NULL) too. NULL is like "black hole". So you have to skip NULL at start.

function cAMA(input, factor) {
	/// Code by AmiBroker.com 	
	/// @link https://www.amibroker.com/guide/afl/ama.html
	/// (mod. to skip Null by fxshrat@gmail.com)
	/// @link https://forum.amibroker.com/t/how-to-pass-vwap-array-to-my-custom-function/21918/6	
	/// Skip Null at start
	/// (Note: NullCount may exceed index range)	
	start = Min(NullCount(input), BarCount-1);
	output = Null;
	output[start] = input[start];
	for( i = start+1; i < BarCount; i++ )
		output[ i ] = factor * input[ i ] + (1 - factor) * output[ i - 1 ];
	return output;
}


Bars_so_far_today = 1 + BarsSince( DateNum() != Ref(DateNum(), -1));
TodayVolume = Sum(V,Bars_so_far_today);
VWAP = Sum (C * V, Bars_so_far_today)/TodayVolume;
//Plot (VWAP,"VWAP",colorOrange, styleThick);

a = AMA(VWAP, 0.5);
Plot(a,"value",colorRed,styleLine);

a = cAMA(VWAP, 0.5);
Plot(a,"value",colorOrange,styleLine);

So once you take care of Null both return same results again.
14


BTW...
You can easily find out yourself what happens at every element of array.

2 Likes

Thank you very much @fxshrat , I was struggling a lot for this but you have solved the problem. Also, thanks for providing the link at the last I will definitely look into this.

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.