#NEWLINE# def testBuyAndSell( self ) : #NEWLINE# #INDENT# class Strategy ( TestStrategy ) : #NEWLINE# #INDENT# def __init__( self , feed , brk ) : #NEWLINE# #INDENT# TestStrategy . __init__ ( self , feed , brk ) #NEWLINE# self . buyOrder = None #NEWLINE# self . sellOrder = None #NEWLINE# #UNINDENT# def onOrderUpdated( self , order ) : #NEWLINE# #INDENT# TestStrategy . onOrderUpdated ( self , order ) #NEWLINE# if ( ( order == self . buyOrder ) and order . isPartiallyFilled ( ) ) : #NEWLINE# #INDENT# if ( self . sellOrder is None ) : #NEWLINE# #INDENT# self . sellOrder = self . limitOrder ( common . btc_symbol , 10 , ( - 0.5 ) ) #NEWLINE# brk . getHTTPClient ( ) . addUserTransaction ( self . sellOrder . getId ( ) , ( - 0.5 ) , 5 , 10 , 0.01 ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( ( order == self . sellOrder ) and order . isFilled ( ) ) : #NEWLINE# #INDENT# self . stop ( ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# def onBars( self , bars ) : #NEWLINE# #INDENT# if ( self . buyOrder is None ) : #NEWLINE# #INDENT# self . buyOrder = self . limitOrder ( common . btc_symbol , 10 , 1 ) #NEWLINE# brk . getHTTPClient ( ) . addUserTransaction ( self . buyOrder . getId ( ) , 0.5 , ( - 5 ) , 10 , 0.01 ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# barFeed = TestingLiveTradeFeed ( ) #NEWLINE# barFeed . addTrade ( datetime . datetime . now ( ) , 1 , 100 , 1 ) #NEWLINE# brk = TestingLiveBroker ( None , None , None ) #NEWLINE# httpClient = brk . getHTTPClient ( ) #NEWLINE# httpClient . setUSDAvailable ( 10 ) #NEWLINE# httpClient . setBTCAvailable ( 0 ) #NEWLINE# strat = Strategy ( barFeed , strat ) #NEWLINE# strat . run ( ) #NEWLINE# self . assertTrue ( strat . buyOrder . isPartiallyFilled ( ) ) #NEWLINE# self . assertTrue ( strat . sellOrder . isFilled ( ) ) #NEWLINE# self . assertEquals ( len ( strat . orderExecutionInfo ) , 4 ) #NEWLINE# self . assertEquals ( strat . orderExecutionInfo [ 0 ] , None ) #NEWLINE# self . assertEquals ( strat . orderExecutionInfo [ 1 ] . getPrice ( ) , 10 ) #NEWLINE# self . assertEquals ( strat . orderExecutionInfo [ 1 ] . getQuantity ( ) , 0.5 ) #NEWLINE# self . assertEquals ( strat . orderExecutionInfo [ 1 ] . getCommission ( ) , 0.01 ) #NEWLINE# self . assertEquals ( strat . orderExecutionInfo [ 1 ] . getDateTime ( ) . date ( ) , datetime . datetime . now ( ) . date ( ) ) #NEWLINE# self . assertEquals ( strat . orderExecutionInfo [ 2 ] , None ) #NEWLINE# self . assertEquals ( strat . orderExecutionInfo [ 3 ] . getPrice ( ) , 10 ) #NEWLINE# self . assertEquals ( strat . orderExecutionInfo [ 3 ] . getQuantity ( ) , 0.5 ) #NEWLINE# self . assertEquals ( strat . orderExecutionInfo [ 3 ] . getCommission ( ) , 0.01 ) #NEWLINE# self . assertEquals ( strat . orderExecutionInfo [ 3 ] . getDateTime ( ) . date ( ) , datetime . datetime . now ( ) . date ( ) )