Skip to content

Instantly share code, notes, and snippets.

@ozitrance
ozitrance / vl_helper.ex
Last active November 9, 2024 00:49
Vega Lite Chart for Triple Barrier Method in Elixir
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"])
@ozitrance
ozitrance / labeling.ex
Last active November 9, 2024 00:49
Triple Barrier Calculation in Elixir
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
@ozitrance
ozitrance / labeling.ex
Last active November 9, 2024 00:47
Triple Barrier Parallel Calculation With Flow
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)
@ozitrance
ozitrance / labeling.ex
Last active November 8, 2024 03:07
Triple Barrier Calculation for Financial Time Series in Elixir
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
@ozitrance
ozitrance / labeling.ex
Last active November 9, 2024 00:48
Vertical Barriers Calculation in Elixir
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
@ozitrance
ozitrance / labeling.ex
Last active November 8, 2024 03:02
Vertical Barriers Function Overload for Vertical Barrier Calculation in Elixir
# 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
@ozitrance
ozitrance / labeling.ex
Last active November 8, 2024 02:53
Triple Barrier Calculation For Financial Time Series in Elixir
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"
@ozitrance
ozitrance / searchsorted.c
Created November 8, 2024 02:43
Searchsorted C NIF For Elixir
#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);
@ozitrance
ozitrance / symmetric_cumulative_sum_with_reset.c
Last active November 9, 2024 00:40
Symmetric Cumulative Sum With Reset - C NIF For Elixir
// 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
@ozitrance
ozitrance / decompress_and_decode_save_to_db_jiffy_tuples.ex
Last active November 7, 2024 19:08
Decompress ZST File in Elixir, Decode Each Line To JSON And Save to Postgres Database
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)