Ejemplo n.º 1
0
def pinbar_detector(data: pd.DataFrame,
                    labelBuy: str = "PIN_Buy",
                    labelSell: str = "PIN_Sell",
                    labelOpen: str = "<OPEN>",
                    labelHigh: str = "<HIGH>",
                    labelLow: str = "<LOW>",
                    labelClose: str = "<CLOSE>") -> pd.DataFrame:
    """
    Pinbar detector takes dataframe with price data (OHLC) and produces new columns 
    with information if given record is a pinbar signal for buy or sell.
    

    Args:
        data (pd.DataFrame): input dataframe with price data
        labelBuy (str, optional): label used for pin buy signal column. Defaults to "PIN_Buy".
        labelSell (str, optional): label used for pin sell signal column. Defaults to "PIN_Sell".
        labelOpen (str, optional): Open price label in input dataframe. Defaults to "<OPEN>".
        labelHigh (str, optional): High price label in input dataframe. Defaults to "<HIGH>".
        labelLow (str, optional): Low price label in input dataframe. Defaults to "<LOW>".
        labelClose (str, optional): Close pirce label in input dataframe. Defaults to "<CLOSE>".

    Returns:
        pd.DataFrame: Input dataframe with added columns for detected signals.
    """
    data[labelBuy] = np.zeros((len(data.index), 1), dtype=int)
    data[labelSell] = np.zeros((len(data.index), 1), dtype=int)
    data, tempColumns = calculate_body_and_tails(data, labelOpen, labelHigh,
                                                 labelLow, labelClose)
    if (tempColumns):
        data.loc[((data["tailDown"] > 3 * data["body"].abs()) &
                  (data["tailUp"] < 0.5 * data["tailDown"])), labelBuy] = 1
        data.loc[((data["tailUp"] > 3 * data["body"].abs()) &
                  (data["tailDown"] < 0.5 * data["tailUp"])), labelSell] = 1
    data = data.drop(tempColumns, axis=1, errors='ignore')
    return data
def test_if_tailUp_is_calculated(dataframe_with_test_data):
    dataframe_out = calculate_body_and_tails(dataframe_with_test_data)[0]
    dataframe_out.loc[
        dataframe_out["body"] > 0,
        "tailUpGT"] = dataframe_out["<HIGH>"] - dataframe_out["<CLOSE>"]
    dataframe_out.loc[
        dataframe_out["body"] <= 0,
        "tailUpGT"] = dataframe_out["<HIGH>"] - dataframe_out["<OPEN>"]
    assert dataframe_out["tailUp"].equals(dataframe_out["tailUpGT"])
Ejemplo n.º 3
0
def fakey_detector(data: pd.DataFrame,
                   labelBuy: str = "Fakey_Buy",
                   labelSell: str = "Fakey_Sell",
                   labelOpen: str = "<OPEN>",
                   labelHigh: str = "<HIGH>",
                   labelLow: str = "<LOW>",
                   labelClose: str = "<CLOSE>") -> pd.DataFrame:
    """
    Fakey detector takes dataframe with price data (OHLC) and produces new columns 
    with information if given record is a fakey signal for buy or sell.

    Args:
        data (pd.DataFrame): input dataframe with price data
        labelBuy (str, optional): label used for fakey buy signal column. Defaults to "Fakey_Buy".
        labelSell (str, optional): label used for fakey sell signal column. Defaults to "Fakey_Sell".
        labelOpen (str, optional): Open price label in input dataframe. Defaults to "<OPEN>".
        labelHigh (str, optional): High price label in input dataframe. Defaults to "<HIGH>".
        labelLow (str, optional): Low price label in input dataframe. Defaults to "<LOW>".
        labelClose (str, optional): Close pirce label in input dataframe. Defaults to "<CLOSE>".

    Returns:
        pd.DataFrame: Input dataframe with added columns for detected signals.
    """
    data[labelBuy] = np.zeros((len(data.index), 1), dtype=int)
    data[labelSell] = np.zeros((len(data.index), 1), dtype=int)
    data, tempColumns = calculate_body_and_tails(data, labelOpen, labelHigh,
                                                 labelLow, labelClose)
    if (tempColumns):
        data["body-1"] = data["body"].shift(1, fill_value=0)
        data["close-1"] = data[labelClose].shift(1, fill_value=0)
        data["open-1"] = data[labelOpen].shift(1, fill_value=0)
        tempColumns += ["body-1", "close-1", "open-1"]
        data.loc[(
            (data["body-1"] > 0)
            & (data[labelHigh] > data["close-1"] + data["body-1"].abs() * 0.25)
            & (data[labelOpen] < data["close-1"])
            & (data[labelClose] < data["close-1"])
            & (data[labelOpen] > data["open-1"])
            & (data[labelClose] > data["open-1"])), labelSell] = 1
        data.loc[(
            (data["body-1"] < 0)
            & (data[labelLow] < data["close-1"] - data["body-1"].abs() * 0.25)
            & (data[labelOpen] > data["close-1"])
            & (data[labelClose] > data["close-1"])
            & (data[labelOpen] < data["open-1"])
            & (data[labelClose] < data["open-1"])), labelBuy] = 1
    data = data.drop(tempColumns, axis=1, errors='ignore')
    return data
def test_if_tails_columns_are_added():
    dataframe_out = calculate_body_and_tails(
        pd.DataFrame([], columns=testColumns))[0]
    assert "tailUp" in dataframe_out.columns
    assert "tailDown" in dataframe_out.columns
def test_if_added_columns_are_in_list():
    dataframe_out, added_columns = calculate_body_and_tails(
        pd.DataFrame([], columns=testColumns))
    for name in dataframe_out.columns:
        if name not in testColumns:
            assert name in added_columns
def test_if_body_is_calculated(dataframe_with_test_data):
    dataframe_out = calculate_body_and_tails(dataframe_with_test_data)[0]
    dataframe_out[
        "bodyGT"] = dataframe_out["<CLOSE>"] - dataframe_out["<OPEN>"]
    assert dataframe_out["body"].equals(dataframe_out["bodyGT"])
def test_if_calculate_body_and_tails_returns_dataframe_with_column_body_added(
):
    dataframe_out = calculate_body_and_tails(
        pd.DataFrame([], columns=testColumns))[0]
    assert "body" in dataframe_out.columns
def test_if_calculate_body_and_tails_inputs_and_outputs_dataframe():
    dataframe_out = pd.DataFrame([])
    assert type(calculate_body_and_tails(dataframe_out)[0]) is pd.DataFrame
    assert type(calculate_body_and_tails(dataframe_out)[1]) is list