def new_portfolio(self, starting_time, ending_time): portfolio = Portfolio(author=self.email, starting_time=starting_time, ending_time=ending_time, author_id=self._id) portfolio.save_to_mongo()
def __init__(self, started_money, data, risk_level=0.1, index_col='', names=(), seq_len=20): self.data = data self.index_col = index_col self.names = names self.seq_len = seq_len if risk_level > 1.0 or not isinstance(risk_level, float): raise ValueError('Risk can be only in 0.01..1.0 range.') self.risk_level = risk_level self.portfolio = Portfolio(started_money) self.dao = None self.done = None self.current_state = None self.current_price = None self.reset() self.action_space = gym.spaces.Discrete(3) self.actions = {0: self.buy, 1: self.sell, 2: self.hold}
def main(): p1 = Portfolio(1, 'janesmith', 'p1', 10000.00, []) t1 = StockTransaction(1, 1, "AAPL", 234.40, 2, datetime.now()) t2 = StockTransaction(2, 1, "NFLX", 218.56, 10, datetime.now()) t3 = StockTransaction(3, 1, "AAPL", 209.29, 1, datetime.now()) p1.process_transaction(t1) #print(p1.stock_holdings) p1.process_transaction(t2) #print(p1.stock_holdings) p1.process_transaction(t3) #print(p1.stock_holdings) print("holdings: " + str(p1.stock_holdings)) print("cash: " + str(p1.cash)) print("value: " + str(p1.portfolio_value)) print("return: " + str(p1.portfolio_return)) t4 = StockTransaction(4, 1, "NFLX", 279.12, -5, datetime.now()) p1.process_transaction(t4) print("holdings: " + str(p1.stock_holdings)) print("cash: " + str(p1.cash)) print("value: " + str(p1.portfolio_value)) print("return: " + str(p1.portfolio_return))
def add_portfolio(self, portfolio: Portfolio): with sqlite3.connect(self._db_path) as connection: cursor = connection.cursor() query = self.build_insert_query( table=PortfoliosTable.TABLE_NAME, columns=(PortfoliosTable.Columns.HOLDER, PortfoliosTable.Columns.NAME, PortfoliosTable.Columns.CASH)) cursor.execute(query, _unpack_portfolio(portfolio)) connection.commit() portfolio.update_with_generated_id(cursor.lastrowid)
def add_asset(managed_portfolio: portfolio.Portfolio, asset_code: Text, asset_name: Text, asset_price: Optional[float] = 0.0, asset_currency: Optional[Text] = '') -> asset.Asset: """Adds an asset to a given portfolio. Args: managed_portfolio: Portfolio where to add asset. asset_code: Code of the asset to add. asset_name: Name of the asset to add. asset_price: Price of the asset. asset_currency: Currency of the asset price. Raises: ValueError: asset already exists in portfolio. Returns: New Asset created. """ if contains_asset(managed_portfolio, asset_code): raise ValueError(f'Asset {asset_code} already exists.') new_asset = asset.Asset(asset_code, asset_name, asset_price, asset_currency) managed_portfolio.assets[asset_code] = new_asset portfolio_manager.store_portfolio(managed_portfolio) return new_asset
def paper_trade(df_trade, indices, prices, clf_name, trading_params, selector_f): start_date, final_date = trading_params['dates'] print("Starting trading for %s: %s from [%s-%s] every %s" % (clf_name, trading_params['mode'], start_date, final_date, trading_params['trade_frequency'])) if trading_params['mode'] == 'sell_all': update_positions = update_positions_buy_sell_all elif trading_params['mode'] == 'avoid_fees': update_positions = update_positions_avoiding_fees portfolios = [] positions = [] stash = 100000 for i in range(0, len(indices)): day = indices[i] # sort by predicted increment per stock of classifier clf_name topk, botk = selector_f(df_trade, day, clf_name, trading_params) old_positions = positions old_stash = stash stash, positions = update_positions(prices, day, old_stash, old_positions, botk, topk) pf = Portfolio(day, cash=stash, positions=positions) portfolios.append(pf) print("Finished trading for %s" % clf_name) return portfolios
def portfolio_asset(portfolio_id=None): user = User.get_by_email(session['email']) portfolio = user.get_portfolio() portfolio = Portfolio.from_mongo(portfolio_id) asset = portfolio.get_asset() return render_template('Aasset.html', posts=posts, portfolio_title=portfolio.title, portfolio_id=portfolio._id)
def _build_portfolio_from_cursor_output(self, output_row) -> Portfolio: portfolio_id = output_row[0] holder = output_row[1] name = output_row[2] cash = output_row[3] stock_transactions = self._build_stock_transactions_for_portfolio( portfolio_id) return Portfolio(portfolio_id, holder, name, cash, stock_transactions)
def test_update_portfolio(self): portfolio = Portfolio(1, 'test', 'myFirstPort', 200000.00, []) self.portfolio_repository.update_portfolio(portfolio) portfolio = self.portfolio_repository.get_portfolio_by_id(1) self.assertEqual(portfolio.portfolio_id, 1) self.assertEqual(portfolio.holder, 'test') self.assertEqual(portfolio.name, 'myFirstPort') self.assertEqual(portfolio.cash, 200000.00)
def test_add_portfolio(self): portfolio = Portfolio(None, 'test', 'myThirdPort', 15000.00, []) self.portfolio_repository.add_portfolio(portfolio) portfolio = self.portfolio_repository.get_portfolio_by_id(portfolio.portfolio_id) self.assertEqual(portfolio.portfolio_id, 3) self.assertEqual(portfolio.holder, 'test') self.assertEqual(portfolio.name, 'myThirdPort') self.assertEqual(portfolio.cash, 15000.00)
def create_portfolio(self, algorithm_name): date_time = datetime.now() # Create Portfolio Object uid = str(uuid.uuid4()) portfolio = Portfolio(date_time=date_time, algorithm=algorithm_name, risk=self.risk_score, link=uid) # portfolio.id = db.session.query(Portfolio).count() + 1 return portfolio
def create_portfolio_for_user(self, user_id: str, name: str, cash: int = 0) -> Portfolio: if self._portfolio_repository.has_portfolio_by_user_and_name(user_id, name): match = re.search(r'(.*\()([0-9]+)\)', name) if match: new_name = f'{match.group(1)}{int(match.group(2)) + 1})' else: new_name = f'{name} (2)' return self.create_portfolio_for_user(user_id, new_name, cash) portfolio = Portfolio(None, user_id, name, cash, []) self._portfolio_repository.add_portfolio(portfolio) return portfolio
import sys import os from analysis.backtest.strategies.technical_analysis.moving_average import MovingAverageStrategy from datetime import datetime import pytz from models.portfolio import Portfolio if __name__ == "__main__": sys.path.append(os.getcwd() + '\\') start = datetime(2014, 1, 1, 0, 0, 0, 0, pytz.utc) end = datetime(2019, 1, 1, 0, 0, 0, 0, pytz.utc) assets = ['MSFT', 'TSX:MSI'] benchmark = 'MSFT' portfolio = Portfolio(assets, benchmark) mov_avg_strategy = MovingAverageStrategy() mov_avg_strategy.run(portfolio, start, end) print(portfolio.performance) print(portfolio.performance[['portfolio_value', 'benchmark_period_return']])
def main(ticker): """engine to run script""" TIME_DEBUG = 0 MAX_ITER_DEBUG = 1000 CONTROL_BOUGHT = None CONTROL_SALE = None CONTROL_BEGIN = None CONTROL_END = None market_data_client = MarketDataClient('data/stooq_hourly/wmt.us.txt', ticker) market_data_stream = MarketDataStream(STREAM_PERIOD) portfolio = Portfolio([ticker]) decision_client = AlgoSignals() try: for _ in range(WARMUP_DATA_COUNT): # saturate the stream tick_data = market_data_client.get_parsed_tick_data() market_data_stream.update( tick_data['open'], tick_data['high'], tick_data['low'], tick_data['close']) CONTROL_BEGIN = tick_data['date'] CONTROL_BOUGHT = tick_data['open'] sma = SimpleMovingAverage() stochk = StochasticOscillatorK() stochd = StochasticOscillatorD() stochdslow = StochasticOscillatorD() rsi = RSI(market_data_stream.open, market_data_stream.close) while True: tick_data = market_data_client.get_parsed_tick_data() market_data_stream.update( tick_data['open'], tick_data['high'], tick_data['low'], tick_data['close']) sma.calc(market_data_stream.close) stochk.calc(market_data_stream.high, market_data_stream.low, market_data_stream.close) stochd.calc(stochk) stochdslow.calc(stochd) rsi.calc(market_data_stream.open, market_data_stream.close) if ( sma.saturated() and stochk.saturated() and stochd.saturated() and rsi.saturated() ): # ASSET TRANSACTION LOGIC, THIS CUSTOM LOGIC ENSURES # THAT BUYING AND SELLING CANNOT HAPPEN AT THE SAME TIME # this is changeable decision = decision_client.buy( portfolio, market_data_client.ticker, market_data_stream.close.curr(), rsi, stochk, stochd) TIME_DEBUG += 1 MAX_ITER_DEBUG -= 1 if decision['make_transaction']: print 'Buying {} at {} after {} ticks'.format( decision['amount'], decision['price'], TIME_DEBUG) TIME_DEBUG = 0 print portfolio.cash print sma print stochk print stochd print stochdslow print rsi portfolio.purchase( market_data_client.ticker, decision['amount'], decision['price']) #input('Press enter to continue: ') continue decision = decision_client.sell( portfolio, market_data_client.ticker, market_data_stream.close.curr(), rsi, stochk, stochd) if decision['make_transaction']: print 'Selling {} at {} after {} ticks'.format( decision['amount'], decision['price'], TIME_DEBUG) portfolio.sale(ticker, decision['amount'], decision['price']) print 'cash at hand: {}'.format(portfolio.cash) TIME_DEBUG = 0 #input('Press enter to continue: ') if portfolio.cash < 0: raise Exception('wtf, you really messed up') if MAX_ITER_DEBUG < 0: CONTROL_END = tick_data['date'] raise IndexError('normal') CONTROL_SALE = tick_data['close'] except IndexError: # terrible hack to know when we reach end of file print 'BOUGHT AT {}, SOLD AT {}'.format(CONTROL_BOUGHT, CONTROL_SALE) print 'from {} to {}'.format(CONTROL_BEGIN, CONTROL_END)
def get_portfolio(self): return Portfolio.find_by_author_id(self._id)
def new_asset(portfolio_id, asset_name, asset_weight): #, date=datetime.datetime.utcnow()): portfolio = Portfolio.from_mongo(portfolio_id) portfolio.new_asset(asset_name=asset_name, asset_weight=asset_weight)
def setup(self): self.p = Portfolio()
from analysis.backtest.strategies.modern_portfolio_theory.capm import CAPM from models.portfolio import Portfolio if __name__ == "__main__": sys.path.append(os.getcwd() + '\\') # markowitz model # start = datetime(2010, 1, 1, 0, 0, 0, 0, pytz.utc) # end = datetime(2017, 1, 1, 0, 0, 0, 0, pytz.utc) benchmark = '^GSPC' stocks = ['AAPL', 'MSFT', 'WMT', 'AMZN'] p = Portfolio(stocks, benchmark) p.download_historical_data() print(p.historical_data) m = MarkowitzModel(p) m.generate_portfolios() m.plot_portfolios() m.find_optimal_weights() print(m.optimal_weights) p.weights = m.optimal_weights m.print_optimal_portfolio() m.plot_optimal_portfolio() # CAPM capm = CAPM(p) capm.run() capm.plot()
class Environment(gym.Env): def __init__(self, started_money, data, risk_level=0.1, index_col='', names=(), seq_len=20): self.data = data self.index_col = index_col self.names = names self.seq_len = seq_len if risk_level > 1.0 or not isinstance(risk_level, float): raise ValueError('Risk can be only in 0.01..1.0 range.') self.risk_level = risk_level self.portfolio = Portfolio(started_money) self.dao = None self.done = None self.current_state = None self.current_price = None self.reset() self.action_space = gym.spaces.Discrete(3) self.actions = {0: self.buy, 1: self.sell, 2: self.hold} # TODO: define observation space # self.observation_space = gym.spaces.Discrete() def buy(self, count): if self.portfolio.bankroll < count * self.current_price: return -1 return self.portfolio.open_order('ETH', count, self.current_price) def sell(self): if not self.portfolio.opened_orders: return -1 return self.portfolio.close_order(self.current_price) def hold(self): if self.portfolio.opened_orders: buy_price = self.portfolio.opened_orders[0]['buy_price'] price_diff = np.abs((buy_price - self.current_price) / ((buy_price + self.current_price) / 2)) if price_diff > 0.2: # add this to a Strategy model return -1 return 0 def reset(self): self.dao = DataAccessObject(self.data, self.index_col, self.names, self.seq_len) self.done = False def update_state(self): try: self.current_state = next(self.dao.sequential_data) self.current_price = next(self.dao.close_price_iterator) except StopIteration: self.done = True @staticmethod def _clip_reward(reward): if reward > 0: reward = 1 elif reward < 0: reward = -1 else: reward = 0 return reward def step(self, action): if action == 0: # buy buy_count = (self.portfolio.bankroll * self.risk_level) / self.current_price reward = self.buy(buy_count) self.update_state() elif action == 1: # sell reward = self.sell() self.update_state() else: # hold reward = self.hold() self.update_state() return self.current_state, self._clip_reward( reward), self.done, self.portfolio.__str__()
year = int('20' + date[-2:]) month = int(date[3:-3]) day = int(date[0:2]) return datetime.replace(year = year, month = month, day = day).format('YYYY-MM-DDTHH:mm:ss') def get_index_value_history(csv_name): date_prices = [] with open(csv_name, newline='') as fd: reader = csv.reader(fd) for nrow, row in enumerate(reader): date_prices.append({'created_at': datetime_from_string(row[0]), 'portfolio_value': float(row[1])}) for indx, sector in enumerate(SECTORS): date_prices[nrow][sector] = float(row[2 + indx]) return date_prices if __name__ == '__main__': INDEX_VALUATIONS = '../csvs/index_valuations.csv' user = User.where('email', 'admin@alpha50').first() portfolio = Portfolio.where('name', 'Alpha50').where('user_id', user.id).first() valuations = get_index_value_history(INDEX_VALUATIONS) for valuation in valuations: valuation['portfolio_id'] = portfolio.id PortfolioValuation.create(valuation)
#!/usr/bin/env python3 import os, sys, inspect currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) parentdir = os.path.dirname(currentdir) sys.path.insert(0,parentdir) import arrow from models.stock import Stock from models.portfolio import Portfolio from models.portfolio_valuation import PortfolioValuation SECTORS = ['information_technology', 'energy', 'consumer_discretionary', 'health_care', 'industrials', 'telecommunications_services', 'financials', 'consumer_staples'] for portfolio in Portfolio.all(): total_value = portfolio.total_cash portfolio_info = {} for sector in SECTORS: portfolio_info[sector] = 0 portfolio_info['portfolio_id'] = portfolio.id for stock in portfolio.stocks_portfolios().get_results(): market_stock = Stock.find(stock.stock_id) sector = market_stock.sector.lower().replace(' ', '_') if not market_stock.latest_price: market_stock.latest_price = 0 total_value += market_stock.latest_price * stock.quantity_held portfolio_info[sector] += market_stock.latest_price * stock.quantity_held portfolio_info['portfolio_value'] = total_value portfolio_info['created_at'] = arrow.now('US/Pacific').format('YYYY-MM-DDTHH:mm:ss') PortfolioValuation.create(portfolio_info)