├── crontab.txt ├── DataSet └── Symbol-Data.mq4 ├── ML-Models ├── SVM.py ├── Decision Tree.py ├── KNN.py ├── Logistic Regression.py ├── RandomForest.py └── neural-network-MLPClassifier.py ├── Feature-Calculator ├── collector.py └── featuresc.py ├── README.md ├── Connector ├── connect.py └── featuresc.py └── Tester └── Back Tester.py /crontab.txt: -------------------------------------------------------------------------------- 1 | 11 * * * * cd connect && /usr/bin/python3.6 connect.py >> test.out 2 | 0 0 * * * cd connect && rm test.out 3 | 30 * * * * sudo reboot -------------------------------------------------------------------------------- /DataSet/Symbol-Data.mq4: -------------------------------------------------------------------------------- 1 | //+------------------------------------------------------------------+ 2 | //| Symbol-Data.mq4 | 3 | //| Copyright 2019, MetaQuotes Software Corp. | 4 | //| https://www.mql5.com | 5 | //+------------------------------------------------------------------+ 6 | #property copyright "Copyright 2019, MetaQuotes Software Corp." 7 | #property link "https://www.mql5.com" 8 | #property version "1.00" 9 | #property strict 10 | //+------------------------------------------------------------------+ 11 | //| Script program start function | 12 | //+------------------------------------------------------------------+ 13 | void OnStart() 14 | { 15 | //--- 16 | int num_bar = 10000; 17 | long volume; 18 | double open,close,high,low; 19 | datetime date; 20 | int filehandle=FileOpen("Name_Of_File.csv",FILE_WRITE|FILE_READ|FILE_CSV,','); 21 | if(filehandle!=INVALID_HANDLE) 22 | { 23 | FileWrite(filehandle,"date","open","high","low","close","volume"); 24 | for(int i=num_bar;i>10;i--){ 25 | date=iTime(NULL,0,i); 26 | open=Open[i]; 27 | high=High[i]; 28 | low=Low[i]; 29 | close=Close[i]; 30 | volume=Volume[i]; 31 | FileWrite(filehandle,date,open,high,low,close,volume); 32 | } 33 | FileClose(filehandle); 34 | } 35 | else Print("Operation FileOpen failed, error ",GetLastError()); 36 | } 37 | //+------------------------------------------------------------------+ 38 | -------------------------------------------------------------------------------- /ML-Models/SVM.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | import os 3 | from sklearn.externals import joblib 4 | from sklearn.model_selection import train_test_split 5 | from sklearn.metrics import classification_report 6 | from sklearn import svm 7 | from sklearn.preprocessing import MinMaxScaler 8 | from sklearn.preprocessing import StandardScaler 9 | 10 | df = pd.read_csv('calculated.csv') 11 | 12 | exists = os.path.isfile('saved_model_svm.pkl') 13 | if exists: 14 | os.remove('saved_model_svm.pkl') 15 | 16 | columns = ['momentum3close','momentum4close' 17 | ,'momentum5close','momentum8close','momentum9close','momentum10close' 18 | ,'stoch3K','stoch3D','stoch4K','stoch4D' 19 | ,'stoch5K','stoch5D','stoch8K','stoch8D' 20 | ,'stoch9K','stoch9D','stoch10K' 21 | ,'stoch10D','will6R','will7R','will8R' 22 | ,'will9R','will10R','proc12close','proc13close' 23 | ,'proc14close','proc15close','wadl15close','adosc2AD' 24 | ,'adosc3AD','adosc4AD','adosc5AD','macd1530','cci15close' 25 | ,'bollinger15upper','bollinger15mid','bollinger15lower','paverage2open' 26 | ,'paverage2high','paverage2low','paverage2close','slope3high','slope4high','slope5high' 27 | ,'slope10high','slope20high','slope30high' 28 | ,'fourier10a0','fourier10a1','fourier10b1','fourier10w','fourier20a0','fourier20a1','fourier20b1','fourier20w','fourier30a0' 29 | ,'fourier30a1','fourier30b1','fourier30w','sine5a0','sine5b1','sine5w','sine6a0','sine6b1','sine6w','open','high','low','close'] 30 | labels = df['market0Market'].values 31 | features = df[list(columns)].values 32 | Standarization = True 33 | 34 | if Standarization == True: 35 | minmax = False 36 | Standard = True 37 | if minmax == True: 38 | min_max = MinMaxScaler() 39 | newfeatures = min_max.fit_transform(features) 40 | X_train, X_test, y_train, y_test = train_test_split(newfeatures, labels, test_size=0.1) 41 | elif Standard == True: 42 | std = StandardScaler() 43 | newfeatures = std.fit_transform(features) 44 | X_train, X_test, y_train, y_test = train_test_split(newfeatures, labels, test_size=0.1) 45 | else: 46 | X_train, X_test, y_train, y_test = train_test_split(features, labels, test_size=0.1) 47 | 48 | 49 | clf = svm.SVC(kernel='linear') 50 | clf = clf.fit(X_train, y_train) 51 | 52 | accuracy = clf.score(X_train, y_train) 53 | print (' traning data accuracy ', accuracy*100) 54 | 55 | accuracy = clf.score(X_test, y_test) 56 | print (' testing data accuracy ', accuracy*100) 57 | 58 | ypredict = clf.predict(X_train) 59 | print ('\n Training classification report\n', classification_report(y_train, ypredict)) 60 | 61 | ypredict = clf.predict(X_test) 62 | print ('\n Testing classification report\n', classification_report(y_test, ypredict)) 63 | 64 | 65 | # Output a pickle file for the model 66 | joblib.dump(clf, 'saved_model_svm.pkl') 67 | 68 | -------------------------------------------------------------------------------- /ML-Models/Decision Tree.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | import os 3 | from sklearn.externals import joblib 4 | from sklearn.model_selection import train_test_split 5 | from sklearn.metrics import classification_report 6 | from sklearn import tree 7 | from sklearn.preprocessing import MinMaxScaler 8 | from sklearn.preprocessing import StandardScaler 9 | 10 | df = pd.read_csv('calculated.csv') 11 | 12 | exists = os.path.isfile('saved_model_dt.pkl') 13 | if exists: 14 | os.remove('saved_model_dt.pkl') 15 | 16 | columns = ['momentum3close','momentum4close' 17 | ,'momentum5close','momentum8close','momentum9close','momentum10close' 18 | ,'stoch3K','stoch3D','stoch4K','stoch4D' 19 | ,'stoch5K','stoch5D','stoch8K','stoch8D' 20 | ,'stoch9K','stoch9D','stoch10K' 21 | ,'stoch10D','will6R','will7R','will8R' 22 | ,'will9R','will10R','proc12close','proc13close' 23 | ,'proc14close','proc15close','wadl15close','adosc2AD' 24 | ,'adosc3AD','adosc4AD','adosc5AD','macd1530','cci15close' 25 | ,'bollinger15upper','bollinger15mid','bollinger15lower','paverage2open' 26 | ,'paverage2high','paverage2low','paverage2close','slope3high','slope4high','slope5high' 27 | ,'slope10high','slope20high','slope30high' 28 | ,'fourier10a0','fourier10a1','fourier10b1','fourier10w','fourier20a0','fourier20a1','fourier20b1','fourier20w','fourier30a0' 29 | ,'fourier30a1','fourier30b1','fourier30w','sine5a0','sine5b1','sine5w','sine6a0','sine6b1','sine6w','open','high','low','close'] 30 | 31 | labels = df['market0Market'].values 32 | features = df[list(columns)].values 33 | 34 | Standarization = True 35 | 36 | if Standarization == True: 37 | minmax = False 38 | Standard = True 39 | if minmax == True: 40 | min_max = MinMaxScaler() 41 | newfeatures = min_max.fit_transform(features) 42 | X_train, X_test, y_train, y_test = train_test_split(newfeatures, labels, test_size=0.1) 43 | elif Standard == True: 44 | std = StandardScaler() 45 | newfeatures = std.fit_transform(features) 46 | X_train, X_test, y_train, y_test = train_test_split(newfeatures, labels, test_size=0.1) 47 | else: 48 | X_train, X_test, y_train, y_test = train_test_split(features, labels, test_size=0.1) 49 | 50 | 51 | clf = tree.DecisionTreeClassifier() 52 | clf = clf.fit(X_train, y_train) 53 | 54 | accuracy = clf.score(X_train, y_train) 55 | print (' traning data accuracy ', accuracy*100) 56 | 57 | accuracy = clf.score(X_test, y_test) 58 | print (' testing data accuracy ', accuracy*100) 59 | 60 | ypredict = clf.predict(X_train) 61 | print ('\n Training classification report\n', classification_report(y_train, ypredict)) 62 | 63 | ypredict = clf.predict(X_test) 64 | print ('\n Testing classification report\n', classification_report(y_test, ypredict)) 65 | 66 | 67 | # Output a pickle file for the model 68 | joblib.dump(clf, 'saved_model_dt.pkl') 69 | 70 | -------------------------------------------------------------------------------- /ML-Models/KNN.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | import os 3 | from sklearn.externals import joblib 4 | from sklearn.model_selection import train_test_split 5 | from sklearn.metrics import classification_report 6 | from sklearn.neighbors import KNeighborsClassifier 7 | from sklearn.preprocessing import MinMaxScaler 8 | from sklearn.preprocessing import StandardScaler 9 | 10 | df = pd.read_csv('calculated.csv') 11 | 12 | exists = os.path.isfile('saved_model_knn.pkl') 13 | if exists: 14 | os.remove('saved_model_knn.pkl') 15 | 16 | columns = ['momentum3close','momentum4close' 17 | ,'momentum5close','momentum8close','momentum9close','momentum10close' 18 | ,'stoch3K','stoch3D','stoch4K','stoch4D' 19 | ,'stoch5K','stoch5D','stoch8K','stoch8D' 20 | ,'stoch9K','stoch9D','stoch10K' 21 | ,'stoch10D','will6R','will7R','will8R' 22 | ,'will9R','will10R','proc12close','proc13close' 23 | ,'proc14close','proc15close','wadl15close','adosc2AD' 24 | ,'adosc3AD','adosc4AD','adosc5AD','macd1530','cci15close' 25 | ,'bollinger15upper','bollinger15mid','bollinger15lower','paverage2open' 26 | ,'paverage2high','paverage2low','paverage2close','slope3high','slope4high','slope5high' 27 | ,'slope10high','slope20high','slope30high' 28 | ,'fourier10a0','fourier10a1','fourier10b1','fourier10w','fourier20a0','fourier20a1','fourier20b1','fourier20w','fourier30a0' 29 | ,'fourier30a1','fourier30b1','fourier30w','sine5a0','sine5b1','sine5w','sine6a0','sine6b1','sine6w','open','high','low','close'] 30 | labels = df['market0Market'].values 31 | features = df[list(columns)].values 32 | 33 | Standarization = True 34 | 35 | if Standarization == True: 36 | minmax = False 37 | Standard = True 38 | if minmax == True: 39 | min_max = MinMaxScaler() 40 | newfeatures = min_max.fit_transform(features) 41 | X_train, X_test, y_train, y_test = train_test_split(newfeatures, labels, test_size=0.1) 42 | elif Standard == True: 43 | std = StandardScaler() 44 | newfeatures = std.fit_transform(features) 45 | X_train, X_test, y_train, y_test = train_test_split(newfeatures, labels, test_size=0.1) 46 | else: 47 | X_train, X_test, y_train, y_test = train_test_split(features, labels, test_size=0.1) 48 | 49 | 50 | clf = KNeighborsClassifier() 51 | clf = clf.fit(X_train, y_train) 52 | 53 | accuracy = clf.score(X_train, y_train) 54 | print (' traning data accuracy ', accuracy*100) 55 | 56 | accuracy = clf.score(X_test, y_test) 57 | print (' testing data accuracy ', accuracy*100) 58 | 59 | ypredict = clf.predict(X_train) 60 | print ('\n Training classification report\n', classification_report(y_train, ypredict)) 61 | 62 | ypredict = clf.predict(X_test) 63 | print ('\n Testing classification report\n', classification_report(y_test, ypredict)) 64 | 65 | 66 | # Output a pickle file for the model 67 | joblib.dump(clf, 'saved_model_knn.pkl') 68 | 69 | -------------------------------------------------------------------------------- /ML-Models/Logistic Regression.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | import os 3 | from sklearn.externals import joblib 4 | from sklearn.model_selection import train_test_split 5 | from sklearn.metrics import classification_report 6 | from sklearn.linear_model import LogisticRegression 7 | from sklearn.preprocessing import MinMaxScaler 8 | from sklearn.preprocessing import StandardScaler 9 | 10 | df = pd.read_csv('calculated.csv') 11 | 12 | exists = os.path.isfile('saved_model_lr.pkl') 13 | if exists: 14 | os.remove('saved_model_lr.pkl') 15 | 16 | columns = ['momentum3close','momentum4close' 17 | ,'momentum5close','momentum8close','momentum9close','momentum10close' 18 | ,'stoch3K','stoch3D','stoch4K','stoch4D' 19 | ,'stoch5K','stoch5D','stoch8K','stoch8D' 20 | ,'stoch9K','stoch9D','stoch10K' 21 | ,'stoch10D','will6R','will7R','will8R' 22 | ,'will9R','will10R','proc12close','proc13close' 23 | ,'proc14close','proc15close','wadl15close','adosc2AD' 24 | ,'adosc3AD','adosc4AD','adosc5AD','macd1530','cci15close' 25 | ,'bollinger15upper','bollinger15mid','bollinger15lower','paverage2open' 26 | ,'paverage2high','paverage2low','paverage2close','slope3high','slope4high','slope5high' 27 | ,'slope10high','slope20high','slope30high' 28 | ,'fourier10a0','fourier10a1','fourier10b1','fourier10w','fourier20a0','fourier20a1','fourier20b1','fourier20w','fourier30a0' 29 | ,'fourier30a1','fourier30b1','fourier30w','sine5a0','sine5b1','sine5w','sine6a0','sine6b1','sine6w','open','high','low','close'] 30 | labels = df['market0Market'].values 31 | features = df[list(columns)].values 32 | 33 | Standarization = True 34 | 35 | if Standarization == True: 36 | minmax = False 37 | Standard = True 38 | if minmax == True: 39 | min_max = MinMaxScaler() 40 | newfeatures = min_max.fit_transform(features) 41 | X_train, X_test, y_train, y_test = train_test_split(newfeatures, labels, test_size=0.1) 42 | elif Standard == True: 43 | std = StandardScaler() 44 | newfeatures = std.fit_transform(features) 45 | X_train, X_test, y_train, y_test = train_test_split(newfeatures, labels, test_size=0.1) 46 | else: 47 | X_train, X_test, y_train, y_test = train_test_split(features, labels, test_size=0.1) 48 | 49 | 50 | clf = LogisticRegression() 51 | clf = clf.fit(X_train, y_train) 52 | 53 | accuracy = clf.score(X_train, y_train) 54 | print (' traning data accuracy ', accuracy*100) 55 | 56 | accuracy = clf.score(X_test, y_test) 57 | print (' testing data accuracy ', accuracy*100) 58 | 59 | ypredict = clf.predict(X_train) 60 | print ('\n Training classification report\n', classification_report(y_train, ypredict)) 61 | 62 | ypredict = clf.predict(X_test) 63 | print ('\n Testing classification report\n', classification_report(y_test, ypredict)) 64 | 65 | 66 | # Output a pickle file for the model 67 | joblib.dump(clf, 'saved_model_lr.pkl') -------------------------------------------------------------------------------- /ML-Models/RandomForest.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | import os 3 | from sklearn.externals import joblib 4 | from sklearn.model_selection import train_test_split 5 | from sklearn.ensemble import RandomForestClassifier 6 | from sklearn.metrics import classification_report 7 | from sklearn.preprocessing import MinMaxScaler 8 | from sklearn.preprocessing import StandardScaler 9 | 10 | 11 | df = pd.read_csv('calculated.csv') 12 | 13 | exists = os.path.isfile('saved_model_rf.pkl') 14 | if exists: 15 | os.remove('saved_model_rf.pkl') 16 | 17 | columns = ['momentum3close','momentum4close' 18 | ,'momentum5close','momentum8close','momentum9close','momentum10close' 19 | ,'stoch3K','stoch3D','stoch4K','stoch4D' 20 | ,'stoch5K','stoch5D','stoch8K','stoch8D' 21 | ,'stoch9K','stoch9D','stoch10K' 22 | ,'stoch10D','will6R','will7R','will8R' 23 | ,'will9R','will10R','proc12close','proc13close' 24 | ,'proc14close','proc15close','wadl15close','adosc2AD' 25 | ,'adosc3AD','adosc4AD','adosc5AD','macd1530','cci15close' 26 | ,'bollinger15upper','bollinger15mid','bollinger15lower','paverage2open' 27 | ,'paverage2high','paverage2low','paverage2close','slope3high','slope4high','slope5high' 28 | ,'slope10high','slope20high','slope30high' 29 | ,'fourier10a0','fourier10a1','fourier10b1','fourier10w','fourier20a0','fourier20a1','fourier20b1','fourier20w','fourier30a0' 30 | ,'fourier30a1','fourier30b1','fourier30w','sine5a0','sine5b1','sine5w','sine6a0','sine6b1','sine6w','open','high','low','close'] 31 | labels = df['market0Market'].values 32 | features = df[list(columns)].values 33 | 34 | Standarization = True 35 | 36 | if Standarization == True: 37 | minmax = False 38 | Standard = True 39 | if minmax == True: 40 | min_max = MinMaxScaler() 41 | newfeatures = min_max.fit_transform(features) 42 | X_train, X_test, y_train, y_test = train_test_split(newfeatures, labels, test_size=0.1) 43 | elif Standard == True: 44 | std = StandardScaler() 45 | newfeatures = std.fit_transform(features) 46 | X_train, X_test, y_train, y_test = train_test_split(newfeatures, labels, test_size=0.1) 47 | else: 48 | X_train, X_test, y_train, y_test = train_test_split(features, labels, test_size=0.1) 49 | 50 | clf = RandomForestClassifier(n_estimators=10) 51 | clf = clf.fit(X_train, y_train) 52 | 53 | accuracy = clf.score(X_train, y_train) 54 | print (' Traning Data accuracy ', accuracy*100) 55 | 56 | accuracy = clf.score(X_test, y_test) 57 | print (' Testing Data accuracy ', accuracy*100) 58 | 59 | ypredict = clf.predict(X_train) 60 | print ('\n Training classification report\n', classification_report(y_train, ypredict)) 61 | 62 | ypredict = clf.predict(X_test) 63 | print ('\n Testing classification report\n', classification_report(y_test, ypredict)) 64 | 65 | # Output a pickle file for the model 66 | joblib.dump(clf, 'saved_model_rf.pkl') 67 | -------------------------------------------------------------------------------- /ML-Models/neural-network-MLPClassifier.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | import os 3 | from sklearn.externals import joblib 4 | from sklearn.model_selection import train_test_split 5 | from sklearn.metrics import classification_report 6 | from sklearn.neural_network import MLPClassifier 7 | from sklearn.preprocessing import MinMaxScaler 8 | from sklearn.preprocessing import StandardScaler 9 | 10 | df = pd.read_csv('calculated.csv') 11 | 12 | exists = os.path.isfile('saved_model_nn.pkl') 13 | if exists: 14 | os.remove('saved_model_nn.pkl') 15 | 16 | columns = ['momentum3close','momentum4close' 17 | ,'momentum5close','momentum8close','momentum9close','momentum10close' 18 | ,'stoch3K','stoch3D','stoch4K','stoch4D' 19 | ,'stoch5K','stoch5D','stoch8K','stoch8D' 20 | ,'stoch9K','stoch9D','stoch10K' 21 | ,'stoch10D','will6R','will7R','will8R' 22 | ,'will9R','will10R','proc12close','proc13close' 23 | ,'proc14close','proc15close','wadl15close','adosc2AD' 24 | ,'adosc3AD','adosc4AD','adosc5AD','macd1530','cci15close' 25 | ,'bollinger15upper','bollinger15mid','bollinger15lower','paverage2open' 26 | ,'paverage2high','paverage2low','paverage2close','slope3high','slope4high','slope5high' 27 | ,'slope10high','slope20high','slope30high' 28 | ,'fourier10a0','fourier10a1','fourier10b1','fourier10w','fourier20a0','fourier20a1','fourier20b1','fourier20w','fourier30a0' 29 | ,'fourier30a1','fourier30b1','fourier30w','sine5a0','sine5b1','sine5w','sine6a0','sine6b1','sine6w','open','high','low','close'] 30 | labels = df['market0Market'].values 31 | features = df[list(columns)].values 32 | 33 | Standarization = True 34 | 35 | if Standarization == True: 36 | minmax = False 37 | Standard = True 38 | if minmax == True: 39 | min_max = MinMaxScaler() 40 | newfeatures = min_max.fit_transform(features) 41 | X_train, X_test, y_train, y_test = train_test_split(newfeatures, labels, test_size=0.1) 42 | elif Standard == True: 43 | std = StandardScaler() 44 | newfeatures = std.fit_transform(features) 45 | X_train, X_test, y_train, y_test = train_test_split(newfeatures, labels, test_size=0.1) 46 | else: 47 | X_train, X_test, y_train, y_test = train_test_split(features, labels, test_size=0.1) 48 | 49 | 50 | clf = MLPClassifier(activation='relu',max_iter=100000) 51 | clf = clf.fit(X_train, y_train) 52 | 53 | accuracy = clf.score(X_train, y_train) 54 | print (' traning data accuracy ', accuracy*100) 55 | 56 | accuracy = clf.score(X_test, y_test) 57 | print (' testing data accuracy ', accuracy*100) 58 | 59 | ypredict = clf.predict(X_train) 60 | print ('\n Training classification report\n', classification_report(y_train, ypredict)) 61 | 62 | ypredict = clf.predict(X_test) 63 | print ('\n Testing classification report\n', classification_report(y_test, ypredict)) 64 | 65 | 66 | # Output a pickle file for the model 67 | joblib.dump(clf, 'saved_model_nn.pkl') -------------------------------------------------------------------------------- /Feature-Calculator/collector.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | from featuresc import * 3 | 4 | data = pd.read_csv('Name_of_file.csv') 5 | data.columns = ['date','open','high','low','close','volume'] 6 | data['date']=pd.to_datetime(data.date) 7 | 8 | 9 | for i in range(0,len(data)): 10 | if data.date.dt.minute.iloc[i] == 0: 11 | break 12 | data = data[i:] 13 | 14 | data = data.set_index(pd.to_datetime(data.date)) 15 | data = data[['open','high','low','close','volume']] 16 | prices = data.drop_duplicates(keep=False) 17 | 18 | prices = resamble(prices) 19 | 20 | momentumKey = [3,4,5,8,9,10] 21 | stochasticKey = [3,4,5,8,9,10] 22 | williamsKey = [6,7,8,9,10] 23 | procKey = [12,13,14,15] 24 | wadlKey = [15] 25 | adoscKey = [2,3,4,5] 26 | macdKey = [15,30] 27 | cciKey = [15] 28 | bollingerKey = [15] 29 | heikenashiKey = [15] 30 | paverageKey = [2] 31 | slopeKey = [3,4,5,10,20,30] 32 | fourierKey = [10,20,30] 33 | sineKey = [5,6] 34 | marketKey = [0] 35 | 36 | 37 | keylist = [momentumKey,stochasticKey,williamsKey,procKey,wadlKey,adoscKey,macdKey,cciKey,bollingerKey 38 | ,paverageKey,slopeKey,fourierKey,sineKey,marketKey] 39 | 40 | 41 | momentumDict = momentum(prices,momentumKey) 42 | print('1') 43 | stochasticDict = stochastic(prices,stochasticKey) 44 | print('2') 45 | williamsDict = williams(prices,williamsKey) 46 | print('3') 47 | procDict = proc(prices,procKey) 48 | print('4') 49 | wadlDict = wadl(prices,wadlKey) 50 | print('5') 51 | adoscDict = adosc(prices,adoscKey) 52 | print('6') 53 | macdDict = macd(prices,macdKey) 54 | print('7') 55 | cciDict = cci(prices,cciKey) 56 | print('8') 57 | bollingerDict = bollinger(prices,bollingerKey,2) 58 | print('9') 59 | ''' 60 | hkaprices = prices.copy() 61 | hkaprices['Symbol']='SYMB' 62 | HKA = OHLCresample(hkaprices,'1H') 63 | 64 | heikenDict = Heiken_Ashi(HKA,heikenashiKey) 65 | print('10') 66 | ''' 67 | paverageDict = pavarage(prices,paverageKey) 68 | print('11') 69 | slopeDict = slopes(prices,slopeKey) 70 | print('12') 71 | fourierDict = fourier(prices,fourierKey) 72 | print('13') 73 | sineDict = sine(prices,sineKey) 74 | print('14') 75 | marketDict = Market(prices,marketKey) 76 | print('15') 77 | # Create list of dictionaries 78 | 79 | dictlist = [momentumDict.close,stochasticDict.close,williamsDict.close 80 | ,procDict.proc,wadlDict.wadl,adoscDict.AD,macdDict.line 81 | ,cciDict.cci,bollingerDict.bands,paverageDict.avs 82 | ,slopeDict.slope,fourierDict.coeffs,sineDict.coeffs,marketDict.slope] 83 | 84 | # list of column name on csv 85 | 86 | colFeat = ['momentum','stoch','will','proc','wadl','adosc','macd', 87 | 'cci','bollinger','paverage','slope','fourier','sine','market'] 88 | 89 | masterFrame = pd.DataFrame(index = prices.index) 90 | for i in range(0,len(dictlist)): 91 | if colFeat[i] == 'macd': 92 | colID = colFeat[i] + str(keylist[6][0]) + str(keylist[6][1]) 93 | masterFrame[colID] = dictlist[i] 94 | else: 95 | for j in keylist[i]: 96 | for k in list(dictlist[i][j]): 97 | colID = colFeat[i] + str(j) + str(k) 98 | masterFrame[colID] = dictlist[i][j][k] 99 | 100 | threshold = round(0.7*len(masterFrame)) 101 | masterFrame[['open','high','low','close']] = prices[['open','high','low','close']] 102 | ''' 103 | masterFrame.heiken150 = masterFrame.heiken150.fillna(method='bfill') 104 | masterFrame.heiken151 = masterFrame.heiken151.fillna(method='bfill') 105 | masterFrame.heiken152 = masterFrame.heiken152.fillna(method='bfill') 106 | masterFrame.heiken153 = masterFrame.heiken153.fillna(method='bfill') 107 | # Drop columns that have 30% or more NAN data 108 | ''' 109 | masterFrameCleaned = masterFrame.copy() 110 | 111 | masterFrameCleaned = masterFrameCleaned.dropna(axis=1,thresh=threshold) 112 | masterFrameCleaned = masterFrameCleaned.dropna(axis=0) 113 | masterFrameCleaned.to_csv('calculated.csv') 114 | print('completed') 115 | 116 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Market-Analysis-2 2 | 3 | ### Overview 4 | 5 | In Market Analysis 2 we upgrade Market Analysis to be better in all of its component , plus that we add a new component called Feature-Calculator. 6 | 7 | ## Components 8 | 9 | * [Dataset](#Dataset) 10 | 11 | * [Feature-Calculator](#Feature-Calculator) 12 | 13 | * [ML-Models](#ML-Models) 14 | 15 | * [Tester](#Tester) 16 | 17 | * [Connection](#Connection) 18 | 19 | **Note:** The order in setup is important. 20 | 21 | ## Dataset 22 | 23 | ### Introduction 24 | 25 | Create a Dataset for any symbol in any period of time in Forex market (Metatrader 4) that contain the basic Features 26 | (open, high, low, close). 27 | 28 | ### How we do it 29 | We do it by pulling data from MQL4 in to CSV file , the data is pulled using MQL4 build in functions that create our Features. 30 | 31 | ### Setup 32 | 1. Download the code in Dataset [(here)](https://github.com/Financial-ML/Market-Analysis-2/tree/master/DataSet) 33 | 1. Git in the code and write the number of bars you need. 34 | 1. Name the csv file then compile it. 35 | 1. Run the script (Dataset) in any symbol and any period of time. 36 | #### Features 37 | "OPEN","HIGH","LOW","CLOSE","VOLUME" 38 | open=OPEN(i); 39 | high=HIGH(i); 40 | low=LOW(i); 41 | close=CLOSE(i); 42 | volume=VOLUME(i); 43 | 44 | ## Feature-Calculator 45 | ### Introduction 46 | In Feature-Calculator we calculate the machine learning Feature based on this research [(here)](http://www.wseas.us/e-library/conferences/2011/Penang/ACRE/ACRE-05.pdf). 47 | 48 | ### How we do it 49 | * We build it in python . 50 | * It based on the main Features [Dataset](#Dataset). 51 | * And we save it after finsh extraction the feature in csv file. 52 | 53 | ### Setup 54 | 1. After calculating the [Dataset](#Dataset). copy the CSV file in to your python project. 55 | 1. Download the code in Feature-Calculators [(here)](https://github.com/Financial-ML/Market-Analysis-2/tree/master/Feature-Calculator) in to your python project. 56 | 1. Run the collector program and it will generate the Calculated Feature in a CSV file. 57 | 58 | 59 | 60 | ## ML-Models 61 | 62 | ### Introduction 63 | Different Machine Learning models that we used to learn from the [Feature](#Feature-Calculator). 64 | 65 | ### How we do it 66 | * We build the the models in python using scikit-learn. 67 | * It learn from our predefined [Feature](#Feature-Calculator). 68 | * And then save it after finsh traning in PKL file. 69 | 70 | ### Models 71 | * Decision Tree. 72 | * k-nearest neighbor. 73 | * Logistic Regression. 74 | * RandomForest. 75 | * Support vector machine. 76 | * Neural-network-MLPClassifier. 77 | 78 | ### Setup 79 | 1. After calculating the [Feature](#Feature-Calculator). copy the CSV file in to your python project. 80 | 1. Download the code in ML-Models [(here)](https://github.com/Financial-ML/Market-Analysis-2/tree/master/ML-Models) in to your python project. 81 | 1. Run the program and it will generate the PKL file. 82 | 83 | ## Tester 84 | 85 | ### Introduction 86 | Tool that use to test the strategy that has been developed outside MQL4 in python. 87 | 88 | ### How we do it 89 | * We do it by build our algorithmic trading strategy in python. 90 | * And load our Models in Tester program. 91 | * Then calculate the profits by saving the enter price then subtract from it the close price. 92 | 93 | ### Algorithmic Trading 94 | We call the models (Decision Tree and RandomForest) every one hour and say if the first 10 min is the same prediction do the predicted action. 95 | ### What we test 96 | 1. Profit 97 | 1. Total number of trades 98 | 1. Sum of wining trades 99 | 1. Sum of loss trades 100 | 1. Max drawdown 101 | 1. Best trade 102 | 103 | ### Setup 104 | 1. After traning the models [ML-Models](#ML-Models). 105 | 1. Get your testing data from [Dataset](#Dataset). 106 | 1. Download the code in Tester [(here)](https://github.com/Financial-ML/Market-Analysis-2/tree/master/Tester) in to your python project. 107 | 1. Run the program and it will test the stratgy. 108 | 109 | ## Connection 110 | ### Introduction 111 | Connect the strategy that has been developed in Python with fxcm company in a real time connection the structure has been developed to run on cloud every 1 hour. 112 | ### How we do it 113 | * Pull the basic data from fxcm by connect to a generated token you can git it from [(here)](https://www.fxcm.com/markets/). 114 | * After pulling data we predict the price using our machine learning models. 115 | * All of this process is done in the cloud every 1 hour. 116 | ### Setup 117 | 1. Create a new Python project. 118 | 1. Download the code in the project [(here)](https://github.com/Financial-ML/Market-Analysis-2/tree/master/Connector). 119 | 1. Copy the generated models from [ML-Models](#ML-Models). 120 | 1. Put it in a any cloud you are familiar with it. 121 | 1. If you use ubuntu use crontab to set a time to run the code. 122 | 1. the timer that are used in crontab is (11 * * * * * ) it means run the code every hour in minute 11. 123 | 124 | 125 | 126 | 127 | 128 | -------------------------------------------------------------------------------- /Connector/connect.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | from featuresc import * 3 | from sklearn.externals import joblib 4 | import numpy as np 5 | from sklearn.preprocessing import StandardScaler 6 | import fxcmpy 7 | import time 8 | import datetime as dt 9 | #------------------------------------------------------------------------------ 10 | 11 | try: 12 | print(dt.datetime.now()) 13 | TOKEN = "10ca2fab839f9a9f56c99bfdf676a364ce3ff2c7" 14 | 15 | con = fxcmpy.fxcmpy(access_token=TOKEN, log_level='error') 16 | 17 | if con.is_connected() == True: 18 | print("Data retrieved...") 19 | df = con.get_candles('EUR/USD', period='m5',number=200) 20 | df = df.drop(columns=['bidopen', 'bidclose','bidhigh','bidlow']) 21 | 22 | df = df.rename(columns={"askopen": "open", "askhigh": "high","asklow": "low", "askclose": "close","tickqty": "volume"}) 23 | df = df[['open','high','low','close','volume']] 24 | df = df[~df.index.duplicated()] 25 | prices = df.copy() 26 | else: 27 | print('No connection with fxcm') 28 | 29 | #------------------------------------------------------------------------------ 30 | 31 | market_alive = prices.index[-1].minute 32 | 33 | if market_alive == 5: 34 | prices.index=pd.to_datetime(prices.index) 35 | prices['B'] = prices.index.minute 36 | for i in range(0,len(prices)): 37 | if prices.B.iloc[i] == 0: 38 | break 39 | prices = prices[i:] 40 | prices.drop(['B'], axis=1) 41 | 42 | #------------------------------------------------------------------------------ 43 | 44 | momentumKey = [3,4,5,8,9,10] 45 | stochasticKey = [3,4,5,8,9,10] 46 | williamsKey = [6,7,8,9,10] 47 | procKey = [12,13,14,15] 48 | wadlKey = [15] 49 | adoscKey = [2,3,4,5] 50 | macdKey = [15,30] 51 | cciKey = [15] 52 | bollingerKey = [15] 53 | paverageKey = [2] 54 | slopeKey = [3,4,5,10,20,30] 55 | fourierKey = [10,20,30] 56 | sineKey = [5,6] 57 | 58 | 59 | keylist = [momentumKey,stochasticKey,williamsKey,procKey,wadlKey,adoscKey,macdKey,cciKey,bollingerKey 60 | ,paverageKey,slopeKey,fourierKey,sineKey] 61 | 62 | momentumDict = momentum(prices,momentumKey) 63 | print('1') 64 | stochasticDict = stochastic(prices,stochasticKey) 65 | print('2') 66 | williamsDict = williams(prices,williamsKey) 67 | print('3') 68 | procDict = proc(prices,procKey) 69 | print('4') 70 | wadlDict = wadl(prices,wadlKey) 71 | print('5') 72 | adoscDict = adosc(prices,adoscKey) 73 | print('6') 74 | macdDict = macd(prices,macdKey) 75 | print('7') 76 | cciDict = cci(prices,cciKey) 77 | print('8') 78 | bollingerDict = bollinger(prices,bollingerKey,2) 79 | print('9') 80 | paverageDict = pavarage(prices,paverageKey) 81 | print('11') 82 | slopeDict = slopes(prices,slopeKey) 83 | print('12') 84 | fourierDict = fourier(prices,fourierKey) 85 | print('13') 86 | sineDict = sine(prices,sineKey) 87 | print('14') 88 | 89 | # Create list of dictionaries 90 | 91 | dictlist = [momentumDict.close,stochasticDict.close,williamsDict.close 92 | ,procDict.proc,wadlDict.wadl,adoscDict.AD,macdDict.line 93 | ,cciDict.cci,bollingerDict.bands,paverageDict.avs 94 | ,slopeDict.slope,fourierDict.coeffs,sineDict.coeffs] 95 | 96 | # list of column name on csv 97 | 98 | colFeat = ['momentum','stoch','will','proc','wadl','adosc','macd', 99 | 'cci','bollinger','paverage','slope','fourier','sine'] 100 | masterFrame = pd.DataFrame(index = prices.index) 101 | 102 | 103 | for i in range(0,len(dictlist)): 104 | if colFeat[i] == 'macd': 105 | colID = colFeat[i] + str(keylist[6][0]) + str(keylist[6][1]) 106 | masterFrame[colID] = dictlist[i] 107 | else: 108 | for j in keylist[i]: 109 | for k in list(dictlist[i][j]): 110 | colID = colFeat[i] + str(j) + str(k) 111 | masterFrame[colID] = dictlist[i][j][k] 112 | 113 | 114 | threshold = round(0.7*len(masterFrame)) 115 | masterFrame[['open','high','low','close']] = prices[['open','high','low','close']] 116 | 117 | # Drop columns that have 30% or more NAN data 118 | 119 | masterFrameCleaned = masterFrame.copy() 120 | 121 | masterFrameCleaned = masterFrameCleaned.dropna(axis=1,thresh=threshold) 122 | masterFrameCleaned = masterFrameCleaned.dropna(axis=0) 123 | print('completed') 124 | 125 | columns = ['momentum3close','momentum4close' 126 | ,'momentum5close','momentum8close','momentum9close','momentum10close' 127 | ,'stoch3K','stoch3D','stoch4K','stoch4D' 128 | ,'stoch5K','stoch5D','stoch8K','stoch8D' 129 | ,'stoch9K','stoch9D','stoch10K' 130 | ,'stoch10D','will6R','will7R','will8R' 131 | ,'will9R','will10R','proc12close','proc13close' 132 | ,'proc14close','proc15close','wadl15close','adosc2AD' 133 | ,'adosc3AD','adosc4AD','adosc5AD','macd1530','cci15close' 134 | ,'bollinger15upper','bollinger15mid','bollinger15lower','paverage2open' 135 | ,'paverage2high','paverage2low','paverage2close','slope3high','slope4high','slope5high' 136 | ,'slope10high','slope20high','slope30high' 137 | ,'fourier10a0','fourier10a1','fourier10b1','fourier10w','fourier20a0','fourier20a1','fourier20b1','fourier20w','fourier30a0' 138 | ,'fourier30a1','fourier30b1','fourier30w','sine5a0','sine5b1','sine5w','sine6a0','sine6b1','sine6w','open','high','low','close'] 139 | 140 | 141 | clf_load_dt = joblib.load('saved_model_dt.pkl') 142 | clf_load_rf = joblib.load('saved_model_rf.pkl') 143 | 144 | 145 | 146 | df = masterFrameCleaned[list(columns)].values 147 | a = [] 148 | std = StandardScaler() 149 | newfeaturess = std.fit_transform(df) 150 | 151 | a=[] 152 | a1=[] 153 | newfeatures = newfeaturess[-1].reshape(1, 69) 154 | 155 | predicted_dt = clf_load_dt.predict(newfeatures) 156 | a = np.append(a,predicted_dt) 157 | 158 | predicted_rf = clf_load_rf.predict(newfeatures) 159 | a = np.append(a,predicted_rf) 160 | 161 | newfeatures = newfeaturess[-2].reshape(1, 69) 162 | predicted_dt = clf_load_dt.predict(newfeatures) 163 | a1 = np.append(a1,predicted_dt) 164 | 165 | predicted_rf = clf_load_rf.predict(newfeatures) 166 | a1 = np.append(a1,predicted_rf) 167 | 168 | print(a) 169 | print(a1) 170 | 171 | 172 | 173 | if con.is_connected() == True: 174 | if a[0]==1 and a[1]==1 and a1[0]==1 and a1[1]==1: 175 | print("buy") 176 | if con.open_pos == {}: 177 | con.create_market_buy_order('EUR/USD', 5) 178 | else: 179 | tradeId = con.get_open_trade_ids()[0] 180 | pos = con.get_open_position(tradeId) 181 | if pos.get_isBuy()==False: 182 | con.close_all_for_symbol('EUR/USD') 183 | time.sleep(5) 184 | con.create_market_buy_order('EUR/USD', 5) 185 | #see if there is no order enter one 186 | #if there is order buy keep it else complet 187 | #close sell order 188 | #open order buy 189 | if a[0]==0 and a[1]==0 and a1[0]==0 and a1[1]==0: 190 | print("sell") 191 | if con.open_pos == {}: 192 | con.create_market_sell_order('EUR/USD', 5) 193 | else: 194 | tradeId = con.get_open_trade_ids()[0] 195 | pos = con.get_open_position(tradeId) 196 | if pos.get_isBuy()==True: 197 | con.close_all_for_symbol('EUR/USD') 198 | time.sleep(5) 199 | con.create_market_sell_order('EUR/USD', 5) 200 | #see if there is order 201 | #see the type if sell keep it else complet 202 | #close order buy 203 | #open order sell 204 | else: 205 | print('No connection with fxcm') 206 | else: 207 | print('Market is close...') 208 | except Exception as e: 209 | 210 | print("code does not work:",e) 211 | 212 | if con.is_connected() == True: 213 | con.close_all_for_symbol('EUR/USD') 214 | else: 215 | print('No connection with fxcm') 216 | #------------------------------------------------------------------------------ 217 | 218 | 219 | -------------------------------------------------------------------------------- /Tester/Back Tester.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | from featuresc import * 3 | from sklearn.externals import joblib 4 | import numpy as np 5 | from sklearn.preprocessing import StandardScaler 6 | 7 | data = pd.read_csv('EUR1k5m4.csv') 8 | data.columns = ['date','open','high','low','close','volume'] 9 | data['date']=pd.to_datetime(data.date) 10 | 11 | for i in range(0,len(data)): 12 | if data.date.dt.minute.iloc[i] == 0: 13 | break 14 | data = data[i:] 15 | 16 | data = data.set_index(pd.to_datetime(data.date)) 17 | data = data[['open','high','low','close','volume']] 18 | prices = data.drop_duplicates(keep=False) 19 | 20 | prices = resamble(prices) 21 | 22 | momentumKey = [3,4,5,8,9,10] 23 | stochasticKey = [3,4,5,8,9,10] 24 | williamsKey = [6,7,8,9,10] 25 | procKey = [12,13,14,15] 26 | wadlKey = [15] 27 | adoscKey = [2,3,4,5] 28 | macdKey = [15,30] 29 | cciKey = [15] 30 | bollingerKey = [15] 31 | heikenashiKey = [15] 32 | paverageKey = [2] 33 | slopeKey = [3,4,5,10,20,30] 34 | fourierKey = [10,20,30] 35 | sineKey = [5,6] 36 | 37 | keylist = [momentumKey,stochasticKey,williamsKey,procKey,wadlKey,adoscKey,macdKey,cciKey,bollingerKey 38 | ,paverageKey,slopeKey,fourierKey,sineKey] 39 | 40 | momentumDict = momentum(prices,momentumKey) 41 | print('1') 42 | stochasticDict = stochastic(prices,stochasticKey) 43 | print('2') 44 | williamsDict = williams(prices,williamsKey) 45 | print('3') 46 | procDict = proc(prices,procKey) 47 | print('4') 48 | wadlDict = wadl(prices,wadlKey) 49 | print('5') 50 | adoscDict = adosc(prices,adoscKey) 51 | print('6') 52 | macdDict = macd(prices,macdKey) 53 | print('7') 54 | cciDict = cci(prices,cciKey) 55 | print('8') 56 | bollingerDict = bollinger(prices,bollingerKey,2) 57 | print('9') 58 | ''' 59 | hkaprices = prices.copy() 60 | hkaprices['Symbol']='SYMB' 61 | HKA = OHLCresample(hkaprices,'1H') 62 | heikenDict = Heiken_Ashi(HKA,heikenashiKey) 63 | print('10' ) 64 | ''' 65 | paverageDict = pavarage(prices,paverageKey) 66 | print('11') 67 | slopeDict = slopes(prices,slopeKey) 68 | print('12') 69 | fourierDict = fourier(prices,fourierKey) 70 | print('13') 71 | sineDict = sine(prices,sineKey) 72 | print('14') 73 | 74 | # Create list of dictionaries 75 | 76 | dictlist = [momentumDict.close,stochasticDict.close,williamsDict.close 77 | ,procDict.proc,wadlDict.wadl,adoscDict.AD,macdDict.line 78 | ,cciDict.cci,bollingerDict.bands,paverageDict.avs 79 | ,slopeDict.slope,fourierDict.coeffs,sineDict.coeffs] 80 | 81 | # list of column name on csv 82 | 83 | colFeat = ['momentum','stoch','will','proc','wadl','adosc','macd', 84 | 'cci','bollinger','paverage','slope','fourier','sine'] 85 | 86 | masterFrame = pd.DataFrame(index = prices.index) 87 | for i in range(0,len(dictlist)): 88 | if colFeat[i] == 'macd': 89 | colID = colFeat[i] + str(keylist[6][0]) + str(keylist[6][1]) 90 | masterFrame[colID] = dictlist[i] 91 | else: 92 | for j in keylist[i]: 93 | for k in list(dictlist[i][j]): 94 | colID = colFeat[i] + str(j) + str(k) 95 | masterFrame[colID] = dictlist[i][j][k] 96 | 97 | threshold = round(0.7*len(masterFrame)) 98 | masterFrame[['open','high','low','close']] = prices[['open','high','low','close']] 99 | ''' 100 | masterFrame.heiken150 = masterFrame.heiken150.fillna(method='bfill') 101 | masterFrame.heiken151 = masterFrame.heiken151.fillna(method='bfill') 102 | masterFrame.heiken152 = masterFrame.heiken152.fillna(method='bfill') 103 | masterFrame.heiken153 = masterFrame.heiken153.fillna(method='bfill') 104 | ''' 105 | # Drop columns that have 30% or more NAN data 106 | 107 | masterFrameCleaned = masterFrame.copy() 108 | 109 | masterFrameCleaned = masterFrameCleaned.dropna(axis=1,thresh=threshold) 110 | masterFrameCleaned = masterFrameCleaned.dropna(axis=0) 111 | masterFrameCleaned.to_csv('calculated_BT.csv') 112 | print('completed') 113 | 114 | 115 | masterFrameCleaned.index=pd.to_datetime(masterFrameCleaned.index) 116 | masterFrameCleaned['B'] = masterFrameCleaned.index.minute 117 | for i in range(0,len(masterFrameCleaned)): 118 | if masterFrameCleaned.B.iloc[i] == 0: 119 | break 120 | masterFrameCleaned = masterFrameCleaned[i:] 121 | masterFrameCleaned.drop(['B'], axis=1) 122 | masterFrameCleaned.to_csv('calculated_BT.csv') 123 | 124 | columns = ['momentum3close','momentum4close' 125 | ,'momentum5close','momentum8close','momentum9close','momentum10close' 126 | ,'stoch3K','stoch3D','stoch4K','stoch4D' 127 | ,'stoch5K','stoch5D','stoch8K','stoch8D' 128 | ,'stoch9K','stoch9D','stoch10K' 129 | ,'stoch10D','will6R','will7R','will8R' 130 | ,'will9R','will10R','proc12close','proc13close' 131 | ,'proc14close','proc15close','wadl15close','adosc2AD' 132 | ,'adosc3AD','adosc4AD','adosc5AD','macd1530','cci15close' 133 | ,'bollinger15upper','bollinger15mid','bollinger15lower','paverage2open' 134 | ,'paverage2high','paverage2low','paverage2close','slope3high','slope4high','slope5high' 135 | ,'slope10high','slope20high','slope30high' 136 | ,'fourier10a0','fourier10a1','fourier10b1','fourier10w','fourier20a0' 137 | ,'fourier20a1','fourier20b1','fourier20w','fourier30a0' 138 | ,'fourier30a1','fourier30b1','fourier30w','sine5a0','sine5b1','sine5w' 139 | ,'sine6a0','sine6b1','sine6w','open','high','low','close'] 140 | 141 | 142 | clf_load_dt = joblib.load('saved_model_dt.pkl') 143 | clf_load_knn = joblib.load('saved_model_knn.pkl') 144 | clf_load_lr = joblib.load('saved_model_lr.pkl') 145 | clf_load_nn = joblib.load('saved_model_nn.pkl') 146 | clf_load_rf = joblib.load('saved_model_rf.pkl') 147 | clf_load_svm = joblib.load('saved_model_svm.pkl') 148 | 149 | #------------------------------------------------------------------------------ 150 | save_state_old = 3 151 | save_state_new = 3 152 | save_price = 0 153 | sum_profit = 0 154 | first = 1 155 | maxx=0 156 | minn=0 157 | c=0 158 | s=0 159 | x=1 160 | 161 | 162 | df = masterFrameCleaned[list(columns)].values 163 | a = [] 164 | 165 | std = StandardScaler() 166 | newfeaturess = std.fit_transform(df) 167 | 168 | buyy = 9999999 169 | selll = 999999 170 | change = 4 171 | change1 = 4 172 | for i in range(0,len(masterFrameCleaned)): 173 | profit = 0 174 | a=[] 175 | newfeatures = newfeaturess[i].reshape(1, 69) 176 | 177 | predicted_dt = clf_load_dt.predict(newfeatures) 178 | a = np.append(a,predicted_dt) 179 | 180 | predicted_rf = clf_load_rf.predict(newfeatures) 181 | a = np.append(a,predicted_rf) 182 | ''' 183 | predicted_knn = clf_load_knn.predict(newfeatures) 184 | a = np.append(a,predicted_knn) 185 | 186 | predicted_lr = clf_load_lr.predict(newfeatures) 187 | a = np.append(a,predicted_lr) 188 | 189 | predicted_nn = clf_load_nn.predict(newfeatures) 190 | a = np.append(a,predicted_nn) 191 | 192 | predicted_svm = clf_load_svm.predict(newfeatures) 193 | a = np.append(a,predicted_svm) 194 | ''' 195 | masterFrameCleaned.index=pd.to_datetime(masterFrameCleaned.index) 196 | masterFrameCleaned['B'] = masterFrameCleaned.index.minute 197 | if masterFrameCleaned.B.iloc[i] == 0 : 198 | buyy = 9999999 199 | selll = 999999 200 | buy = 0 201 | sell = 0 202 | for ii in a: 203 | if ii == 1: 204 | buy = buy + 1 205 | elif ii == 0: 206 | sell = sell + 1 207 | if masterFrameCleaned.B.iloc[i] == 5 : 208 | buyy = 0 209 | selll = 0 210 | for ii in a: 211 | if ii == 1: 212 | buyy = buyy + 1 213 | elif ii == 0: 214 | selll = selll + 1 215 | 216 | ''' 217 | if i<30 : 218 | print(a) 219 | else: 220 | break 221 | ''' 222 | 223 | #------------------------------------------------------------------------------ 224 | #calculate profit 225 | if buy==buyy or sell==selll: 226 | if first == 1: 227 | if buy > 1:#buy 228 | first=2 229 | save_state_new = 1 230 | save_price = masterFrameCleaned.close.iloc[i] 231 | elif sell > 1:#sell 232 | first=2 233 | save_state_new = 0 234 | save_price = masterFrameCleaned.close.iloc[i] 235 | save_state_old = save_state_new 236 | 237 | if first == 2: 238 | if buy > 1:#buy 239 | save_state_new = 1 240 | elif sell > 1:#sell 241 | save_state_new = 0 242 | else:#hold 243 | save_state_new = save_state_old 244 | 245 | if save_state_old != save_state_new: 246 | if save_state_old == 1: 247 | profit = masterFrameCleaned.close.iloc[i] - save_price - 0.0002 248 | if profit > 0: 249 | c=c+1 250 | elif profit < 0: 251 | s=s+1 252 | save_price = masterFrameCleaned.close.iloc[i] 253 | elif save_state_old == 0: 254 | profit = save_price - masterFrameCleaned.close.iloc[i] - 0.0002 255 | if profit > 0: 256 | c=c+1 257 | elif profit < 0: 258 | s=s+1 259 | save_price = masterFrameCleaned.close.iloc[i] 260 | elif save_state_old == 3: 261 | save_price = masterFrameCleaned.close.iloc[i] 262 | 263 | if maxx < profit: 264 | maxx = profit 265 | if minn > profit: 266 | minn = profit 267 | save_state_old = save_state_new 268 | 269 | sum_profit = sum_profit + profit 270 | #------------------------------------------------------------------------------ 271 | ''' 272 | if save_state_old == save_state_new: 273 | if save_state_old == 1: 274 | profit = masterFrameCleaned.close.iloc[i] - save_price - 0.0002 275 | if profit > 0: 276 | c=c+1 277 | elif profit < 0: 278 | s=s+1 279 | elif save_state_old == 0: 280 | profit = save_price - masterFrameCleaned.close.iloc[i] - 0.0002 281 | if profit > 0: 282 | c=c+1 283 | elif profit < 0: 284 | s=s+1 285 | 286 | if maxx < profit: 287 | maxx = profit 288 | if minn > profit: 289 | minn = profit 290 | sum_profit = sum_profit + profit 291 | ''' 292 | 293 | print('profit of 10 lots:',sum_profit*10000) 294 | print('Total number of trades:',s+c) 295 | print('sum of wining trades:',c) 296 | print('sum of loss trades:',s) 297 | print('max Down for 10 lots:',minn*10000) 298 | print('max up for 10 lots:',maxx*10000) 299 | 300 | 301 | #------------------------------------------------------------------------------ 302 | 303 | 304 | 305 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | -------------------------------------------------------------------------------- /Feature-Calculator/featuresc.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | import numpy as np 3 | from scipy import stats 4 | import scipy.optimize 5 | from scipy.optimize import OptimizeWarning 6 | import warnings 7 | import matplotlib.pyplot as plt 8 | from sklearn.linear_model import LinearRegression 9 | 10 | #------------------------------------------------------------------------------ 11 | class Holder: 12 | 1 13 | #------------------------------------------------------------------------------ 14 | 15 | def Heiken_Ashi(price,period): 16 | result = Holder() 17 | dict = {} 18 | 19 | HAclose = price[['open','high','low','close']].sum(axis=1)/4 20 | HAopen = HAclose.copy() 21 | HAopen.iloc[0] = HAclose.iloc[0] 22 | HAhigh = HAclose.copy() 23 | HAlow = HAclose.copy() 24 | 25 | for i in range(1,len(price)): 26 | HAopen.iloc[i] = (HAopen.iloc[i-1]+HAclose.iloc[i-1])/2 27 | HAhigh.iloc[i] = np.array([price.high.iloc[i],HAopen.iloc[i],HAclose.iloc[i]]).max() 28 | HAlow.iloc[i] = np.array([price.low.iloc[i],HAopen.iloc[i],HAclose.iloc[i]]).min() 29 | df = pd.concat((HAopen,HAhigh,HAlow,HAclose),axis=1) 30 | df.coulmns = ['open','high','low','close'] 31 | 32 | df.index = df.index.droplevel(0) 33 | 34 | dict[period[0]]=df 35 | result.candles=dict 36 | return result 37 | #------------------------------------------------------------------------------ 38 | def detrend(price,method='difference'): 39 | if method=='difference': 40 | detrended=price.close[1:]-price.close[:-1].values 41 | elif method=='linear': 42 | x = np.arange(0,len(price)) 43 | y = price.close.values 44 | model = LinearRegression() 45 | model.fit(x.reshape(-1,1),y.reshape(-1,1)) 46 | trend = model.predict(x.reshape(-1,1)) 47 | trend = trend.reshape((len(price),)) 48 | detrended = price.close - trend 49 | else: 50 | print("error trend") 51 | return detrended 52 | #------------------------------------------------------------------------------ 53 | def fseries(x,a0,a1,b1,w): 54 | f = a0 + a1 * np.cos(w * x) + b1 * np.sin(w * x) 55 | return f 56 | #------------------------------------------------------------------------------ 57 | def sseries(x,a0,b1,w): 58 | f = a0 + b1 * np.sin(w * x) 59 | return f 60 | #------------------------------------------------------------------------------ 61 | def fourier(price,period,method='difference'): 62 | results = Holder() 63 | dict = {} 64 | plot = False 65 | detrended = detrend(price,method) 66 | 67 | for i in range(0,len(period)): 68 | coeffs =[] 69 | for j in range(period[i],len(price)): 70 | x = np.arange(0,period[i]) 71 | y = detrended.iloc[j-period[i]:j] 72 | with warnings.catch_warnings(): 73 | warnings.simplefilter('error',OptimizeWarning) 74 | 75 | try: 76 | res = scipy.optimize.curve_fit(fseries,x,y) 77 | 78 | except (RuntimeError,OptimizeWarning): 79 | res = np.empty((1,4)) 80 | res[0,:] = np.NAN 81 | 82 | if plot == True: 83 | xt = np.linspace(0,period[i],100) 84 | yt = fseries(xt,res[0][0],res[0][1],res[0][2],res[0][3]) 85 | 86 | plt.plot(x,y) 87 | plt.plot(xt,yt,'r') 88 | 89 | plt.show() 90 | 91 | coeffs = np.append(coeffs,res[0],axis=0) 92 | 93 | warnings.filterwarnings('ignore',category=np.VisibleDeprecationWarning) 94 | 95 | coeffs = np.array(coeffs).reshape(((len(coeffs)//4,4))) 96 | df = pd.DataFrame(coeffs,index=price.iloc[period[i]:len(price)].index) 97 | df.columns = ['a0','a1','b1','w'] 98 | df = df.fillna(method='bfill') 99 | dict[period[i]] = df 100 | results.coeffs = dict 101 | return results 102 | #------------------------------------------------------------------------------ 103 | def sine(price,period,method='difference'): 104 | results = Holder() 105 | dict = {} 106 | plot = False 107 | detrended = detrend(price,method) 108 | 109 | for i in range(0,len(period)): 110 | coeffs =[] 111 | for j in range(period[i],len(price)): 112 | x = np.arange(0,period[i]) 113 | y = detrended.iloc[j-period[i]:j] 114 | with warnings.catch_warnings(): 115 | warnings.simplefilter('error',OptimizeWarning) 116 | 117 | try: 118 | res = scipy.optimize.curve_fit(sseries,x,y) 119 | 120 | except (RuntimeError,OptimizeWarning): 121 | res = np.empty((1,3)) 122 | res[0,:] = np.NAN 123 | 124 | if plot == True: 125 | xt = np.linspace(0,period[i],100) 126 | yt = sseries(xt,res[0][0],res[0][1],res[0][2]) 127 | 128 | plt.plot(x,y) 129 | plt.plot(xt,yt,'r') 130 | 131 | plt.show() 132 | 133 | coeffs = np.append(coeffs,res[0],axis=0) 134 | 135 | warnings.filterwarnings('ignore',category=np.VisibleDeprecationWarning) 136 | 137 | coeffs = np.array(coeffs).reshape(((len(coeffs)//3,3))) 138 | df = pd.DataFrame(coeffs,index=price.iloc[period[i]:len(price)].index) 139 | df.columns = ['a0','b1','w'] 140 | df = df.fillna(method='bfill') 141 | dict[period[i]]=df 142 | results.coeffs = dict 143 | return results 144 | #------------------------------------------------------------------------------ 145 | def wadl(price,period): 146 | 147 | results = Holder() 148 | dict = {} 149 | 150 | for i in range(0,len(period)): 151 | 152 | WAD = [] 153 | 154 | for j in range(period[i],len(price)): 155 | 156 | TRH = np.array([price.high.iloc[j],price.close.iloc[j-1]]).max() 157 | TRL = np.array([price.low.iloc[j],price.close.iloc[j-1]]).min() 158 | 159 | if price.close.iloc[j] > price.close.iloc[j-1]: 160 | PM = price.close.iloc[j]-TRL 161 | elif price.close.iloc[j] < price.close.iloc[j-1]: 162 | PM = price.close.iloc[j]-TRH 163 | elif price.close.iloc[j] == price.close.iloc[j-1]: 164 | PM = 0 165 | else: 166 | print("error in wadl") 167 | 168 | AD = PM * price.volume.iloc[j] 169 | WAD = np.append(WAD,AD) 170 | 171 | WAD = WAD.cumsum() 172 | WAD = pd.DataFrame(WAD,index=price.iloc[period[i]:len(price)].index) 173 | WAD.columns = ['close'] 174 | dict[period[i]] = WAD 175 | 176 | results.wadl = dict 177 | return results 178 | #------------------------------------------------------------------------------ 179 | 180 | def OHLCresample(DataFrame,TimeFrame,column='ask'): 181 | 182 | grouped = DataFrame.groupby('Symbol') 183 | 184 | if np.any(DataFrame.columns == 'Ask'): 185 | 186 | if column =='ask': 187 | ask = grouped['Ask'].resample(TimeFrame).ohlc() 188 | askVol = grouped['AskVol'].resample(TimeFrame).count() 189 | resampled = pd.DataFrame(ask) 190 | resampled['AskVol'] = askVol 191 | 192 | elif column =='bid': 193 | bid = grouped['Bid'].resample(TimeFrame).ohlc() 194 | bidVol = grouped['BidVol'].resample(TimeFrame).count() 195 | resampled = pd.DataFrame(bid) 196 | resampled['BidVol'] = bidVol 197 | 198 | else: 199 | raise ValueError('error OHLCresample') 200 | 201 | elif np.any(DataFrame.columns == 'close'): 202 | open = grouped['open'].resample(TimeFrame).ohlc() 203 | close = grouped['close'].resample(TimeFrame).ohlc() 204 | high = grouped['high'].resample(TimeFrame).ohlc() 205 | low = grouped['low'].resample(TimeFrame).ohlc() 206 | askVol = grouped['volume'].resample(TimeFrame).count() 207 | 208 | resampled = pd.DataFrame(open) 209 | resampled['high'] = high 210 | resampled['low'] = low 211 | resampled['close'] = close 212 | resampled['volume'] = askVol 213 | 214 | 215 | resampled = resampled.dropna() 216 | 217 | return resampled 218 | #------------------------------------------------------------------------------ 219 | def momentum(prices,periods): 220 | 221 | results = Holder() 222 | open = {} 223 | close = {} 224 | for i in range(0,len(periods)): 225 | 226 | open[periods[i]] = pd.DataFrame(prices.open.iloc[periods[i]:]-prices.open.iloc[:-periods[i]].values, index=prices.iloc[periods[i]:].index) 227 | close[periods[i]] = pd.DataFrame(prices.close.iloc[periods[i]:] - prices.close.iloc[:-periods[i]].values, index=prices.iloc[periods[i]:].index) 228 | 229 | open[periods[i]].columns = ['open'] 230 | close[periods[i]].columns = ['close'] 231 | 232 | results.open = open 233 | results.close = close 234 | 235 | return results 236 | 237 | #------------------------------------------------------------------------------ 238 | 239 | def stochastic(prices,periods): 240 | results = Holder() 241 | close = {} 242 | 243 | for i in range(0,len(periods)): 244 | Ks=[] 245 | for j in range(periods[i],len(prices)): 246 | C= prices.close.iloc[j] 247 | H = prices.high.iloc[j-periods[i]:j-1].max() 248 | L= prices.low.iloc[j-periods[i]:j-1].min() 249 | if H == L: 250 | K = 0 251 | else: 252 | K= 100*(C-L)/(H-L) 253 | Ks = np.append(Ks,K) 254 | 255 | df = pd.DataFrame(Ks,index =prices.iloc[periods[i]:len(prices)].index) 256 | df.columns = ['K'] 257 | df['D'] = df.K.rolling(3).mean() 258 | df = df.dropna() 259 | close[periods[i]] = df 260 | results.close = close 261 | 262 | return results 263 | 264 | #------------------------------------------------------------------------------ 265 | 266 | def williams(prices,periods): 267 | results = Holder() 268 | close = {} 269 | for i in range(0,len(periods)): 270 | Rs=[] 271 | for j in range(periods[i],len(prices)): 272 | 273 | C= prices.close.iloc[j] 274 | H = prices.high.iloc[j-periods[i]:j-1].max() 275 | L= prices.low.iloc[j-periods[i]:j-1].min() 276 | if H == L: 277 | R = 0 278 | else: 279 | R= 100*(H-C)/(H-L) 280 | Rs = np.append(Rs,R) 281 | 282 | df = pd.DataFrame(Rs,index = prices.iloc[periods[i]:len(prices)].index) 283 | df.columns = ['R'] 284 | df = df.dropna() 285 | close[periods[i]] = df 286 | 287 | results.close = close 288 | 289 | return results 290 | 291 | #------------------------------------------------------------------------------ 292 | 293 | def proc(prices,periods): 294 | results = Holder() 295 | proc = {} 296 | for i in range(0,len(periods)): 297 | proc[periods[i]] = pd.DataFrame((prices.close.iloc[periods[i]:] - prices.close.iloc[:-periods[i]].values)/prices.close.iloc[:-periods[i]].values) 298 | proc[periods[i]].columns = ['close'] 299 | 300 | results.proc = proc 301 | return results 302 | 303 | #------------------------------------------------------------------------------ 304 | 305 | def adosc(prices,periods): 306 | results = Holder() 307 | accdist = {} 308 | for i in range(0,len(periods)): 309 | AD=[] 310 | for j in range(periods[i],len(prices)): 311 | 312 | C = prices.close.iloc[j] 313 | H = prices.high.iloc[j-periods[i]:j-1].max() 314 | L = prices.low.iloc[j-periods[i]:j-1].min() 315 | V = prices.volume.iloc[j] 316 | if H == L: 317 | CLV = 0 318 | else: 319 | CLV= ((C-L)-(H-C))/(H-L) 320 | AD = np.append(AD,CLV*V) 321 | AD = AD.cumsum() 322 | AD = pd.DataFrame(AD,index =prices.iloc[periods[i]:len(prices)].index) 323 | AD.columns = ['AD'] 324 | accdist[periods[i]] = AD 325 | 326 | results.AD = accdist 327 | 328 | return results 329 | 330 | #------------------------------------------------------------------------------ 331 | 332 | def macd(prices,periods): 333 | 334 | results = Holder() 335 | 336 | EMA1 = prices.close.ewm(span=periods[0]).mean() 337 | EMA2 = prices.close.ewm( span=periods[1]).mean() 338 | 339 | MACD = pd.DataFrame(EMA1 - EMA2) 340 | MACD.columns = ['L'] 341 | 342 | sigMACD = MACD.rolling(3).mean() 343 | sigMACD.columns = ['SL'] 344 | 345 | results.line = MACD 346 | results.signal = sigMACD 347 | 348 | return results 349 | 350 | #------------------------------------------------------------------------------ 351 | 352 | def cci (prices,periods): 353 | results = Holder() 354 | CCI = {} 355 | 356 | for i in range(0,len(periods)): 357 | MA = prices.close.rolling(periods[i]).mean() 358 | std = prices.close.rolling(periods[i]).std() 359 | 360 | D = (prices.close-MA)/std 361 | CCI[periods[i]] = pd.DataFrame((prices.close-MA)/(0.015*D)) 362 | CCI[periods[i]].columns = ['close'] 363 | 364 | results.cci = CCI 365 | 366 | return results 367 | 368 | #------------------------------------------------------------------------------ 369 | 370 | def bollinger (prices,periods,deviations): 371 | results = Holder() 372 | boll = {} 373 | for i in range(0,len(periods)): 374 | mid = prices.close.rolling(periods[i]).mean() 375 | std = prices.close.rolling(periods[i]).std() 376 | 377 | upper = mid+deviations*std 378 | lower = mid-deviations*std 379 | 380 | df = pd.concat((upper,mid,lower),axis=1) 381 | df.columns = ['upper','mid','lower'] 382 | 383 | boll[periods[i]] = df 384 | 385 | results.bands = boll 386 | return results 387 | 388 | #------------------------------------------------------------------------------ 389 | 390 | def pavarage (prices,periods): 391 | results = Holder() 392 | avs = {} 393 | for i in range(0,len(periods)): 394 | avs[periods[i]] = pd.DataFrame(prices[['open','high','low','close']].rolling(periods[i]).mean()) 395 | 396 | results.avs = avs 397 | return results 398 | 399 | #------------------------------------------------------------------------------ 400 | 401 | def slopes (prices,periods): 402 | results = Holder() 403 | slope = {} 404 | 405 | for i in range(0,len(periods)): 406 | ms=[] 407 | for j in range(periods[i],len(prices)): 408 | y = prices.high.iloc[j-periods[i]:j].values 409 | x = np.arange(0,len(y)) 410 | 411 | res = stats.linregress(x,y=y) 412 | m = res.slope 413 | ms = np.append(ms,m) 414 | 415 | 416 | ms = pd.DataFrame(ms,index=prices.iloc[periods[i]:len(prices)].index) 417 | ms.columns = ['high'] 418 | slope[periods[i]] = ms 419 | 420 | 421 | results.slope = slope 422 | 423 | return results 424 | 425 | #------------------------------------------------------------------------------ 426 | 427 | def Market (prices,periods): 428 | results = Holder() 429 | slope = {} 430 | m=3 431 | prices.index = pd.to_datetime(prices.index) 432 | prices['B'] = prices.index.minute 433 | 434 | for i in range(0,len(periods)): 435 | ms=[] 436 | for j in range(periods[i],len(prices)-60): 437 | if prices.B.iloc[j]==0: 438 | if prices.open[j] < prices.close[j+60]: 439 | m=1 440 | elif prices.open[j] >= prices.close[j+60]: 441 | m=0 442 | ms.append(m) 443 | ms = pd.DataFrame(ms,index=prices.iloc[periods[i]:len(prices)-60].index) 444 | ms.columns = ['Market'] 445 | slope[periods[i]] = ms 446 | 447 | 448 | results.slope = slope 449 | 450 | return results 451 | 452 | #------------------------------------------------------------------------------ 453 | def resamble(prices): 454 | results = Holder() 455 | maxHigh=0 456 | maxLow=0 457 | msOpen=[] 458 | msHigh=[] 459 | msLow=[] 460 | msClose=[] 461 | msVolume=[] 462 | prices.index = pd.to_datetime(prices.index) 463 | prices['B'] = prices.index.minute 464 | Open=prices.open.iloc[0] 465 | High=prices.high.iloc[0] 466 | Low=prices.low.iloc[0] 467 | Close=prices.close.iloc[0] 468 | Volume=prices.volume.iloc[0] 469 | 470 | for i in range(0,len(prices)): 471 | x=0 472 | if prices.B.iloc[i]==0: 473 | Openn=prices.open.iloc[i] 474 | maxHigh=prices.open.iloc[i] 475 | maxLow=prices.open.iloc[i] 476 | Volume=prices.volume.iloc[i] 477 | x=1 478 | if maxHighprices.low.iloc[i]: 481 | maxLow=prices.low.iloc[i] 482 | 483 | Open = Openn 484 | High=maxHigh 485 | Low=maxLow 486 | Close=prices.close.iloc[i] 487 | if x!=1: 488 | Volume=Volume+prices.volume.iloc[i] 489 | 490 | msOpen=np.append(msOpen,Open) 491 | msHigh=np.append(msHigh,High) 492 | msLow=np.append(msLow,Low) 493 | msClose=np.append(msClose,Close) 494 | msVolume=np.append(msVolume,Volume) 495 | 496 | df1 = pd.DataFrame(msOpen,index=prices.iloc[0:len(prices)].index) 497 | df1.columns = ['open'] 498 | df2 = pd.DataFrame(msHigh,index=prices.iloc[0:len(prices)].index) 499 | df2.columns = ['high'] 500 | df3 = pd.DataFrame(msLow,index=prices.iloc[0:len(prices)].index) 501 | df3.columns = ['low'] 502 | df4 = pd.DataFrame(msClose,index=prices.iloc[0:len(prices)].index) 503 | df4.columns = ['close'] 504 | df5 = pd.DataFrame(msVolume,index=prices.iloc[0:len(prices)].index) 505 | df5.columns = ['volume'] 506 | result = pd.concat([df1, df2, df3, df4, df5], axis=1, sort=False) 507 | 508 | results = result 509 | 510 | return results 511 | 512 | #------------------------------------------------------------------------------ 513 | 514 | 515 | 516 | 517 | 518 | 519 | 520 | 521 | 522 | 523 | 524 | 525 | 526 | -------------------------------------------------------------------------------- /Connector/featuresc.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | import numpy as np 3 | from scipy import stats 4 | import scipy.optimize 5 | from scipy.optimize import OptimizeWarning 6 | import warnings 7 | import matplotlib.pyplot as plt 8 | from sklearn.linear_model import LinearRegression 9 | import sys 10 | 11 | #------------------------------------------------------------------------------ 12 | class Holder: 13 | 1 14 | #------------------------------------------------------------------------------ 15 | 16 | def Heiken_Ashi(price,period): 17 | result = Holder() 18 | dict = {} 19 | 20 | HAclose = price[['open','high','low','close']].sum(axis=1)/4 21 | HAopen = HAclose.copy() 22 | HAopen.iloc[0] = HAclose.iloc[0] 23 | HAhigh = HAclose.copy() 24 | HAlow = HAclose.copy() 25 | 26 | for i in range(1,len(price)): 27 | HAopen.iloc[i] = (HAopen.iloc[i-1]+HAclose.iloc[i-1])/2 28 | HAhigh.iloc[i] = np.array([price.high.iloc[i],HAopen.iloc[i],HAclose.iloc[i]]).max() 29 | HAlow.iloc[i] = np.array([price.low.iloc[i],HAopen.iloc[i],HAclose.iloc[i]]).min() 30 | df = pd.concat((HAopen,HAhigh,HAlow,HAclose),axis=1) 31 | df.coulmns = ['open','high','low','close'] 32 | 33 | df.index = df.index.droplevel(0) 34 | 35 | dict[period[0]]=df 36 | result.candles=dict 37 | return result 38 | #------------------------------------------------------------------------------ 39 | def detrend(price,method='difference'): 40 | if method=='difference': 41 | detrended=price.close[1:]-price.close[:-1].values 42 | elif method=='linear': 43 | x = np.arange(0,len(price)) 44 | y = price.close.values 45 | model = LinearRegression() 46 | model.fit(x.reshape(-1,1),y.reshape(-1,1)) 47 | trend = model.predict(x.reshape(-1,1)) 48 | trend = trend.reshape((len(price),)) 49 | detrended = price.close - trend 50 | else: 51 | print("error trend") 52 | return detrended 53 | #------------------------------------------------------------------------------ 54 | def fseries(x,a0,a1,b1,w): 55 | f = a0 + a1 * np.cos(w * x) + b1 * np.sin(w * x) 56 | return f 57 | #------------------------------------------------------------------------------ 58 | def sseries(x,a0,b1,w): 59 | f = a0 + b1 * np.sin(w * x) 60 | return f 61 | #------------------------------------------------------------------------------ 62 | def fourier(price,period,method='difference'): 63 | results = Holder() 64 | dict = {} 65 | plot = False 66 | detrended = detrend(price,method) 67 | 68 | for i in range(0,len(period)): 69 | coeffs =[] 70 | for j in range(period[i],len(price)): 71 | x = np.arange(0,period[i]) 72 | y = detrended.iloc[j-period[i]:j] 73 | with warnings.catch_warnings(): 74 | warnings.simplefilter('error',OptimizeWarning) 75 | 76 | try: 77 | res = scipy.optimize.curve_fit(fseries,x,y) 78 | 79 | except (RuntimeError,OptimizeWarning): 80 | res = np.empty((1,4)) 81 | res[0,:] = np.NAN 82 | 83 | if plot == True: 84 | xt = np.linspace(0,period[i],100) 85 | yt = fseries(xt,res[0][0],res[0][1],res[0][2],res[0][3]) 86 | 87 | plt.plot(x,y) 88 | plt.plot(xt,yt,'r') 89 | 90 | plt.show() 91 | 92 | coeffs = np.append(coeffs,res[0],axis=0) 93 | 94 | warnings.filterwarnings('ignore',category=np.VisibleDeprecationWarning) 95 | 96 | coeffs = np.array(coeffs).reshape(((len(coeffs)//4,4))) 97 | df = pd.DataFrame(coeffs,index=price.iloc[period[i]:len(price)].index) 98 | df.columns = ['a0','a1','b1','w'] 99 | df = df.fillna(method='bfill') 100 | dict[period[i]] = df 101 | results.coeffs = dict 102 | return results 103 | #------------------------------------------------------------------------------ 104 | def sine(price,period,method='difference'): 105 | results = Holder() 106 | dict = {} 107 | plot = False 108 | detrended = detrend(price,method) 109 | 110 | for i in range(0,len(period)): 111 | coeffs =[] 112 | for j in range(period[i],len(price)): 113 | x = np.arange(0,period[i]) 114 | y = detrended.iloc[j-period[i]:j] 115 | with warnings.catch_warnings(): 116 | warnings.simplefilter('error',OptimizeWarning) 117 | 118 | try: 119 | res = scipy.optimize.curve_fit(sseries,x,y) 120 | 121 | except (RuntimeError,OptimizeWarning): 122 | res = np.empty((1,3)) 123 | res[0,:] = np.NAN 124 | 125 | if plot == True: 126 | xt = np.linspace(0,period[i],100) 127 | yt = sseries(xt,res[0][0],res[0][1],res[0][2]) 128 | 129 | plt.plot(x,y) 130 | plt.plot(xt,yt,'r') 131 | 132 | plt.show() 133 | 134 | coeffs = np.append(coeffs,res[0],axis=0) 135 | 136 | warnings.filterwarnings('ignore',category=np.VisibleDeprecationWarning) 137 | 138 | coeffs = np.array(coeffs).reshape(((len(coeffs)//3,3))) 139 | df = pd.DataFrame(coeffs,index=price.iloc[period[i]:len(price)].index) 140 | df.columns = ['a0','b1','w'] 141 | df = df.fillna(method='bfill') 142 | dict[period[i]]=df 143 | results.coeffs = dict 144 | return results 145 | #------------------------------------------------------------------------------ 146 | def wadl(price,period): 147 | 148 | results = Holder() 149 | dict = {} 150 | 151 | for i in range(0,len(period)): 152 | 153 | WAD = [] 154 | 155 | for j in range(period[i],len(price)): 156 | 157 | TRH = np.array([price.high.iloc[j],price.close.iloc[j-1]]).max() 158 | TRL = np.array([price.low.iloc[j],price.close.iloc[j-1]]).min() 159 | 160 | if price.close.iloc[j] > price.close.iloc[j-1]: 161 | PM = price.close.iloc[j]-TRL 162 | elif price.close.iloc[j] < price.close.iloc[j-1]: 163 | PM = price.close.iloc[j]-TRH 164 | elif price.close.iloc[j] == price.close.iloc[j-1]: 165 | PM = 0 166 | else: 167 | print("error in wadl") 168 | 169 | AD = PM * price.volume.iloc[j] 170 | WAD = np.append(WAD,AD) 171 | 172 | WAD = WAD.cumsum() 173 | WAD = pd.DataFrame(WAD,index=price.iloc[period[i]:len(price)].index) 174 | WAD.columns = ['close'] 175 | dict[period[i]] = WAD 176 | 177 | results.wadl = dict 178 | return results 179 | #------------------------------------------------------------------------------ 180 | 181 | def OHLCresample(DataFrame,TimeFrame,column='ask'): 182 | 183 | grouped = DataFrame.groupby('Symbol') 184 | 185 | if np.any(DataFrame.columns == 'Ask'): 186 | 187 | if column =='ask': 188 | ask = grouped['Ask'].resample(TimeFrame).ohlc() 189 | askVol = grouped['AskVol'].resample(TimeFrame).count() 190 | resampled = pd.DataFrame(ask) 191 | resampled['AskVol'] = askVol 192 | 193 | elif column =='bid': 194 | bid = grouped['Bid'].resample(TimeFrame).ohlc() 195 | bidVol = grouped['BidVol'].resample(TimeFrame).count() 196 | resampled = pd.DataFrame(bid) 197 | resampled['BidVol'] = bidVol 198 | 199 | else: 200 | raise ValueError('error OHLCresample') 201 | 202 | elif np.any(DataFrame.columns == 'close'): 203 | open = grouped['open'].resample(TimeFrame).ohlc() 204 | close = grouped['close'].resample(TimeFrame).ohlc() 205 | high = grouped['high'].resample(TimeFrame).ohlc() 206 | low = grouped['low'].resample(TimeFrame).ohlc() 207 | askVol = grouped['volume'].resample(TimeFrame).count() 208 | 209 | resampled = pd.DataFrame(open) 210 | resampled['high'] = high 211 | resampled['low'] = low 212 | resampled['close'] = close 213 | resampled['volume'] = askVol 214 | 215 | 216 | resampled = resampled.dropna() 217 | 218 | return resampled 219 | #------------------------------------------------------------------------------ 220 | def momentum(prices,periods): 221 | 222 | results = Holder() 223 | open = {} 224 | close = {} 225 | for i in range(0,len(periods)): 226 | 227 | open[periods[i]] = pd.DataFrame(prices.open.iloc[periods[i]:]-prices.open.iloc[:-periods[i]].values, index=prices.iloc[periods[i]:].index) 228 | close[periods[i]] = pd.DataFrame(prices.close.iloc[periods[i]:] - prices.close.iloc[:-periods[i]].values, index=prices.iloc[periods[i]:].index) 229 | 230 | open[periods[i]].columns = ['open'] 231 | close[periods[i]].columns = ['close'] 232 | 233 | results.open = open 234 | results.close = close 235 | 236 | return results 237 | 238 | #------------------------------------------------------------------------------ 239 | 240 | def stochastic(prices,periods): 241 | results = Holder() 242 | close = {} 243 | 244 | for i in range(0,len(periods)): 245 | Ks=[] 246 | for j in range(periods[i],len(prices)): 247 | C= prices.close.iloc[j] 248 | H = prices.high.iloc[j-periods[i]:j-1].max() 249 | L= prices.low.iloc[j-periods[i]:j-1].min() 250 | if H == L: 251 | K = 0 252 | else: 253 | K= 100*(C-L)/(H-L) 254 | Ks = np.append(Ks,K) 255 | 256 | df = pd.DataFrame(Ks,index =prices.iloc[periods[i]:len(prices)].index) 257 | df.columns = ['K'] 258 | df['D'] = df.K.rolling(3).mean() 259 | df = df.dropna() 260 | close[periods[i]] = df 261 | results.close = close 262 | 263 | return results 264 | 265 | #------------------------------------------------------------------------------ 266 | 267 | def williams(prices,periods): 268 | results = Holder() 269 | close = {} 270 | for i in range(0,len(periods)): 271 | Rs=[] 272 | for j in range(periods[i],len(prices)): 273 | 274 | C= prices.close.iloc[j] 275 | H = prices.high.iloc[j-periods[i]:j-1].max() 276 | L= prices.low.iloc[j-periods[i]:j-1].min() 277 | if H == L: 278 | R = 0 279 | else: 280 | R= 100*(H-C)/(H-L) 281 | Rs = np.append(Rs,R) 282 | 283 | df = pd.DataFrame(Rs,index = prices.iloc[periods[i]:len(prices)].index) 284 | df.columns = ['R'] 285 | df = df.dropna() 286 | close[periods[i]] = df 287 | 288 | results.close = close 289 | 290 | return results 291 | 292 | #------------------------------------------------------------------------------ 293 | 294 | def proc(prices,periods): 295 | results = Holder() 296 | proc = {} 297 | for i in range(0,len(periods)): 298 | proc[periods[i]] = pd.DataFrame((prices.close.iloc[periods[i]:] - prices.close.iloc[:-periods[i]].values)/prices.close.iloc[:-periods[i]].values) 299 | proc[periods[i]].columns = ['close'] 300 | 301 | results.proc = proc 302 | return results 303 | 304 | #------------------------------------------------------------------------------ 305 | 306 | def adosc(prices,periods): 307 | results = Holder() 308 | accdist = {} 309 | for i in range(0,len(periods)): 310 | AD=[] 311 | for j in range(periods[i],len(prices)): 312 | 313 | C = prices.close.iloc[j] 314 | H = prices.high.iloc[j-periods[i]:j-1].max() 315 | L = prices.low.iloc[j-periods[i]:j-1].min() 316 | V = prices.volume.iloc[j] 317 | if H == L: 318 | CLV = 0 319 | else: 320 | CLV= ((C-L)-(H-C))/(H-L) 321 | AD = np.append(AD,CLV*V) 322 | AD = AD.cumsum() 323 | AD = pd.DataFrame(AD,index =prices.iloc[periods[i]:len(prices)].index) 324 | AD.columns = ['AD'] 325 | accdist[periods[i]] = AD 326 | 327 | results.AD = accdist 328 | 329 | return results 330 | 331 | #------------------------------------------------------------------------------ 332 | 333 | def macd(prices,periods): 334 | 335 | results = Holder() 336 | 337 | EMA1 = prices.close.ewm(span=periods[0]).mean() 338 | EMA2 = prices.close.ewm( span=periods[1]).mean() 339 | 340 | MACD = pd.DataFrame(EMA1 - EMA2) 341 | MACD.columns = ['L'] 342 | 343 | sigMACD = MACD.rolling(3).mean() 344 | sigMACD.columns = ['SL'] 345 | 346 | results.line = MACD 347 | results.signal = sigMACD 348 | 349 | return results 350 | 351 | #------------------------------------------------------------------------------ 352 | 353 | def cci (prices,periods): 354 | results = Holder() 355 | CCI = {} 356 | 357 | for i in range(0,len(periods)): 358 | MA = prices.close.rolling(periods[i]).mean() 359 | std = prices.close.rolling(periods[i]).std() 360 | 361 | D = (prices.close-MA)/std 362 | CCI[periods[i]] = pd.DataFrame((prices.close-MA)/(0.015*D)) 363 | CCI[periods[i]].columns = ['close'] 364 | 365 | results.cci = CCI 366 | 367 | return results 368 | 369 | #------------------------------------------------------------------------------ 370 | 371 | def bollinger (prices,periods,deviations): 372 | results = Holder() 373 | boll = {} 374 | for i in range(0,len(periods)): 375 | mid = prices.close.rolling(periods[i]).mean() 376 | std = prices.close.rolling(periods[i]).std() 377 | 378 | upper = mid+deviations*std 379 | lower = mid-deviations*std 380 | 381 | df = pd.concat((upper,mid,lower),axis=1) 382 | df.columns = ['upper','mid','lower'] 383 | 384 | boll[periods[i]] = df 385 | 386 | results.bands = boll 387 | return results 388 | 389 | #------------------------------------------------------------------------------ 390 | 391 | def pavarage (prices,periods): 392 | results = Holder() 393 | avs = {} 394 | for i in range(0,len(periods)): 395 | avs[periods[i]] = pd.DataFrame(prices[['open','high','low','close']].rolling(periods[i]).mean()) 396 | 397 | results.avs = avs 398 | return results 399 | 400 | #------------------------------------------------------------------------------ 401 | 402 | def slopes (prices,periods): 403 | results = Holder() 404 | slope = {} 405 | 406 | for i in range(0,len(periods)): 407 | ms=[] 408 | for j in range(periods[i],len(prices)): 409 | y = prices.high.iloc[j-periods[i]:j].values 410 | x = np.arange(0,len(y)) 411 | 412 | res = stats.linregress(x,y=y) 413 | m = res.slope 414 | ms = np.append(ms,m) 415 | 416 | 417 | ms = pd.DataFrame(ms,index=prices.iloc[periods[i]:len(prices)].index) 418 | ms.columns = ['high'] 419 | slope[periods[i]] = ms 420 | 421 | 422 | results.slope = slope 423 | 424 | return results 425 | 426 | #------------------------------------------------------------------------------ 427 | 428 | def Market (prices,periods): 429 | results = Holder() 430 | slope = {} 431 | m=3 432 | prices.index = pd.to_datetime(prices.index) 433 | prices['B'] = prices.index.minute 434 | 435 | for i in range(0,len(periods)): 436 | ms=[] 437 | for j in range(periods[i],len(prices)-60): 438 | if prices.B.iloc[j]==0: 439 | if prices.open[j] < prices.close[j+60]: 440 | m=1 441 | elif prices.open[j] >= prices.close[j+60]: 442 | m=0 443 | ms.append(m) 444 | ms = pd.DataFrame(ms,index=prices.iloc[periods[i]:len(prices)-60].index) 445 | ms.columns = ['Market'] 446 | slope[periods[i]] = ms 447 | 448 | 449 | results.slope = slope 450 | 451 | return results 452 | 453 | #------------------------------------------------------------------------------ 454 | def resamble(prices): 455 | results = Holder() 456 | maxHigh=0 457 | maxLow=0 458 | msOpen=[] 459 | msHigh=[] 460 | msLow=[] 461 | msClose=[] 462 | msVolume=[] 463 | prices.index = pd.to_datetime(prices.index) 464 | prices['B'] = prices.index.minute 465 | Open=prices.open.iloc[0] 466 | High=prices.high.iloc[0] 467 | Low=prices.low.iloc[0] 468 | Close=prices.close.iloc[0] 469 | Volume=prices.volume.iloc[0] 470 | 471 | for i in range(0,len(prices)): 472 | x=0 473 | if prices.B.iloc[i]==0: 474 | Openn=prices.open.iloc[i] 475 | maxHigh=prices.open.iloc[i] 476 | maxLow=prices.open.iloc[i] 477 | Volume=prices.volume.iloc[i] 478 | x=1 479 | if maxHighprices.low.iloc[i]: 482 | maxLow=prices.low.iloc[i] 483 | 484 | Open = Openn 485 | High=maxHigh 486 | Low=maxLow 487 | Close=prices.close.iloc[i] 488 | if x!=1: 489 | Volume=Volume+prices.volume.iloc[i] 490 | 491 | msOpen=np.append(msOpen,Open) 492 | msHigh=np.append(msHigh,High) 493 | msLow=np.append(msLow,Low) 494 | msClose=np.append(msClose,Close) 495 | msVolume=np.append(msVolume,Volume) 496 | 497 | df1 = pd.DataFrame(msOpen,index=prices.iloc[0:len(prices)].index) 498 | df1.columns = ['open'] 499 | df2 = pd.DataFrame(msHigh,index=prices.iloc[0:len(prices)].index) 500 | df2.columns = ['high'] 501 | df3 = pd.DataFrame(msLow,index=prices.iloc[0:len(prices)].index) 502 | df3.columns = ['low'] 503 | df4 = pd.DataFrame(msClose,index=prices.iloc[0:len(prices)].index) 504 | df4.columns = ['close'] 505 | df5 = pd.DataFrame(msVolume,index=prices.iloc[0:len(prices)].index) 506 | df5.columns = ['volume'] 507 | result = pd.concat([df1, df2, df3, df4, df5], axis=1, sort=False) 508 | 509 | results = result 510 | 511 | return results 512 | 513 | #------------------------------------------------------------------------------ 514 | 515 | 516 | 517 | 518 | 519 | 520 | 521 | 522 | 523 | 524 | 525 | 526 | 527 | --------------------------------------------------------------------------------