Exemplo n.º 1
0
    def __init__(self, ticker, time, live_data, do_trade):
        '''
        Constructor
        '''
        #self.GRANULARITY = (60,300,900,3600, 21600, 86400)
        self.GRANULARITY = [60]

        self.ticker = ticker
        self.live_data = live_data
        self.do_trade = do_trade

        # self.future_df = self.getFuture()
        # inialize future as empty dataframe but we might not need it
        self.future_df = pd.DataFrame()

        # we need to connect even if data is not live
        self.login = Login()
        self.trade_client = cbpro.AuthenticatedClient(self.login.key,
                                                      self.login.b64secret,
                                                      self.login.passphrase)

        if (live_data):

            self.virtual_time = datetime.datetime.utcnow().replace(
                microsecond=0, second=0)

        else:

            self.virtual_time = time
            # data is not live, get from file
            file = r'../data/coinbase_BTC_master.csv'
            self.file_data = FileDataReader(file)
Exemplo n.º 2
0
    def fit(self):
        '''
        Use features data file to fit all the data to this model
        '''
        features_file = r'../data/20151031-20190930_Features_150_3.csv'
        X = FileDataReader.getFeatures(features_file)

        #X.dropna(axis = 0, how ='any', inplace=True)

        #print ('features:', X.shape)

        #print (X.head())
        #print (X.tail())

        #print ('nan count:', X.isna().sum())

        target_file = r'../data/20151031-20190930_Targets.csv'

        y = FileDataReader.getTargets(target_file)

        #print ('targets', y.shape)
        #print (collections.Counter(y))

        #X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.20)
        # train on the whole dataset
        X_train = X
        y_train = y

        # we need to undersample the majority class (0)
        # merge the datasets
        # concatenate our training data back together
        merged_df = pd.concat([X_train, y_train], ignore_index=True, axis=1)

        #print (merged_df.shape)

        do_nothing = merged_df[merged_df[450] == 0]
        buys = merged_df[merged_df[450] == 1]
        sells = merged_df[merged_df[450] == 2]

        #print ('0:', do_nothing.shape)
        #print ('1:', buys.shape)
        #print ('2:', sells.shape)

        do_nothing_downsampled = resample(do_nothing,
                                          replace=False,
                                          n_samples=1200,
                                          random_state=13)

        # recombine

        downsampled = pd.concat([do_nothing_downsampled, buys, sells])

        #print (downsampled.shape)

        y_train = downsampled.iloc[:, -1]
        X_train = downsampled.drop(downsampled.columns[-1], axis=1)

        #print (y_train.shape)
        #print (X_train.shape)

        y_train = y_train.values.ravel()

        self.classifier.fit(X_train, y_train)
Exemplo n.º 3
0
@author: rich
'''

from data_loader import DataLoader
import datetime
from datetime import timedelta
from filedata_reader import FileDataReader

if __name__ == '__main__':

    # get hostory and future data from coinbase at starttime
    ticker = 'BTC-USD'
    start_time_str = '2018-10-31 00:00:00.000000'
    start_time = datetime.datetime.strptime(start_time_str,
                                            '%Y-%m-%d %H:%M:%S.%f')
    livetrade = False
    data = DataLoader(ticker, start_time, livetrade)

    end_time_str = '2018-10-31 04:30:00.000000'
    end_time = datetime.datetime.strptime(end_time_str, '%Y-%m-%d %H:%M:%S.%f')
    granularity = 60

    history_df = data.getHistoryRange(start_time, end_time, granularity)

    print('history', history_df.tail())

    filedata = FileDataReader()

    print(filedata.data.loc['2018-10-31'].head())
Exemplo n.º 4
0
import time
from datetime import datetime
from datetime import timedelta
from filedata_reader import FileDataReader
from data_loader import DataLoader
import pandas as pd

if __name__ == '__main__':
    '''
    Read in data from main data file, transform columns
    merge with 2019 data file, save as new csv 
    '''

    file = r'../../../../config/coinbaseUSD_1-min_data_2014-12-01_to_2019-01-09.csv'

    history = FileDataReader(file)

    # make the time the index
    history.data['time'] = pd.to_datetime(history.data['Timestamp'], unit='s')
    #history.data['time'] = pd.to_datetime(history.data['time'])

    history.data.set_index('time', inplace=True)

    history.data.drop(['Timestamp'], axis=1, inplace=True)

    history.data.drop(['Volume_(Currency)'], axis=1, inplace=True)

    history.data.drop(['Weighted_Price'], axis=1, inplace=True)

    history.data.rename(columns={
        'Open': 'open',
Exemplo n.º 5
0
from sklearn import svm
import pandas as pd
import collections
from sklearn.svm import SVC
from sklearn.utils import resample
#from datetime import timedelta
#from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report, confusion_matrix

from filedata_reader import FileDataReader
#from data_loader import DataLoader

if __name__ == '__main__':
    features_file = r'../data/20151031-20190930_Features_150_3.csv'
    X = FileDataReader.getFeatures(features_file)

    #X.dropna(axis = 0, how ='any', inplace=True)

    print('features:', X.shape)

    #print (X.head())
    #print (X.tail())

    #print ('nan count:', X.isna().sum())

    target_file = r'../data/20151031-20190930_Targets_Test.csv'

    y = FileDataReader.getTargets(target_file)

    print('targets', y.shape)
Exemplo n.º 6
0
from datetime import datetime
from datetime import timedelta
from filedata_reader import FileDataReader
from data_loader import DataLoader
import pandas as pd
from collections import OrderedDict

if __name__ == '__main__':
    '''
    Gather the data from the end of the datafile until current time
    and then update the current data file
    '''

    data_file = r'../data/coinbase_BTC_master.csv'

    filedata = FileDataReader(data_file)

    filedata.data = filedata.data.resample('60S').agg(
        OrderedDict([
            ('open', 'first'),
            ('high', 'max'),
            ('low', 'min'),
            ('close', 'last'),
            ('volume', 'sum'),
        ])).ffill()

    #print (filedata.data.index[-1])

    lastdate = filedata.data.index[-1]

    print('get data from', lastdate, 'to 300 minutes ago')
Exemplo n.º 7
0
            
    
    print (target_df.tail(10))
    
    print ('num sales:', num_sales)
    print ('compound_gain', compound_gain)
    print ('average gain', (gain_sum / num_sales))
    
    # then merge target and range_df
    new_df = pd.concat([range_df,target_df], axis=1)
    
    '''
    base_price = new_df['open'].iloc[0]

    # normalize open price by base price
    new_df['open'] = ((new_df['open'] - base_price) / base_price) * 100

    print ('base price is:', base_price)
    
    new_df['target'] = 0
    
    '''
    
    #print ('new_df:', new_df.tail(20))

    #plot2 = Plotter.showSinglePlot(new_df)
    
    output_file = r'../data/coinbase_BTC_targets.csv'
        
    FileDataReader.write_to_csv(new_df, output_file)
Exemplo n.º 8
0
@author: rjhumphrey
'''

from filedata_reader import FileDataReader
#from sklearn import svm
from data_loader import DataLoader
import datetime
from datetime import timedelta
import pandas as pd
from sklearn.preprocessing import StandardScaler

if __name__ == '__main__':
    
    file = r'../data/coinbase_BTC_targets.csv'
    file_data = FileDataReader(file)
    
    # file_data.data contains hourly OHLC data and 
    # target 0 - nothing, 1 - buy, 2 - sell
    
    # create a model and train
    #print (file_data.data.shape)
    
    hour_data = file_data.data
    
    # drop hour and target data before 02/01/2016?
    
    
    targets = hour_data.iloc[ :, -1:].copy()
    targets.reset_index(drop = True, inplace = True)