<s> from datetime import datetime <EOL> from datetime import timedelta <EOL> from math import ceil <EOL> from math import floor <EOL> from random import choice <EOL> from random import randint <EOL> from random import random <EOL> from random import sample <EOL> from quotes . models import CASH_SYMBOL <EOL> from quotes . models import quotes_by_symbols <EOL> from transactions . models import Transaction <EOL> DEMO_INSTRUMENTS = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> DEMO_COMMISSIONS = [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] <EOL> DEMO_MIN_INSTRUMENTS = <NUM_LIT:4> <EOL> DEMO_MAX_INSTRUMENTS = <NUM_LIT:8> <EOL> DEMO_MIN_INVESTMENT = <NUM_LIT> <EOL> DEMO_MAX_INVESTMENT = <NUM_LIT> <EOL> DEMO_PORTFOLIOS = <NUM_LIT:10> <EOL> DEMO_DAYS_CUTOFF = <NUM_LIT> <EOL> DEMO_TARGET_TRANSACTIONS = <NUM_LIT> <EOL> DEMO_TARGET_DEPOSITS = <NUM_LIT:5> <EOL> DEMO_BUY_SELL_RATIO = <NUM_LIT:4> <EOL> DEMO = [ ] <EOL> def get_demo_transactions ( ) : <EOL> global DEMO <EOL> if len ( DEMO ) == <NUM_LIT:0> : <EOL> print '<STR_LIT>' <EOL> DEMO = _generate_demo_portfolios ( DEMO_PORTFOLIOS ) <EOL> return choice ( DEMO ) <EOL> def _generate_demo_portfolios ( count ) : <EOL> out = [ ] <EOL> for i in range ( count ) : <EOL> instruments = sample ( DEMO_INSTRUMENTS , randint ( DEMO_MIN_INSTRUMENTS , DEMO_MAX_INSTRUMENTS ) ) <EOL> total_investment = randint ( DEMO_MIN_INVESTMENT , DEMO_MAX_INVESTMENT ) <EOL> commission = choice ( DEMO_COMMISSIONS ) <EOL> out . append ( _generate_random_transactions ( instruments , <EOL> total_investment , <EOL> commission ) <EOL> ) <EOL> return out <EOL> def _generate_random_transactions ( instruments , total_amount , commission ) : <EOL> quotes = quotes_by_symbols ( instruments ) <EOL> cutoff_date = datetime . now ( ) . date ( ) - timedelta ( days = DEMO_DAYS_CUTOFF ) <EOL> prices = dict ( [ ( quote . symbol , { } ) for quote in quotes ] ) <EOL> dates = set ( [ ] ) <EOL> quote_map = { } <EOL> for quote in quotes : <EOL> quote_map [ quote . symbol ] = quote <EOL> for history in quote . pricehistory_set . filter ( as_of_date__gte = cutoff_date ) . order_by ( '<STR_LIT>' ) : <EOL> cur_date = history . as_of_date . date ( ) <EOL> prices . get ( quote . symbol ) [ cur_date ] = history . price <EOL> dates . add ( cur_date ) <EOL> transaction_probability = DEMO_TARGET_TRANSACTIONS / float ( len ( dates ) ) <EOL> deposit_probability = DEMO_TARGET_DEPOSITS / float ( DEMO_TARGET_TRANSACTIONS ) <EOL> buy_sell_probability = DEMO_BUY_SELL_RATIO / float ( DEMO_BUY_SELL_RATIO + <NUM_LIT:1> ) <EOL> transactions = [ ] <EOL> quantities = dict ( [ ( symbol , <NUM_LIT:0.0> ) for symbol in instruments ] ) <EOL> undeposited_cash = total_amount <EOL> cash = <NUM_LIT:0> <EOL> for date in sorted ( dates ) : <EOL> sell_candidates = [ q [ <NUM_LIT:0> ] for q in quantities . items ( ) if q [ <NUM_LIT:1> ] > <NUM_LIT:0> ] <EOL> if random ( ) <= transaction_probability or len ( transactions ) == <NUM_LIT:0> : <EOL> if undeposited_cash > <NUM_LIT:1> and random ( ) <= deposit_probability : <EOL> deposit = min ( [ undeposited_cash , round ( undeposited_cash * ( randint ( <NUM_LIT:10> , <NUM_LIT:100> ) / <NUM_LIT> ) , - <NUM_LIT:2> ) , total_amount * <NUM_LIT:0.5> ] ) <EOL> undeposited_cash -= deposit <EOL> cash += deposit <EOL> transactions . append ( Transaction ( type = '<STR_LIT>' , <EOL> as_of_date = date , <EOL> symbol = CASH_SYMBOL , <EOL> quantity = deposit , <EOL> price = <NUM_LIT:1.0> , <EOL> total = deposit , <EOL> ) ) <EOL> elif random ( ) <= buy_sell_probability : <EOL> amount = min ( [ cash , round ( cash * ( randint ( <NUM_LIT:20> , <NUM_LIT:100> ) / <NUM_LIT> ) ) , total_amount * <NUM_LIT:0.1> ] ) <EOL> symbol = choice ( instruments ) <EOL> price = ( prices . get ( symbol ) . get ( date ) if not quote_map . get ( symbol ) . cash_equivalent else <NUM_LIT:1.0> ) <EOL> quantity = floor ( ( amount - commission ) / price ) <EOL> if quantity > <NUM_LIT:0> : <EOL> total = ( quantity * price ) + commission <EOL> cash -= total <EOL> quantities [ symbol ] = quantities . get ( symbol ) + quantity <EOL> transactions . append ( Transaction ( type = '<STR_LIT>' , <EOL> as_of_date = date , <EOL> symbol = symbol , <EOL> quantity = quantity , <EOL> price = price , <EOL> total = total , <EOL> ) ) <EOL> elif len ( sell_candidates ) > <NUM_LIT:0> : <EOL> symbol = choice ( sell_candidates ) <EOL> price = ( prices . get ( symbol ) . get ( date ) if not quote_map . get ( symbol ) . cash_equivalent else <NUM_LIT:1.0> ) <EOL> available_quantity = quantities . get ( symbol ) <EOL> quantity = min ( available_quantity , round ( available_quantity * ( randint ( <NUM_LIT:20> , <NUM_LIT:100> ) / <NUM_LIT> ) ) ) <EOL> if quantity > <NUM_LIT:0> : <EOL> total = ( quantity * price ) - commission <EOL> cash += total <EOL> quantities [ symbol ] = quantities . get ( symbol ) - quantity <EOL> transactions . append ( Transaction ( type = '<STR_LIT>' , <EOL> as_of_date = date , <EOL> symbol = symbol , <EOL> quantity = quantity , <EOL> price = price , <EOL> total = total , <EOL> ) ) <EOL> return transactions </s>
