Exemple #1
0
    def fastPath(self, target_0, record):
        row, col = target_0
        logging.debug(
            'GridOperator::fastPath() check row: {} col: {}'.format(*target_0))
        #element = self.grid.itemAtPosition(*target).widget()
        #[row][column] = (function, config, self_sync)
        #function, config, self_sync = self.grid[row][col]
        function, self_sync = self.grid[row][col]
        logging.debug('GridOperator::fastPath() function: {}'.format(function))
        logging.debug('GridOperator::fastPath() isinstance ExecRB: {}'.format(
            isinstance(function, ExecRBFunction)))
        logging.debug(
            'GridOperator::fastPath() isinstance ExecRB#####: {} '.format(
                str(type(function))))
        logging.debug('GridOperator::fastPath() isinstance ExecR: {}'.format(
            isinstance(function, ExecRFunction)))

        if str(
                type(function)
        ) == "<class 'Pythonic.elements.basicelements_func.ExecRBFunction'>":  # jump to the next target
            # record_1 -> record_0 when goNext() is called recursively
            # returning only target_0 and record_0
            new_rec = Record((row, col - 1), (row + 1, col), record)
            return new_rec
        elif str(
                type(function)
        ) == "<class 'Pythonic.elements.basicelements_func.ExecRFunction'>":  # jump to the next target
            #hier testen ob target fings
            # record_1 -> record_0 when goNext() is called recursively
            # returning only target_0 and record_0
            new_rec = Record((row, col - 1), (row, col + 1), record)
            return new_rec
        else:
            return None
Exemple #2
0
    def execute(self, record):

        grid, wrk_selecctor_index, wrk_pos, log_state = self.config
        target_0 = (grid, wrk_pos[0], wrk_pos[1])
        log_txt = '{{BASIC RETURN}} Grid {} - Pos {}|{}'.format(grid, wrk_pos[0], alphabet[wrk_pos[1]])
        result = Record(self.getPos(), target_0, record, log=log_state, log_txt=log_txt)
        return result
Exemple #3
0
    def execute(self, record):

        log_state, code_input = self.config

        proc_dict = {
            'record': record,
            'input': None,
            'output': None,
            'log_txt': ""
        }

        exec_string = 'input = record\r\n'
        exec_string += 'output = record\r\n'

        #logging.warning('Exec-String:\r\n{}'.format(exec_string))

        if code_input:
            #logging.warning('Appending user specific code')
            exec_string += code_input

        exec(exec_string, proc_dict)

        log_txt = '{{BASIC OPERATION}}        {}'.format(proc_dict['log_txt'])

        output = proc_dict['output']

        result = Record(self.getPos(), (self.row + 1, self.column),
                        output,
                        log=log_state,
                        log_txt=log_txt)
        return result
Exemple #4
0
    def execute(self, record):

        log_state, code_input, custom_edit_state, cmd = self.config

        proc_dict = {
            'record': record,
            'input': None,
            'output': None,
            'log_txt': None
        }

        exec_string = 'input = record\r\n'
        exec_string += 'output = record\r\n'

        if code_input:
            exec_string += code_input

        exec(exec_string, proc_dict)

        output = proc_dict['output']
        log_txt = proc_dict['log_txt']
        if log_txt:
            log_txt = '{{BASIC OPERATION}}        {}'.format(
                proc_dict['log_txt'])
        else:
            log_txt = '{{BASIC OPERATION}}        {}'.format(
                proc_dict['output'])

        result = Record(self.getPos(), (self.row + 1, self.column),
                        output,
                        log=log_state,
                        log_txt=log_txt)

        return result
Exemple #5
0
    def fastPath(self, target, record):

        logging.debug('GridOperator::fastPath() check row: {} col: {}'.format(*target))
        element = self.grid.itemAtPosition(*target).widget()

        if isinstance(element, ExecRB): # jump to the next target
            # record_1 -> record_0 when goNext() is called recursively
            # returning only target_0 and record_0
            new_rec = Record(element.getPos(), (element.row+1, element.column), record)
            return new_rec
        elif isinstance(element, ExecR): # jump to the next target
            #hier testen ob target fings
            # record_1 -> record_0 when goNext() is called recursively
            # returning only target_0 and record_0
            new_rec = Record(element.getPos(), (element.row, element.column+1), record)
            return new_rec
        else:
            return None
Exemple #6
0
    def execute(self, record):

        if self.config:
            compare_with, operation, op_index, negate, log_state = self.config
        else:
            result = Record(self.getPos(), None, record)
            return result

        exec_scope = {}
        exec_string = 'result = True if '

        if negate:
            exec_string += 'not '

        if isinstance(record, str):
            exec_string += '"{}" {} {} '.format(record, operation,
                                                compare_with)
        else:
            exec_string += '{} {} {} '.format(record, operation, compare_with)

        exec_string += 'else False'

        exec(exec_string, exec_scope)

        logging.info('exec_string: {}'.format(exec_string))

        if exec_scope['result']:
            target = (self.row + 1, self.column)
            log_txt = '{{BASIC BRANCH}}           >>TRUE<< go to {}|{}'.format(
                self.row + 1, alphabet[self.column])
        else:
            target = (self.row, self.column + 1)
            log_txt = '{{BASIC BRANCH}}           <<FALSE>> go to {}|{}'.format(
                self.row, alphabet[self.column + 1])

        result = Record(self.getPos(),
                        target,
                        record,
                        log=log_state,
                        log_txt=log_txt)
        return result
    def execute(self, record):

        scale_option, scale_mean, scale_std, predict_val, filename, rel_path, log_state = self.config
        b_open_succeeded = True

        if filename:
            if rel_path:
                filename = os.path.join(os.environ['HOME'], filename)

            try:
                with open(filename, 'rb') as f:
                    clf = pickle.load(f)
            except Exception as e:
                # not writeable?
                log_txt = '{SVM Predict}            Error opening model'
                record = None
                b_open_succeeded = False
        else:
            b_open_succeeded = False
            log_txt = '{SVM Predict}            No model file specified'

        if b_open_succeeded:
            if isinstance(record, (list, tuple, pd.DataFrame)):
                # scaling option only here available
                if not isinstance(record, pd.DataFrame):
                    record = pd.DataFrame(record)

                record = preprocessing.scale(record, with_mean=scale_mean, with_std=scale_std)
                record = clf.predict(record)
            elif record:
                # when only one value is passed
                predict_val = False
                record = pd.DataFrame([record])
                record = clf.predict([record])
            else:
                log_txt = '{SVM Predict}            No input specified'

                
            if predict_val:
                # predict only last value
                record = pd.DataFrame([record[-1]])
                record = clf.predict(record)

            log_txt = '{{SVM Predict}}            Predicted {} value'.format(len(record))

        result = Record(self.getPos(), (self.row +1, self.column), record,
                 log=log_state, log_txt=log_txt)
        
        return result
Exemple #8
0
    def execute(self, record):

        log_state, code_input, custom_edit_state, cmd = self.config

        proc_dict = {
            'record': record,
            'callback': self.callback,
            'pos': self.getPos,
            'input': None,
            'output': None,
            'log_txt': None
        }

        exec_string = 'input = record\r\n'
        exec_string += 'output = record\r\n'

        if code_input:
            exec_string += code_input

        exec(exec_string, proc_dict)
        """
        n_cnt = 0

        while True:
            n_cnt += 1
            # switch grid
            #ret_data = PipeRecord(self.getPos(), (1, self.row+1, self.column), n_cnt)
            my_text = 'Hello log'
            ret_data = PipeRecord(self.getPos(), (self.row+1, self.column), n_cnt, True, my_text)
            self.callback(ret_data)
            time.sleep(2)
        """

        output = proc_dict['output']
        log_txt = proc_dict['log_txt']
        if log_txt:
            log_txt = '{{BASIC OPERATION}}        {}'.format(
                proc_dict['log_txt'])
        else:
            log_txt = '{{BASIC OPERATION}}        {}'.format(
                proc_dict['output'])

        result = Record(self.getPos(), (self.row + 1, self.column),
                        output,
                        log=log_state,
                        log_txt=log_txt)

        return result
Exemple #9
0
    def execute(self, record):

        # pass_input, url, log_state
        pass_input, url, log_state = self.config

        if pass_input:
            recv_string = requests.get(str(record))
        else:
            recv_string = requests.get(url)

        record = json.loads(recv_string.text)

        log_txt = '{{REST (GET)}}             {} bytes received'.format(
            getsizeof(recv_string.text))

        result = Record(self.getPos(), (self.row + 1, self.column),
                        record,
                        log=log_state,
                        log_txt=log_txt)

        return result
Exemple #10
0
    def execute(self, record):

        scale_option, scale_mean, scale_std, train_eval, decision_function, \
                gamma_mode, gamma_value, filename, rel_path, log_state = self.config

        # expect a tuple (Xdata, Ylabels) as input
        X, Y = record

        if scale_option == 1:
            # X, axis, with_mean, with_std, copy
            X = preprocessing.scale(X, 0, scale_mean, scale_std, False)

        if train_eval == 0:  # 90/10
            test_share = 0.1
        elif train_eval == 1:  # 80/20
            test_share = 0.2
        elif train_eval == 2:  # 70/30
            test_share = 0.3
        elif train_eval == 3:  # 60/40
            test_share = 0.4
        else:  # 50/50
            test_share = 0.5

        X_train, X_test, Y_train, Y_test = train_test_split(
            X, Y, test_size=test_share)

        if decision_function == 0:  # one vs. one
            dec_func_shape = 'ovo'
        else:  # one vs. rest
            dec_func_shape = 'ovr'

        if gamma_mode == 0:  # auto
            gamma_arg = 'auto'
        elif gamma_mode == 1:  #scaled
            gamma_arg = 'scaled'
        else:  # manual
            gamma_arg = gamma_value

        clf = svm.SVC(decision_function_shape='ovr', gamma=gamma_arg)
        clf.fit(X_train, Y_train)

        Y_predicted = clf.predict(X_test)

        tp = 0
        tn = 0
        fp = 0
        fn = 0

        for idx, Y_pre in enumerate(Y_predicted):
            if Y_pre == Y_test[idx]:  # true positives or true negatives

                if Y_test[idx] != 0:  # true positive
                    tp += 1
                else:  #true negative
                    tn += 1

            else:  #false positives or false negatives

                if Y_test[idx] != 0:  # false positive
                    fp += 1
                else:  # false negative
                    fn += 1

        log_txt = '{SVM}                    Successful trained'

        if filename:
            if rel_path:
                filename = os.path.join(os.environ['HOME'], filename)

            try:
                with open(filename, 'wb') as f:
                    pickle.dump(clf, f)
            except Exception as e:
                # not writeable?
                log_txt = '{SVM}                    Successful trained - Error writing model to HDD'

        record = {'tp': tp, 'tn': tn, 'fp': fp, 'fn': fn}

        result = Record(self.getPos(), (self.row + 1, self.column),
                        record,
                        log=log_state,
                        log_txt=log_txt)

        return result
Exemple #11
0
    def execute(self, record):

        # recipient, sender, password, server_url, server_port, subject
        # input_opt_index, input_opt_data, filename, pass_input, message_state, message_txt, log_state

        recipient, sender, password, server_url, server_port, subject, \
                input_opt_index, input_opt_data, filename, pass_input, message_state, \
                message_txt, log_state = self.config

        if input_opt_index == 1:  # Use input as message txt
            if message_state:  # In case there is already a message, append input
                message_txt += '\n\n'
                message_txt += str(record)
            else:
                message_state = True
                message_txt = str(record)

        if isinstance(record, dict):  # Dictionary has always priority
            if 'subject' in record:
                subject = record['subject']
            if 'message' in record:
                message_state = True
                message_txt = record['message']

        rcp_list = recipient.split(' ')

        # Message constructor
        msg = EmailMessage()
        msg['Subject'] = subject
        msg['From'] = sender
        msg['To'] = ', '.join(rcp_list)
        msg.set_default_type('text/plain')
        if message_state:
            msg.set_content(message_txt)

        # Attachment

        if input_opt_index == 2:  # Attach input object as string
            if not filename:
                filename = 'filename.txt'
            msg.add_attachment(str(record), 'text/plain', filename=filename)

        if input_opt_index == 3:  # Attach input object as binary
            attachement = pickle.dumps(record)
            if not filename:
                filename = 'filename.txt'
            msg.add_attachment(attachement,
                               maintype='application',
                               subtype='octet-stream',
                               filename='filename.bin')

        context = ssl.create_default_context()

        with smtplib.SMTP_SSL(server_url, server_port,
                              context=context) as server:
            server.login(sender, password)
            server.send_message(msg)

        if not pass_input:
            record = None

        log_txt = '{Message send succesfull}'
        log_output = '{} bytes send'.format(getsizeof(msg.__str__()))
        result = Record(self.getPos(), (self.row + 1, self.column),
                        record,
                        log=log_state,
                        log_txt=log_txt,
                        log_output=log_output)

        return result
Exemple #12
0
 def execute(self, record):
     #record = 'Hello from ProcessElement {}'.format((self.row, self.column))
     target_0 = (self.row + 1, self.column)
     target_1 = (self.row, self.column + 1)
     result = Record(self.getPos(), target_0, record, target_1, record)
     return result
Exemple #13
0
    def execute(self, record):

        interval_str, interval_index, offset, log_state = self.config

        if isinstance(record, tuple) and isinstance(record[0],
                                                    datetime.datetime):

            while record[0] > datetime.datetime.now():
                sleep(1)

            record = record[1]
            target = (self.row + 1, self.column)
            log_txt = '{BINANCE SCHEDULER}      >>>EXECUTE<<<'

            result = Record(self.getPos(),
                            target,
                            record,
                            log=log_state,
                            log_txt=log_txt)

        else:

            client = Client('', '')

            try:
                binance_time = client.get_server_time()
            except Exception as e:
                log_txt = '{{BINANCE SCHEDULER}}      Exception caught: {}'.format(
                    str(e))
                result = Record(self.getPos(),
                                None,
                                None,
                                log=True,
                                log_txt=log_txt)

            binance_time = binance_time['serverTime']
            binance_time /= 1000
            binance_timestamp = datetime.datetime.fromtimestamp(binance_time)

            offset = datetime.timedelta(seconds=offset)

            ohlc_step = datetime.timedelta(minutes=ohlc_steps[interval_str])

            date = datetime.datetime.now().date()
            # 00:00 o'clock for the actual date
            sync_time = datetime.datetime(date.year, date.month, date.day)

            # while loop leaves when the next ohlc_step target time is found
            while sync_time < binance_timestamp:
                sync_time += ohlc_step

            sync_time += offset
            countdown = sync_time - datetime.datetime.now()

            target = self.getPos()
            record = (sync_time, record)
            hours = countdown.seconds // 3600
            minutes = (countdown.seconds // 60) % 60
            seconds = countdown.seconds % 60
            log_txt = '{{BINANCE SCHEDULER}}      Synchronization successful, '\
                    'execution starts in {:02}:{:02}:{:02}'.format(hours, minutes, seconds)

            result = Record(self.getPos(),
                            target,
                            record,
                            log=log_state,
                            log_txt=log_txt)

        return result
Exemple #14
0
    def raiseExcpetion(self, e):

        # Bug, method is not called sometimes
        logging.error('Executor::raiseExcpetion() 2. Exception caught!!!')
        exceptRecord = Record(self.element.getPos(), None, e)
        self.signals.finished.emit(exceptRecord)
Exemple #15
0
 def execute(self, record):
     result = Record(self.getPos(), None, record)
     return result
Exemple #16
0
    def execute(self, record):

        # filename, read_mode, write_mode, b_array_limits, n_array_limits, log_state
        filename, read_mode, write_mode, delete_read, b_array_limits, \
                n_array_limits, log_state = self.config

        if not filename:
            raise OSError(QC.translate('', 'Filename not specified'))

        if not n_array_limits:
            n_array_limits = 20

        debug_text = ''

        try:
            # if the file already exists
            f = open(filename, 'rb+')
        except Exception as e:
            try:
                # create new file
                f = open(filename, 'wb+')
            except Exception as e:
                # not writeable?
                return e

        try:
            # latin 1 for numpy arrays
            stack = pickle.load(f)
            debug_text = 'Pickle loaded'
        except Exception as e:
            # create new array
            #return e
            debug_text = 'pickle not loaded'
            stack = []

        ##### WRITING #####

        #if write_mode == 0: # Nothing
        #record = 'Nothing'
        if write_mode == 1:  # Insert
            #record = 'Insert'
            stack.insert(0, record)
        elif write_mode == 2:  # Append
            #record = 'Append'
            stack.append(record)

        ### CHECK FOR MAXIMUM LIST SIZE
        if b_array_limits:
            while len(stack
                      ) > n_array_limits:  #delete more elements if necessary
                if write_mode == 1:  # delete last elements
                    stack.pop()
                if write_mode == 2:  # delete first elements
                    stack.pop(0)

        ##### READING #####
        f.seek(os.SEEK_SET)  # go back to the start of the stream

        if read_mode == 0:  # Nothing
            #record += ' Nothing'
            record = None

        #elif read_mode == 1: # Pass through
        # record = record
        elif read_mode == 2:  # First Out
            if delete_read:
                record = stack.pop(0)
            else:
                record = stack[0]
        elif read_mode == 3:  # Last out
            if delete_read:
                record = stack.pop()
            else:
                record = stack[-1]
        elif read_mode == 4:  # All out
            record = stack.copy()
            if delete_read:
                stack.clear()

        pickle.dump(stack, f)
        f.close()

        log_txt = '{BASIC STACK}            '
        result = Record(self.getPos(), (self.row + 1, self.column),
                        record,
                        log=log_state,
                        log_txt=log_txt)
        return result
Exemple #17
0
 def execute(self, record):
     result = Record(self.getPos(), (self.row, self.column + 1), record)
     return result
Exemple #18
0
    def execute(self, record):

        # exchange, api_key, sec_key, method, params, log_state
        current_exchange, api_key, sec_key, \
            current_method, params, log_state = self.config

        exchange = getattr(ccxt, current_exchange)()
        exchange.apiKey = api_key
        exchange.secret = sec_key

        method = getattr(exchange, current_method)

        method_args = []

        for key in params:
            if key == 'args':
                varArgs = params['args']
                for varKey in varArgs:

                    # first check if argument can be converted to int
                    try:
                        method_args.append(int(varArgs[varKey]))
                        continue
                    except Exception:
                        pass

                    # second check if value can be converted to float
                    try:
                        method_args.append(float(varArgs[varKey]))
                        continue
                    except Exception:
                        pass

                    method_args.append(varArgs[varKey])

            else:
                param_value = params[key]

                # first check if argument can be converted to int
                try:
                    method_args.append(int(param_value))
                    continue
                except Exception:
                    pass

                # second check if value can be converted to float
                try:
                    method_args.append(float(param_value))
                    continue
                except Exception:
                    pass

                # append argument as it is (string)
                method_args.append(params[key])

        res = method(*method_args)

        log_txt = '{{CCXT}}                   {}::{} CALLED'.format(
            current_exchange, current_method)
        result = Record(self.getPos(), (self.row + 1, self.column),
                        res,
                        log=log_state,
                        log_txt=log_txt)

        return result
Exemple #19
0
    def execute(self, record):

        
        pub_key, prv_key, side_index, side_txt, symbol_txt, quantity, \
                order_index, order_string, order_config, log_state = self.config

        timeInForce = None
        stopPrice   = None
        price       = None

        client = Client(pub_key, prv_key)

        if order_string == 'MARKET':

            if isinstance(record, dict):
                if 'quantity' in record: 
                    quantity = record['quantity']

            order = client.create_order(
                    symbol      = symbol_txt,
                    side        = side_txt,
                    type        = order_string,
                    quantity    = '{:.8f}'.format(quantity),
                    )

        elif order_string == 'LIMIT':

            timeInForce = order_config[0]
            price       = '{:.8f}'.format(order_config[2])

            if isinstance(record, dict):
                if 'price' in record: 
                    price = '{:.8f}'.format(record['price'])
                if 'quantity' in record: 
                    quantity = record['quantity']
                if 'type' in record: 
                    timeInForce = record['type']

            order = client.create_order(
                    symbol      = symbol_txt,
                    side        = side_txt,
                    type        = order_string,
                    quantity    = '{:.8f}'.format(quantity),
                    timeInForce = timeInForce,
                    price       = price,
                    )

        elif order_string == 'STOP_LOSS':

            stopPrice   = '{:.8f}'.format(order_config[0])

            if isinstance(record, dict):
                if 'stopPrice' in record: 
                    stopPrice = '{:.8f}'.format(record['stopPrice'])
                if 'quantity' in record: 
                    quantity = record['quantity']

            order = client.create_order(
                    symbol      = symbol_txt,
                    side        = side_txt,
                    type        = order_string,
                    quantity    = '{:.8f}'.format(quantity),
                    stopPrice   = stopPrice
                    )

        elif order_string == 'STOP_LOSS_LIMIT':

            timeInForce = order_config[0]
            price       = '{:.8f}'.format(order_config[2])
            stopPrice   = '{:.8f}'.format(order_config[3])

            if isinstance(record, dict):
                if 'price' in record: 
                    price = '{:.8f}'.format(record['price'])
                if 'stopPrice' in record: 
                    stopPrice = '{:.8f}'.format(record['stopPrice'])
                if 'quantity' in record: 
                    quantity = record['quantity']
                if 'type' in record: 
                    timeInForce = record['type']


            order = client.create_order(
                    symbol      = symbol_txt,
                    side        = side_txt,
                    type        = order_string,
                    quantity    = '{:.8f}'.format(quantity),
                    timeInForce = timeInForce,
                    price       = price,
                    stopPrice   = stopPrice
                    )

        elif order_string == 'TAKE_PROFIT':

            stopPrice   = '{:.8f}'.format(order_config[0])

            if isinstance(record, dict):
                if 'stopPrice' in record: 
                    stopPrice = '{:.8f}'.format(record['stopPrice'])
                if 'quantity' in record: 
                    quantity = record['quantity']



            order = client.create_order(
                    symbol      = symbol_txt,
                    side        = side_txt,
                    type        = order_string,
                    quantity    = '{:.8f}'.format(quantity),
                    stopPrice   = stopPrice
                    )

        elif order_string == 'TAKE_PROFIT_LIMIT':

            timeInForce = order_config[0]
            price       = '{:.8f}'.format(order_config[2])
            stopPrice   = '{:.8f}'.format(order_config[3])

            if isinstance(record, dict):
                if 'price' in record: 
                    price = '{:.8f}'.format(record['price'])
                if 'stopPrice' in record: 
                    stopPrice = '{:.8f}'.format(record['stopPrice'])
                if 'quantity' in record: 
                    quantity = record['quantity']
                if 'type' in record: 
                    timeInForce = record['type']

            order = client.create_order(
                    symbol      = symbol_txt,
                    side        = side_txt,
                    type        = order_string,
                    quantity    = '{:.8f}'.format(quantity),
                    timeInForce = timeInForce,
                    price       = price,
                    stopPrice   = stopPrice
                    )

        elif order_string == 'LIMIT_MAKER':

            if isinstance(record, dict):
                if 'quantity' in record: 
                    quantity = record['quantity']

            order = client.create_order(
                    symbol      = symbol_txt,
                    side        = side_txt,
                    type        = order_string,
                    quantity    = '{:.8f}'.format(quantity)
                    )

        elif order_string == 'MARKET':

            if isinstance(record, dict):
                if 'quantity' in record: 
                    quantity = record['quantity']

            order = client.create_order(
                    symbol      = symbol_txt,
                    side        = side_txt,
                    type        = order_string,
                    quantity    = '{:.8f}'.format(quantity)
                    )


        """
        logging.error('Order: {}'.format(order)) 
        logging.error('symbol = {}'.format(symbol_txt))
        logging.error('side = {}'.format(side_txt))
        logging.error('type = {}'.format(order_string))
        logging.error('quantity = {}'.format(quantity))
        logging.error('timeInForce = {}'.format(timeInForce))
        logging.error('price = {}'.format(price))
        logging.error('stopPrice = {}'.format(stopPrice))
        """

        log_txt = '{{BINANCE ORDER}}          {} ORDER EXECUTED'.format(order_string)
        result = Record(self.getPos(), (self.row +1, self.column), order,
                 log=log_state, log_txt=log_txt)

        return result
    def execute(self, record):

        target_0 = (self.row + 1, self.column)
        target_1 = self.getPos()

        def modulo_time(interval):

            while True:
                sleep(0.8)
                if datetime.now().second % int(interval) == 0:
                    break

        def threshold_time(threshold):

            while threshold > datetime.now():
                sleep(1)

        if self.config[1]:  # mode_data != None (scheduler active)

            mode_index, mode_data, log_state = self.config

            # None selected
            if mode_index == 0:

                result = Record(self.getPos(), target_0, record, log=log_state)

            # interval
            elif mode_index == 1:

                repeat_val, time_base = mode_data
                # 0 = Seconds
                # 1 = Minutes
                # 2 = Hours
                if time_base == 1:
                    delta_t = 60
                elif time_base == 2:
                    delta_t = 3600
                else:
                    delta_t = 1

                delta_t *= int(repeat_val)

                if isinstance(record, tuple) and isinstance(
                        record[0], datetime):  # synchronization (target_1)
                    # called by tareget_0 (self_sync)
                    # check every second

                    # regular interval
                    # record[0] = sync_time of preceding call

                    # DELAY
                    threshold_time(record[0])

                    offset = timedelta(seconds=delta_t)
                    sync_time = datetime.now() + offset

                    record_0 = record[1]
                    record_1 = (sync_time, record[1])

                else:

                    # first execution
                    # only trigger in interval mode

                    offset = timedelta(seconds=delta_t)
                    sync_time = datetime.now() + offset

                    record_0 = record
                    record_1 = (sync_time, record)

                log_txt = '{BASIC SCHEDULER}        >>>EXECUTE<<<'
                result = Record(self.getPos(),
                                target_0,
                                record_0,
                                target_1,
                                record_1,
                                log=log_state,
                                log_txt=log_txt)

            # interval between times
            elif mode_index == 2 or mode_index == 5:

                repeat_val, time_base, start_time, stop_time, day_list = self.config[
                    1]
                # day_list = [0 (Monady), 4(Friday), 6(Sunday)]
                start_hour, start_minute = start_time
                stop_hour, stop_minute = stop_time

                if time_base == 1:
                    delta_t = 60
                elif time_base == 2:
                    delta_t = 3600
                else:
                    delta_t = 1

                delta_t *= int(repeat_val)

                if isinstance(record, tuple) and isinstance(
                        record[0], datetime):  # synchronization (target_1)

                    stop_time = time(hour=stop_hour, minute=stop_minute)
                    stop_time = datetime.combine(date.today(), stop_time)

                    if mode_index == 2 or record[2]:
                        # check for normal interval or the special flag for modulo time op
                        threshold_time(record[0])
                    else:
                        modulo_time(delta_t)

                    offset = timedelta(seconds=delta_t)
                    sync_time = datetime.now() + offset

                    # payload data = record[1]
                    record_0 = record[1]
                    record_1 = (sync_time, record[1], False)

                    log_txt = '{BASIC SCHEDULER}        >>>EXECUTE<<<'
                    # when stop time is reached
                    if sync_time > stop_time:
                        # prevent fast firing at the end of the time frame
                        # caused by jumpgin between the two possible states: calc start time
                        # and regular interval mode
                        record_1 = (True, record[1])
                        log_txt = '{BASIC SCHEDULER}        >>>LAST EXECUTION<<<'
                        target_0 = record_0 = None
                        # go to else part 'first activation' to calculate the new start time
                        result = Record(self.getPos(),
                                        target_0,
                                        record_0,
                                        target_1,
                                        record_1,
                                        log=log_state,
                                        log_txt=log_txt)

                    else:
                        log_txt = '{BASIC SCHEDULER}        >>>EXECUTE<<<'
                        result = Record(self.getPos(),
                                        target_0,
                                        record_0,
                                        target_1,
                                        record_1,
                                        log=log_state,
                                        log_txt=log_txt)

                else:

                    # first activation (when record[0] != datetime)
                    if isinstance(record, tuple) and isinstance(
                            record[0], bool):
                        # prevent a fast firing after the last execution
                        sleep(delta_t)
                        # change record to original
                        record = record[1]

                    now = datetime.now()
                    today = datetime.now().weekday()
                    start_day = None
                    #start_day = next((i for i, e in enumerate(active_days) if e), None)
                    active_days = list(
                        (i for i, e in enumerate(day_list) if e))
                    day_cycle = cycle(active_days)

                    #check if at least one day is aktivated
                    if not active_days:
                        result = Record(self.getPos(), None, record)
                        return result

                    # check the start day
                    # does not work when only the actual day is selected
                    start_day = next(day_cycle)
                    if any(i >= today for i in active_days):
                        while start_day < today:
                            start_day = next(day_cycle)
                        day_offset = start_day - today
                    else:
                        # e.g. today = Thu, start = Tue
                        # start the smallest day
                        # wait for one week (6)
                        # plus one day bacause of negative time_offset (6+1)
                        day_offset = 7 - today + start_day

                    day_offset = timedelta(days=day_offset)

                    start_time = time(hour=start_hour, minute=start_minute)
                    stop_time = time(hour=stop_hour, minute=stop_minute)

                    start_time = datetime.combine(date.today(), start_time)
                    stop_time = datetime.combine(date.today(), stop_time)

                    # could be negative if stop_time < now
                    # funktioniert nur wenn start_time > now
                    time_offset = start_time - now

                    # check if the time has already passed
                    if start_day == today and time_offset.days < 0:
                        start_day = next(day_cycle)
                        # when the next cycle is today too (when only one day is selected)
                        if start_day == today:
                            # wait for one week (6)
                            # plus one day bacause of negative time_offset (6+1)
                            day_offset = 7
                        elif start_day < today:
                            # plus one day bacause of negative time_offset (6+1)
                            # if the start day is next week
                            day_offset = 7 - today + start_day
                        else:
                            day_offset = start_day - today

                        day_offset = timedelta(days=day_offset)

                    offset = day_offset + time_offset
                    sync_time = datetime.now() + offset
                    log_txt = '{{BASIC SCHEDULER}}        Start in: {}'.format(
                        offset)
                    record_1 = (sync_time, record, False)
                    if mode_index == 5:
                        # special flag for module time operation
                        record_1 = (sync_time, record, True)

                    result = Record(self.getPos(),
                                    None,
                                    None,
                                    target_1,
                                    record_1,
                                    log=log_state,
                                    log_txt=log_txt)

            # at specific time
            elif mode_index == 3:

                time_input, day_list = self.config[1]
                hour, minute = time_input

                if isinstance(record, tuple) and isinstance(
                        record[0], datetime):  # synchronization (target_1)
                    # next activation
                    # check secondly if execution can be started

                    # DELAY
                    while record[0] > datetime.now():
                        sleep(1)

                    record = record[1]  # payload
                    # target_0: execute subsequent elements
                    # target_1: calculate next execution time
                    log_txt = '{BASIC SCHEDULER}        >>>EXECUTE<<<'
                    result = Record(self.getPos(),
                                    target_0,
                                    record,
                                    target_1,
                                    record,
                                    log=log_state)

                else:  # first activation (when record[0] != datetime)
                    now = datetime.now()
                    today = datetime.now().weekday()
                    # None = Abbruch
                    start_day = None
                    #start_day = next((i for i, e in enumerate(active_days) if e), None)
                    active_days = list(
                        (i for i, e in enumerate(day_list) if e))
                    day_cycle = cycle(active_days)

                    #check if at least one day is aktivated
                    if not active_days:
                        result = Record(self.getPos(), None, record)
                        return result

                    # determine the start day
                    if any(True for i in active_days if i >= today):
                        # start today or a day > as today
                        start_day = next(day_cycle)
                        while start_day < today:
                            start_day = next(day_cycle)
                        day_offset = start_day - today

                    else:
                        # start the smallest day (next week)
                        start_day = next(day_cycle)
                        day_offset = 7 - today + start_day

                    day_offset = timedelta(days=day_offset)

                    start_time = time(hour=hour, minute=minute)
                    actual_time = datetime.now().time()

                    start_time = datetime.combine(date.min, start_time)
                    actual_time = datetime.combine(date.min, actual_time)

                    time_offset = start_time - actual_time

                    # check if the time has already passed
                    if start_day == today and time_offset.days < 0:
                        start_day = next(day_cycle)
                        # when the next cycle is today too (when only one day is selected)
                        if start_day == today:
                            # wait for one week (6)
                            # plus one day bacause of negative time_offset (6+1)
                            day_offset = 7
                        elif start_day < today:
                            # plus one day bacause of negative time_offset (6+1)
                            # if the start day is next week
                            day_offset = 7 - today + start_day
                        else:
                            day_offset = start_day - today

                        day_offset = timedelta(days=day_offset)

                    offset = day_offset + time_offset
                    sync_time = datetime.now() + offset

                    log_txt = '{{BASIC SCHEDULER}}        Start in: {}'.format(
                        offset)
                    record_1 = (sync_time, record)

                    result = Record(self.getPos(),
                                    None,
                                    None,
                                    target_1,
                                    record_1,
                                    log=log_state,
                                    log_txt=log_txt)

            # on every full interval
            elif mode_index == 4:
                # every full interval (modulo)
                repeat_val, time_base = mode_data
                # 0 = Seconds
                # 1 = Minutes
                # 2 = Hours

                t_now = datetime.now()

                if time_base == 1:
                    modulo_numerator = t_now.minute
                    delta_t = 60
                elif time_base == 2:
                    modulo_numerator = t_now.hour
                    delta_t = 3600
                else:
                    modulo_numerator = t_now.second
                    delta_t = 1

                if isinstance(record, tuple) and isinstance(
                        record[0], datetime):

                    while record[0] > datetime.now():
                        sleep(1)

                    modulo_time(repeat_val)

                    offset = timedelta(seconds=delta_t)
                    sync_time = datetime.now() + offset

                    record_0 = record[1]  # regular execute
                    record_1 = (sync_time, record[1]
                                )  #trigger next waiting phase

                    log_txt = '{BASIC SCHEDULER}        >>>EXECUTE<<<'

                else:
                    # first execution only
                    # get time and check for execution
                    sync_time = datetime.now()
                    record_1 = (sync_time, record)
                    # overwrite existing information
                    target_0 = None
                    record_0 = None
                    log_txt = '{BASIC SCHEDULER}        Initial time synchronization'

                result = Record(self.getPos(),
                                target_0,
                                record_0,
                                target_1,
                                record_1,
                                log=log_state,
                                log_txt=log_txt)

        else:  # mode_data == None (no scheduler)
            # config = mode_index, mode_data, log_state
            result = Record(self.getPos(),
                            target_0,
                            record,
                            log=self.config[2])

        return result
Exemple #21
0
    def execute(self, record):

        log_txt = '{{BASIC RETURN}}           Return to {}|{}'.format(self.config[0][0], alphabet[self.config[0][1]])
        result = Record(self.getPos(), self.config[0], record, log=self.config[2], log_txt=log_txt)
        return result
Exemple #22
0
    def execute(self, record):

        interval_str, inteval_index, symbol_txt, log_state = self.config

        client = Client('', '')
        try:
            record = client.get_klines(symbol=symbol_txt,
                                       interval=interval_str)
        except Exception as e:
            log_txt = '{{BINANCE SCHEDULER}}      Exception caught: {}'.format(
                str(e))
            result = Record(self.getPos(),
                            None,
                            None,
                            log=log_state,
                            log_txt=log_txt)
            return result

        myList = []
        item = []

        try:
            for item in record:
                n_item = []
                int_ts = int(item[0] / 1000)
                # nur neue timestamps anhängen

                n_item.append(int_ts)  # open time
                n_item.append(float(item[1]))  # open
                n_item.append(float(item[2]))  # high
                n_item.append(float(item[3]))  # low
                n_item.append(float(item[4]))  # close
                n_item.append(float(item[5]))  # volume
                n_item.append(int(item[6] / 1000))  # close_time
                n_item.append(float(item[7]))  # quote_assetv
                n_item.append(int(item[8]))  # trades
                n_item.append(float(item[9]))  # taker_b_asset_v
                n_item.append(float(item[10]))  # taker_b_quote_v
                n_item.append(datetime.datetime.fromtimestamp(n_item[0]))
                myList.append(n_item)
        except:
            #logging.error('Data cant be read!')
            log_txt = '{{BINANCE SCHEDULER}}      Exception caught: {}'.format(
                str(e))
            result = Record(self.getPos(),
                            None,
                            None,
                            log=log_state,
                            log_txt=log_txt)
            return result

        new_ohlc = pd.DataFrame(myList,
                                columns=[
                                    'open_time', 'open', 'high', 'low',
                                    'close', 'volume', 'close_time',
                                    'quote_assetv', 'trades',
                                    'taker_b_asset_v', 'taker_b_quote_v',
                                    'datetime'
                                ])

        log_txt = '{{BINANCE OHLC QUERY}}     Received {} records'.format(
            len(record))
        result = Record(self.getPos(), (self.row + 1, self.column),
                        new_ohlc,
                        log=log_state,
                        log_txt=log_txt)

        return result
Exemple #23
0
    def execute(self, record):

        ta_str, ta_index, ta_config, log_state = self.config
        #logging.error('b_debug = {}'.format(self.b_debug))

        function = ''

        if ta_str == 'MA':

            #logging.warning('execute() - Moving Average selected - {}'.format(ta_config))
            function = 'Moving Averages'
            column_name = 'ma-{}'.format(ta_config[0])
            record[column_name] = record['close'].rolling(window=ta_config[0],
                                                          center=False).mean()

        elif ta_str == 'EMA':

            #logging.warning('execute() - Exponential Moving Average selected - {}'.format(ta_config))
            function = 'Exponential Moving Averages'
            column_name = 'ema-{}'.format(ta_config[0])
            record[column_name] = record['close'].ewm(span=ta_config[0],
                                                      adjust=False).mean()

        elif ta_str == 'STOK':

            #logging.warning('execute() -  Stochastic Oscillator %K selected - {}'.format(ta_config))
            function = 'Stochastic Oscillator %K'
            record['stok'] = pd.Series((record['close'] - record['low']) /
                                       (record['high'] - record['low']),
                                       name='stok')

        elif ta_str == 'STO':

            #logging.warning('execute() -  Stochastic Oscillator %D selected - {}'.format(ta_config))
            function = 'Stochastic Oscillator %D'
            column_name = 'sto-{}'.format(ta_config[0])
            SOk = pd.Series((record['close'] - record['low']) /
                            (record['high'] - record['low']),
                            name='stok')
            record[column_name] = SOk.ewm(span=ta_config[0],
                                          min_periods=ta_config[0] - 1).mean()

        elif ta_str == 'RSI':

            #logging.warning('execute() - Relative Strenght Index selected - {}'.format(ta_config))
            function = 'Relative Strenght Index'

            i = 0
            UpI = [0]
            DoI = [0]

            while i + 1 <= record.index[-1]:
                UpMove = record.get_value(i + 1, 'high') - record.get_value(
                    i, 'high')
                DoMove = record.get_value(i, 'low') - record.get_value(
                    i + 1, 'low')

                if UpMove > DoMove and UpMove > 0:
                    UpD = UpMove
                else:
                    UpD = 0

                UpI.append(UpD)

                if DoMove > UpMove and DoMove > 0:
                    DoD = DoMove
                else:
                    DoD = 0

                DoI.append(DoD)

                i = i + 1

            UpI = pd.Series(UpI)
            DoI = pd.Series(DoI)

            PosDI = UpI.ewm(span=ta_config[0],
                            min_periods=ta_config[0] - 1).mean()
            NegDI = DoI.ewm(span=ta_config[0],
                            min_periods=ta_config[0] - 1).mean()

            column_name = 'rsi-{}'.format(ta_config[0])
            record[column_name] = pd.Series(PosDI / (PosDI + NegDI))
        """
        else:
            logging.warning('execute() - No config found')
        """

        log_txt = '{{TECHNICAL ANALYSIS}}     {}'.format(function)

        result = Record(self.getPos(), (self.row + 1, self.column),
                        record,
                        log=log_state,
                        log_txt=log_txt)

        return result