Beispiel #1
0
def func_resample(args):
    output_path = args.outputPath
    input_path = args.inputPath
    resample_time = "W"

    print("Resample input data to:")
    print("Input path : {}".format(input_path))
    print("Output path : {}".format(output_path))
    print("Resample to : {}".format(resample_time))

    files = [input_path
             ] if os.path.isfile(input_path) else os.listdir(input_path)

    with tqdm(total=len(files)) as pbar:
        for file in files:
            full_path = os.path.join(input_path, file)
            df = Loader.get_historical_data(full_path,
                                            Source.LocalData,
                                            time_frame=TimeFrame.Daily)
            df = df.set_index("date")
            result_df = resample_data(df, resample_time)
            full_path = os.path.join(output_path, file)
            result_df.to_csv(full_path, index=True, header=True)
            pbar.set_description("done %s" % file)
            pbar.update(1)
Beispiel #2
0
def test_that_local_data_source_return_right_schema_dataframe():
    base_dir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
    test_file_path = os.path.join(base_dir, "sample_data.csv")
    df = Loader.get_historical_data(
        test_file_path, Source.LocalData, base_date, TimeFrame.Weekly
    )

    assert (df.columns.sort_values() == data_source_required_columns).all()
        def get_hyperwave_from_source(source, symbol, ticker):
            pbar.set_description("Processing %s" % symbol)
            df_raw_data = Loader.get_historical_data(
                symbol, source, time_frame=TimeFrame.Weekly)

            (df_hull_hyperwave, hw_phases_temp,
             hyperwave) = hw.get_hyperwave(df_raw_data)
            hyperwave["ticker"] = ticker
            pbar.update(1)
            return hyperwave
        def get_hyperwave_from_source(source, symbol, ticker):
            pbar.set_description("Processing %s" % symbol)
            df_raw_data = Loader.get_historical_data(
                symbol, source, time_frame=TimeFrame.Weekly)

            result = (consensio.get_consonsio(df_raw_data)[[
                "date", "consensio"
            ]].rename(columns={
                "consensio": ticker
            }).reset_index(drop=True).set_index("date"))
            pbar.update(1)
            return result
Beispiel #5
0
def task_fetch_symbol(source: Source, symbol: str, output_path: str,
                      timeframe: TimeFrame):
    try:
        data = Loader.get_historical_data(symbol, source, time_frame=timeframe)
        data.to_csv(
            output_path,
            columns=["date", "close", "high", "low", "open", "volume"],
            index=False,
            header=True,
        )
    except Exception as e:
        return (e, symbol)

    return (None, symbol)
Beispiel #6
0
def test_that_investopedia_source_return_right_schema_dataframe(monkeypatch):
    def mock_pandas_read_html(url_symbol, header, parse_dates):
        data = [
            {
                "Date": "Oct 01, 2018",
                "Open": 227.95,
                "High": 229.42,
                "Low": 226.35,
                "Adj. Close": 227.26,
                "Volume": 23600802,
            }
        ]
        return [pd.DataFrame(data)]

    monkeypatch.setattr(pd, "read_html", mock_pandas_read_html)
    df = Loader.get_historical_data("AAPL", Source.Investopedia, base_date)

    assert (df.columns.sort_values() == data_source_required_columns).all()
def test_consensio(source_path: str, result_file_path: str, comment: str):
    root_test, result_file_name = os.path.split(result_file_path)
    df_source = Loader.get_historical_data(source_path,
                                           Source.LocalData,
                                           time_frame=TimeFrame.Weekly)
    df_consensio = get_consonsio(df_source,
                                 "close",
                                 low_ma=3,
                                 mid_ma=7,
                                 high_ma=30)

    if not os.path.exists(os.path.join(root_test, "results")):
        os.mkdir(os.path.join(root_test, "results"))

    result_output_path = os.path.join(root_test, "results", result_file_name)
    df_consensio.sort_values("date", ascending=True).to_csv(result_output_path,
                                                            header=True,
                                                            index=False)

    if not os.path.exists(result_file_path):
        print("Result file doesnt exist")
        return

    df_result = pd.read_csv(result_file_path,
                            header=0,
                            parse_dates=["date"],
                            infer_datetime_format=True).set_index("date")
    df_result["date"] = df_result.index

    df_result = (df_result.reset_index(drop=True).sort_values(
        "date", ascending=True).reset_index())
    df_consensio = (df_consensio.reset_index(drop=True).sort_values(
        "date", ascending=True).reset_index())
    pd.testing.assert_frame_equal(
        df_result.sort_values("date", ascending=True)[["date", "consensio"]],
        df_consensio.sort_values("date", ascending=True)[["date",
                                                          "consensio"]],
        check_less_precise=True,
        check_like=True,
    )
Beispiel #8
0
def func_check(args):
    hw = Hyperwave.get_standard_hyperwave()
    df_raw_data = Loader.get_historical_data(
        args.symbol, args.source, time_frame=TimeFrame.Weekly
    )

    (df_hull_hyperwave, hw_phases_temp, hyperwave) = hw.get_hyperwave(df_raw_data)

    if args.outputType == "display":
        print(df_hull_hyperwave)
        print(hw_phases_temp)
        print(hyperwave)
    elif args.outputType == "file":
        output_path = args.outputPath
        if not os.path.exists(output_path):
            print(
                "The path {} doesn't exist. Please select a valid outputPath".format(
                    output_path
                )
            )

        file_name = "{}_{}_{:%Y%m%d}".format(
            args.source, args.symbol, datetime.datetime.now()
        )
        raw_data_file_name = "{}.csv".format(file_name)
        hull_file_name = "{}.hull.csv".format(file_name)
        hw_file_name = "{}.hw.csv".format(file_name)

        df_raw_data.to_csv(
            os.path.join(output_path, raw_data_file_name),
            columns=["date", "close", "high", "low", "open"],
            index=False,
            header=True,
        )
        df_hull_hyperwave.to_csv(
            os.path.join(output_path, hull_file_name), header=True, index=False
        )
        hyperwave.to_csv(
            os.path.join(output_path, hw_file_name), header=True, index=False
        )
Beispiel #9
0
def test_that_cryptocompare_source_return_right_schema_dataframeHyperwave_Path_Finder():
    df = Loader.get_historical_data(
        "BTC-USD", Source.CryptoCompare, base_date, TimeFrame.Weekly
    )
    assert (df.columns.sort_values() == data_source_required_columns).all()
Beispiel #10
0
def test_that_source_available_is_equal_to_5():
    sources = Loader.get_available_sources()
    assert len(sources) == 5
Beispiel #11
0
def test_tiingo_can_load_data():
    df = Loader.get_historical_data("AAPL", Source.Tiingo, base_date)
    assert (df.columns.sort_values() == data_source_required_columns).all()
Beispiel #12
0
def test_stooq_fail_load_apple():
    with pytest.raises(NameError):
        df = Loader.get_historical_data("AAPL", Source.Stooq, base_date)
Beispiel #13
0
def test_that_we_can_load_from_stooq():
    df = Loader.get_historical_data("BTCUSD", Source.Stooq, base_date)
    assert (df.columns.sort_values() == data_source_required_columns).all()
Beispiel #14
0
def test_that_local_data_source_raise_filenotfound_error():
    with pytest.raises(FileNotFoundError):
        df = Loader.get_historical_data(
            "unknow_file", Source.LocalData, base_date, TimeFrame.Weekly
        )