Example #1
0
def load_backtest_settings(config_obj):
    """
    Loads backtest settings from JSON file and sets them to backtest settings.
    :param config_obj: Configuration QDialog object (from configuration.py)
    """
    targetPath = create_appropriate_config_folders(config_obj, 'Backtest')
    filePath, _ = QFileDialog.getOpenFileName(config_obj, 'Load Credentials',
                                              targetPath, "JSON (*.json)")
    try:
        config = helpers.load_json_file(filePath)
        file = os.path.basename(filePath)
        if config['type'] != BACKTEST:
            QMessageBox.about(
                config_obj, 'Warning',
                'Incorrect type of non-backtest configuration provided.')
        else:
            config_obj.backtestTickerLineEdit.setText(str(config['ticker']))
            config_obj.backtestIntervalComboBox.setCurrentIndex(
                config['interval'])
            config_obj.backtestStartingBalanceSpinBox.setValue(
                config['startingBalance'])
            config_obj.backtestPrecisionSpinBox.setValue(config['precision'])
            config_obj.backtestMarginTradingCheckBox.setChecked(
                config['marginTrading'])
            helper_load(config_obj, BACKTEST, config)
            config_obj.backtestConfigurationResult.setText(
                f"Loaded backtest configuration successfully from {file}.")
    except Exception as e:
        config_obj.logger.exception(str(e))
        config_obj.backtestConfigurationResult.setText(
            "Could not load backtest configuration.")
Example #2
0
def load_simulation_settings(config_obj):
    """
    Loads simulation settings from JSON file and sets it to simulation settings.
    :param config_obj: Configuration QDialog object (from configuration.py)
    """
    targetPath = create_appropriate_config_folders(config_obj, 'Simulation')
    filePath, _ = QFileDialog.getOpenFileName(config_obj, 'Load Credentials',
                                              targetPath, "JSON (*.json)")
    try:
        config = helpers.load_json_file(filePath)
        file = os.path.basename(filePath)
        if config['type'] != SIMULATION:
            QMessageBox.about(
                config_obj, 'Warning',
                'Incorrect type of non-simulation configuration provided.')
        else:
            config_obj.simulationTickerLineEdit.setText(str(config['ticker']))
            config_obj.simulationIntervalComboBox.setCurrentIndex(
                config['interval'])
            config_obj.simulationStartingBalanceSpinBox.setValue(
                config['startingBalance'])
            config_obj.simulationPrecisionSpinBox.setValue(config['precision'])
            config_obj.lowerIntervalSimulationCheck.setChecked(
                config['lowerInterval'])
            helper_load(config_obj, SIMULATION, config)
            config_obj.simulationConfigurationResult.setText(
                f"Loaded sim configuration successfully from {file}.")
    except Exception as e:
        config_obj.logger.exception(str(e))
        config_obj.simulationConfigurationResult.setText(
            "Could not load simulation configuration.")
Example #3
0
def load_live_settings(config_obj):
    """
    Loads live settings from JSON file and sets it to live settings.
    :param config_obj: Configuration QDialog object (from configuration.py)
    """
    targetPath = create_appropriate_config_folders(config_obj, 'Live')
    filePath, _ = QFileDialog.getOpenFileName(config_obj, 'Load Credentials',
                                              targetPath, "JSON (*.json)")
    try:
        config = helpers.load_json_file(filePath)
        file = os.path.basename(filePath)
        if config['type'] != LIVE:
            QMessageBox.about(
                config_obj, 'Warning',
                'Incorrect type of non-live configuration provided.')
        else:
            config_obj.tickerLineEdit.setText(str(config['ticker']))
            config_obj.intervalComboBox.setCurrentIndex(config['interval'])
            config_obj.precisionSpinBox.setValue(config['precision'])
            config_obj.usRegionRadio.setChecked(config['usRegion'])
            config_obj.otherRegionRadio.setChecked(config['otherRegion'])
            config_obj.isolatedMarginAccountRadio.setChecked(
                config['isolatedMargin'])
            config_obj.crossMarginAccountRadio.setChecked(
                config['crossMargin'])
            config_obj.lowerIntervalCheck.setChecked(config['lowerInterval'])
            helper_load(config_obj, LIVE, config)
            config_obj.configurationResult.setText(
                f"Loaded live configuration successfully from {file}.")
    except Exception as e:
        config_obj.logger.exception(str(e))
        config_obj.configurationResult.setText(
            "Could not load live configuration.")
Example #4
0
def load_credentials(config_obj, auto: bool = True):
    """
    Attempts to load credentials automatically from path program regularly stores credentials in if auto is True.
    :param config_obj: Configuration QDialog object (from configuration.py)
    :param auto: Boolean regarding whether bot called this function or not. If bot called it, silently try to load
    credentials. If a user called it, however, open a file dialog to ask for the file path to credentials.
    """
    targetFolder = os.path.join(helpers.ROOT_DIR, config_obj.credentialsFolder)
    if helpers.create_folder_if_needed(targetFolder):
        config_obj.credentialResult.setText('No credentials found.')
        return

    if not auto:
        filePath, _ = QFileDialog.getOpenFileName(config_obj,
                                                  'Load Credentials',
                                                  targetFolder,
                                                  "JSON (*.json)")
    else:
        filePath = os.path.join(targetFolder, 'default.json')

    try:
        credentials = helpers.load_json_file(jsonfile=filePath)
        config_obj.binanceApiKey.setText(credentials['apiKey'])
        config_obj.binanceApiSecret.setText(credentials['apiSecret'])
        config_obj.telegramApiKey.setText(credentials['telegramApiKey'])
        config_obj.telegramChatID.setText(credentials['chatID'])
        config_obj.credentialResult.setText(
            f'Credentials loaded successfully from {os.path.basename(filePath)}.'
        )
    except FileNotFoundError:
        config_obj.credentialResult.setText('Could not load credentials.')
    except Exception as e:
        config_obj.credentialResult.setText(str(e))
Example #5
0
    def load_credentials(self, auto: bool = True):
        """
        Attempts to load credentials automatically from path program regularly stores credentials in if auto is True.
        """
        targetFolder = os.path.join(helpers.ROOT_DIR, self.credentialsFolder)
        if helpers.create_folder_if_needed(targetFolder):
            self.credentialResult.setText('No credentials found.')
            return

        if not auto:
            filePath, _ = QFileDialog.getOpenFileName(self, 'Load Credentials', targetFolder, "JSON (*.json)")
        else:
            filePath = os.path.join(targetFolder, 'default.json')

        try:
            credentials = helpers.load_json_file(jsonfile=filePath)
            self.binanceApiKey.setText(credentials['apiKey'])
            self.binanceApiSecret.setText(credentials['apiSecret'])
            self.telegramApiKey.setText(credentials['telegramApiKey'])
            self.telegramChatID.setText(credentials['chatID'])
            self.credentialResult.setText(f'Credentials loaded successfully from {os.path.basename(filePath)}.')
        except FileNotFoundError:
            self.credentialResult.setText('Could not load credentials.')
        except Exception as e:
            self.credentialResult.setText(str(e))
Example #6
0
    def load_state(self):
        """
        This function will attempt to load previous basic configuration settings from self.basicFilePath.
        :return: None
        """
        if os.path.exists(self.basicFilePath):
            try:
                config = helpers.load_json_file(self.basicFilePath)

                self.lightModeRadioButton.setChecked(config['lightTheme'])
                self.darkModeRadioButton.setChecked(config['darkTheme'])
                self.bloombergModeRadioButton.setChecked(config['bloombergTheme'])
                self.bullModeRadioButton.setChecked(config['bullTheme'])
                self.bearModeRadioButton.setChecked(config['bearTheme'])

                self.balanceColor.setCurrentIndex(config['balanceColor'])
                self.avg1Color.setCurrentIndex(config['avg1Color'])
                self.avg2Color.setCurrentIndex(config['avg2Color'])
                self.avg3Color.setCurrentIndex(config['avg3Color'])
                self.avg4Color.setCurrentIndex(config['avg4Color'])
                self.hoverLineColor.setCurrentIndex(config['lineColor'])

                self.graphIndicatorsCheckBox.setChecked(config['averagePlot'])

                if self.parent:
                    self.parent.add_to_live_activity_monitor('Loaded previous state successfully.')
            except Exception as e:
                self.logger.exception(str(e))

                if self.parent:
                    self.parent.add_to_live_activity_monitor('Failed to load previous state.')
Example #7
0
def load_config_helper(config_obj, caller, result_label: QLabel,
                       func: Callable):
    """
    Helper function to load configurations.
    :param config_obj: Configuration object (configuration.py).
    :param caller: Caller that called this function (optimizer, simulation, backtest, live).
    :param result_label: QLabel object to modify upon an action.
    :param func: Function to call for remaining configuration loading.
    """
    caller_str = get_caller_string(caller)
    targetPath = create_appropriate_config_folders(config_obj,
                                                   caller_str.capitalize())
    filePath, _ = QFileDialog.getOpenFileName(config_obj,
                                              f'Load {caller_str} config',
                                              targetPath, "JSON (*.json)")

    try:
        config = helpers.load_json_file(filePath)
        file = os.path.basename(filePath)

        if config['type'] != caller:
            QMessageBox.about(
                config_obj, 'Warning',
                f'Incorrect type of non-{caller_str} configuration provided.')
        else:
            func(config_obj, config)

            # TODO: Fix optimizer saving/loading.
            if caller != OPTIMIZER:
                helper_load(config_obj, caller, config)

            result_label.setText(
                f"Loaded {caller_str} configuration successfully from {file}.")
    except Exception as e:
        config_obj.logger.exception(str(e))
        result_label.setText(f"Could not load {caller_str} configuration.")
Example #8
0
def load_state(config_obj: Configuration):
    """
    This function will attempt to load previous basic configuration settings from config_obj.stateFilePath.
    :param config_obj: Configuration object.
    """
    if os.path.exists(config_obj.stateFilePath):
        try:
            config = helpers.load_json_file(config_obj.stateFilePath)

            config_obj.lightModeRadioButton.setChecked(config['lightTheme'])
            config_obj.darkModeRadioButton.setChecked(config['darkTheme'])
            config_obj.bloombergModeRadioButton.setChecked(
                config['bloombergTheme'])
            config_obj.bullModeRadioButton.setChecked(config['bullTheme'])
            config_obj.bearModeRadioButton.setChecked(config['bearTheme'])

            set_color_to_label(config_obj.balanceColor, config['balanceColor'])
            set_color_to_label(config_obj.hoverLineColor,
                               (config['lineColor']))

            config_obj.graphIndicatorsCheckBox.setChecked(
                config['averagePlot'])
            config_obj.enableHoverLine.setChecked(config['hoverLine'])
            config_obj.failureLimitSpinBox.setValue(int(
                config['failureLimit']))
            config_obj.failureSleepSpinBox.setValue(int(
                config['failureSleep']))
            config_obj.tokenPass = config['tokenPass']
            config_obj.chatPass = config['chatPass']
            config_obj.telegrationConnectionResult.setText(
                config['telegramResult'])

            # Load saved tickers.
            config_obj.tickerLineEdit.setText(config['mainTicker'])
            config_obj.simulationTickerLineEdit.setText(config['simTicker'])
            config_obj.optimizerTickerLineEdit.setText(
                config['optimizerTicker'])
            config_obj.backtestTickerLineEdit.setText(config['backtestTicker'])

            # Load intervals.
            config_obj.intervalComboBox.setCurrentIndex(config['mainInterval'])
            config_obj.simulationIntervalComboBox.setCurrentIndex(
                config['simInterval'])
            config_obj.optimizerIntervalComboBox.setCurrentIndex(
                config['optimizerInterval'])
            config_obj.optimizerStrategyIntervalCombobox.setCurrentIndex(
                config['optimizerStrategyInterval'])
            config_obj.optimizerStrategyIntervalEndCombobox.setCurrentIndex(
                config['optimizerStrategyEndInterval'])
            config_obj.backtestIntervalComboBox.setCurrentIndex(
                config['backtestInterval'])
            config_obj.backtestStrategyIntervalCombobox.setCurrentIndex(
                config['backtestStrategyInterval'])

            config_obj.hiddenStrategies = set(config['hiddenStrategies'])
            delete_strategy_slots(config_obj)
            load_strategy_slots(config_obj)

            if config_obj.parent:
                config_obj.parent.add_to_live_activity_monitor(
                    'Loaded previous state successfully.')
        except Exception as e:
            config_obj.logger.exception(str(e))

            if config_obj.parent:
                config_obj.parent.add_to_live_activity_monitor(
                    'Failed to fully load previous state because of a '
                    'potential new update/install. Try restarting Algobot.')