<jupyter_start><jupyter_text>Generate example dummy data<jupyter_code>np.random.seed(0)

# market data
if not os.path.exists("example_market.csv"):
    idx = pd.date_range("2018-04-01 00:00:00", "2018-06-30 23:59:59", freq="10S")
    ask = np.cumsum(np.random.rand(len(idx)) - 0.5) + 100.0
    bid = ask - 0.02
    pd.DataFrame(
        index=idx,
        data=np.array([ask, bid]).T,
        columns=["ask", "bid"]
    ).abs().to_csv("example_market.csv")

# signal data
if not os.path.exists("example_signal.csv"):
    sig = np.random.rand(3, len(idx)).T
    n = np.sum(sig, axis=1)
    pd.DataFrame(
        index=idx,
        data=sig / n.reshape(-1, 1),
        columns=["down", "neutral", "up"]
    ).to_csv("example_signal.csv")<jupyter_output><empty_output><jupyter_text>Configuration<jupyter_code>model_id = ""
start_dt = "2018-06-03 00:00:00+0000"
end_dt = "2018-06-06 00:00:00+0000"
sig_url = "file:///{}/example_signal.csv".format(os.getcwd())
mkt_url = "file:///{}/example_market.csv".format(os.getcwd())<jupyter_output><empty_output><jupyter_text>loading the signal<jupyter_code>from backlight import signal

SYMBOL = "USDJPY"
sig = signal.load_signal(SYMBOL, sig_url, start_dt, end_dt)
sig.tail()
sig.symbol<jupyter_output><empty_output><jupyter_text>loading the marketdata<jupyter_code>from backlight import datasource

mkt = datasource.load_marketdata(
    sig.symbol,
    sig.start_dt,
    sig.end_dt,
    mkt_url,
)
mkt.tail()<jupyter_output><empty_output><jupyter_text>generating labels  Fixed Neutral<jupyter_code>from backlight import labelizer
from backlight.labelizer.ternary.fixed_neutral import FixedNeutralLabelizer


lbl_fix = labelizer.create_labels(
    mkt,
    FixedNeutralLabelizer(
        lookahead="3Min",
        neutral_atol=0.075,
        neutral_rtol=0.00,
    ))
lbl_fix.head()
lbl_fix.label_type
# (-1.0, 0.0, 1.0) : (Down, Neutral, Up)
lbl_fix.groupby("label").label.count() / len(lbl_fix)<jupyter_output><empty_output><jupyter_text>Dynamic Neutral<jupyter_code>from backlight.labelizer.ternary.dynamic_neutral import DynamicNeutralLabelizer

lbl_dyn = labelizer.create_labels(
    mkt,
    DynamicNeutralLabelizer(
        lookahead="3Min",
        neutral_ratio=0.38,
        neutral_window="1H",
        neutral_hard_limit=0.0,
    ))
lbl_dyn.head()
lbl_dyn.label_type
# (-1.0, 0.0, 1.0) : (Down, Neutral, Up)
lbl_dyn.groupby("label").label.count() / len(lbl_dyn)<jupyter_output><empty_output><jupyter_text>simulate trading strategy<jupyter_code>from backlight import strategies
trades = strategies.simple_entry_and_exit(mkt, sig, max_holding_time=pd.Timedelta('30min'))<jupyter_output><empty_output><jupyter_text>simulate and evaluate positions<jupyter_code>from backlight import positions
positions = positions.calc_positions(trades, mkt)
positions.head()<jupyter_output><empty_output><jupyter_text>Calculate Metrics - based on the raw signals<jupyter_code>from backlight import metrics
m = metrics.calc_metrics(sig, lbl_fix)
m
from backlight import metrics
m = metrics.calc_metrics(sig, lbl_dyn)
m<jupyter_output>/usr/local/lib/python3.6/dist-packages/backlight/metrics/ternary.py:36: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  pl.loc[sig.pred == TD.D.value] *= -1<jupyter_text>Calculate Performance<jupyter_code>from backlight import metrics
m = metrics.calc_position_performance(positions)
m
from backlight import metrics
m = metrics.calc_trade_performance(trades, mkt)
m<jupyter_output><empty_output><jupyter_text>Plot positions<jupyter_code>from backlight import plot

plot.plot_pl(positions)
from backlight import plot

plot.plot_cumulative_pl(positions)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12988716872212017, 'fraction_numerical': 0.029913408554185252, 'mean_word_length': 5.417508417508418, 'pattern_counts': {'":': 0, '<': 44, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '31649', 'license': 'MIT', 'n_tokens_mistral': 1494, 'n_tokens_neox': 1404, 'n_words': 226, 'path': 'examples/example.ipynb', 'repo_name': 'keisuke-umezawa/backlight', 'size': '3811'}