Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build out user dir #31

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ repos:
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
- repo: http://gitlab.com/PyCQA/flake8
rev: 3.9.0
- repo: https://gitlab.com/PyCQA/flake8
rev: 3.9.2
hooks:
- id: flake8
- repo: http://github.com/PyCQA/isort
- repo: https://github.com/PyCQA/isort
rev: 5.7.0
hooks:
- id: isort
62 changes: 34 additions & 28 deletions algobot/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
QMainWindow, QMessageBox, QTableWidgetItem)

import algobot.assets
from algobot import helpers
inverse marked this conversation as resolved.
Show resolved Hide resolved
from algobot.algodict import get_interface_dictionary
from algobot.data import Data
from algobot.enums import (AVG_GRAPH, BACKTEST, LIVE, LONG, NET_GRAPH,
Expand All @@ -26,8 +27,8 @@
setup_graph_plots, setup_graphs,
update_backtest_graph_limits,
update_main_graphs)
from algobot.helpers import (ROOT_DIR, create_folder, create_folder_if_needed,
get_caller_string, open_file_or_folder)
from algobot.helpers import (PATHS, create_folder_if_needed, get_caller_string,
open_file_or_folder)
from algobot.interface.about import About
from algobot.interface.config_utils.state_utils import load_state, save_state
from algobot.interface.config_utils.strategy_utils import get_strategies
Expand All @@ -47,7 +48,7 @@
from algobot.traders.simulationtrader import SimulationTrader

app = QApplication(sys.argv)
mainUi = os.path.join(ROOT_DIR, 'UI', 'algobot.ui')
mainUi = os.path.join(PATHS.get_ui_dir(), 'algobot.ui')


class Interface(QMainWindow):
Expand Down Expand Up @@ -252,21 +253,23 @@ def export_optimizer(self, file_type: str):
"""
if self.optimizer:
if len(self.optimizer.optimizerRows) > 0:
optimizerFolderPath = create_folder('Optimizer Results')
innerPath = os.path.join(optimizerFolderPath, self.optimizer.symbol)
create_folder_if_needed(innerPath, optimizerFolderPath)
defaultFileName = self.optimizer.get_default_result_file_name('optimizer', ext=file_type.lower())
defaultPath = os.path.join(innerPath, defaultFileName)
filePath, _ = QFileDialog.getSaveFileName(self, 'Save Optimizer', defaultPath,
f'{file_type} (*.{file_type.lower()})')
if not filePath:
optimizer_folder_path = helpers.PATHS.get_optimizer_results_dir()
create_folder_if_needed(optimizer_folder_path)
inner_path = os.path.join(optimizer_folder_path, self.optimizer.symbol)
create_folder_if_needed(inner_path)
default_file_name = self.optimizer.get_default_result_file_name(optimizer_folder_path,
'optimizer', ext=file_type.lower())
default_path = os.path.join(inner_path, default_file_name)
file_path, _ = QFileDialog.getSaveFileName(self, 'Save Optimizer', default_path,
f'{file_type} (*.{file_type.lower()})')
if not file_path:
create_popup(self, "Export cancelled.")
else:
self.optimizer.export_optimizer_rows(filePath, file_type)
create_popup(self, f'Exported successfully to {filePath}.')
self.optimizer.export_optimizer_rows(file_path, file_type)
create_popup(self, f'Exported successfully to {file_path}.')

if open_from_msg_box(text='Do you want to open the optimization report?', title='Optimizer Report'):
open_file_or_folder(filePath)
open_file_or_folder(file_path)

else:
create_popup(self, "No table rows found.")
Expand Down Expand Up @@ -407,18 +410,19 @@ def end_backtest(self):
"""
Ends backtest and prompts user if they want to see the results.
"""
backtestFolderPath = create_folder('Backtest Results')
innerPath = os.path.join(backtestFolderPath, self.backtester.symbol)
create_folder_if_needed(innerPath, backtestFolderPath)
defaultFile = os.path.join(innerPath, self.backtester.get_default_result_file_name())
fileName, _ = QFileDialog.getSaveFileName(self, 'Save Result', defaultFile, 'TXT (*.txt)')
fileName = fileName.strip()
fileName = fileName if fileName != '' else None
backtest_folder_path = helpers.PATHS.get_backtest_results_dir()
create_folder_if_needed(backtest_folder_path)
inner_path = os.path.join(backtest_folder_path, self.backtester.symbol)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably refactor this code to use makedirs()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

create_folder_if_needed calls that under the hood.

create_folder_if_needed(inner_path)
default_file = os.path.join(inner_path, self.backtester.get_default_result_file_name(backtest_folder_path))
file_name, _ = QFileDialog.getSaveFileName(self, 'Save Result', default_file, 'TXT (*.txt)')
file_name = file_name.strip()
file_name = file_name if file_name != '' else None

if not fileName:
if not file_name:
self.add_to_backtest_monitor('Ended backtest.')
else:
path = self.backtester.write_results(resultFile=fileName)
path = self.backtester.write_results(resultFile=file_name)
self.add_to_backtest_monitor(f'Ended backtest and saved results to {path}.')

if open_from_msg_box(text=f"Backtest results have been saved to {path}.", title="Backtest Results"):
Expand Down Expand Up @@ -1293,12 +1297,13 @@ def export_trades(self, caller):
trade.append(item.text())
trades.append(trade)

path = create_folder("Trade History")
trade_history_dir = helpers.PATHS.get_trade_history_dir()
create_folder_if_needed(trade_history_dir)

if caller == LIVE:
defaultFile = os.path.join(path, 'live_trades.csv')
defaultFile = os.path.join(trade_history_dir, 'live_trades.csv')
else:
defaultFile = os.path.join(path, 'simulation_trades.csv')
defaultFile = os.path.join(trade_history_dir, 'simulation_trades.csv')

path, _ = QFileDialog.getSaveFileName(self, 'Export Trades', defaultFile, 'CSV (*.csv)')

Expand All @@ -1317,8 +1322,9 @@ def import_trades(self, caller):
"""
table = self.interfaceDictionary[caller]['mainInterface']['historyTable']
label = self.interfaceDictionary[caller]['mainInterface']['historyLabel']
path = create_folder("Trade History")
path, _ = QFileDialog.getOpenFileName(self, 'Import Trades', path, "CSV (*.csv)")
trade_history_dir = helpers.PATHS.get_trade_history_dir()
create_folder_if_needed(trade_history_dir)
path, _ = QFileDialog.getOpenFileName(self, 'Import Trades', trade_history_dir, "CSV (*.csv)")

try:
with open(path, 'r') as f:
Expand Down
30 changes: 16 additions & 14 deletions algobot/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
from binance.client import Client
from binance.helpers import interval_to_milliseconds

from algobot.helpers import (ROOT_DIR, get_logger, get_normalized_data,
from algobot import helpers
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here also, why not just have from algobot.helpers import ...?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated all

from algobot.helpers import (PATHS, get_logger, get_normalized_data,
get_ups_and_downs)
from algobot.typing_hints import DATA_TYPE

Expand Down Expand Up @@ -142,12 +143,12 @@ def get_database_file(self) -> str:
Retrieves database file path.
:return: Database file path.
"""
database_folder = os.path.join(ROOT_DIR, 'Databases')
database_folder = PATHS.get_database_dir()
if not os.path.exists(database_folder):
os.mkdir(database_folder)
os.makedirs(database_folder)

filePath = os.path.join(database_folder, f'{self.symbol}.db')
return filePath
file_path = os.path.join(database_folder, f'{self.symbol}.db')
return file_path

def create_table(self):
"""
Expand Down Expand Up @@ -521,19 +522,20 @@ def get_interval_minutes(self) -> int:
else:
raise ValueError("Invalid interval.", 4)

def create_folders_and_change_path(self, folderName: str):
def create_folders_and_change_path(self, folder_name: str):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice change! We should really convert camel case to snake case over time

Copy link
Contributor Author

@inverse inverse Jul 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

snake case is the python way. If you're happy with something we could use black?

https://black.readthedocs.io/en/stable/

We can configure it as a pre-commit hook even :)

Although not sure how it handles variable naming come to think about it 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pylint will help enforce it ;)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeap. We should really start leveraging pylint and mypy

"""
Creates appropriate folders for data storage then changes current working directory to it.
:param folderName: Folder to create.
:param folder_name: Folder to create.
"""
os.chdir(ROOT_DIR)
if not os.path.exists(folderName): # Create CSV folder if it doesn't exist
os.mkdir(folderName)
os.chdir(folderName) # Go inside the folder.
if not os.path.exists(folder_name):
helpers.create_folder_if_needed(folder_name)

if not os.path.exists(self.symbol): # Create symbol folder inside CSV folder if it doesn't exist.
os.chdir(folder_name)

if not os.path.exists(self.symbol):
os.mkdir(self.symbol)
os.chdir(self.symbol) # Go inside the folder.

os.chdir(self.symbol)

def write_csv_data(self, totalData: list, fileName: str, armyTime: bool = True) -> str:
"""
Expand All @@ -544,7 +546,7 @@ def write_csv_data(self, totalData: list, fileName: str, armyTime: bool = True)
:return: Absolute path to CSV file.
"""
currentPath = os.getcwd()
self.create_folders_and_change_path(folderName="CSV")
self.create_folders_and_change_path(helpers.PATHS.get_csv_dir())

with open(fileName, 'w') as f:
f.write("Date_UTC, Open, High, Low, Close, Volume, Quote_Asset_Volume, Number_of_Trades, "
Expand Down
Loading