Calculate profit/Loss for the day

I want to calculate profit/loss made in the day. This counter will be reset at the start of a new day. I tried this
but i’s not working.

pltrade=somecalculation;
pltradeComplete=IIf(Sell, pltrade, 0);
plday=iif(datenum()!=Ref(datenum(), -1), Ref(plday, -1)+pltradeComplete);

Can someone please share their code for p/l calculation.

Read this

1 Like

Yes realised that. I tried the below with sum but still not getting the right number :frowning:

pltrade=somecalculation;
pltradeComplete=IIf(Sell, pltrade, 0);
plday=Sum(pltradeComplete, BarsSince(DateNum()!=Ref(DateNum(), -1)));
1 Like

I went through Understaning AFL tutorial again. I am not using any loops in the system and have coded the entire system using readymade functions like ValueWhen() HighesSince() etc. The above code works but from 2nd day onwards. This is because highestSince(BuyRange, Buy==sigScaleOut) returns a value that is printed as 0 but is NOT 0. This happens when there are no occurence of Buy==sigScaleOut yet (first day). Due to which some other code is not working and resultantly my p/l code also fails. Can you tell me what is returned by HighestSince(cond1, array1) when condition1 has not occurred even once till then. _trace reports it as 0 but _trace(“val=”+(val==0)) reports val=0 which means HighestSince is not returning 0.

sorry, The above should read :
This is because highestSince(BuyRange, Buy==sigScaleOut) returns a value that is printed as 0 but is NOT 0.
This happens when there are no occurrence of BuyRange yet (first day).

HighestSince( EXPRESSION, ARRAY, Nth = 1 )

What will be returned by the above when EXPRESSION has not occurred even once till then?

As posted before, debug your AFL. Instead of asking what will return HighestSince, why not Plot the result and find out by yourself! This is called debug.

1 Like

offcourse i debugged. Like i said it returns something that prints out as 0 but it’s not 0.

ok I figured this. It is returning Null but it is displayed as 0. If you have a intraday chart run this code and select a bar in the first day.

barsSinceStartOfDay=BarsSince(DateNum()!=Ref(DateNum(), -1));
_Trace("IsNull(barsSinceStartOfDay)="+IsNull(barsSinceStartOfDay));

I think we need to add extra code like below to reflect the right value. add the 2 lines below and we are good to go. :+1:

barsSinceStartOfDay=IIf(IsNull(barsSinceStartOfDay), BarIndex(), barsSinceStartOfDay);
_TRACE("barsSinceStartOfDay="+barsSinceStartOfDay);

That brings me to the question do we have to do a Null Check everytime we use inbuilt functions?

Here works as expected. The entire first day plots EMPTY

barsSinceStartOfDay=BarsSince(DateNum()!=Ref(DateNum(), -1));
Plot( barsSinceStartOfDay, "\nbarsSinceStartOfDay", colorBlack, styleOwnScale );