Beispiel #1
0
    def get_instrument_raw_carry_data(self, instrument_code):
        """
        Returns a pd. dataframe with the 4 columns PRICE, CARRY, PRICE_CONTRACT, CARRY_CONTRACT

        These are specifically needed for futures trading

        :param instrument_code: instrument to get carry data for
        :type instrument_code: str

        :returns: pd.DataFrame

        >>> data=csvFuturesData("sysdata.tests")
        >>> data.get_instrument_raw_carry_data("US10").tail(4)
                                  PRICE  CARRY CARRY_CONTRACT PRICE_CONTRACT
        2015-12-10 23:00:00  126.328125    NaN         201606         201603
        2015-12-11 14:35:15  126.835938    NaN         201606         201603
        2015-12-11 16:06:35  126.914062    NaN         201606         201603
        2015-12-11 17:24:06  126.945312    NaN         201606         201603
        """

        self.log.msg(
            "Loading csv carry data for %s" % instrument_code,
            instrument_code=instrument_code)

        filename = os.path.join(self._datapath,
                                instrument_code + "_carrydata.csv")
        instrcarrydata = pd_readcsv(filename)

        instrcarrydata.CARRY_CONTRACT = instrcarrydata.CARRY_CONTRACT.apply(
            str_of_int)
        instrcarrydata.PRICE_CONTRACT = instrcarrydata.PRICE_CONTRACT.apply(
            str_of_int)

        return instrcarrydata
Beispiel #2
0
    def get_instrument_price(self, instrument_code):
        """
        Get instrument price

        :param instrument_code: instrument to get prices for
        :type instrument_code: str

        :returns: pd.DataFrame

        >>> data=csvFuturesData("sysdata.tests")
        >>> data.get_instrument_price("EDOLLAR").tail(2)
                               price
        2015-12-11 17:08:14  97.9675
        2015-12-11 19:33:39  97.9875
        >>> data["US10"].tail(2)
                                  price
        2015-12-11 16:06:35  126.914062
        2015-12-11 17:24:06  126.945312
        """

        # Read from .csv
        filename = os.path.join(self._datapath, instrument_code + "_price.csv")
        instrpricedata = pd_readcsv(filename)
        instrpricedata.columns = ["price"]
        instrpricedata=instrpricedata.groupby(level=0).last()
        return instrpricedata
Beispiel #3
0
    def get_raw_price(self, instrument_code):
        """
        Get instrument price

        :param instrument_code: instrument to get prices for
        :type instrument_code: str

        :returns: pd.DataFrame

        >>> data=csvFuturesData("sysdata.tests")
        >>> data.get_raw_price("EDOLLAR").tail(2)
        2015-12-11 17:08:14    97.9675
        2015-12-11 19:33:39    97.9875
        Name: price, dtype: float64
        >>> data["US10"].tail(2)
        2015-12-11 16:06:35    126.914062
        2015-12-11 17:24:06    126.945312
        Name: price, dtype: float64
        """

        # Read from .csv
        self.log.msg(
            "Loading csv data for %s" % instrument_code,
            instrument_code=instrument_code)
        filename = os.path.join(self._datapath, instrument_code + "_price.csv")
        instrpricedata = pd_readcsv(filename)
        instrpricedata.columns = ["price"]
        instrpricedata = instrpricedata.groupby(level=0).last()
        instrpricedata = pd.Series(instrpricedata.iloc[:, 0])
        return instrpricedata
Beispiel #4
0
    def get_raw_price(self, instrument_code):
        """
        Get instrument price

        :param instrument_code: instrument to get prices for
        :type instrument_code: str

        :returns: pd.DataFrame

        >>> data=csvFuturesData("sysdata.tests")
        >>> data.get_raw_price("EDOLLAR").tail(2)
        2015-12-11 17:08:14    97.9675
        2015-12-11 19:33:39    97.9875
        Name: price, dtype: float64
        >>> data["US10"].tail(2)
        2015-12-11 16:06:35    126.914062
        2015-12-11 17:24:06    126.945312
        Name: price, dtype: float64
        """

        # Read from .csv
        self.log.msg("Loading csv data for %s" % instrument_code,
                     instrument_code=instrument_code)
        filename = os.path.join(self._datapath, instrument_code + "_price.csv")
        instrpricedata = pd_readcsv(filename)
        instrpricedata.columns = ["price"]
        instrpricedata = instrpricedata.groupby(level=0).last()
        instrpricedata = pd.Series(instrpricedata.iloc[:, 0])
        return instrpricedata
Beispiel #5
0
    def get_instrument_raw_carry_data(self, instrument_code):
        """
        Returns a pd. dataframe with the 4 columns PRICE, CARRY, PRICE_CONTRACT, CARRY_CONTRACT

        These are specifically needed for futures trading

        :param instrument_code: instrument to get carry data for
        :type instrument_code: str

        :returns: pd.DataFrame

        >>> data=csvFuturesData("sysdata.tests")
        >>> data.get_instrument_raw_carry_data("US10").tail(4)
                                  PRICE  CARRY CARRY_CONTRACT PRICE_CONTRACT
        2015-12-10 23:00:00  126.328125    NaN         201606         201603
        2015-12-11 14:35:15  126.835938    NaN         201606         201603
        2015-12-11 16:06:35  126.914062    NaN         201606         201603
        2015-12-11 17:24:06  126.945312    NaN         201606         201603
        """

        self.log.msg("Loading csv carry data for %s" % instrument_code,
                     instrument_code=instrument_code)

        filename = os.path.join(self._datapath,
                                instrument_code + "_carrydata.csv")
        instrcarrydata = pd_readcsv(filename)

        instrcarrydata.CARRY_CONTRACT = instrcarrydata.CARRY_CONTRACT.apply(
            str_of_int)
        instrcarrydata.PRICE_CONTRACT = instrcarrydata.PRICE_CONTRACT.apply(
            str_of_int)

        return instrcarrydata
    def _get_prices_for_contract_object_no_checking(self,
                                                    futures_contract_object):
        """
        Read back the prices for a given contract object

        :param contract_object:  futuresContract
        :return: data
        """
        keyname = self._keyname_given_contract_object(futures_contract_object)
        filename = self._filename_given_key_name(keyname)

        date_format = self._input_date_format
        date_time_column = self._input_date_time_column
        input_column_mapping = self._input_column_mapping
        skiprows = self._input_skiprows
        skipfooter = self._input_skipfooter

        try:
            instrpricedata = pd_readcsv(
                filename,
                date_index_name=date_time_column,
                date_format=date_format,
                input_column_mapping=input_column_mapping,
                skiprows=skiprows,
                skipfooter=skipfooter)
        except OSError:
            self.log.warning("Can't find adjusted price file %s" % filename)
            return futuresContractPrices.create_empty()

        instrpricedata = instrpricedata.groupby(level=0).last()

        instrpricedata = futuresContractPrices(instrpricedata)

        return instrpricedata
Beispiel #7
0
def get_data(path):
    """
    returns: DataFrame or Series if 1 col
    """
    df = pd_readcsv(get_filename_for_package(path))
    if len(df.columns) == 1:
        return df[df.columns[0]]
    return df
Beispiel #8
0
    def _get_fx_prices_without_checking(self, code):
        filename = self._filename_given_fx_code(code)
        fx_data = pd_readcsv(filename)
        fx_data = pd.Series(fx_data.iloc[:, 0])

        fx_data = fxPrices(fx_data)

        return fx_data
    def _get_roll_calendar_without_checking(self, instrument_code):

        try:
            filename = self._filename_given_instrument_code(instrument_code)
            roll_calendar = pd_readcsv(filename, date_index_name=DATE_INDEX_NAME)
        except OSError:
            self.log.warning("Can't find roll calendar file %s" % filename)
            return rollCalendar.create_empty()

        return roll_calendar
    def _get_roll_calendar_without_checking(self, instrument_code):

        try:
            filename = self._filename_given_instrument_code(instrument_code)
            roll_calendar = pd_readcsv(filename, date_index_name=DATE_INDEX_NAME)
        except OSError:
            self.log.warning("Can't find roll calendar file %s" % filename)
            return rollCalendar.create_empty()

        return roll_calendar
Beispiel #11
0
    def _get_adjusted_prices_without_checking(self, instrument_code):
        filename = self._filename_given_instrument_code(instrument_code)

        instrpricedata = pd_readcsv(filename)
        instrpricedata.columns = ["price"]
        instrpricedata = instrpricedata.groupby(level=0).last()
        instrpricedata = pd.Series(instrpricedata.iloc[:, 0])

        instrpricedata = futuresAdjustedPrices(instrpricedata)

        return instrpricedata
    def _get_multiple_prices_without_checking(self, instrument_code):
        filename = self._filename_given_instrument_code(instrument_code)

        instr_all_price_data = pd_readcsv(filename, date_index_name=DATE_INDEX_NAME)

        instr_all_price_data.CARRY_CONTRACT = instr_all_price_data.CARRY_CONTRACT.apply(
            str_of_int)
        instr_all_price_data.PRICE_CONTRACT = instr_all_price_data.PRICE_CONTRACT.apply(
            str_of_int)
        instr_all_price_data.FORWARD_CONTRACT = instr_all_price_data.FORWARD_CONTRACT.apply(
            str_of_int)

        return futuresMultiplePrices(instr_all_price_data)
Beispiel #13
0
    def _get_fx_prices_without_checking(self, code):
        filename = self._filename_given_fx_code(code)
        try:
            fx_data = pd_readcsv(filename)
        except OSError:
            self.log.warning("Can't find currency price file %s" % filename)
            return fxPrices.create_empty()

        fx_data = pd.Series(fx_data.iloc[:, 0])

        fx_data = fxPrices(fx_data)

        return fx_data
    def _get_fx_prices_without_checking(self, code):
        filename = self._filename_given_fx_code(code)
        try:
            fx_data = pd_readcsv(filename)
        except OSError:
            self.log.warning("Can't find currency price file %s" % filename)
            return fxPrices.create_empty()

        fx_data = pd.Series(fx_data.iloc[:, 0])

        fx_data = fxPrices(fx_data)

        return fx_data
    def _read_instrument_prices(self, instrument_code: str) -> pd.DataFrame:
        filename = self._filename_given_instrument_code(instrument_code)

        try:
            instr_all_price_data = pd_readcsv(filename,
                                              date_index_name=DATE_INDEX_NAME)
        except OSError:
            self.log.warning(
                "Can't find multiple price file %s or error reading" %
                filename,
                instrument_code=instrument_code)
            return futuresMultiplePrices.create_empty()

        return instr_all_price_data
Beispiel #16
0
    def _get_spreads_without_checking(
            self, instrument_code: str) -> spreadsForInstrument:
        filename = self._filename_given_instrument_code(instrument_code)

        try:
            spreads_from_pd = pd_readcsv(filename,
                                         date_index_name=DATE_INDEX_NAME)
        except OSError:
            self.log.warn("Can't find adjusted price file %s" % filename)
            return spreadsForInstrument()

        spreads_as_series = pd.Series(spreads_from_pd[SPREAD_COLUMN_NAME])
        spreads = spreadsForInstrument(spreads_as_series)

        return spreads
    def _get_adjusted_prices_without_checking(self, instrument_code):
        filename = self._filename_given_instrument_code(instrument_code)

        try:
            instrpricedata = pd_readcsv(filename)
        except OSError:
            self.log.warning("Can't find adjusted price file %s" % filename)
            return futuresAdjustedPrices.create_empty()

        instrpricedata.columns = ["price"]
        instrpricedata = instrpricedata.groupby(level=0).last()
        instrpricedata = pd.Series(instrpricedata.iloc[:, 0])

        instrpricedata = futuresAdjustedPrices(instrpricedata)

        return instrpricedata
    def _get_adjusted_prices_without_checking(self, instrument_code):
        filename = self._filename_given_instrument_code(instrument_code)

        try:
            instrpricedata = pd_readcsv(filename)
        except OSError:
            self.log.warning("Can't find adjusted price file %s" % filename)
            return futuresAdjustedPrices.create_empty()

        instrpricedata.columns = ["price"]
        instrpricedata = instrpricedata.groupby(level=0).last()
        instrpricedata = pd.Series(instrpricedata.iloc[:, 0])

        instrpricedata = futuresAdjustedPrices(instrpricedata)

        return instrpricedata
Beispiel #19
0
    def _get_fx_data(self, currency1, currency2):
        """
        Get fx data

        :param currency1: numerator currency
        :type currency1: str

        :param currency2: denominator currency
        :type currency2: str

        :returns: Tx1 pd.DataFrame, or None if not available

        >>> data=csvFuturesData()
        >>> # datapath="tests/"
        >>> data._get_fx_data("EUR", "USD").tail(2)
                     EURUSD
        2015-12-09  1.09085
        2015-12-10  1.09641
        >>> data._get_fx_cross("EUR", "GBP").tail(2)
                          fx
        2015-12-09  0.724663
        2015-12-10  0.724463
        >>> data._get_fx_cross("USD", "GBP").tail(2)
                          fx
        2015-12-09  0.664311
        2015-12-10  0.660759
        >>> data._get_fx_cross( "GBP", "USD").tail(2)
                     GBPUSD
        2015-12-09  1.50532
        2015-12-10  1.51341
        """

        self.log.msg("Loading csv fx data", fx="%s%s" % (currency1, currency2))

        if currency1 == currency2:
            return self._get_default_series()

        filename = os.path.join(self._datapath,
                                "%s%sfx.csv" % (currency1, currency2))
        try:
            fxdata = pd_readcsv(filename)
        except:
            return None

        fxdata.columns = ["%s%s" % (currency1, currency2)]

        return fxdata
Beispiel #20
0
    def _get_fx_data(self, currency1, currency2):
        """
        Get fx data

        :param currency1: numerator currency
        :type currency1: str

        :param currency2: denominator currency
        :type currency2: str

        :returns: Tx1 pd.DataFrame, or None if not available

        >>> data=csvFuturesData()
        >>> # datapath="tests/"
        >>> data._get_fx_data("EUR", "USD").tail(2)
                     EURUSD
        2015-12-09  1.09085
        2015-12-10  1.09641
        >>> data._get_fx_cross("EUR", "GBP").tail(2)
                          fx
        2015-12-09  0.724663
        2015-12-10  0.724463
        >>> data._get_fx_cross("USD", "GBP").tail(2)
                          fx
        2015-12-09  0.664311
        2015-12-10  0.660759
        >>> data._get_fx_cross( "GBP", "USD").tail(2)
                     GBPUSD
        2015-12-09  1.50532
        2015-12-10  1.51341
        """

        self.log.msg("Loading csv fx data", fx="%s%s" % (currency1, currency2))

        if currency1 == currency2:
            return self._get_default_series()

        filename = os.path.join(
            self._datapath, "%s%sfx.csv" % (currency1, currency2))
        try:
            fxdata = pd_readcsv(filename)
        except:
            return None

        fxdata.columns = ["%s%s" % (currency1, currency2)]

        return fxdata
    def _get_multiple_prices_without_checking(self, instrument_code):
        filename = self._filename_given_instrument_code(instrument_code)

        try:
            instr_all_price_data = pd_readcsv(filename, date_index_name=DATE_INDEX_NAME)
        except OSError:
            self.log.warning("Can't find multiple price file %s" % filename)
            return futuresMultiplePrices.create_empty()

        instr_all_price_data.CARRY_CONTRACT = instr_all_price_data.CARRY_CONTRACT.apply(
            str_of_int)
        instr_all_price_data.PRICE_CONTRACT = instr_all_price_data.PRICE_CONTRACT.apply(
            str_of_int)
        instr_all_price_data.FORWARD_CONTRACT = instr_all_price_data.FORWARD_CONTRACT.apply(
            str_of_int)

        return futuresMultiplePrices(instr_all_price_data)
    def _get_prices_for_contract_object_no_checking(
        self, futures_contract_object: futuresContract
    ) -> futuresContractPrices:
        """
        Read back the prices for a given contract object

        :param contract_object:  futuresContract
        :return: data
        """
        keyname = self._keyname_given_contract_object(futures_contract_object)
        filename = self._filename_given_key_name(keyname)
        config = self.config

        date_format = config.input_date_format
        date_time_column = config.input_date_index_name
        input_column_mapping = config.input_column_mapping
        skiprows = config.input_skiprows
        skipfooter = config.input_skipfooter
        multiplier = config.apply_multiplier
        inverse = config.apply_inverse

        try:
            instrpricedata = pd_readcsv(
                filename,
                date_index_name=date_time_column,
                date_format=date_format,
                input_column_mapping=input_column_mapping,
                skiprows=skiprows,
                skipfooter=skipfooter,
            )
        except OSError:
            log = futures_contract_object.log(self.log)
            log.warn("Can't find adjusted price file %s" % filename)
            return futuresContractPrices.create_empty()

        instrpricedata = instrpricedata.groupby(level=0).last()
        for col_name in ["OPEN", "HIGH", "LOW", "FINAL"]:
            column_series = instrpricedata[col_name]
            if inverse:
                column_series = 1 / column_series
            column_series *= multiplier
            instrpricedata[col_name] = column_series.round(2)

        instrpricedata = futuresContractPrices(instrpricedata)

        return instrpricedata
Beispiel #23
0
    def _get_fx_data(self, currency1, currency2):
        """
        Get fx data

        :param currency1: numerator currency
        :type currency1: str

        :param currency2: denominator currency
        :type currency2: str

        :returns: Tx1 pd.DataFrame, or None if not available

        >>> data=csvFuturesData("sysdata.tests")
        >>> data._get_fx_data("EUR", "USD").tail(2)
        2015-12-09    1.09085
        2015-12-10    1.09641
        Name: FX, dtype: float64
        >>> data._get_fx_cross("EUR", "GBP").tail(2)
        2015-12-09    0.724663
        2015-12-10    0.724463
        Freq: B, Name: FX, dtype: float64
        2015-12-09    0.664311
        2015-12-10    0.660759
        dtype: float64
        >>> data._get_fx_cross( "GBP", "USD").tail(2)
        2015-12-09    1.50532
        2015-12-10    1.51341
        Name: FX, dtype: float64
        """

        self.log.msg("Loading csv fx data", fx="%s%s" % (currency1, currency2))

        if currency1 == currency2:
            return self._get_default_series()

        filename = os.path.join(self._datapath,
                                "%s%sfx.csv" % (currency1, currency2))
        try:
            fxdata = pd_readcsv(filename)
        except:
            return None

        fxdata = pd.Series(fxdata.iloc[:, 0])

        return fxdata
Beispiel #24
0
    def _get_fx_data(self, currency1, currency2):
        """
        Get fx data

        :param currency1: numerator currency
        :type currency1: str

        :param currency2: denominator currency
        :type currency2: str

        :returns: Tx1 pd.DataFrame, or None if not available

        >>> data=csvFuturesData("sysdata.tests")
        >>> data._get_fx_data("EUR", "USD").tail(2)
        2015-12-09    1.09085
        2015-12-10    1.09641
        Name: FX, dtype: float64
        >>> data._get_fx_cross("EUR", "GBP").tail(2)
        2015-12-09    0.724663
        2015-12-10    0.724463
        Freq: B, Name: FX, dtype: float64
        2015-12-09    0.664311
        2015-12-10    0.660759
        dtype: float64
        >>> data._get_fx_cross( "GBP", "USD").tail(2)
        2015-12-09    1.50532
        2015-12-10    1.51341
        Name: FX, dtype: float64
        """

        self.log.msg("Loading csv fx data", fx="%s%s" % (currency1, currency2))

        if currency1 == currency2:
            return self._get_default_series()

        filename = os.path.join(self._datapath, "%s%sfx.csv" % (currency1,
                                                                currency2))
        try:
            fxdata = pd_readcsv(filename)
        except:
            return None

        fxdata = pd.Series(fxdata.iloc[:, 0])

        return fxdata
Beispiel #25
0
    def _get_fx_prices_without_checking(self, code):
        filename = self._filename_given_fx_code(code)
        price_column = self._price_column
        date_column = self._date_column
        date_format = self._date_format

        try:
            fx_data = pd_readcsv(filename,
                                 date_format=date_format,
                                 date_index_name=date_column)
        except OSError:
            self.log.warn("Can't find currency price file %s" % filename)
            return fxPrices.create_empty()

        fx_data = pd.Series(fx_data[price_column])

        fx_data = fxPrices(fx_data.sort_index())

        return fx_data
    def _get_roll_calendar_without_checking(self, instrument_code):

        filename = self._filename_given_instrument_code(instrument_code)
        return pd_readcsv(filename, date_index_name=DATE_INDEX_NAME)