This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule VlHelper do | |
require Explorer.DataFrame | |
require Explorer.Series | |
alias Explorer.Series | |
alias VegaLite, as: Vl | |
alias Explorer.DataFrame, as: DF | |
def get_final_vertical_barriers_chart(df, events_df) do | |
df = df | |
# Marking first barrier touch rows with true (events_df["event"]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Labeling do | |
.... | |
def compute_tripple_barriers do | |
.... | |
############ This part is mostly for Vega Lite charts ############ | |
# Stacking our vertical barrier timestamps, and first touches for each event side by side (just like in a table/df) using Nx so we can find the first/lowest one out of all | |
stacked_tensors = Nx.stack([events_df["vertical_barrier_timestamp"] |> Series.cast({:f, 64}), events_df["tp"] |> Series.cast({:f, 64}) |> Series.fill_missing(:infinity), events_df["sl"] |> Series.cast({:f, 64}) |> Series.fill_missing(:infinity)], axis: 1) | |
# Creating a tensor of the smallest timestamp (first) for each row out of those 3 series - first barrier touch for each event |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Labeling do | |
.... | |
def compute_tripple_barriers do | |
.... | |
# INSIDE Flow.map | |
# Grabbing the range for the entry event until the vertical barrier | |
sliced = DF.slice(df, entry_event..vertical_barrier) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Labeling do | |
.... | |
def compute_tripple_barriers do | |
..... | |
# Calculating Touches and Labeling each trade entry | |
events_df = | |
# Iterating over each trade entry event and its correspending vertical barrier | |
Enum.zip(entry_events_indices |> Nx.to_list, vertical_barriers_indices |> Nx.to_list) | |
# Using Flow to run them independently in parallel |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Labeling do | |
.... | |
def compute_tripple_barriers do | |
..... | |
# Adding Vertical Barriers for each entry using time offset (commented row below using bars offset) | |
vertical_barriers_indices = vertical_barriers(entry_events_indices, df["timestamp"], @vertical_barrier_offset) | |
# vertical_barriers_indices = vertical_barriers(entry_events_indices, df["timestamp"], 10, "bars") | |
side = 1 # Direction of our trade: 1 for long, -1 for short | |
tp = 1.0 # Profit taking multiple |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Functin defintion with our defaults | |
def vertical_barriers(entry_events_indices, timestamps, offset, duration_or_bars \\ "druation") | |
# If we get text we convert to duration in integer and send it with this argument | |
def vertical_barriers(entry_events_indices, timestamps, time_offset, "druation") when is_binary(time_offset) do | |
time_offset = ConvertDuration.to_milliseconds(time_offset) | |
vertical_barriers(entry_events_indices, timestamps, time_offset, "druation") | |
end | |
# Where we actually calculate the barriers when using duration as offset |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Labeling do | |
require Explorer.DataFrame | |
alias Explorer.DataFrame, as: DF | |
alias Explorer.Series | |
@lookback_window 100 # For our daily_return standard deviation ewma calculation | |
@vertical_barrier_offset "60 min" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <erl_nif.h> | |
ERL_NIF_TERM searchsorted(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) { | |
if (argc != 3) return enif_make_badarg(env); | |
// Getting our offset value | |
int offset; | |
if (!enif_get_int(env, argv[1], &offset)) { | |
return enif_make_badarg(env); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Including the Erlang NIF header file to populate our file with all the relevant functions | |
#include <erl_nif.h> | |
#include <math.h> | |
// Creating our function starting with the result type (ERL_NIF_TERM), and the arguments: the Erlang enviorment, argument count, and the arguments array | |
ERL_NIF_TERM symmetric_cumulative_sum_with_reset(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) { | |
// Quick check to make sure we got the amount of arguments were expecting | |
if (argc != 2) return enif_make_badarg(env); | |
// Getting our threshold value |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def save_to_db(list_of_rows_to_insert) do | |
# Falttening our list of lists to one long list | |
params = List.flatten(list_of_rows_to_insert) | |
# Creating our query with all the placeholders ($1, $2, $3, etc...) | |
query = "INSERT INTO submissions | |
# (id, subreddit_id, author_fullname, title, ups, downs, created_utc, selftext, thumbnail, url) | |
# (id, subreddit_id, author_fullname, title, ups, downs, created_utc, selftext, thumbnail, url) | |
# (id, subreddit_id, author_fullname, title, ups, downs, created_utc, selftext, thumbnail, url) | |
# (id, subreddit_id, author_fullname, title, ups, downs, created_utc, selftext, thumbnail, url) | |
# (id, subreddit_id, author_fullname, title, ups, downs, created_utc, selftext, thumbnail, url) |
NewerOlder