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
| from MetaTrader5 import * | |
| import pandas as pd | |
| pd.set_option('display.max_columns', 500) # number of columns to be displayed | |
| pd.set_option('display.width', 1500) # max table width to display | |
| import matplotlib.pyplot as plt | |
| MT5Initialize() # connect to MT5 | |
| MT5WaitForTerminal() # wait till MT5 ready | |
| ### get candle data |
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
| import pandas as pd | |
| import numpy as np | |
| import datetime as dt | |
| import math | |
| import warnings | |
| warnings.filterwarnings("ignore") | |
| prices = pd.read_csv("adjclose.csv", index_col="Date", parse_dates=True) | |
| volumechanges = pd.read_csv("volume.csv", index_col="Date", parse_dates=True).pct_change()*100 |
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
| class StockDataset(Dataset): | |
| def __init__(self, df_seq, feat_num, seq_len, target_len, df_cat): | |
| # SEQUENTIAL PART | |
| self.df_seq = df_seq.iloc[:,:-target_len] | |
| self.df_cat = df_cat | |
| self.target = df_seq.iloc[:,-target_len:] | |
| def __getitem__(self, index): | |
| return(torch.tensor(self.df_seq.iloc[index].values.reshape(seq_len,feat_num), dtype=torch.float, device=device), | |
| torch.tensor(self.df_cat.iloc[index], dtype=torch.long, device=device), |
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
| run_algorithm( | |
| capital_base = 100000, | |
| data_frequency = 'minute', | |
| initialize = initialize, | |
| handle_data = handle_data, | |
| analyze = analyze, | |
| exchange_name = 'bitfinex', | |
| quote_currency = 'usd', | |
| start = pd.to_datetime('2018-1-1', utc = True), | |
| end = pd.to_datetime('2019-5-22', utc = True)) |
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 analyze(context, perf): | |
| sns.set() | |
| # Summary output | |
| print("Total return: " + str(perf.algorithm_period_return[-1])) | |
| print("Sortino coef: " + str(perf.sortino[-1])) | |
| print("Max drawdown: " + str(np.min(perf.max_drawdown[-1]))) | |
| print("alpha: " + str(perf.alpha[-1])) | |
| print("beta: " + str(perf.beta[-1])) |
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 handle_data(context, data): | |
| current_date = get_datetime().date() | |
| current_time = get_datetime().time() | |
| # Just one time in a day (first minute) | |
| if current_time.hour == 0 and current_time.minute == 0 and current_time.second == 0: | |
| prices = pd.DataFrame() | |
| volumes = pd.DataFrame() | |
| try: |
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 initialize(context): | |
| context.asset = symbol('btc_usd') | |
| context.leverage = 1.0 | |
| context.std_period = 10 | |
| context.ma_period = 10 | |
| context.price_deviation_period = 10 | |
| context.volume_deviation_period = 10 | |
| context.n_periods = 5 + int(np.max([context.std_period, context.ma_period, | |
| context.price_deviation_period, context.volume_deviation_period])) |
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
| import matplotlib.pyplot as plt | |
| import numpy as np | |
| import pandas as pd | |
| import joblib | |
| from hmmlearn.hmm import GaussianHMM | |
| import datetime | |
| import seaborn as sns | |
| from catalyst import run_algorithm | |
| from catalyst.api import (record, symbol, order_target_percent, date_rules, time_rules, get_datetime) |
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
| joblib.dump(model, 'quandl_' + asset.replace('/', '_') + '_final_model.pkl') |
NewerOlder