Hi all,
Continuing the discussion from Bars since higher or equal value:
As @ Tomasz has given the following functions in the above thread, it appears this request falls outside the scope of those available:
Bars since highest counting from some event
http://www.amibroker.com/f?highestsincebars
or (bars since highest in given period):
http://www.amibroker.com/f?hhvbars
or (bars since all time highest):
http://www.amibroker.com/f?highestbars
I'm looking for the logic behind how I could:
- Test a condition, in this case if the same day
- Find the Min/Max value when the condition was met i.e. like LowestSince
- Repeat ONLY the Min/Max value across all bars when the condition was true.
Essentially I guess what I'm looking or perhaps even make a request for is the equivalent of a "MaxSinceLast" function. The function would work as MaxSinceLast(Condition,Array); where it will test the condition for true false and if true, will return only the max value of the specified array until the condition returns false where it will then return the next max value
* HighestSinceLast() OR MaxSinceLast(Condition,Array)
* HighestSumSince() OR MaxSumSince(Condition,Array)
* LowestSinceLast() OR LowestSince(Condition,Array)
* LowestSumSince() OR MinSumSince(Condition,Array) // Probably not needed because may always = 0
* ValueWhenLast(Condition,Array)
* HighestBarsSinceLast() OR MaxBarsSince(Condition,Array)
* LowestBarsSinceLast() OR LowestBarsSince(Condition,Array) // Probably not needed because may always = 0
The rationale in this example is to find the number of bars when a condition occurred on a given time interval, then divide this by the total bars that occurred on that time interval to get a percentage of how long the situation occurred over that timeframe, like a time in market gauge. By being able to conditionally return only the highest/lowest value, you could then also use this to compare other values from a different condition.
Below is scenario to make illustrate the requirement:
Sameday = DateNum() == Ref(DateNum(),-1); // find if the same day.
BarsCount = BarsSince(Sameday)
MaxBarsInDay = MaxSinceLast(Sameday,BarsCount) // Returns the max bars when same day is true until false.
ConditionTF = C>5 // Returns a true for when the condition is met
ConditionSum = MaxSumSince(Sameday, ConditionTF) // While a sumSince would sum when the condition is met and create a tally the MaxSumSince would return the highest of SumSince across all bars where the condition is met. So if Condition1TF occurred 100 times, the function would return 100 for all times true opposed to the running count.
TimeInMarket = Condition1Sum / MaxBarsInDay //You can now compare the output of highest and lowest across multiple conditions within a given timeframe which is really useful for exploration and distribution information.
The image above is the closest I've come to a valid result as shown in the Repeat Values and HHV Values columns.
My code is:
Y51 = NewDay != 1;
Y52 = BarsSince(Y51 == 0);
Y50 = IIf(NewDay + 1 == Ref(NewDay,1),Y52, Null);
Y54 = ValueWhen(Y52>Ref(Y52,1),Y52);
Y55 = HHV(Y50,Y50);
Y56 = BarsSinceCompare(Y51,"<", Y51);
AddColumn(BI, "BI",1);
AddColumn(Y50,"Last Bar",1);
AddColumn(Y51,"New Day",1);
AddColumn(Y52,"Max Bars",1);
AddColumn(Y54,"Repeat Values",1);
AddColumn(Y55,"HHV Values",1);
AddColumn(Y56,"BarsSinceCompare",1);
Functions I've explored include:
Highest - Only pulls the highest value of the entire array, not the highest since last true.
HighestSince - works well, but creates a moving highest value instead of returning ONLY the highest since condition was true.
BarsSince - Used this to work out the number of bars since the condition was true. I need the only the highest of this value to be repeated on the same day.
HHV - Thought this may have solved it but only end up retrieving the previous days bars.
BarsSinceCompare - Another I thought may work, but again only ended up achieving the running count again.
Cheers!
John