def check_saved_roll_calendar(instrument_code,
                              input_datapath=arg_not_supplied,
                              input_prices=arg_not_supplied):

    if input_datapath is None:
        print(
            "This will check the roll calendar in the default directory : are you are that's what you want to do?"
        )

    csv_roll_calendars = csvRollCalendarData(input_datapath)

    roll_calendar = csv_roll_calendars.get_roll_calendar(instrument_code)

    if input_prices is arg_not_supplied:
        prices = arcticFuturesContractPriceData()
    else:
        prices = input_prices

    dict_of_all_futures_contract_prices = prices.get_all_prices_for_instrument(
        instrument_code)
    dict_of_futures_contract_prices = dict_of_all_futures_contract_prices.final_prices(
    )

    print(roll_calendar)

    # checks - this might fail
    roll_calendar.check_if_date_index_monotonic()

    # this should never fail
    roll_calendar.check_dates_are_valid_for_prices(
        dict_of_futures_contract_prices)

    return roll_calendar
Exemplo n.º 2
0
def init_arctic_with_csv_futures_contract_prices_for_code(
    instrument_code: str, datapath: str, csv_config=arg_not_supplied
):
    print(instrument_code)
    csv_prices = csvFuturesContractPriceData(datapath, config=csv_config)
    arctic_prices = arcticFuturesContractPriceData()

    print("Getting .csv prices may take some time")
    csv_price_dict = csv_prices.get_all_prices_for_instrument(instrument_code)

    print("Have .csv prices for the following contracts:")
    print(str(csv_price_dict.keys()))

    for contract_date_str, prices_for_contract in csv_price_dict.items():
        print("Processing %s" % contract_date_str)
        print(".csv prices are \n %s" % str(prices_for_contract))
        contract = futuresContract(instrument_code, contract_date_str)
        print("Contract object is %s" % str(contract))
        print("Writing to arctic")
        arctic_prices.write_prices_for_contract_object(
            contract, prices_for_contract, ignore_duplication=True
        )
        print("Reading back prices from arctic to check")
        written_prices = arctic_prices.get_prices_for_contract_object(contract)
        print("Read back prices are \n %s" % str(written_prices))
def _get_data_inputs(csv_roll_data_path, csv_multiple_data_path):
    csv_roll_calendars = csvRollCalendarData(csv_roll_data_path)
    arctic_individual_futures_prices = arcticFuturesContractPriceData()
    arctic_multiple_prices = arcticFuturesMultiplePricesData()
    csv_multiple_prices = csvFuturesMultiplePricesData(csv_multiple_data_path)

    return csv_roll_calendars, arctic_individual_futures_prices, arctic_multiple_prices, csv_multiple_prices
Exemplo n.º 4
0
    def _create_comparison(self, instrument_code: str, price_date_str: str,
                           forward_date_str: str):
        """
        :param instrument_code: symbol for instrument.
        :param price_date_str: Contract ID for the priced contract - for example; "20220500"
        :param forward_date_str: Contract ID for the forward contract - for example; "20220500"

        :return: a dataframe where prices from the priced and forward contracts are merged together.
                  columns separated with suffixes _price and _forward
        """
        price_contract = futuresContract(instrument_object=instrument_code,
                                         contract_date_object=price_date_str)
        forward_contract = futuresContract(
            instrument_object=instrument_code,
            contract_date_object=forward_date_str)

        contract_prices = arcticFuturesContractPriceData()
        price_prices = contract_prices.get_prices_for_contract_object(
            price_contract)
        forward_prices = contract_prices.get_prices_for_contract_object(
            forward_contract)

        merged_contracts = pd.merge(price_prices,
                                    forward_prices,
                                    how='outer',
                                    left_index=True,
                                    right_index=True,
                                    suffixes=('_price', '_forward'))

        return merged_contracts
def adjust_roll_calendar(instrument_code, roll_calendar):
    arctic_prices_per_contract = arcticFuturesContractPriceData()
    print("Getting prices to adjust roll calendar")
    dict_of_prices = arctic_prices_per_contract.get_all_prices_for_instrument(instrument_code)
    dict_of_futures_contract_prices = dict_of_prices.final_prices()
    roll_calendar = adjust_to_price_series(roll_calendar, dict_of_futures_contract_prices)

    return roll_calendar
Exemplo n.º 6
0
def build_and_write_roll_calendar(instrument_code,
                                  output_datapath=arg_not_supplied,
                                  check_before_writing=True):

    if output_datapath is arg_not_supplied:
        print(
            "*** WARNING *** This will overwrite the provided roll calendar. Might be better to use a temporary directory!"
        )
    else:
        print("Writing to %s" % output_datapath)

    arctic_prices = arcticFuturesContractPriceData()
    mongo_rollparameters = mongoRollParametersData()
    csv_roll_calendars = csvRollCalendarData(output_datapath)

    dict_of_all_futures_contract_prices = arctic_prices.get_all_prices_for_instrument(
        instrument_code)
    dict_of_futures_contract_prices = dict_of_all_futures_contract_prices.final_prices(
    )

    roll_parameters_object = mongo_rollparameters.get_roll_parameters(
        instrument_code)

    # might take a few seconds
    print("Prepping roll calendar... might take a few seconds")
    roll_calendar = rollCalendar.create_from_prices(
        dict_of_futures_contract_prices, roll_parameters_object)

    # checks - this might fail
    roll_calendar.check_if_date_index_monotonic()

    # this should never fail
    roll_calendar.check_dates_are_valid_for_prices(
        dict_of_futures_contract_prices)

    # Write to csv
    # Will not work if an existing calendar exists

    if check_before_writing:
        check_happy_to_write = input(
            "Are you ok to write this csv to path %s? [might be worth writing and hacking manually] (yes/other)?"
            % csv_roll_calendars.datapath)
    else:
        check_happy_to_write = "yes"

    if check_happy_to_write == "yes":
        print("Adding roll calendar")
        csv_roll_calendars.add_roll_calendar(instrument_code,
                                             roll_calendar,
                                             ignore_duplication=True)
    else:
        print("Not writing")

    return roll_calendar
Exemplo n.º 7
0
def init_arctic_with_csv_futures_contract_prices_for_code(
        instrument_code: str, datapath: str, csv_config=arg_not_supplied):
    print(instrument_code)
    csv_prices = csvFuturesContractPriceData(datapath, config=csv_config)
    arctic_prices = arcticFuturesContractPriceData()

    print("Getting .csv prices may take some time")
    csv_price_dict = csv_prices.get_all_prices_for_instrument(instrument_code)

    for contract_date_str, prices_for_contract in csv_price_dict.items():
        print(contract_date_str)
        contract = futuresContract(instrument_code, contract_date_str)
        arctic_prices.write_prices_for_contract_object(contract,
                                                       prices_for_contract,
                                                       ignore_duplication=True)
def get_and_write_prices_for_contract_list_from_quandl_to_arctic(list_of_contracts):
    quandl_prices_data = quandlFuturesContractPriceData()
    arctic_prices_data = arcticFuturesContractPriceData()

    for contract_object in list_of_contracts:
        print("Processing %s" % contract_object.ident())
        quandl_price = quandl_prices_data.get_prices_for_contract_object(contract_object)

        if quandl_price.empty:
            print("Problem reading price data this contract - skipping")
        else:
            print("Read ok, trying to write to arctic")
            try:
                arctic_prices_data.write_prices_for_contract_object(contract_object, quandl_price)
            except:
                raise Exception("Some kind of issue with arctic - stopping so you can fix it")
def build_and_write_roll_calendar(
    instrument_code, output_datapath=None, check_before_writing=True
):

    artic_prices = arcticFuturesContractPriceData()
    mongo_rollparameters = mongoRollParametersData()
    csv_roll_calendars = csvRollCalendarData(output_datapath)

    dict_of_all_futures_contract_prices = artic_prices.get_all_prices_for_instrument(
        instrument_code)
    dict_of_futures_contract_prices = dict_of_all_futures_contract_prices.final_prices()

    roll_parameters_object = mongo_rollparameters.get_roll_parameters(
        instrument_code)

    # might take a few seconds
    print("Prepping roll calendar... might take a few seconds")
    roll_calendar = rollCalendar.create_from_prices(
        dict_of_futures_contract_prices, roll_parameters_object
    )

    # checks - this might fail
    check_monotonic = roll_calendar.check_if_date_index_monotonic()

    # this should never fail
    check_valid = roll_calendar.check_dates_are_valid_for_prices(
        dict_of_futures_contract_prices
    )

    # Write to csv
    # Will not work if an existing calendar exists

    if check_before_writing:
        check_happy_to_write = input(
            "Are you ok to write this csv? [might be worth writing and hacking manually] (yes/other)?"
        )
    else:
        check_happy_to_write = "yes"

    if check_happy_to_write == "yes":
        csv_roll_calendars.add_roll_calendar(
            roll_calendar, instrument_code, ignore_duplication=True
        )
    else:
        print("Not writing")

    return roll_calendar
Exemplo n.º 10
0
def get_and_write_prices_for_contract_list_from_quandl_to_arctic(
        list_of_contracts):
    quandl_prices_data = quandlFuturesContractPriceData()
    arctic_prices_data = arcticFuturesContractPriceData()

    for contract_object in list_of_contracts:
        print("Processing %s" % contract_object.ident())
        quandl_price = quandl_prices_data.get_prices_for_contract_object(
            contract_object)

        if quandl_price.empty:
            print("Problem reading price data this contract - skipping")
        else:
            print("Read ok, trying to write to arctic")
            try:
                arctic_prices_data.write_prices_for_contract_object(
                    contract_object, quandl_price)
            except:
                raise Exception(
                    "Some kind of issue with arctic - stopping so you can fix it"
                )
Exemplo n.º 11
0
matplotlib.use("TkAgg")

from sysdata.arctic.arctic_futures_per_contract_prices import arcticFuturesContractPriceData
from sysdata.csv.csv_roll_calendars import csvRollCalendarData
from sysdata.csv.csv_multiple_prices import csvFuturesMultiplePricesData
from sysdata.arctic.arctic_multiple_prices import arcticFuturesMultiplePricesData

from sysdata.futures.multiple_prices import futuresMultiplePrices

# could get these from stdin
ADD_TO_ARCTIC = True
ADD_TO_CSV = False

if __name__ == '__main__':
    csv_roll_calendars = csvRollCalendarData()
    arctic_individual_futures_prices = arcticFuturesContractPriceData()
    arctic_multiple_prices = arcticFuturesMultiplePricesData()
    csv_multiple_prices = csvFuturesMultiplePricesData()

    instrument_list = arctic_individual_futures_prices.get_instruments_with_price_data(
    )
    instrument_list = ["LIVECOW"]
    for instrument_code in instrument_list:
        print(instrument_code)
        roll_calendar = csv_roll_calendars.get_roll_calendar(instrument_code)
        dict_of_futures_contract_prices = arctic_individual_futures_prices.get_all_prices_for_instrument(
            instrument_code)
        dict_of_futures_contract_closing_prices = dict_of_futures_contract_prices.final_prices(
        )

        multiple_prices = futuresMultiplePrices.create_from_raw_data(
Exemplo n.º 12
0
from sysdata.arctic.arctic_futures_per_contract_prices import (
    arcticFuturesContractPriceData, )
from sysdata.arctic.arctic_multiple_prices import arcticFuturesMultiplePricesData
from sysdata.arctic.arctic_adjusted_prices import arcticFuturesAdjustedPricesData

from sysdata.csv.csv_roll_calendars import csvRollCalendarData
from sysdata.csv.csv_multiple_prices import csvFuturesMultiplePricesData
from sysdata.csv.csv_adjusted_prices import csvFuturesAdjustedPricesData

from sysobjects.contracts import futuresContract

db_data_individual_prices = arcticFuturesContractPriceData()
db_data_multiple_prices = arcticFuturesMultiplePricesData()
db_data_adjusted_prices = arcticFuturesAdjustedPricesData()

csv_roll_calendar = csvRollCalendarData()
csv_multiple = csvFuturesMultiplePricesData()
csv_adjusted = csvFuturesAdjustedPricesData()


def clone_data_for_instrument(instrument_from: str,
                              instrument_to: str,
                              write_to_csv: bool = False):

    clone_prices_per_contract(instrument_from, instrument_to)
    if write_to_csv:
        clone_roll_calendar(instrument_from, instrument_to)

    clone_multiple_prices(instrument_from,
                          instrument_to,
                          write_to_csv=write_to_csv)
    def test_futures_prices(self):
        data = arcticFuturesContractPriceData(database_name="test")
        data._arctic.store.delete_library(data._arctic.library_name)

        # we need some sham data
        dummy_series = [5.0] * 5
        dummy_series2 = [2.0] * 5

        price_data = pd.DataFrame(
            dict(
                OPEN=dummy_series,
                CLOSE=dummy_series,
                HIGH=dummy_series,
                LOW=dummy_series,
                SETTLE=dummy_series,
            ),
            index=pd.date_range(
                pd.datetime(
                    2000,
                    1,
                    1),
                pd.datetime(
                    2000,
                    1,
                    5)),
        )

        price_data2 = pd.DataFrame(
            dict(
                OPEN=dummy_series2,
                CLOSE=dummy_series2,
                HIGH=dummy_series2,
                LOW=dummy_series2,
                SETTLE=dummy_series2,
            ),
            index=pd.date_range(
                pd.datetime(
                    2000,
                    1,
                    1),
                pd.datetime(
                    2000,
                    1,
                    5)),
        )

        list_of_contracts = data.contracts_with_price_data_for_instrument_code(
            "thing one"
        )
        self.assertEqual(list_of_contracts, [])

        empty_thing = data.get_prices_for_instrument_code_and_contract_date(
            "thing one", "201801"
        )
        self.assertEqual(len(empty_thing.index), 0)
        self.assertEqual(
            data.has_data_for_instrument_code_and_contract_date(
                "thing one", "201801"), False, )

        data.write_prices_for_contract_object(
            futuresContract.simple("thing one", "201801"), price_data
        )
        data.write_prices_for_contract_object(
            futuresContract.simple("thing one", "20180215"), price_data
        )
        data.write_prices_for_contract_object(
            futuresContract.simple("thing two", "201803"), price_data2
        )

        list_of_contracts = data.contracts_with_price_data_for_instrument_code(
            "thing one"
        )
        self.assertEqual(list_of_contracts, ["20180100", "20180215"])
        list_of_contracts = data.contracts_with_price_data_for_instrument_code(
            "thing two"
        )
        self.assertEqual(list_of_contracts, ["20180300"])

        self.assertEqual(
            data.has_data_for_instrument_code_and_contract_date(
                "thing one", "201801"), True, )
        self.assertEqual(
            data.has_data_for_instrument_code_and_contract_date(
                "thing one", "20180215"
            ),
            True,
        )
        self.assertEqual(
            data.has_data_for_instrument_code_and_contract_date(
                "thing one", "201807"), False, )
        self.assertEqual(
            data.has_data_for_contract(
                futuresContract.simple(
                    "thing two", "201803")), True, )

        getback1 = data.get_prices_for_instrument_code_and_contract_date(
            "thing one", "201801"
        )
        getback2 = data.get_prices_for_contract_object(
            futuresContract.simple("thing two", "201803")
        )

        self.assertEqual(getback1.OPEN.values[0], 5.0)
        self.assertEqual(getback2.OPEN.values[0], 2.0)

        data.delete_prices_for_contract_object(
            futuresContract.simple("thing one", "201807"), areyousure=True
        )

        data.delete_prices_for_contract_object(
            futuresContract.simple("thing one", "201801"), areyousure=True
        )
        list_of_contracts = data.contracts_with_price_data_for_instrument_code(
            "thing one"
        )
        self.assertEqual(list_of_contracts, ["20180215"])
        list_of_contracts = data.contracts_with_price_data_for_instrument_code(
            "thing two"
        )
        self.assertEqual(list_of_contracts, ["20180300"])

        data.delete_prices_for_contract_object(
            futuresContract.simple("thing one", "20180215"), areyousure=True
        )
        list_of_contracts = data.contracts_with_price_data_for_instrument_code(
            "thing one"
        )
        self.assertEqual(list_of_contracts, [])

        data.delete_prices_for_contract_object(
            futuresContract.simple("thing two", "201803"), areyousure=True
        )
        list_of_contracts = data.contracts_with_price_data_for_instrument_code(
            "thing two"
        )
        self.assertEqual(list_of_contracts, [])
from sysdata.csv.csv_roll_calendars import csvRollCalendarData

import sys

"""
Generate a 'best guess' roll calendar based on some price data for individual contracts

"""

if __name__ == '__main__':

    #instrument_code = sys.argv[1]
    instrument_code="EDOLLAR"

    ##
    artic_prices = arcticFuturesContractPriceData()
    mongo_rollparameters = mongoRollParametersData()
    csv_roll_calendars = csvRollCalendarData()

    dict_of_futures_contract_prices =artic_prices.get_all_prices_for_instrument(instrument_code)
    dict_of_futures_contract_prices = dict_of_futures_contract_prices.settlement_prices()

    roll_parameters = mongo_rollparameters.get_roll_parameters(instrument_code)

    ## might take a few seconds
    print("Prepping roll calendar... might take a few seconds")
    roll_calendar = rollCalendar.create_from_prices(dict_of_futures_contract_prices, roll_parameters)

    ## checks - this might fail
    check_monotonic = roll_calendar.check_if_date_index_monotonic()
"""

from sysdata.arctic.arctic_futures_per_contract_prices import arcticFuturesContractPriceData
from sysdata.csv.csv_roll_calendars import csvRollCalendarData
from sysdata.csv.csv_multiple_prices import csvFuturesMultiplePricesData
from sysdata.arctic.arctic_multiple_prices import arcticFuturesMultiplePricesData

from sysdata.futures.multiple_prices import futuresMultiplePrices

# could get these from stdin
ADD_TO_ARCTIC = True
ADD_TO_CSV = True

if __name__ == '__main__':
    csv_roll_calendars = csvRollCalendarData()
    artic_individual_futures_prices = arcticFuturesContractPriceData()
    arctic_multiple_prices = arcticFuturesMultiplePricesData()
    csv_multiple_prices = csvFuturesMultiplePricesData()

    instrument_list = artic_individual_futures_prices.get_instruments_with_price_data()

    for instrument_code in instrument_list:
        print(instrument_code)
        roll_calendar = csv_roll_calendars.get_roll_calendar(instrument_code)
        dict_of_futures_contract_prices = artic_individual_futures_prices.get_all_prices_for_instrument(instrument_code)
        dict_of_futures_contract_settlement_prices = dict_of_futures_contract_prices.settlement_prices()

        multiple_prices = futuresMultiplePrices.create_from_raw_data(roll_calendar, dict_of_futures_contract_settlement_prices)

        print(multiple_prices)
Exemplo n.º 16
0
from sysdata.futures.roll_calendars import rollCalendar
from sysdata.csv.csv_roll_calendars import csvRollCalendarData

import sys
"""
Generate a 'best guess' roll calendar based on some price data for individual contracts

"""

if __name__ == '__main__':

    #instrument_code = sys.argv[1]
    instrument_code = "EDOLLAR"

    ##
    artic_prices = arcticFuturesContractPriceData()
    mongo_rollparameters = mongoRollParametersData()
    csv_roll_calendars = csvRollCalendarData()

    dict_of_futures_contract_prices = artic_prices.get_all_prices_for_instrument(
        instrument_code)
    dict_of_futures_contract_prices = dict_of_futures_contract_prices.settlement_prices(
    )

    roll_parameters = mongo_rollparameters.get_roll_parameters(instrument_code)

    ## might take a few seconds
    print("Prepping roll calendar... might take a few seconds")
    roll_calendar = rollCalendar.create_from_prices(
        dict_of_futures_contract_prices, roll_parameters)