/* Collect contiguous ranges. These are where the underlying is expected to be at the front-month. Start with an empty array of calendars (the "position"). (The array will contain the final, optimized position.) Starting at the leftmost strike that's within the user-defined spot-price range, find the cheapest (though not necessarily most capital-efficient) front-month/back-month combo, and append it to the calendar-array. Check profitability, as follows: Check profitability assuming the underlying expires at the leftmost spot-price of the leftmost range. The next spot-prices to check are at the strikes _within_ the range, followed by the rightmost spot-price of the range; followed by doing the same for the rest of the ranges. If, at "expiration" (i.e. the front-month), the position is profitable at this spot-price [*1]: If there are more spot-prices to check to the right of this one: Check the next spot-price. Else (i.e. if there are _not_ more spot-prices to check to the right of this one): That means we're "covered". Return position. Else (i.e. if, at expiration, the position is _not_ profitable at this spot-price): Try to shift the rightmost calendar to the right (regardless of whether it's towards or away from the current spot-price) by one strike. Check profitability starting from the leftmost spot-price-at-expiration, up to and including the spot-price we're currently evaluating: If the tweaked position is profitable at this spot-price: If we're holding by the spot-price we're currently evaluating: Keep the tweaked position, and continue with it as we would with a "profitable-thus-far" position (i.e. [*1]). Else (i.e. if we're _not yet_ holding by the spot-price we're currently evaluating): Continue checking with the next spot-price. Else (i.e. if the tweaked position is _not_ profitable at this spot-price): If we're holding by the spot-price we're currently evaluating: Shift, by one more strike, the rightmost calendar in the position. Else if we're holding by the */ export const calculateBestCombinationOfCalendars(){ let cheapest = null; front_months.forEach(front_month=>{ front_month.ranges.forEach(range=>{ range.back_months.forEach(back_month=>{ // begin by making a calendar at every available strike in the range. }) }) }) }