本文整理汇总了Python中qstrader.price_parser.PriceParser类的典型用法代码示例。如果您正苦于以下问题:Python PriceParser类的具体用法?Python PriceParser怎么用?Python PriceParser使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了PriceParser类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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
开发者ID:Gwill,项目名称:qstrader,代码行数:27,代码来源:rebalance.py
示例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)
)
开发者ID:femtotrader,项目名称:qstrader,代码行数:9,代码来源:test_position.py
示例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")
开发者ID:barrygolden,项目名称:qstrader,代码行数:10,代码来源:test_price_handler.py
示例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)
])
开发者ID:Gwill,项目名称:qstrader,代码行数:13,代码来源:example.py
示例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)
开发者ID:femtotrader,项目名称:qstrader,代码行数:14,代码来源:test_position.py
示例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)
开发者ID:mhallsmoore,项目名称:qstrader,代码行数:7,代码来源:test_priceparser.py
示例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
开发者ID:mhallsmoore,项目名称:qstrader,代码行数:58,代码来源:mac_backtest_tearsheet.py
示例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
开发者ID:femtotrader,项目名称:qstrader,代码行数:57,代码来源:pandas_bar_display_prices_backtest.py
示例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)
开发者ID:Gwill,项目名称:qstrader,代码行数:9,代码来源:test_portfolio.py
示例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
开发者ID:MeanRev99,项目名称:qstrader,代码行数:49,代码来源:buy_and_hold_backtest.py
示例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"
#.........这里部分代码省略.........
开发者ID:barrygolden,项目名称:qstrader,代码行数:101,代码来源:test_price_handler.py
示例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
)
开发者ID:Gwill,项目名称:qstrader,代码行数:31,代码来源:test_position.py
示例13: test_unparsed_display
def test_unparsed_display(self):
displayed = PriceParser.display(self.float)
self.assertEqual(displayed, 10.12)
开发者ID:MeanRev99,项目名称:qstrader,代码行数:3,代码来源:test_priceparser.py
示例14: test_display
def test_display(self):
parsed = PriceParser.parse(self.float)
displayed = PriceParser.display(parsed)
self.assertEqual(displayed, 10.12)
开发者ID:MeanRev99,项目名称:qstrader,代码行数:4,代码来源:test_priceparser.py
示例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)
开发者ID:MeanRev99,项目名称:qstrader,代码行数:5,代码来源:test_priceparser.py
示例16: test_price_from_int
def test_price_from_int(self):
parsed = PriceParser.parse(self.int)
self.assertEqual(parsed, 200)
self.assertIsInstance(parsed, int)
开发者ID:MeanRev99,项目名称:qstrader,代码行数:4,代码来源:test_priceparser.py
示例17: test_calculate_round_trip
def test_calculate_round_trip(self):
"""
Purchase/sell multiple lots of AMZN and GOOG
at various prices/commissions to check the
arithmetic and cost handling.
"""
# Buy 300 of AMZN over two transactions
self.portfolio.transact_position(
"BOT", "AMZN", 100,
PriceParser.parse(566.56), PriceParser.parse(1.00)
)
self.portfolio.transact_position(
"BOT", "AMZN", 200,
PriceParser.parse(566.395), PriceParser.parse(1.00)
)
# Buy 200 GOOG over one transaction
self.portfolio.transact_position(
"BOT", "GOOG", 200,
PriceParser.parse(707.50), PriceParser.parse(1.00)
)
# Add to the AMZN position by 100 shares
self.portfolio.transact_position(
"SLD", "AMZN", 100,
PriceParser.parse(565.83), PriceParser.parse(1.00)
)
# Add to the GOOG position by 200 shares
self.portfolio.transact_position(
"BOT", "GOOG", 200,
PriceParser.parse(705.545), PriceParser.parse(1.00)
)
# Sell 200 of the AMZN shares
self.portfolio.transact_position(
"SLD", "AMZN", 200,
PriceParser.parse(565.59), PriceParser.parse(1.00)
)
# Multiple transactions bundled into one (in IB)
# Sell 300 GOOG from the portfolio
self.portfolio.transact_position(
"SLD", "GOOG", 100,
PriceParser.parse(704.92), PriceParser.parse(1.00)
)
self.portfolio.transact_position(
"SLD", "GOOG", 100,
PriceParser.parse(704.90), PriceParser.parse(0.00)
)
self.portfolio.transact_position(
"SLD", "GOOG", 100,
PriceParser.parse(704.92), PriceParser.parse(0.50)
)
# Finally, sell the remaining GOOG 100 shares
self.portfolio.transact_position(
"SLD", "GOOG", 100,
PriceParser.parse(704.78), PriceParser.parse(1.00)
)
# The figures below are derived from Interactive Brokers
# demo account using the above trades with prices provided
# by their demo feed.
self.assertEqual(len(self.portfolio.positions), 0)
self.assertEqual(len(self.portfolio.closed_positions), 2)
self.assertEqual(PriceParser.display(self.portfolio.cur_cash), 499100.50)
self.assertEqual(PriceParser.display(self.portfolio.equity), 499100.50)
self.assertEqual(PriceParser.display(self.portfolio.unrealised_pnl), 0.00)
self.assertEqual(PriceParser.display(self.portfolio.realised_pnl), -899.50)
开发者ID:Gwill,项目名称:qstrader,代码行数:64,代码来源:test_portfolio.py
示例18: test_calculating_statistics
def test_calculating_statistics(self):
"""
Purchase/sell multiple lots of AMZN, GOOG
at various prices/commissions to ensure
the arithmetic in calculating equity, drawdowns
and sharpe ratio is correct.
"""
# Create Statistics object
price_handler = PriceHandlerMock()
self.portfolio = Portfolio(price_handler, PriceParser.parse(500000.00))
portfolio_handler = PortfolioHandlerMock(self.portfolio)
statistics = SimpleStatistics(self.config, portfolio_handler)
# Check initialization was correct
self.assertEqual(PriceParser.display(statistics.equity[0]), 500000.00)
self.assertEqual(PriceParser.display(statistics.drawdowns[0]), 00)
self.assertEqual(statistics.equity_returns[0], 0.0)
# Perform transaction and test statistics at this tick
self.portfolio.transact_position(
"BOT", "AMZN", 100,
PriceParser.parse(566.56), PriceParser.parse(1.00)
)
t = "2000-01-01 00:00:00"
statistics.update(t, portfolio_handler)
self.assertEqual(PriceParser.display(statistics.equity[1]), 499807.00)
self.assertEqual(PriceParser.display(statistics.drawdowns[1]), 193.00)
self.assertEqual(statistics.equity_returns[1], -0.0386)
# Perform transaction and test statistics at this tick
self.portfolio.transact_position(
"BOT", "AMZN", 200,
PriceParser.parse(566.395), PriceParser.parse(1.00)
)
t = "2000-01-02 00:00:00"
statistics.update(t, portfolio_handler)
self.assertEqual(PriceParser.display(statistics.equity[2]), 499455.00)
self.assertEqual(PriceParser.display(statistics.drawdowns[2]), 545.00)
self.assertEqual(statistics.equity_returns[2], -0.0705)
# Perform transaction and test statistics at this tick
self.portfolio.transact_position(
"BOT", "GOOG", 200,
PriceParser.parse(707.50), PriceParser.parse(1.00)
)
t = "2000-01-03 00:00:00"
statistics.update(t, portfolio_handler)
self.assertEqual(PriceParser.display(statistics.equity[3]), 499046.00)
self.assertEqual(PriceParser.display(statistics.drawdowns[3]), 954.00)
self.assertEqual(statistics.equity_returns[3], -0.0820)
# Perform transaction and test statistics at this tick
self.portfolio.transact_position(
"SLD", "AMZN", 100,
PriceParser.parse(565.83), PriceParser.parse(1.00)
)
t = "2000-01-04 00:00:00"
statistics.update(t, portfolio_handler)
self.assertEqual(PriceParser.display(statistics.equity[4]), 499164.00)
self.assertEqual(PriceParser.display(statistics.drawdowns[4]), 836.00)
self.assertEqual(statistics.equity_returns[4], 0.0236)
# Perform transaction and test statistics at this tick
self.portfolio.transact_position(
"BOT", "GOOG", 200,
PriceParser.parse(705.545), PriceParser.parse(1.00)
)
t = "2000-01-05 00:00:00"
statistics.update(t, portfolio_handler)
self.assertEqual(PriceParser.display(statistics.equity[5]), 499146.00)
self.assertEqual(PriceParser.display(statistics.drawdowns[5]), 854.00)
self.assertEqual(statistics.equity_returns[5], -0.0036)
# Perform transaction and test statistics at this tick
self.portfolio.transact_position(
"SLD", "AMZN", 200,
PriceParser.parse(565.59), PriceParser.parse(1.00)
)
t = "2000-01-06 00:00:00"
statistics.update(t, portfolio_handler)
self.assertEqual(PriceParser.display(statistics.equity[6]), 499335.00)
self.assertEqual(PriceParser.display(statistics.drawdowns[6]), 665.00)
self.assertEqual(statistics.equity_returns[6], 0.0379)
# Perform transaction and test statistics at this tick
self.portfolio.transact_position(
"SLD", "GOOG", 100,
PriceParser.parse(707.92), PriceParser.parse(1.00)
)
t = "2000-01-07 00:00:00"
statistics.update(t, portfolio_handler)
self.assertEqual(PriceParser.display(statistics.equity[7]), 499580.00)
self.assertEqual(PriceParser.display(statistics.drawdowns[7]), 420.00)
self.assertEqual(statistics.equity_returns[7], 0.0490)
# Perform transaction and test statistics at this tick
self.portfolio.transact_position(
"SLD", "GOOG", 100,
PriceParser.parse(707.90), PriceParser.parse(0.00)
#.........这里部分代码省略.........
开发者ID:Gwill,项目名称:qstrader,代码行数:101,代码来源:test_statistics.py
示例19: 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)
start_date = datetime.datetime(2006, 11, 1)
end_date = datetime.datetime(2016, 10, 12)
# Use Yahoo Daily Price Handler
price_handler = YahooDailyCsvBarPriceHandler(
csv_dir, events_queue, tickers,
start_date=start_date, end_date=end_date
)
# Use the monthly liquidate and rebalance strategy
strategy = MonthlyLiquidateRebalanceStrategy(tickers, events_queue)
strategy = Strategies(strategy, DisplayStrategy())
# Use the liquidate and rebalance position sizer
# with prespecified ticker weights
ticker_weights = {
"SPY": 0.6,
"AGG": 0.4,
}
position_sizer = LiquidateRebalancePositionSizer(ticker_weights)
# 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
title = ["US Equities/Bonds 60/40 ETF Strategy"]
benchmark = "SPY"
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
开发者ID:femtotrader,项目名称:qstrader,代码行数:61,代码来源:monthly_liquidate_rebalance_backtest.py
示例20: setUp
def setUp(self):
self.position = Position(
"BOT", "XOM", 100,
PriceParser.parse(74.78), PriceParser.parse(1.00),
PriceParser.parse(74.77), PriceParser.parse(74.79)
)
开发者ID:Gwill,项目名称:qstrader,代码行数:6,代码来源:test_position.py
注:本文中的qstrader.price_parser.PriceParser类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论