本文整理汇总了Python中qstrader.price_parser.PriceParser类的典型用法代码示例。如果您正苦于以下问题:Python PriceParser类的具体用法?Python PriceParser怎么用?Python PriceParser使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了PriceParser类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: size_order
def size_order(self, portfolio, initial_order):
"""
Size the order to reflect the dollar-weighting of the
current equity account size based on pre-specified
ticker weights.
"""
ticker = initial_order.ticker
if initial_order.action == "EXIT":
# Obtain current quantity and liquidate
cur_quantity = portfolio.positions[ticker].quantity
if cur_quantity > 0:
initial_order.action = "SLD"
initial_order.quantity = cur_quantity
else:
initial_order.action = "BOT"
initial_order.quantity = cur_quantity
else:
weight = self.ticker_weights[ticker]
# Determine total portfolio value, work out dollar weight
# and finally determine integer quantity of shares to purchase
price = portfolio.price_handler.tickers[ticker]["adj_close"]
price = PriceParser.display(price)
equity = PriceParser.display(portfolio.equity)
dollar_weight = weight * equity
weighted_quantity = int(floor(dollar_weight / price))
initial_order.quantity = weighted_quantity
return initial_order
示例2: setUp
def setUp(self):
"""
Set up the Position object that will store the PnL.
"""
self.position = Position(
"BOT", "XOM", 100,
PriceParser.parse(74.78), PriceParser.parse(1.00),
PriceParser.parse(74.78), PriceParser.parse(74.80)
)
示例3: test_get_best_bid_ask
def test_get_best_bid_ask(self):
"""
Tests that the 'get_best_bid_ask' method produces the
correct values depending upon validity of ticker.
"""
bid, ask = self.price_handler.get_best_bid_ask("AMZN")
self.assertEqual(PriceParser.display(bid, 5), 502.10001)
self.assertEqual(PriceParser.display(ask, 5), 502.11999)
bid, ask = self.price_handler.get_best_bid_ask("C")
示例4: record_trade
def record_trade(self, fill):
"""
Append all details about the FillEvent to the CSV trade log.
"""
fname = os.path.expanduser(os.path.join(self.config.OUTPUT_DIR, self.csv_filename))
with open(fname, 'a') as csvfile:
writer = csv.writer(csvfile)
writer.writerow([
fill.timestamp, fill.ticker,
fill.action, fill.quantity,
fill.exchange, PriceParser.display(fill.price, 4),
PriceParser.display(fill.commission, 4)
])
示例5: test_open_short_position
def test_open_short_position(self):
self.assertEqual(PriceParser.display(self.position.cost_basis), -7768.00)
self.assertEqual(PriceParser.display(self.position.market_value), -7769.00)
self.assertEqual(PriceParser.display(self.position.unrealised_pnl), -1.00)
self.assertEqual(PriceParser.display(self.position.realised_pnl), -1.0)
self.position.update_market_value(
PriceParser.parse(77.72), PriceParser.parse(77.72)
)
self.assertEqual(PriceParser.display(self.position.cost_basis), -7768.00)
self.assertEqual(PriceParser.display(self.position.market_value), -7772.00)
self.assertEqual(PriceParser.display(self.position.unrealised_pnl), -4.00)
self.assertEqual(PriceParser.display(self.position.realised_pnl), -4.0)
示例6: test_price_from_long
def test_price_from_long(self):
parsed = PriceParser.parse(self.long)
self.assertEqual(parsed, 200)
if PY2:
self.assertIsInstance(parsed, long) # noqa
else:
self.assertIsInstance(parsed, int)
示例7: run
def run(config, testing, tickers, filename):
# Benchmark ticker
benchmark = 'SP500TR'
# Set up variables needed for backtest
title = [
'Moving Average Crossover Example',
__file__,
','.join(tickers) + ': 100x400'
]
events_queue = queue.Queue()
csv_dir = config.CSV_DATA_DIR
initial_equity = PriceParser.parse(500000.00)
# Use Yahoo Daily Price Handler
price_handler = YahooDailyCsvBarPriceHandler(
csv_dir, events_queue, tickers
)
# Use the MAC Strategy
strategy = MovingAverageCrossStrategy(tickers, events_queue)
# Use an example Position Sizer,
position_sizer = FixedPositionSizer()
# Use an example Risk Manager,
risk_manager = ExampleRiskManager()
# Use the default Portfolio Handler
portfolio_handler = PortfolioHandler(
initial_equity, events_queue, price_handler,
position_sizer, risk_manager
)
# Use the ExampleCompliance component
compliance = ExampleCompliance(config)
# Use a simulated IB Execution Handler
execution_handler = IBSimulatedExecutionHandler(
events_queue, price_handler, compliance
)
# Use the default Statistics
statistics = TearsheetStatistics(
config, portfolio_handler, title, benchmark
)
# Set up the backtest
backtest = Backtest(
price_handler, strategy,
portfolio_handler, execution_handler,
position_sizer, risk_manager,
statistics, initial_equity
)
results = backtest.simulate_trading(testing=testing)
statistics.save(filename)
return results
示例8: run
def run(cache_name, cache_backend, expire_after, data_source, start, end, config, testing, tickers, filename, n, n_window):
# Set up variables needed for backtest
events_queue = queue.Queue()
initial_equity = PriceParser.parse(500000.00)
session = init_session(cache_name, cache_backend, expire_after)
period = 86400 # Seconds in a day
if len(tickers) == 1:
data = web.DataReader(tickers[0], data_source, start, end, session=session)
else:
data = web.DataReader(tickers, data_source, start, end, session=session)
# Use Generic Bar Handler with Pandas Bar Iterator
price_event_iterator = PandasBarEventIterator(data, period, tickers[0])
price_handler = GenericPriceHandler(events_queue, price_event_iterator)
# Use the Display Strategy
strategy1 = DisplayStrategy(n=n, n_window=n_window)
strategy2 = BuyAndHoldStrategy(tickers, events_queue)
strategy = Strategies(strategy1, strategy2)
# Use an example Position Sizer
position_sizer = FixedPositionSizer()
# Use an example Risk Manager
risk_manager = ExampleRiskManager()
# Use the default Portfolio Handler
portfolio_handler = PortfolioHandler(
initial_equity, events_queue, price_handler,
position_sizer, risk_manager
)
# Use the ExampleCompliance component
compliance = ExampleCompliance(config)
# Use a simulated IB Execution Handler
execution_handler = IBSimulatedExecutionHandler(
events_queue, price_handler, compliance
)
# Use the default Statistics
statistics = SimpleStatistics(config, portfolio_handler)
# Set up the backtest
backtest = Backtest(
price_handler, strategy,
portfolio_handler, execution_handler,
position_sizer, risk_manager,
statistics, initial_equity
)
results = backtest.simulate_trading(testing=testing)
statistics.save(filename)
return results
示例9: setUp
def setUp(self):
"""
Set up the Portfolio object that will store the
collection of Position objects, supplying it with
$500,000.00 USD in initial cash.
"""
ph = PriceHandlerMock()
cash = PriceParser.parse(500000.00)
self.portfolio = Portfolio(ph, cash)
示例10: run
def run(config, testing, tickers, filename):
# Set up variables needed for backtest
events_queue = queue.Queue()
csv_dir = config.CSV_DATA_DIR
initial_equity = PriceParser.parse(500000.00)
# Use Yahoo Daily Price Handler
price_handler = YahooDailyCsvBarPriceHandler(
csv_dir, events_queue, tickers
)
# Use the Buy and Hold Strategy
strategy = BuyAndHoldStrategy(tickers, events_queue)
strategy = Strategies(strategy, DisplayStrategy())
# Use an example Position Sizer
position_sizer = FixedPositionSizer()
# Use an example Risk Manager
risk_manager = ExampleRiskManager()
# Use the default Portfolio Handler
portfolio_handler = PortfolioHandler(
initial_equity, events_queue, price_handler,
position_sizer, risk_manager
)
# Use the ExampleCompliance component
compliance = ExampleCompliance(config)
# Use a simulated IB Execution Handler
execution_handler = IBSimulatedExecutionHandler(
events_queue, price_handler, compliance
)
# Use the default Statistics
statistics = SimpleStatistics(config, portfolio_handler)
# Set up the backtest
backtest = Backtest(
price_handler, strategy,
portfolio_handler, execution_handler,
position_sizer, risk_manager,
statistics, initial_equity
)
results = backtest.simulate_trading(testing=testing)
statistics.save(filename)
return results
示例11: test_stream_all_ticks
def test_stream_all_ticks(self):
"""
The initialisation of the class will open the three
test CSV files, then merge and sort them. They will
then be stored in a member "tick_stream". This will
be used for streaming the ticks.
"""
# Stream to Tick #1 (GOOG)
self.price_handler.stream_next()
self.assertEqual(
self.price_handler.tickers["GOOG"]["timestamp"].strftime(
"%d-%m-%Y %H:%M:%S.%f"
),
"01-02-2016 00:00:01.358000"
)
self.assertEqual(
PriceParser.display(self.price_handler.tickers["GOOG"]["bid"], 5),
683.56000
)
self.assertEqual(
PriceParser.display(self.price_handler.tickers["GOOG"]["ask"], 5),
683.58000
)
# Stream to Tick #2 (AMZN)
self.price_handler.stream_next()
self.assertEqual(
self.price_handler.tickers["AMZN"]["timestamp"].strftime(
"%d-%m-%Y %H:%M:%S.%f"
),
"01-02-2016 00:00:01.562000"
)
self.assertEqual(
PriceParser.display(self.price_handler.tickers["AMZN"]["bid"], 5),
502.10001
)
self.assertEqual(
PriceParser.display(self.price_handler.tickers["AMZN"]["ask"], 5),
502.11999
)
# Stream to Tick #3 (MSFT)
self.price_handler.stream_next()
self.assertEqual(
self.price_handler.tickers["MSFT"]["timestamp"].strftime(
"%d-%m-%Y %H:%M:%S.%f"
),
"01-02-2016 00:00:01.578000"
)
self.assertEqual(
PriceParser.display(self.price_handler.tickers["MSFT"]["bid"], 5),
50.14999
)
self.assertEqual(
PriceParser.display(self.price_handler.tickers["MSFT"]["ask"], 5),
50.17001
)
# Stream to Tick #10 (GOOG)
for i in range(4, 11):
self.price_handler.stream_next()
self.assertEqual(
self.price_handler.tickers["GOOG"]["timestamp"].strftime(
"%d-%m-%Y %H:%M:%S.%f"
),
"01-02-2016 00:00:05.215000"
)
self.assertEqual(
PriceParser.display(self.price_handler.tickers["GOOG"]["bid"], 5),
683.56001
)
self.assertEqual(
PriceParser.display(self.price_handler.tickers["GOOG"]["ask"], 5),
683.57999
)
# Stream to Tick #20 (GOOG)
for i in range(11, 21):
self.price_handler.stream_next()
self.assertEqual(
self.price_handler.tickers["MSFT"]["timestamp"].strftime(
"%d-%m-%Y %H:%M:%S.%f"
),
"01-02-2016 00:00:09.904000"
)
self.assertEqual(
PriceParser.display(self.price_handler.tickers["MSFT"]["bid"], 5),
50.15000
)
self.assertEqual(
PriceParser.display(self.price_handler.tickers["MSFT"]["ask"], 5),
50.17000
)
# Stream to Tick #30 (final tick, AMZN)
for i in range(21, 31):
self.price_handler.stream_next()
self.assertEqual(
self.price_handler.tickers["AMZN"]["timestamp"].strftime(
"%d-%m-%Y %H:%M:%S.%f"
#.........这里部分代码省略.........
示例12: test_realised_unrealised_calcs
def test_realised_unrealised_calcs(self):
self.assertEqual(
PriceParser.display(self.position.unrealised_pnl), -1.00
)
self.assertEqual(
PriceParser.display(self.position.realised_pnl), 0.00
)
self.position.update_market_value(
PriceParser.parse(75.77), PriceParser.parse(75.79)
)
self.assertEqual(
PriceParser.display(self.position.unrealised_pnl), 99.00
)
self.position.transact_shares(
"SLD", 100,
PriceParser.parse(75.78), PriceParser.parse(1.00)
)
self.assertEqual(
PriceParser.display(self.position.unrealised_pnl), 99.00
) # still high
self.assertEqual(
PriceParser.display(self.position.realised_pnl), 98.00
)
self.position.update_market_value(
PriceParser.parse(75.77), PriceParser.parse(75.79)
)
self.assertEqual(
PriceParser.display(self.position.unrealised_pnl), 0.00
)
示例13: test_unparsed_display
def test_unparsed_display(self):
displayed = PriceParser.display(self.float)
self.assertEqual(displayed, 10.12)
示例14: test_display
def test_display(self):
parsed = PriceParser.parse(self.float)
displayed = PriceParser.display(parsed)
self.assertEqual(displayed, 10.12)
示例15: test_rounded_float
def test_rounded_float(self):
parsed = PriceParser.parse(self.rounded_float)
# Expect 100,000,000
self.assertEqual(parsed, 100000000)
self.assertIsInstance(parsed, int)