Hello,

I'm developing a script which analyzes financial trades. I want to find out the average number of trades open at the same time. I have the start and end dates of each trade, but I cannot figure out a way to do this properly.

I have found a few ways to compare date ranges and see if they overlap... However the problem is that I think it needs to be recursive.. Because I really need to compare each trade to all the other trades.

For instance, trade #1 might be a very long trade lasting several weeks. During this time dozens of other trades open and close. Using a simple date overlap method, I would find dozens of trades "open at the same time" as trade #1. However this does not help me find the average number of open trades, because these trades all had their own open/close dates which may or may not overlap with the rest of them. All I know for sure is that they all overlap with trade #1 at some point in time.

So I would greatly appreciate anyone's input on this. Again I'm looking for the average number of trades open at one time.


In reply to Recursive method to check overlap of many date ranges by bigbot

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.