Skip to content

Instantly share code, notes, and snippets.

@beannguyen
beannguyen / vn-stock-momentum-strategy.ipynb
Created April 3, 2021 16:08
VN Stock Momentum Strategy.ipynb
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@beannguyen
beannguyen / 101Alpha_code_1.py
Created September 3, 2020 14:10 — forked from kangchihlun/101Alpha_code_1.py
101Alpha_code_1.py
import numpy as np
import pandas as pd
from numpy import abs
from numpy import log
from numpy import sign
from scipy.stats import rankdata
# region Auxiliary functions
def ts_sum(df, window=10):
"""
@beannguyen
beannguyen / ml_cross_val_score.py
Last active July 11, 2020 06:22
Custom ml_cross_val_score function from mlfinlab for the Keras Model
from sklearn.metrics import log_loss, f1_score, accuracy_score
def ml_cross_val_score(
baseline_fn,
X: pd.DataFrame,
y: pd.Series,
cv_gen,
sample_weight_train: np.ndarray = None,
sample_weight_score: np.ndarray = None,
scoring = log_loss):
from sklearn.svm import SVC
from time import time
from sklearn.metrics import f1_score
def train_classifier(clf, X_train, y_train):
''' Fits a classifier to the training data. '''
# Start the clock, train the classifier, then stop the clock
start = time()
clf.fit(X_train, y_train)
_train_data, _test_data, _eval_data = create_dataset(['bitcoin.csv'])
print(_train_data.shape, _test_data.shape, _eval_data.shape)
train_scaler, train_data = scale_data(_train_data)
train_x, train_y = extract_features_labels(train_data)
test_scaler, test_data = scale_data(_test_data)
test_x, test_y = extract_features_labels(test_data)
def preprocessing(file):
_data = []
print('Processing file ', file)
f = open(file, 'r')
try:
df = pd.read_csv(f, delimiter=',', usecols=['date', 'open', 'high', 'low', 'close'])
df = df.sort_values('date')
df = df.drop(['date'], axis=1)
dataset = df.values
Datetime Open High Low Close Volume MarketCap
2013-04-28T00:00:00 135.3 135.98 132.1 134.21 0 1500519936
2013-04-29T00:00:00 134.44 147.49 134 144.54 0 1491160064
2013-04-30T00:00:00 144 146.93 134.05 139 0 1597779968
2013-05-01T00:00:00 139 139.89 107.72 116.99 0 1542819968
2013-05-02T00:00:00 116.38 125.6 92.28 105.21 0 1292189952
2013-05-03T00:00:00 106.25 108.13 79.1 97.75 0 1180070016
2013-05-04T00:00:00 98.1 115 92.5 112.5 0 1089890048
2013-05-05T00:00:00 112.9 118.8 107.14 115.91 0 1254759936
2013-05-06T00:00:00 115.98 124.66 106.64 112.3 0 1289469952
from __future__ import absolute_import, division, print_function
from math import sqrt
import numpy as np
import pandas as pd
import tensorflow as tf
from keras import Sequential, optimizers, metrics
from keras.layers import LSTM, Dropout, Dense
from keras.losses import mean_squared_error
@beannguyen
beannguyen / xpath_soup.py
Created September 14, 2018 13:29 — forked from ergoithz/xpath_soup.py
Generate unique XPATH for BeautifulSoup element
#!/usr/bin/python
# -*- coding: utf-8 -*-
def xpath_soup(element):
"""
Generate xpath from BeautifulSoup4 element
:param element: BeautifulSoup4 element.
:type element: bs4.element.Tag or bs4.element.NavigableString
:return: xpath as string