def __init__(self, stock=None): if stock is None: self._stock = Stock() else: self._stock = stock self._orders = [] self._completed = []
def simulate_market(): all_trades = [] for item in TEST_DATA: current_trade_raw = item.pop('trade') current_stock = Stock(**item) current_trade_raw['stock'] = current_stock current_trade = Trade(**current_trade_raw) all_trades.append(current_trade) return all_trades
def __init__(self): self._id = 0 self._order = [ ] # a list of Order. order's id should be in increasing order # self._category = ['Mains': self._mainsMenu, 'Drinks': self._drinksMenu, 'Sides': self._sidesMenu] self._name = "" # Restaurant name self._mainsMenu = [] # a list of Mains self._drinksMenu = [] # a list of drinks self._sidesMenu = [] # a list of sides self._stock = Stock() self._statusList = [ 'Not Submitted', 'Pending', 'Preparing', 'Ready', 'Picked Up' ]
def test_price_should_be_null(self): stock = Stock("GooG") self.assertIsNone(stock.price)
def test_negative_price_should_throw_ValueError(self): goog = Stock("GooG") with self.assertRaises(ValueError): goog.update(datetime(2014, 2, 3), -1)
def test_stock_update(self): goog = Stock("GooG") goog.update(datetime(2014, 2, 12), 10) self.assertEqual(10, goog.price)
import sys from os.path import abspath, join, dirname sys.path.insert(0, join(abspath(dirname(__file__)), '..')) dirpath = join(abspath(dirname(__file__)), '..') outpath = join(dirpath, 'out') from src.stock import Stock from src.simulate_pool import run_simulate_pool from src.test_strategy_pool import get_strategy_pool from src.log import init_log logging = init_log() stock = Stock() def get_stock_pool(): return [ '600000.SH', '002032.SZ', '600030.SH', '601318.SH', '000001.SZ', '000002.SZ', '600612.SH', '600188.SH', '600340.SH', '600048.SH', '000048.SH', '601966.SH',
import sys from os.path import abspath, join, dirname sys.path.insert(0, join(abspath(dirname(__file__)), '..')) print(sys.path) from src.stock import Stock import pandas as pd sys.path.append("..") tsStock = Stock() keyword = '000001' df = tsStock.get_daily_data(keyword, '2019-01-02', '') print(df) # start_date = pd.to_datetime('20100101'); # print (start_date) # # print (start_date.strftime("%Y-%m-%d"))
def test_stock(self): # we will buy 10 stocks of apple on 2019-12-02 # we will sell 1 stock on 2019-12-03 # we will sell 9 stocks on 2019-12-09 # Open prices # 2019-12-02: 267.269989 # 2019-12-03: 258.309998 # 2019-12-09: 270.000000 test_stock = Stock('AAPL') self.assertEqual(test_stock.get_symbol(), 'AAPL') #get price test_price = test_stock.get_price('2019-12-02') self.assertAlmostEqual(test_price, 267.269989, places=2) # get price for a range test_price_df = test_stock.get_price_history(start_date='2019-12-01', end_date='2019-12-09') #market was open on 02,03,04,05,06,09:6 rows self.assertEqual(test_price_df.shape[0], 6) # buy 10 test_stock.buy(date='2019-12-02', num=10) self.assertAlmostEqual(test_stock.get_total_buy_cost(), 2672.69989, places=2) self.assertAlmostEqual(test_stock.get_total_sell_cost(), 0., places=2) # sell 1 test_stock.sell(date='2019-12-03', num=1) self.assertEqual(test_stock.is_held(), True) self.assertAlmostEqual(test_stock.get_total_buy_cost(), 2672.69989, places=2) self.assertAlmostEqual(test_stock.get_total_sell_cost(), 258.309998, places=2) # 258.309998 * 9 self.assertAlmostEqual(test_stock.get_valuation('2019-12-03'), 2324.7899820000002, places=2) # sell the remaining one and check not held test_stock.sell(date='2019-12-09', num=9) self.assertEqual(test_stock.is_held(), False) self.assertAlmostEqual(test_stock.get_total_buy_cost(), 2672.69989, places=2) # 258.309998 * 1 + 270.000000 * 9 self.assertAlmostEqual(test_stock.get_total_sell_cost(), 2688.309998, places=2) self.assertAlmostEqual(test_stock.get_valuation('2019-12-09'), 0., places=2)
def test_ma_factor(self): test_stock = Stock('AAPL') ma_fac = MovingAverageFactor(short_term=20, long_term=100) ma_1 = ma_fac(stock=test_stock, end_date='2019-11-15')
def test_linreg_factor(self): test_stock = Stock('AAPL') linreg = LinRegFactor(num_days=30) beta = linreg(stock=test_stock, end_date='2019-11-15')