Последовательное совпадение в разных таймфреймахPhp

Кемеровские программисты php общаются здесь
Ответить Пред. темаСлед. тема
Anonymous
 Последовательное совпадение в разных таймфреймах

Сообщение Anonymous »


Imagine we're having this array:

$results = [ '1h' => [ 0 => [ 'isConditionMet' => false, ], 1 => [ 'isConditionMet' => true, ], ], '30m' => [ 0 => [ 'isConditionMet' => false, ], 1 => [ 'isConditionMet' => false, ], 2 => [ 'isConditionMet' => true, ], 3 => [ 'isConditionMet' => false, ], ], ]; this is the result of checking occurrences of an event in 2 hours period of data shown in 2 different timeframes: 1 hour and 30 minutes, since its 2 hours of data, we have 2 items in 1h and 4 based on 30 minutes timeframes. so each item in 1h array corresponds with 2 items in 30m array. the problem in here is that I want to make sure if a condition has happend on a higher timeframe (1h in here), there must be at least one occurrences of that on its shorter timeframe (30m in here), so for the example above, the condition is met on second hour, which corresponds with items of 2 and 3 on it's shorter timeframe, so i know its a match. but there is a catch in here:
  • number of items in $results are dynamic, it might have 1 item or multiple items.
  • timeframes are also dynamic, available timeframes are: ['1m', '3m', '5m', '15m', '30m', '1h', '2h', '4h', '6h', '8h', '12h', '1d', '3d', '1w', '1M'];
  • there might be multiple items of the same timeframe, i mean there might be multiple 1h timeframes in the $results array.

what I have so far:

function findSequentialMatch($results) { // Timeframe to minutes mapping for calculations $timeframeToMinutes = [ '1m' => 1, '3m' => 3, '5m' => 5, '15m' => 15, '30m' => 30, '1h' => 60, '2h' => 120, '4h' => 240, '6h' => 360, '8h' => 480, '12h' => 720, '1d' => 1440, '3d' => 4320, '1w' => 10080, '1M' => 43200 ]; // Extract and sort timeframes from the results based on their duration in descending order $timeframes = array_keys($results); usort($timeframes, function ($a, $b) use ($timeframeToMinutes) { return $timeframeToMinutes[$b] - $timeframeToMinutes[$a]; }); // Validate sequential match for each timeframe against its next shorter timeframe for ($i = 0; $i < count($timeframes) - 1; $i++) { $currentLongerTimeframe = $timeframes[$i]; $currentShorterTimeframe = $timeframes[$i + 1]; $ratio = $timeframeToMinutes[$currentLongerTimeframe] / $timeframeToMinutes[$currentShorterTimeframe]; foreach ($results[$currentLongerTimeframe] as $index => $condition) { if ($condition['isConditionMet']) { $foundMatchInShorterTimeframe = false; $start = $index * $ratio; $end = $start + $ratio - 1; for ($j = $start; $j

Источник: https://stackoverflow.com/questions/780 ... timeframes
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «Php»