コード例 #1
0
    def on_time(self, timestamp:pd.Timestamp, timestamp_next:pd.Timestamp):
        """
        This method is called with every iteration and provides the timestamps
        for both current and next iteration. The given interval may be used to
        submit orders before a specific point in time.

        :param timestamp:
            pd.Timestamp, timestamp recorded
        :param timestamp_next:
            pd.Timestamp, timestamp recorded in next iteration
        """

        # close active positions and cancel active orders two minutes before the market closes
        if timestamp.time() >= datetime.time(16, 28) and self.time_to_trade:
            for market_id, market in self.markets.items():

                # cancel active orders for this market
                if self.get_filtered_orders(market_id, status="ACTIVE"):
                    [self.cancel_order(order) for order in self.get_filtered_orders(market_id, status="ACTIVE")]

                # close positions for this market
                if self.exposure[market_id] > 0:
                    self.submit_order(market_id, "sell", self.default_quantity)
                if self.exposure[market_id] < 0:
                    self.submit_order(market_id, "buy", self.default_quantity)
                
                # ...
                self.time_to_trade = False
            
        # we want to start trading 15 minutes after XETRA has opened
        if timestamp.time() >= datetime.time(8, 15) and not self.time_to_trade:
            self.time_to_trade = True  
コード例 #2
0
ファイル: time.py プロジェクト: newlyedward/qlib
def cal_sam_minute(x: pd.Timestamp,
                   sam_minutes: int,
                   region: str = REG_CN) -> pd.Timestamp:
    """
    align the minute-level data to a down sampled calendar

    e.g. align 10:38 to 10:35 in 5 minute-level(10:30 in 10 minute-level)

    Parameters
    ----------
    x : pd.Timestamp
        datetime to be aligned
    sam_minutes : int
        align to `sam_minutes` minute-level calendar
    region: str
        Region, for example, "cn", "us"

    Returns
    -------
    pd.Timestamp:
        the datetime after aligned
    """
    cal = get_min_cal(C.min_data_shift, region)[::sam_minutes]
    idx = bisect.bisect_right(cal, x.time()) - 1
    _date, new_time = x.date(), cal[idx]
    return concat_date_time(_date, new_time)
コード例 #3
0
    def on_time(self, timestamp: pd.Timestamp, timestamp_next: pd.Timestamp):
        """
        This method is called with every iteration and provides the timestamps
        for both current and next iteration. The given interval may be used to
        submit orders before a specific point in time.

        :param timestamp:
            pd.Timestamp, timestamp recorded
        :param timestamp_next:
            pd.Timestamp, timestamp recorded in next iteration
        """

        # set flag if time is past 16:25, triggered only once
        if timestamp.time() >= datetime.time(16, 25) and not self.eod:
            for market_id, _ in self.markets.items():

                # cancel active orders for this market
                [
                    self.cancel_order(order)
                    for order in self.get_filtered_orders(market_id,
                                                          status="ACTIVE")
                ]

                # close positions for this market
                if self.exposure[market_id] > 0:
                    self.submit_order(market_id, "sell", self.default_quantity)
                if self.exposure[market_id] < 0:
                    self.submit_order(market_id, "buy", self.default_quantity)

            # ...
            self.eod = True

        # unset flag at start of (next) trading day
        if timestamp.day != timestamp_next.day:
            self.eod = False
コード例 #4
0
ファイル: data.py プロジェクト: zkwiatkowska/buses-warsaw
def _adjust_date(column: pd.Series, start_from: pd.Timestamp):
    validate_data_is_type(column, pd.Series)
    validate_data_is_time_column(column)

    pre_start = column[column.dt.time < start_from.time()]
    post_start = column[column.dt.time >= start_from.time()]

    post_start = pd.to_datetime(
        post_start.apply(lambda x: f'{start_from.date()} {x.time()}'))
    day_after = start_from.date() + pd.Timedelta(days=1)
    pre_start = pd.to_datetime(
        pre_start.apply(lambda x: f'{day_after} {x.time()}'))

    output = pd.concat((pre_start, post_start))
    assert output.shape == column.shape

    return output
コード例 #5
0
def download_cgi(
        dataset: str, variables: List[str], start: pd.Timestamp, end: pd.Timestamp
) -> Iterable[dict]:
    if start.time() < time(9):
        start -= pd.Timedelta(1, 'D')
    request = dict(
        daybeg=start.strftime('%d'),
        monthbeg=start.strftime('%b'),
        yearbeg=start.strftime('%Y'),
        dayend=end.strftime('%d'),
        monthend=end.strftime('%b'),
        yearend=end.strftime('%Y'),
        nexttask='retrieve',
    )
    for variable in variables:
        request[variable] = 'y'

    response = requests.post(
        'https://metdata.reading.ac.uk/cgi-bin/climate_extract.cgi',
        data=request
    )
    content = response.text
    try:
        download_url = PATTERN.search(content).group(1)
    except:
        csv_text = content\
                       .split(MARKER)[-1]\
                       .replace('\n', '')\
                       .replace('<br>', '\n')\
                       .strip() + '\n'
    else:
        csv_text = requests.get(download_url).text.replace(' ', '')

    for row in DictReader(StringIO(csv_text)):
        row['TimeStamp'] = str(
            datetime(*(int(row.get(i)) for i in ('year', 'month', 'day')), hour=9)+timedelta(days=1)
        )
        yield row