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 tpqoa | |
| import pickle | |
| import numpy as np | |
| import pandas as pd | |
| import talib as ta | |
| class MLTrader(tpqoa.tpqoa): | |
| def __init__(self, config_file, algorithm): | |
| super(MLTrader, self).__init__(config_file) | |
| self.model = algorithm['model'] |
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 numpy as np | |
| import pandas as pd | |
| import talib as ta | |
| import pandas_datareader.data as web | |
| import warnings | |
| warnings.simplefilter('ignore') | |
| import random | |
| import tensorflow.compat.v1 as tf | |
| tf.disable_v2_behavior() |
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
| # MOMENTUM | |
| data['ADX'] = ta.ADX(data.High.values, data.Low.values,data.Close.values) | |
| data['ADXR'] = ta.ADXR(data.High.values, data.Low.values,data.Close.values) | |
| data['APO'] = ta.APO(data.Close.values) | |
| data['AROONdown'],data['AROONup'] = ta.AROON(data.High.values, data.Low.values) | |
| data['AROONdmu'] = data['AROONdown'] - data['AROONup'] | |
| data['AROONOSC'] = ta.AROONOSC(data.High.values, data.Low.values) | |
| data['BOP'] = ta.BOP(data.Open.values,data.High.values, data.Low.values,data.Close.values) | |
| data['CCI'] = ta.CCI(data.High.values, data.Low.values,data.Close.values) | |
| data['CMO'] = ta.CMO(data.Close.values) |
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 numpy as np | |
| import pandas as pd | |
| from pylab import plt | |
| import talib as ta | |
| from binance.client import Client | |
| import pandas_datareader.data as web | |
| import warnings | |
| from sklearn.naive_bayes import GaussianNB | |
| from sklearn.linear_model import LogisticRegression | |
| from sklearn.tree import DecisionTreeClassifier |
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 math | |
| import numpy as np | |
| import numpy.random as npr | |
| from pylab import plt, mpl | |
| # Jump-Diffusion--------------------------------------------------------------- | |
| # jumps/price shocks added to GBM model | |
| S0 = 100. # initial price | |
| r = 0.05 # short rate | |
| sigma = 0.2 # vol |
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 math | |
| import numpy as np | |
| import numpy.random as npr | |
| from pylab import plt, mpl | |
| # Stochastic Volatility-------------------------------------------------------- | |
| # <><><> the index is simulated using stochastic volatility <><><><><><><> | |
| S0 = 100. # starting index price | |
| r = 0.05 # short rate | |
| v0 = 0.25 # initial volatility |
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 math | |
| import numpy as np | |
| import numpy.random as npr | |
| from pylab import plt, mpl | |
| # Square-Root Diffusion (mean-reverting process)------------------------------- | |
| # widely used for short rates & volatility | |
| x0 = 1.00 # starting value | |
| kappa = 3.0 # mean-reversion paramater | |
| theta = 1.00 # long term mean |
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 math | |
| import numpy as np | |
| import numpy.random as npr | |
| from pylab import plt, mpl | |
| # Geometric Brownian Motion (granual/steps) | |
| S0 = 100 # starting price | |
| r = 0.05 # short rate | |
| sigma = 0.25 # vol | |
| I = 10000 # simulations |
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 bbands_MA_backtest(data, p=(20, 2, 2, 100), plot=False, costs=True, output=True): | |
| data['returns'] = np.log(data['close'] / data['close'].shift(1)) | |
| data['SMA'] = data['close'].rolling(int(p[3])).mean() | |
| # derive bbands data | |
| data['upbnd'],data['mid'],data['lwbnd'] = ta.BBANDS(data.close.values, | |
| p[0], p[1] , p[2]) # mid, ub, lb | |
| data['distance'] = data['close'] - data['mid'] | |
| data['position'] = np.where((data['close'] > data['upbnd']) &\ | |
| (data['close'] < data['SMA']), -1, np.nan) # shorts |
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 bbands_backtest(data, p=(20, 2, 2), plot=False, costs=True, output=True): | |
| data['returns'] = np.log(data['close'] / data['close'].shift(1)) | |
| # derive bbands data | |
| data['upbnd'],data['mid'],data['lwbnd'] = ta.BBANDS(data.close.values, | |
| p[0], p[1] , p[2]) # mid, ub, lb | |
| data['distance'] = data['close'] - data['mid'] # center line diff | |
| data['position'] = np.where(data['close'] > data['upbnd'], -1, np.nan) # shorts | |
| data['position'] = np.where(data['close'] < data['lwbnd'], 1, data['position']) # longs | |
| # creates zeros which ffill below will fill up to |
NewerOlder