Skip to content

Commit

Permalink
Merge pull request #24 from ilcardella/refactor-model
Browse files Browse the repository at this point in the history
Refactor model
  • Loading branch information
ilcardella committed Feb 10, 2019
2 parents 92e30a0 + 0446dcc commit 653aa6d
Show file tree
Hide file tree
Showing 18 changed files with 1,975 additions and 593 deletions.
31 changes: 24 additions & 7 deletions docs/source/modules.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@ Model
The ``Model`` module contains the business logic and the data management of
TradingMate.

Model
"""""

.. automodule:: Model.Model

.. autoclass:: Model
:members:

Holding
"""""""
Expand All @@ -43,6 +36,22 @@ Portfolio
.. autoclass:: Portfolio
:members:

DatabaseHandler
"""""""""""""""

.. automodule:: Model.DatabaseHandler

.. autoclass:: DatabaseHandler
:members:

StockPriceGetter
""""""""""""""""

.. automodule:: Model.StockPriceGetter

.. autoclass:: StockPriceGetter
:members:

UI
^^^

Expand Down Expand Up @@ -111,6 +120,14 @@ TaskThread
.. autoclass:: TaskThread
:members:

Trade
^^^^^^^^^^

.. automodule:: Utils.Trade

.. autoclass:: Trade
:members:

Utils
^^^^^

Expand Down
96 changes: 96 additions & 0 deletions src/Model/DatabaseHandler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import os
import sys
import inspect
import logging

currentdir = os.path.dirname(os.path.abspath(
inspect.getfile(inspect.currentframe())))
parentdir = os.path.dirname(currentdir)
sys.path.insert(0, parentdir)

from Utils.Utils import Utils
from Utils.Trade import Trade

class DatabaseHandler():
"""
Handles the IO operation with the database to handle persistent data
"""
def __init__(self, config):
"""
Initialise
"""
# By default use the configured filepath
self.db_filepath = config.get_trading_database_path()
# Create an empty list to store trades from database
self.trading_history = []

def read_data(self, filepath=None):
"""
Read the trade history from the json database and return the list of trades
- **filepath**: optional, if not set the configured path will be used
"""
try:
path = filepath if filepath is not None else self.db_filepath
self.db_filepath = path
json_obj = Utils.load_json_file(path)

# Create a list of all the trades in the json file
for item in json_obj['trades']:
trade = Trade.from_dict(item)
# Store the list internally
self.trading_history.append(trade)
except Exception as e:
logging.error(e)
raise RuntimeError('Unable to read data from the database')


def write_data(self, filepath=None):
"""
Write the trade history to the database
"""
try:
path = filepath if filepath is not None else self.db_filepath
# Create a json object and store the trade history into it
json_obj = {
'trades': []
}
for t in self.trading_history:
json_obj['trades'].append(t.to_dict())
# Write to file
return Utils.write_json_file(path, json_obj)
except Exception as e:
logging.error(e)
raise RuntimeError('Unable to write data to the database')

def get_db_filepath(self):
"""
Return the database filepath
"""
return self.db_filepath

def get_trades_list(self):
"""
Return the list of trades stored in the db
"""
return self.trading_history

def add_trade(self, trade):
"""
Add a trade to the database
"""
try:
self.trading_history.append(trade)
except Exception as e:
logging.error(e)
raise RuntimeError('Unable to add trade to the database')

def remove_last_trade(self):
"""
Remove the last trade from the trade history
"""
try:
del self.trading_history[-1]
except Exception as e:
logging.error(e)
raise RuntimeError('Unable to delete last trade')
6 changes: 6 additions & 0 deletions src/Model/Holding.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ def set_amount(self, value):
raise ValueError("Invalid amount")
self._amount = value

def add_quantity(self, value):
"""
Add or subtract (if value is negative) the value to the holding quantity
"""
self._amount += value

def set_last_price_invalid(self):
self._lastPriceValid = False

Expand Down
213 changes: 0 additions & 213 deletions src/Model/Model.py

This file was deleted.

0 comments on commit 653aa6d

Please sign in to comment.