Percentage change from Given Period

#1
Hi, for an exploration AFL code:

I wish to make an exploration column that can show the percentage change from a given period of time given this criteia:

From a paramlist that meets for: "Day to Date", "Weak to Date", "Month to Date" and "Year to Date". And if it's possible, print a field on the Column header that meets the paramlist selected.

Cheers.
 
#3
Filter = 1; /* all symbols and quotes accepted */

Period = ParamList("Period","Day|Week|Month|Year",0);
if(Period=="Day") { P = 1; }
else { if (Period=="Week") { P=7; }
else { if (Period=="Month") { P=30; }
if (Period=="Year")
{ P=365; }}}
Change = ROC(C,P);
AddColumn(Change,"Change",1.4);
 
#4
Filter = 1; /* all symbols and quotes accepted */

Period = ParamList("Period","Day|Week|Month|Year",0);
if(Period=="Day") { P = 1; }
else { if (Period=="Week") { P=7; }
else { if (Period=="Month") { P=30; }
if (Period=="Year")
{ P=365; }}}
Change = ROC(C,P);
AddColumn(Change,"Change",1.4);
Thanks a lot, I have tried this by means of "TimeFrameGetPrice" function, but my concern is regard of calculate weeks by count something like: "Today" from "Last Monday", same for Months, by "last first of given month", as for years by means of "last first of january". Is that possible ? I am thinking or writing a little funcion call that generates: lastday(), lastweek(), lastmonth() lastyear() using for counting dayback with "1" and for the others: dayofweek(), month() and dayofyear(). but still thinking how.

Eventhough Your approach is quite good, thanks a lot !!!
 
Last edited:
#5
Ok, it seems that I almost already resolved, the main formula end's as follow:

Code:
_SECTION_BEGIN("Profit/Loss % Change over Time");
// PARAMETERS
	TtD_Param	=	ParamList("Period","Day to Date|Week to Date|Month to Date|Year to Date",0);
// VARIABLES
	if			(TtD_Param == "Day to Date")	P = 1;
	else	if	(TtD_Param == "Week to Date")	P = DayOfWeek();	
	else	if	(TtD_Param == "Month to Date")	P = Day();			
	else	if	(TtD_Param == "Year to Date")	P = DayOfYear();
// FORMULA
	TtD_Change		=	ROC(C,P);
//	TtD_Change		=	C-P;
_SECTION_END();

_SECTION_BEGIN("Explorer");
// FILTER
Filter = 1;
// DISPLAY COLUMNS
	AddColumn(TtD_Change,"P/L % Change",1.3,IIf(TtD_Change>0,colorGreen,colorRed));
_SECTION_END();
But the AFL Syntax verification gives me the following error
Code:
Error 5. Argument #2 has incorrect type (the function expects different argument type here)
I don't know why, I replace de "P" variable with the number returned by the functions and it work flawlesly, but with the variable on the ROC function does not.:confused:
 

trash

Well-Known Member
#6
P can only be constant.

You need to write a function using looping that makes ROC being able using variable values

like

Code:
_SECTION_BEGIN("Profit/Loss % Change over Time");
#include <cROC.afl>;

// PARAMETERS
TtD_Param = ParamList("Period","Day to Date|Week to Date|Month to Date|Year to Date",0);
// VARIABLES
if(TtD_Param == "Day to Date")	P = 1;
else if(TtD_Param == "Week to Date")	P = BarsSince( DayOfWeek() == 1 ) + 1;	
else if(TtD_Param == "Month to Date")	P = BarsSince( Month() != Ref(Month(), -1) ) + 1;			
else if(TtD_Param == "Year to Date")	P = BarsSince( DayOfYear() != Ref(DayOfYear(), -1) ) + 1;
// FORMULA
TtD_Change = VarPer_ROC(C,P);
_SECTION_END();

_SECTION_BEGIN("Explorer");
// FILTER
Filter = 1;
// DISPLAY COLUMNS
AddColumn(TtD_Change,"P/L % Change",1.3,IIf(TtD_Change>0,colorGreen,colorRed));
_SECTION_END();
include file has your custom ROC being saved
 
Last edited:
#7
O.k. I already have success in the code by replacing ROC with common formula doing this:
Code:
TtD_Change = 100 * (Close - Ref(Close, -TtD_Period) ) / Ref(Close, -TtD_Period);
And my final AFL looks now like this:

Code:
_SECTION_BEGIN("Profit/Loss % Change over Time");
// PARAMETERS
	TtD_Param	=	ParamList("Period","Day to Date|Week to Date|Month to Date|Year to Date",0);
// VARIABLES
	TtD_Period		=	IIf(TtD_Param	==	"Day to Date",1,
						IIf(TtD_Param	==	"Week to Date",DayOfWeek(),
						IIf(TtD_Param	==	"Month to Date",Day(),
						IIf(TtD_Param	==	"Year to Date",DayOfYear,0))));
// FORMULA
//	TtD_Change	=ROC(C,TtD_Period);
	TtD_Change = 100 * (Close - Ref(Close, -TtD_Period) ) / Ref(Close, -TtD_Period);
_SECTION_END();

_SECTION_BEGIN("Explorer");
// FILTER
Filter = 1;
// DISPLAY COLUMNS
	//AddColumn(High52,"52 Week High");
	//AddColumn(Low52,"52 Week Low");
	AddColumn(C,"Price Close",1.3,IIf(C>Ref(C,-1),colorGreen,colorRed));
	AddColumn(V,"Volume",1,IIf(V>Ref(V,-1),colorGreen,colorRed));
	AddColumn(TtD_Change,"P/L % Change",1.2,IIf(TtD_Change>0,colorGreen,colorRed));
_SECTION_END();
My new concern, is that apparently DayOfYear() and for months Day() functions gives me count of bars including weekends. I am trying to find some method to persuade this and have finally good readings. Any of you have some clue regarding this ?

Thanks a lot Guys. :thumb:
 
#8
Ready, already finished with success, my main change was on Months and years finding bars, creating this two following lines replacing Day() for months and DayOfYear() for years with this:

Code:
FDayMonth	= BarsSince(Month() != Ref(Month(),-1));
FDayYear	= BarsSince(Year() != Ref(Year(),-1));
Finaly, this is the Working and Tested code:

Code:
_SECTION_BEGIN("Profit/Loss % Change over Time");
// PARAMETERS
TtD_Param = ParamList("Period","Day to Date|Week to Date|Month to Date|Year to Date",0);

// VARIABLES
FDayMonth	= BarsSince(Month() != Ref(Month(),-1));
FDayYear	= BarsSince(Year() != Ref(Year(),-1));
TtD_Period	= IIf(TtD_Param	==	"Day to Date",1,
		   IIf(TtD_Param	==	"Week to Date",DayOfWeek(),
		   IIf(TtD_Param	==	"Month to Date",FDayMonth,
		   IIf(TtD_Param	==	"Year to Date",FDayYear,0))));
// FORMULA
TtD_Change = 100 * (Close - Ref(Close, -TtD_Period) ) / Ref(Close, -TtD_Period);
_SECTION_END();

_SECTION_BEGIN("Explorer");
// FILTER
Filter = 1;
// DISPLAY COLUMNS
AddColumn(TtD_Change,"P/L % Change",1.2,IIf(TtD_Change>0,colorGreen,colorRed));
_SECTION_END();
Thanks a lot four your reply's and feedback.

Cheers :clap: