<s> """<STR_LIT>""" <EOL> import unittest <EOL> import broker_backtesting_test <EOL> from pyalgotrade import broker <EOL> from pyalgotrade . broker import slippage <EOL> from pyalgotrade . broker import backtesting <EOL> from pyalgotrade import bar <EOL> class BaseTestCase ( unittest . TestCase ) : <EOL> TestInstrument = "<STR_LIT>" <EOL> class NoSlippageTestCase ( BaseTestCase ) : <EOL> def setUp ( self ) : <EOL> BaseTestCase . setUp ( self ) <EOL> self . slippage = slippage . NoSlippage ( ) <EOL> self . barsBuilder = broker_backtesting_test . BarsBuilder ( BaseTestCase . TestInstrument , bar . Frequency . DAY ) <EOL> def __test_impl ( self , action ) : <EOL> order = backtesting . MarketOrder ( <EOL> action , BaseTestCase . TestInstrument , <NUM_LIT:5> , False , broker . IntegerTraits ( ) <EOL> ) <EOL> price = <NUM_LIT:10> <EOL> slippedPrice = self . slippage . calculatePrice ( <EOL> order , price , order . getQuantity ( ) , self . barsBuilder . nextBar ( <NUM_LIT:10> , <NUM_LIT:11> , <NUM_LIT:9> , <NUM_LIT:10> , volume = <NUM_LIT:100> ) , <NUM_LIT:0> <EOL> ) <EOL> self . assertEqual ( price , slippedPrice ) <EOL> slippedPrice = self . slippage . calculatePrice ( <EOL> order , price , order . getQuantity ( ) , self . barsBuilder . nextBar ( <NUM_LIT:10> , <NUM_LIT:11> , <NUM_LIT:9> , <NUM_LIT:10> , volume = <NUM_LIT:100> ) , <NUM_LIT:20> <EOL> ) <EOL> self . assertEqual ( slippedPrice , price ) <EOL> def test_buy_market_order ( self ) : <EOL> self . __test_impl ( broker . Order . Action . BUY ) <EOL> def test_sell_market_order ( self ) : <EOL> self . __test_impl ( broker . Order . Action . SELL ) <EOL> class VolumeShareSlippageTestCase ( BaseTestCase ) : <EOL> def setUp ( self ) : <EOL> BaseTestCase . setUp ( self ) <EOL> self . priceImpact = <NUM_LIT:0.1> <EOL> self . slippage = slippage . VolumeShareSlippage ( self . priceImpact ) <EOL> self . barsBuilder = broker_backtesting_test . BarsBuilder ( BaseTestCase . TestInstrument , bar . Frequency . DAY ) <EOL> def __test_impl ( self , action ) : <EOL> order = backtesting . MarketOrder ( <EOL> action , BaseTestCase . TestInstrument , <NUM_LIT> , False , broker . IntegerTraits ( ) <EOL> ) <EOL> price = <NUM_LIT:10> <EOL> volumeUsed = <NUM_LIT:0> <EOL> slippedPrice = self . slippage . calculatePrice ( <EOL> order , price , order . getQuantity ( ) , self . barsBuilder . nextBar ( <NUM_LIT:10> , <NUM_LIT:11> , <NUM_LIT:9> , <NUM_LIT:10> , volume = <NUM_LIT:100> ) , volumeUsed <EOL> ) <EOL> quantity = order . getQuantity ( ) <EOL> expectedPriceImpactPct = quantity / <NUM_LIT> * quantity / <NUM_LIT> * self . priceImpact <EOL> self . assertEqual ( expectedPriceImpactPct , <NUM_LIT> ) <EOL> if action == broker . Order . Action . BUY : <EOL> self . assertEqual ( slippedPrice , price * ( <NUM_LIT:1> + expectedPriceImpactPct ) ) <EOL> else : <EOL> self . assertEqual ( slippedPrice , price * ( <NUM_LIT:1> - expectedPriceImpactPct ) ) <EOL> volumeUsed += quantity <EOL> quantity += order . getQuantity ( ) <EOL> slippedPrice = self . slippage . calculatePrice ( <EOL> order , price , order . getQuantity ( ) , self . barsBuilder . nextBar ( <NUM_LIT:10> , <NUM_LIT:11> , <NUM_LIT:9> , <NUM_LIT:10> , volume = <NUM_LIT:100> ) , volumeUsed <EOL> ) <EOL> expectedPriceImpactPct = quantity / <NUM_LIT> * quantity / <NUM_LIT> * self . priceImpact <EOL> self . assertEqual ( expectedPriceImpactPct , <NUM_LIT> ) <EOL> if action == broker . Order . Action . BUY : <EOL> self . assertEqual ( slippedPrice , price * ( <NUM_LIT:1> + expectedPriceImpactPct ) ) <EOL> else : <EOL> self . assertEqual ( slippedPrice , price * ( <NUM_LIT:1> - expectedPriceImpactPct ) ) <EOL> def test_buy_market_order ( self ) : <EOL> self . __test_impl ( broker . Order . Action . BUY ) <EOL> def test_sell_market_order ( self ) : <EOL> self . __test_impl ( broker . Order . Action . SELL ) <EOL> def test_full_volume_used ( self ) : <EOL> orderSize = <NUM_LIT:100> <EOL> order = backtesting . MarketOrder ( <EOL> broker . Order . Action . BUY , BaseTestCase . TestInstrument , orderSize , False , broker . IntegerTraits ( ) <EOL> ) <EOL> price = <NUM_LIT:10> <EOL> volumeUsed = <NUM_LIT:0> <EOL> slippedPrice = self . slippage . calculatePrice ( <EOL> order , <EOL> price , <EOL> order . getQuantity ( ) , <EOL> self . barsBuilder . nextBar ( <NUM_LIT:10> , <NUM_LIT:11> , <NUM_LIT:9> , <NUM_LIT:10> , volume = orderSize ) , <EOL> volumeUsed <EOL> ) <EOL> self . assertEqual ( slippedPrice , price * <NUM_LIT> ) </s>
