Ejemplo n.º 1
0
def _ms_bounds(local_symbol, day):
    """Return the inclusive starting and exclusive ending time bounds in
    milliseconds for the specified date.

    This is just a sample function. In practice, there would likely be a
    configuration file that specified different time intervals (trading
    windows) for different instruments. The SPY will trade at a different time
    than the USD/JPY pair for instance.

    Keyword arguments:
    local_symbol -- ticker symbol
    day          -- day in 'yyyy-mm-dd' form

    """
    milliseconds = ds.str_to_ms(day, TIMEZONE, '%Y-%m-%d')
    weekday = ds.ms_to_datetime(milliseconds, TIMEZONE).weekday()
    # Don't support weekends
    if weekday > 4:
        return 0, 0
    start_time, end_time = 0, 0
    # For this example, we assume a '.' in the symbol refers to a currency so
    # we trade between 06:00 and 20:00 in the specified timezone
    if '.' in local_symbol:
        start_time = '06:00'
        end_time = '20:00'
    # Otherwise, we'll assume it's an equity and trade 09:30 to 16:00
    else:
        start_time = '09:30'
        end_time = '16:00'
    min_time = '{0} {1}:00.000'.format(day, start_time)
    min_ms = ds.str_to_ms(min_time, TIMEZONE)
    max_time = '{0} {1}:00.000'.format(day, end_time)
    max_ms = ds.str_to_ms(max_time, TIMEZONE)
    return min_ms, max_ms
Ejemplo n.º 2
0
def create_bar_data(start_date, end_date, min_time, max_time, timezone):
    """Create a list of the form [(bar_time, bar_count), ...] with each
    element in the list representing the timeframe for a historical data
    request. This is calculated using the known constraints that will prevent a
    pacing violation.

    Keyword arguments:
    start_date -- start date in yyyy-mm-dd format
    end_date   -- end date in yyyy-mm-dd format
    min_time   -- minimum inclusive session starting time in hh:mm form
    max_time   -- maximum exclusive session ending time in hh:mm form
    timezone   -- time zone

    """
    # Append times to the dates
    min_hour, min_minute = [int(z) for z in min_time.split(':')]
    max_hour, max_minute = [int(z) for z in max_time.split(':')]
    start_time = '{0} {1}:{2}'.format(start_date, min_hour, min_minute)
    end_time = '{0} {1}:{2}'.format(end_date, max_hour, max_minute)
    # Convert our time to milliseconds
    start_ms = ds.str_to_ms(start_time, timezone, '%Y-%m-%d %H:%M')
    end_ms = ds.str_to_ms(end_time, timezone, '%Y-%m-%d %H:%M')
    # Enumerate each day as a start:end pair
    span_min = (max_hour * 60 + max_minute) - (min_hour * 60 + min_minute)
    span_ms = span_min * 60 * 1000
    days = []
    first_ms, last_ms = start_ms, start_ms + span_ms
    while first_ms < end_ms:
        dtime = ds.ms_to_datetime(last_ms, timezone)
        # Only allow Monday through Friday
        if dtime.weekday() < 5:
            days.append((first_ms, last_ms))
        first_ms += DAY_MS
        last_ms += DAY_MS
    # Break each day into blocks of MAX_BLOCK_SIZE
    blocks = []
    for day in days:
        total_span_secs = (day[1] - day[0]) / 1000
        total_bars = total_span_secs / config.BAR_SIZE_SECONDS
        last_ms = day[0]
        while total_bars > 0:
            bars = min(total_bars, config.MAX_BLOCK_SIZE)
            span_ms = int(bars * config.BAR_SIZE_SECONDS * 1000)
            blocks.append((last_ms + span_ms, bars))
            total_bars -= config.MAX_BLOCK_SIZE
            last_ms += span_ms
    return blocks