Example #1
0
    def fill_change(self, security):

        self.stocks.insert(security.__dict__)
        last_records = self.stocks.find({
            'symbol': security.symbol
        }).sort('date', -1).limit(gb.lag + 1)
        changed = ''
        if last_records.count(with_limit_and_skip=True) == gb.lag + 1:
            df = pd.DataFrame(list(last_records)[::-1])
            id = df.iloc[-1]['_id']
            del df['_id']
            df['close'] = df['close'].astype(float)
            df['m5_change'] = df['close'].pct_change()
            df['m10_change'] = df['close'].pct_change(periods=2)
            df['m15_change'] = df['close'].pct_change(periods=3)
            df['m20_change'] = df['close'].pct_change(periods=4)
            df['m60_change'] = df['close'].pct_change(periods=12)
            df['rolling_mean'] = df['m5_change'].rolling(
                window=gb.rolling_mean).mean()

            df = df.fillna(0)
            df['m5_change'] = df['m5_change'].astype(str)
            df['m10_change'] = df['m10_change'].astype(str)
            df['m15_change'] = df['m15_change'].astype(str)
            df['m20_change'] = df['m20_change'].astype(str)
            df['m60_change'] = df['m60_change'].astype(str)
            df['rolling_mean'] = df['rolling_mean'].astype(str)
            mydict = df.T.to_dict()[len(df) - 1]
            self.stocks.update({'_id': id}, {'$set': mydict})

            if gb.do_prediction:
                security = Security()
                security.to_class(**mydict)
                new_prediction = Prediction(security)
                new_prediction.update_prediction(self.predictions)
Example #2
0
 def __init__(self, address):
     self.address = address
     self.connected_clients = []
     self.loop = asyncio.get_event_loop()
     self.db = ControllerDB()
     self.rooms = {}
     self.sec = Security()
def handler(msg_head):
    decrypt = Security()
    handle = MongoDAO()
    rec_packet = pickle.loads(msg_head)
    metadata = decrypt.app_decrypt(rec_packet[1])

    if rec_packet[2] == 'key':
        pub_key = handle.get_publicKey(metadata[1][0])
        li = [pub_key, 'key']
        send_to_client(pickle.dump(li))

    elif rec_packet[2] == 'msg':
        active = False
        json = {"drop": msg_head, "status": "Not delivered"}
        handle.insert(metadata[1][0], json)

        if active:
            send_to_client(msg_head)

    elif rec_packet[2] == 'signup':
        exist = False
        exist = handle.if_user_exist(metadata[1][0])
        if exist:
            unique = ["User ID is Unique"]
            send_to_client(pickle.dump(unique))
        else:
            Error = unique = ["User ID already exists - ERROR"]
            send_to_client(pickle.dump(Error))
Example #4
0
def main():
    for i in range(p.Runs):

        from Models.Trias.Node import Node
        p.NODES = []
        for i in range(p.Nn):
            p.NODES.append(Node(id=i, hashPower=100 / p.Nn))

        clock = 0  # set clock to 0 at the start of the simulation
        if p.hasTrans:
            if p.Ttechnique == "Light":
                LT.create_transactions()  # generate pending transactions
            elif p.Ttechnique == "Full":
                FT.create_transactions()  # generate pending transactions

        Node.generate_gensis_block(
            p.NODES)  # generate the gensis block for all miners
        BlockCommit.generate_initial_events(
        )  # initiate initial events >= 1 to start with

        if p.attack:
            Security.generate_initial_events()

        while not Queue.isEmpty() and clock <= p.simTime:
            next_event = Queue.pop_event()
            clock = next_event.time  # move clock to the time of the event
            BlockCommit.handle_event(next_event)

        Output.output_to_xlsx("data_v5.xlsx")
        # Output.calculate()
        Output.reset()
Example #5
0
    def fill_all_changes(self, precords):

        records = [x.__dict__ for x in precords]
        df = pd.DataFrame(records)
        # print df

        df['m5_change'] = df['close'].astype(float).pct_change()
        df['m10_change'] = df['close'].astype(float).pct_change(periods=2)
        df['m15_change'] = df['close'].astype(float).pct_change(periods=3)
        df['m20_change'] = df['close'].astype(float).pct_change(periods=4)
        df['m60_change'] = df['close'].astype(float).pct_change(periods=12)
        df['rolling_mean'] = df['m5_change'].rolling(
            window=gb.rolling_mean).mean().astype(float)
        df = df.fillna(0)
        df['m5_change'] = df['m5_change'].astype(str)
        df['m10_change'] = df['m10_change'].astype(str)
        df['m15_change'] = df['m15_change'].astype(str)
        df['m20_change'] = df['m20_change'].astype(str)
        df['m60_change'] = df['m60_change'].astype(str)
        df['rolling_mean'] = df['rolling_mean'].astype(str)

        mydict = df.T.to_dict()
        df_len = len(mydict)
        mypredictions = [0] * df_len

        if gb.do_prediction:
            for i in range(0, df_len):
                security = Security()
                security.to_class(**mydict[i])
                new_prediction = Prediction(security)

                # if new_prediction.is_present(new_prediction,self.predictions):
                #     continue

                if i >= gb.lag:

                    mypredictions[i - gb.lag].next_closing = float(
                        new_prediction.current_closing)

                    if float(new_prediction.current_closing) >= float(
                            mypredictions[i - gb.lag].current_closing):
                        mypredictions[i - gb.lag].actual_movement = 1
                    else:
                        mypredictions[i - gb.lag].actual_movement = 0

                mypredictions[i] = new_prediction

                if np.mod(i, 500) == 0:
                    print i

        for d in range(0, df_len):
            self.stocks.insert(mydict[d])
            if gb.do_prediction:
                print "Inserting Predictions"
                self.predictions.insert(mypredictions[d].__dict__)
Example #6
0
    def get_last_record(self, symbol):

        last = self.stocks.find({'symbol': symbol}).sort('date', -1).limit(1)
        if last.count() > 0:
            last = last[0]
            last.pop('_id', None)
            Security_record = Security()
            Security_record.to_class(**last)
            return Security_record
        print "No Record found for : ", symbol
        return
Example #7
0
def generate_chart_windowed_rebalancing():
    parameters = {}
    parameters['primary'] = raw_input("Enter Stock 1: ")
    parameters['secondary'] = raw_input("Enter Stock 2: ")
    parameters['primary_leverage'] = float(raw_input("Stock 1 Leverage: "))
    parameters['primary_ratio'] = float(
        raw_input("Enter Stock 1 target ratio: "))
    parameters['rebalance_window'] = float(
        raw_input("Enter rebalancing window: "))

    securities = [
        Security(parameters['primary'],
                 leverage=parameters['primary_leverage']),
        parameters['secondary']
    ]
    portfolio = Portfolio(None)
    portfolio.generate_new_portfolio(securities)
    portfolio.set_investments(
        10, {
            parameters['primary']: parameters['primary_ratio'],
            parameters['secondary']: 1 - parameters['primary_ratio']
        })

    params = {}
    params['primary'] = parameters['primary']
    params['secondary'] = parameters['secondary']
    params['rebalance_window'] = parameters['rebalance_window']

    strategy = Strategies.Strategy(Strategies.windowed_rebalancing, params)
    portfolio = execute_simulation(portfolio, strategy)
    GraphingTools.plot_portfolio(portfolio, params)
Example #8
0
    def __init__(self, filename):
        with open(filename) as f:
            self.data = json.load(f)

        self.map = Map(self.data["map"])
        self.security = Security(self.data["security"])

        self.init_disk(self.data["disk"])
Example #9
0
def generate_chart_moving_average():
    parameters = {}
    parameters['primary'] = raw_input("Enter Stock 1: ")
    parameters['secondary'] = raw_input("Enter Stock 2: ")
    parameters['primary_leverage'] = float(raw_input("Stock 1 Leverage: "))
    parameters['moving_window'] = int(
        raw_input("Enter moving average window in days: "))
    parameters['sample'] = raw_input('Random Sampling? [True, False]: ')

    securities = [
        Security(parameters['primary'],
                 leverage=parameters['primary_leverage']),
        parameters['secondary']
    ]
    portfolio = Portfolio(None)
    portfolio.generate_new_portfolio(securities)
    portfolio.set_investments(0, {
        parameters['primary']: 1,
        parameters['secondary']: 0
    })
    portfolio.cash = 10000

    if parameters['sample'] == 'True':
        sampler = RandomSampler(
            portfolio.holdings[parameters['primary']].security)
        portfolio.holdings[
            parameters['primary']].security.data = sampler.sample

    moving_average = Security(parameters['primary'])
    moving_average_base = portfolio.holdings[parameters['primary']].security
    moving_average.set_data(
        parameters['primary'], portfolio.holdings[
            parameters['primary']].security.generate_moving_average(
                parameters['moving_window']))

    params = {}
    params['primary'] = parameters['primary']
    params['secondary'] = parameters['secondary']
    params['moving_average'] = moving_average
    params['moving_average_base'] = moving_average_base

    strategy = Strategies.Strategy(Strategies.moving_average_rebalancing,
                                   params)
    portfolio = execute_simulation(portfolio, strategy)
    GraphingTools.plot_portfolio(portfolio, params)
Example #10
0
    def generate_new_portfolio(self,
                               symbols,
                               align_data=True,
                               max_alignment=-1):
        securities = [Security(s) for s in symbols]

        if align_data:
            series = [s.data for s in securities]
            aligned_series = HelperFunctions.align_data(series, max_alignment)
            for c, s in enumerate(securities):
                s.set_data(s.symbol, aligned_series[c])

        self.holdings = dict([[s.symbol, Holding(s)] for s in securities])
        self.max_day = np.min(
            [np.size(self.holdings[s].security.data) for s in self.holdings])
        self.value = np.zeros(self.max_day)
Example #11
0
    def __init__(self):
        """Initialize LiveTrade"""

        self.portfolio = Portfolio()
        self.gekko = Gekko(self.portfolio)

        # Add ETHBTC as a security
        model = CNN()
        cnn_strat = CNN_Strategy(model=model,
                                 weights_path='model_weights/cnn_weights',
                                 image_path='strategies/images/')
        api_wrapper = BinanceWrapper(
            client_id=
            'nBjgb83VMNvqq45b3JdWUIsJDalWlXxHI2bvDz9oLdW7KgOLPvJCp30CHnthjfNJ',
            client_secret=
            '5bBN7s7h37kUvmGIpF9FTAtspBY93WirwhTh39PV7AlKSlUE2S4EEe9b3OZVYIqd')
        ethbtc = Security('ETHBTC',
                          '1m',
                          shares=0,
                          api_wrapper=api_wrapper,
                          strategy=cnn_strat)

        self.portfolio.add_security(ethbtc)
Example #12
0
class Server:

    def __init__(self, address):
        self.address = address
        self.connected_clients = []
        self.loop = asyncio.get_event_loop()
        self.db = ControllerDB()
        self.rooms = {}
        self.sec = Security()

    def run_server(self):
        start_server = websockets.serve(self.user_registration_handler, self.address, 8765)
        self.loop.run_until_complete(start_server)
        self.loop.run_forever()

    async def registration_handler(self, message, client):
        if message == "/register gus":
            client.set_registration()
        else:
            pass

    async def user_registration_handler(self, websocket, path):
        # main command parser fore relay websocket server
        client = Client(websocket)
        response = "Welcome to Gaucho-Relay-Chat, please login with the command '/login [username] [password]' or if you dont have an account register with '/register [username] [password]'"
        response = self.sec.message_outbound(response, self.sec.general_symm_key)
        await websocket.send(response)
        while True:
            # loop that handles user login/registration/authentication
            if client.logged_in is not True:
                # TODO: Encrypt outbound and decrypt inbound messages
                message = await websocket.recv()
                message = self.sec.message_inbound(message, self.sec.general_symm_key)
                # parse to acknowledge commands
                message = message.split(' ')
                username = message[1]
                # TODO: Catch error if not enough arguments or if invalid command
                password = message[2]
                # print(message)
                if message[0] == '/register':
                    if self.db.username_not_used(username):
                        #username not used so we can register him/her
                        self.db.register_user(username, password)
                        response = "Congratulations, you are now registered as " + username + ". Please login with your new account."
                        response = self.sec.message_outbound(response, self.sec.general_symm_key)
                        await websocket.send(response)
                    else:
                        #username in use, pick another
                        response = "Username is in use, pick another"
                        response = self.sec.message_outbound(response, self.sec.general_symm_key)
                        await websocket.send(response)

                elif message[0] == '/login':
                    if self.db.is_correct_password(username, password):
                        response = "You are now logged in as " + username
                        response = self.sec.message_outbound(response, self.sec.general_symm_key)

                        client.set_registration()
                        client.username = username
                        self.connected_clients.append(client)
                        await websocket.send(response)
                    else:
                        response = "Incorrect password, try again"
                        response = self.sec.message_outbound(response, self.sec.general_symm_key)
                        await websocket.send(response)
                else:
                    response = "You have entered an invalid command, please use /login or /register"
                    response = self.sec.message_outbound(response, self.sec.general_symm_key)
                    await websocket.send(response)

            else:
                # TODO: Encrypt outboynd and decrypt inbound and do message auth check 
                # user is logged in so we can begin the session
                message = await websocket.recv()
                message = self.sec.message_inbound(message, self.sec.general_symm_key)
                if message == "Message has been corrupted":
                    await websocket.send(message)
                else:

                    command = message.split(' ')

                    # now we check the first part of message to see what the command is
                    # first doing the channel creation

                    if command[0] == '/createchannel':
                        # create channel
                        self.rooms[command[1]] = []
                        word = "You have just created the channel: " + command[1] + ". To join use /join [channel_name]"
                        word = self.sec.message_outbound(word, self.sec.general_symm_key)
                        await websocket.send(word)
                    elif command[0] == "/join":

                        try:
                            self.rooms[command[1]].append(client)
                            if client.room == "General":
                                self.connected_clients.remove(client)
                            else:

                                self.rooms[client.room].remove(client)

                            client.set_channel(command[1])
                            wo = "Welcome to the " + command[1] + " channel!"
                            wo = self.sec.message_outbound(wo, self.sec.general_symm_key)
                            await websocket.send(wo)
                        except KeyError:
                            # The room does not exist
                            palabra = "The room you are trying to join does not exist, create it with /createChannel [channel_name]"
                            palabra = self.sec.message_outbound(palabra, self.sec.general_symm_key)
                            await websocket.send(palabra)
                    elif command[0] == '/list_users':
                        # lists current online users of the current room
                        # TODO: users who disconnect are still in list, need way to catch their disconnect remove existence
                        if client.room != 'General':
                            # TODO: change iteration to be on the room's users list
                            users = ""
                            for peer in self.rooms[client.room]:
                                users = users + " " + peer.username
                            y = "Current Online Users in Room: "+ users
                            y = self.sec.message_outbound(y, self.sec.general_symm_key)
                            await websocket.send(y)
                    elif command[0] == "/pm":
                        # initiates private message protocol
                        target_user = command[1]
                        peer_id = str(uuid.uuid4().int)
                        if client.room != 'General':
                            for peer in self.rooms[client.room]:
                                #print(peer.username)
                                if peer.username == target_user:
                                    print("helllloooo")
                                    x = "pm-request" + " " + peer_id + " " + client.username + " is trying to PRIVATE MESSAGE YOU. Click confirm to accept"
                                    x = self.sec.message_outbound(x, self.sec.general_symm_key)
                                    await peer.socket.send(x)
                    elif command[0] == "/accept":
                        target_user = command[1]
                        id_to_connect_to = command[2]
                        peer_id = str(uuid.uuid4().int)
                        if client.room != 'General':
                            for peer in self.rooms[client.room]:
                                if peer.username == target_user:
                                    y = "pm-acceptance" + " " + id_to_connect_to + " " + client.username + " has accepted your request for direct message"
                                    y = self.sec.message_outbound(y, self.sec.general_symm_key)
                                    await peer.socket.send(y)
                    else:
                        # relays message to everyone connected to a given room
                        print(client.username + ": " + message)
                        if client.room == "General":
                            for peer in self.connected_clients:
                                print(peer.username)
                                x = client.username + ": " + message
                                x = self.sec.message_outbound(x, self.sec.general_symm_key)
                                await peer.socket.send(x)
                        else:
                            for peer in self.rooms[client.room]:
                                print(peer.username)
                                x = client.username + ": " + message
                                x = self.sec.message_outbound(x, self.sec.general_symm_key)
                                await  peer.socket.send(x)
Example #13
0
 def AddEquity(self, ticker, resolution=None, extendedMarketHours=False):
     if resolution is None: resolution = Resolution.Minute
     self.Securities[ticker] = Security(ticker, resolution,
                                        extendedMarketHours)
Example #14
0
    def fill_data_file(self,
                       filename,
                       targetZone,
                       sourceZone='Europe/Warsaw',
                       selection=1):
        f = open(filename)
        records = f.readlines()
        records = records[1:len(records)]
        li = list()
        for line in records:
            values = line.split(",")
            new = Security()
            new.name = self.stock.name
            new.symbol = self.stock.symbol
            new.zone = self.stock.zone
            date = values[0]
            time = values[1]

            new.open = float(values[2])
            new.high = float(values[3])
            new.low = float(values[4])
            new.close = float(values[5])
            volume = re.findall("\d+", values[6])
            new.volume = volume[0]

            if selection == 1:
                dt = pytz.timezone(sourceZone).localize(
                    datetime2.datetime.strptime(
                        date + " " + time, '%Y-%m-%d %H:%M:%S')).astimezone(
                            pytz.timezone(targetZone))
            elif selection == 2:
                dt = pytz.timezone(sourceZone).localize(
                    datetime2.datetime.strptime(date + " " + time,
                                                '%d/%m/%y %H:%M')).astimezone(
                                                    pytz.timezone(targetZone))

            dt = dt.replace(tzinfo=None)
            dt = dt.strftime('%Y-%m-%d %H:%M')
            new.date = dt
            li.append(new)

        return li
Example #15
0
        print "New Thread Started for ", self.stock.symbol

        if self.from_file:
            self.fill_from_file()

        if self.from_cloud:
            self.fill_from_cloud()

        if self.max_from_google:
            self.get_max_from_google()

        if self.getLive:
            self.get_live_from_google()


for s in gb.Stocks:

    stock = Security()
    stock.symbol = s['symbol']
    stock.name = s['name']
    stock.zone = s['zone']
    stockthread = ScrapThread(stock)
    stockthread.selection = s['selection']
    stockthread.data_file = s['data_file']
    stockthread.source_zone = s['source_zone']
    stockthread.getLive = s['getLive']
    stockthread.from_file = s['from_file']
    stockthread.from_cloud = s['from_cloud']
    stockthread.max_from_google = s['max_from_google']
    stockthread.max_days = s['max_days']
    stockthread.start()
Example #16
0
def generate_dataset(parameters, func):
    if func.__name__ == Strategies.moving_average_rebalancing.__name__:
        securities = [
            Security(parameters['primary'],
                     leverage=parameters['primary_leverage']),
            parameters['secondary']
        ]
        portfolio = Portfolio(None)
        portfolio.generate_new_portfolio(securities)
        portfolio.set_investments(0, {
            parameters['primary']: 1,
            parameters['secondary']: 0
        })
        portfolio.cash = 10000

        if parameters['leveraged_moving_average'] in [True, 'True']:
            moving_average = Security(parameters['primary'],
                                      leverage=parameters['primary_leverage'])
            moving_average_base = Security(
                parameters['primary'], leverage=parameters['primary_leverage'])
        else:
            moving_average = Security(parameters['primary'])
            moving_average_base = Security(parameters['primary'])

        moving_average.set_data(
            parameters['primary'],
            moving_average.generate_moving_average(
                parameters['moving_window']))

        params = {}
        params['primary'] = parameters['primary']
        params['secondary'] = parameters['secondary']
        params['moving_average'] = moving_average
        params['moving_average_base'] = moving_average_base

        strategy = Strategies.Strategy(parameters['rebalancing_function'],
                                       params)
        return portfolio, strategy
    elif func.__name__ == Strategies.windowed_rebalancing.__name__:
        securities = [
            Security(parameters['primary'],
                     leverage=parameters['primary_leverage']),
            parameters['secondary']
        ]
        portfolio = Portfolio(None)
        portfolio.generate_new_portfolio(securities)
        portfolio.set_investments(
            10, {
                parameters['primary']: parameters['primary_ratio'],
                parameters['secondary']: 1 - parameters['primary_ratio']
            })

        params = {}
        params['primary'] = parameters['primary']
        params['secondary'] = parameters['secondary']
        params['rebalance_window'] = parameters['rebalance_window']

        strategy = Strategies.Strategy(parameters['rebalancing_function'],
                                       params)
        return portfolio, strategy
    else:
        raise Exception('Invalid Trading Strategy')