def initialize(self, sids_and_amounts, *args, **kwargs): self.ordered = False self.sids_and_amounts = sids_and_amounts self.set_commission(us_equities=PerTrade(0), us_futures=PerTrade(0)) self.set_slippage( us_equities=FixedSlippage(0), us_futures=FixedSlippage(0), )
def test_slippage_and_commission_dispatching(self): blotter = SimulationBlotter( self.sim_params, equity_slippage=FixedSlippage(spread=0.0), future_slippage=FixedSlippage(spread=2.0), equity_commission=PerTrade(cost=1.0), future_commission=PerTrade(cost=2.0), ) blotter.order(self.asset_24, 1, MarketOrder()) blotter.order(self.future_cl, 1, MarketOrder()) bar_data = self.create_bardata( simulation_dt_func=lambda: self.sim_params.sessions[-1], ) txns, commissions, _ = blotter.get_transactions(bar_data) # The equity transaction should have the same price as its current # price because the slippage spread is zero. Its commission should be # $1.00. equity_txn = txns[0] self.assertEqual( equity_txn.price, bar_data.current(equity_txn.asset, 'price'), ) self.assertEqual(commissions[0]['cost'], 1.0) # The future transaction price should be 1.0 more than its current # price because half of the 'future_slippage' spread is added. Its # commission should be $2.00. future_txn = txns[1] self.assertEqual( future_txn.price, bar_data.current(future_txn.asset, 'price') + 1.0, ) self.assertEqual(commissions[1]['cost'], 2.0)
def test_per_trade(self): model = PerTrade(cost=10) order, txns = self.generate_order_and_txns() self.assertEqual(10, model.calculate(order, txns[0])) order.commission = 10 self.assertEqual(0, model.calculate(order, txns[1])) self.assertEqual(0, model.calculate(order, txns[2]))
def test_per_trade(self): # Test per trade model for equities. model = PerTrade(cost=10) self.verify_per_trade_commissions(model, expected_commission=10, sid=1) # Test per trade model for futures. model = PerFutureTrade(cost=10) self.verify_per_trade_commissions( model, expected_commission=10, sid=1000, ) # Test per trade model with custom costs per future symbol. model = PerFutureTrade(cost={"CL": 5, "FV": 10}) self.verify_per_trade_commissions( model, expected_commission=5, sid=1000, ) self.verify_per_trade_commissions( model, expected_commission=10, sid=1001, )
def __init__(self, data_frequency, equity_slippage=None, future_slippage=None, equity_commission=None, future_commission=None, cancel_policy=None): # these orders are aggregated by asset self.open_orders = defaultdict(list) # keep a dict of orders by their own id self.orders = {} # holding orders that have come in since the last event. self.new_orders = [] self.current_dt = None self.max_shares = int(1e+11) self.slippage_models = { Equity: equity_slippage or VolumeShareSlippage(), Future: future_slippage or VolumeShareSlippage(), } self.commission_models = { Equity: equity_commission or PerShare(), Future: future_commission or PerTrade(cost=DEFAULT_FUTURE_COST_PER_TRADE, ), } self.data_frequency = data_frequency self.cancel_policy = cancel_policy if cancel_policy else NeverCancel()
def initialize(context): set_slippage(us_futures=InstantSlippage()) set_commission(us_futures=PerTrade(0)) context.contracts = [ continuous_future(contract, offset=0, adjustment='mul', roll='volume') for contract in contracts] context.min_max = {}
def initialize(context): set_slippage(us_futures=InstantSlippage()) set_commission(us_futures=PerTrade(0)) context.contracts = [ continuous_future(contract, offset=0, adjustment='mul', roll='volume') for contract in contracts ] context.min_max = {} # auxiliary variables selectively used in various portfolio optimization methods context.counter = 0 context.target_portfolio = pd.Series()
def initialize(self, tickers, basket): # To keep track of whether we invested in the stock or not self.invested = False self.commission = PerTrade(0.00) #self.starting_cash(10000) self.set_slippage(FixedSlippage()) self.capital_base = 10000 self.tickers = tickers self.basket = basket self.quantity = 0
def test_commission_event(self): with trading.TradingEnvironment(): events = factory.create_trade_history( 1, [10, 10, 10, 10, 10], [100, 100, 100, 100, 100], oneday, self.sim_params ) # Test commission models and validate result # Expected commission amounts: # PerShare commission: 1.00, 1.00, 1.50 = $3.50 # PerTrade commission: 5.00, 5.00, 5.00 = $15.00 # PerDollar commission: 1.50, 3.00, 4.50 = $9.00 # Total commission = $3.50 + $15.00 + $9.00 = $27.50 # Create 3 transactions: 50, 100, 150 shares traded @ $20 transactions = [create_txn(events[0], 20, i) for i in [50, 100, 150]] # Create commission models models = [PerShare(cost=0.01, min_trade_cost=1.00), PerTrade(cost=5.00), PerDollar(cost=0.0015)] # Aggregate commission amounts total_commission = 0 for model in models: for trade in transactions: total_commission += model.calculate(trade)[1] self.assertEqual(total_commission, 27.5) cash_adj_dt = self.sim_params.first_open \ + datetime.timedelta(hours=3) cash_adjustment = factory.create_commission(1, 300.0, cash_adj_dt) # Insert a purchase order. events.insert(0, create_txn(events[0], 20, 1)) events.insert(1, cash_adjustment) results = calculate_results(self, events) # Validate that we lost 320 dollars from our cash pool. self.assertEqual(results[-1]['cumulative_perf']['ending_cash'], 9680) # Validate that the cost basis of our position changed. self.assertEqual(results[-1]['daily_perf']['positions'] [0]['cost_basis'], 320.0)
def test_commission_event(self): with trading.TradingEnvironment(): events = factory.create_trade_history(1, [10, 10, 10, 10, 10], [100, 100, 100, 100, 100], oneday, self.sim_params) # Test commission models and validate result # Expected commission amounts: # PerShare commission: 1.00, 1.00, 1.50 = $3.50 # PerTrade commission: 5.00, 5.00, 5.00 = $15.00 # PerDollar commission: 1.50, 3.00, 4.50 = $9.00 # Total commission = $3.50 + $15.00 + $9.00 = $27.50 # Create 3 transactions: 50, 100, 150 shares traded @ $20 transactions = [ create_txn(events[0], 20, i) for i in [50, 100, 150] ] # Create commission models and validate that produce expected # commissions. models = [ PerShare(cost=0.01, min_trade_cost=1.00), PerTrade(cost=5.00), PerDollar(cost=0.0015) ] expected_results = [3.50, 15.0, 9.0] for model, expected in zip(models, expected_results): total_commission = 0 for trade in transactions: total_commission += model.calculate(trade)[1] self.assertEqual(total_commission, expected) # Verify that commission events are handled correctly by # PerformanceTracker. cash_adj_dt = events[0].dt cash_adjustment = factory.create_commission(1, 300.0, cash_adj_dt) events.append(cash_adjustment) # Insert a purchase order. txns = [create_txn(events[0], 20, 1)] results = calculate_results(self, events, txns=txns) # Validate that we lost 320 dollars from our cash pool. self.assertEqual(results[-1]['cumulative_perf']['ending_cash'], 9680) # Validate that the cost basis of our position changed. self.assertEqual( results[-1]['daily_perf']['positions'][0]['cost_basis'], 320.0)
def initialize(self): self.invested = False self.trade_size = 1000 self.long = False self.short = False self.closed = False self.trading_day_counter = 0 self.pct_stop = 0.025 self.long_stop_price = 0.0 self.short_stop_price = 0.0 self.target = 0.05 commission_cost = self.trade_size * COMMISSION self.set_slippage(FixedSlippage(spread=0.10)) self.set_commission(PerTrade(cost=commission_cost))
def __init__(self, data_frequency, asset_finder, equity_slippage=None, future_slippage=None, equity_commission=None, future_commission=None, cancel_policy=None): # these orders are aggregated by sid self.open_orders = defaultdict(list) # keep a dict of orders by their own id self.orders = {} # all our legacy order management code works with integer sids. # this lets us convert those to assets when needed. ideally, we'd just # revamp all the legacy code to work with assets. self.asset_finder = asset_finder # holding orders that have come in since the last event. self.new_orders = [] self.current_dt = None self.max_shares = int(1e+11) self.slippage_models = { Equity: equity_slippage or VolumeShareSlippage(), Future: future_slippage or VolumeShareSlippage(), } self.commission_models = { Equity: equity_commission or PerShare(), Future: future_commission or PerTrade(cost=DEFAULT_FUTURE_COST_PER_TRADE, ), } self.data_frequency = data_frequency self.cancel_policy = cancel_policy if cancel_policy else NeverCancel()
def initialize(context): context.set_commission( us_equities=PerTrade(0), us_futures=PerTrade(0), )
def initialize(algo, sids_and_amounts, *args, **kwargs): algo.ordered = False algo.sids_and_amounts = sids_and_amounts algo.set_commission(us_equities=PerTrade(0), us_futures=PerTrade(0)) algo.set_slippage(us_equities=FixedSlippage(0), us_futures=FixedSlippage(0))