예제 #1
0
def get_fund_data(isin: str, start_date: np.datetime64,
                  end_date: np.datetime64) -> pd.DataFrame:
    """
    Gets price data for fund for specified period

    Args:
        ticker (str): String ticker with ISIN
        start_date (np.datetime64): Start date to get price data
        end_date (np.datetime64): End date to get price data

    Returns:
        pd.DataFrame: Dataframe containing close, split, dividend data for ticker from start_date to end_date
    """

    try:
        fund_search = investpy.search_funds(by='isin', value=isin)
        name = fund_search.at[0, 'name']
        country = fund_search.at[0, 'country']
        df = investpy.get_fund_historical_data(
            fund=name,
            country=country,
            from_date=start_date.strftime('%d/%m/%Y'),
            to_date=end_date.strftime('%d/%m/%Y'))
        df.drop('Currency', axis=1, inplace=True)
        df.reset_index(inplace=True)
    except RuntimeError:
        # if not in investpy database, check if there is a custom funds module, import and execute custom fund function
        try:
            from utils.custom_funds import get_custom_fund_data
            df = get_custom_fund_data(isin, start_date, end_date)
        except ImportError:
            print('No custom funds module available')
            df = None
    except ValueError:
        df = None

    if isinstance(df, pd.DataFrame):
        df['Stock Splits'] = 0
        df['Dividends'] = 0
        df.set_index(['Date'], inplace=True, drop=True)

    return df
예제 #2
0
def get_prices_from_API(symbol_pair: str, start_date: np.datetime64,
                        end_date: np.datetime64) -> pd.DataFrame:
    """
    Get crypto asset price history from Binance

    Args:
        symbol (str): String ticker of crypto asset (e.g ETH)
        start_date (np.datetime64): start date for historical price data
        end_date (np.datetime64): start date for historical price data

    Returns:
        pd.DataFrame: Dataframe containing binance data for crypto-currency pair from start_date to end_date 
    """

    client = Client(api_key=binance_api_key, api_secret=binance_api_secret)
    try:
        data = client.get_historical_klines(
            symbol_pair, client.KLINE_INTERVAL_1DAY,
            start_date.strftime("%d %b %Y %H:%M:%S"),
            end_date.strftime("%d %b %Y %H:%M:%S"))
    except BinanceAPIException as e:
        print(f'symbol_pair is not available on Binance')
        return None
    else:
        data_df = pd.DataFrame(data,
                               columns=[
                                   'Date', 'Open', 'High', 'Low', 'Close',
                                   'Volume', 'close_time', 'quote_av',
                                   'trades', 'tb_base_av', 'tb_quote_av',
                                   'ignore'
                               ])
        data_df['Date'] = pd.to_datetime(data_df['Date'], unit='ms')
        data_df.set_index(['Date'], inplace=True, drop=True)
        data_df[data_df.columns] = data_df[data_df.columns].apply(
            pd.to_numeric, errors='coerce')
        return data_df
예제 #3
0
def file_path(first_date: np.datetime64,
              last_date: np.datetime64,
              cycle_number: int,
              pass_number: int,
              parameters: settings.Parameters,
              nadir: bool = False) -> str:
    """Get the absolute path of the file to be created.

    Args:
        first_date (numpy.datetime64): Date of the first simulated measurement.
        last_date (numpy.datetime64): Date of the last simulated measurement.
        cycle_number (int): Cycle number of the file to be created.
        pass_number (int): Pass number of the file to be created.
        parameters (settings.Parameters): Simulation parameters.
        nadir (bool, optional): True if the product to be created contains the
            nadir measurements, false if it is a product containing the swath
            measurements.

    Returns:
        str: The path to the file to be created.
    """
    first_date = first_date.astype(datetime.datetime)
    last_date = last_date.astype(datetime.datetime)
    product_type = "nadir" if nadir else "karin"
    dirname = os.path.join(parameters.working_directory, product_type,
                           first_date.strftime("%Y"))
    os.makedirs(dirname, exist_ok=True)
    if nadir:
        filename = (f"SWOT_GPN_2P1P{cycle_number:03d}_{pass_number:03d}_"
                    f"{first_date:%Y%m%d}_{first_date:%H%M%S}_"
                    f"{last_date:%Y%m%d}_{last_date:%H%M%S}.nc")
    else:
        product_type = "".join(
            [item.capitalize() for item in parameters.product_type.split("_")])
        filename = (f"SWOT_L2_LR_SSH_{product_type}_"
                    f"{cycle_number:03d}_{pass_number:03d}_"
                    f"{first_date:%Y%m%dT%H%M%S}_{last_date:%Y%m%dT%H%M%S}_"
                    "DG10_01.nc")
    return os.path.join(dirname, filename)