<s> """<STR_LIT>""" <EOL> import random <EOL> from fms import agents <EOL> from fms . utils import BUY , SELL <EOL> from fms . utils . exceptions import MissingParameter <EOL> class SmartProbeBoundedTrader ( agents . Agent ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , params , offset = <NUM_LIT:0> ) : <EOL> agents . Agent . __init__ ( self , params , offset ) <EOL> try : <EOL> self . maxprice = self . args [ <NUM_LIT:0> ] <EOL> except ( AttributeError , IndexError ) : <EOL> raise MissingParameter , '<STR_LIT>' <EOL> try : <EOL> self . maxbuy = self . args [ <NUM_LIT:1> ] <EOL> except IndexError : <EOL> raise MissingParameter , '<STR_LIT>' <EOL> del self . args <EOL> self . prevorder = { BUY : [ <NUM_LIT> , False ] , SELL : [ self . maxprice , False ] } <EOL> self . resbuy = random . randint ( <NUM_LIT:1> , int ( self . maxprice * <NUM_LIT:50> ) ) / <NUM_LIT> <EOL> self . ressell = random . randint ( int ( self . maxprice * <NUM_LIT:50> ) , self . maxprice * <NUM_LIT:100> ) / <NUM_LIT> <EOL> self . successes = list ( ) <EOL> def act ( self , world = None , market = None ) : <EOL> """<STR_LIT>""" <EOL> if self . successes : <EOL> stockprice = float ( sum ( self . successes ) ) / len ( self . successes ) <EOL> else : <EOL> stockprice = random . randint ( <NUM_LIT:1> , self . maxprice * <NUM_LIT:100> ) / <NUM_LIT> <EOL> if self . prevorder [ SELL ] [ <NUM_LIT:1> ] : <EOL> sellprice = random . randint ( int ( self . prevorder [ SELL ] [ <NUM_LIT:0> ] * <NUM_LIT:100> ) , self . maxprice * <NUM_LIT:100> ) / <NUM_LIT> <EOL> else : <EOL> sellprice = random . randint ( int ( self . ressell * <NUM_LIT:100> - <NUM_LIT:1> ) , int ( self . prevorder [ SELL ] [ <NUM_LIT:0> ] * <NUM_LIT:100> + <NUM_LIT:1> ) ) / <NUM_LIT> <EOL> sellquant = self . stocks <EOL> if self . prevorder [ BUY ] [ <NUM_LIT:1> ] : <EOL> buyprice = random . randint ( <NUM_LIT:1> , int ( self . prevorder [ BUY ] [ <NUM_LIT:0> ] * <NUM_LIT:100> ) ) / <NUM_LIT> <EOL> else : <EOL> buyprice = random . randint ( int ( self . prevorder [ BUY ] [ <NUM_LIT:0> ] * <NUM_LIT:100> - <NUM_LIT:1> ) , int ( self . resbuy * <NUM_LIT:100> + <NUM_LIT:1> ) ) / <NUM_LIT> <EOL> if buyprice <= <NUM_LIT:0> : <EOL> buyprice = <NUM_LIT> <EOL> buyquant = int ( self . money / buyprice ) <EOL> sellwealth = self . money + sellquant * sellprice <EOL> buywealth = self . money - buyquant * buyprice + ( self . stocks + buyquant ) * stockprice <EOL> if sellwealth > buywealth : <EOL> direction = SELL <EOL> price = sellprice <EOL> quantity = sellquant <EOL> else : <EOL> direction = BUY <EOL> price = buyprice <EOL> quantity = buyquant <EOL> self . prevorder [ direction ] = [ price , False ] <EOL> return { '<STR_LIT>' : direction , '<STR_LIT>' : price , '<STR_LIT>' : quantity } <EOL> def record ( self , direction , price , quantity ) : <EOL> """<STR_LIT>""" <EOL> if direction : <EOL> self . stocks -= quantity <EOL> self . money += quantity * price <EOL> self . prevorder [ SELL ] = [ price , True ] <EOL> else : <EOL> self . stocks += quantity <EOL> self . money -= quantity * price <EOL> self . prevorder [ BUY ] = [ price , True ] <EOL> self . successes . append ( price ) <EOL> def _test ( ) : <EOL> """<STR_LIT>""" <EOL> import doctest <EOL> doctest . testmod ( optionflags = + doctest . ELLIPSIS ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> _test ( ) </s>
