Function To Find Closest Bar Number for Given DateTime

User avatar
dataheck
Posts: 30
Joined: 19 Nov 2019
Location: Amherst, Nova Scotia
Been thanked: 5 times
Contact:

Function To Find Closest Bar Number for Given DateTime

Postby dataheck » 09 May 2020

I need to be able to make some historical queries for some of my other projects, and so I need to access a bar somewhere deep in my data series. Unfortunately MultiCharts .NET does not have a built-in feature for this, so I built a function that does this using a binary search.

It's pretty quick (well, since it uses binary search its O(log(N))), and it avoids recalculating if it already found the answer for a given target date.

e: 2020-05-10 modified to take into consideration MaxBarsBack, it's up to you to ensure the function will have enough data to find the answer. It should throw an exception if it doesn't have enough.
Attachments
BarNumberForDateTime.pln
(1.44 KiB) Downloaded 270 times

Emmanuel
Posts: 355
Joined: 21 May 2009
Has thanked: 109 times
Been thanked: 28 times

Re: Function To Find Closest Bar Number for Given DateTime

Postby Emmanuel » 12 May 2020

Hoaw !! very interesting and helpful !!!!

This is a good idea !!!

I was looking for this function !!!!!!

Thank you Mat for this excellent function !!!!

rc76
Posts: 43
Joined: 14 Oct 2020
Has thanked: 10 times

Re: Function To Find Closest Bar Number for Given DateTime

Postby rc76 » 20 Dec 2022

Will there be any chance to create a version for regular MC as well?

This function will be super useful!

Thank you sir in advance!


Return to “User Contributed Studies”