Exemple #1
0
	def __init__(self, date, code, name, number, price, op_type):
		self.stock_code = code
		self.stock_name = name
		self.stock_number = float(number)
		if price == 0:
			price = SinaQuote.getLastClosePrice(code, date)
		self.position_flow = [ (date, number, price, op_type) ]
Exemple #2
0
	def calculate_profit(self, last_date, is_rongquan=False):

		stock_count = 0
		stock_cost = 0
		profit_total = 0
		profit_list = []
		for (date, number, price, op_type) in self.position_flow:
			profit = 0
			if (number>0 and is_rongquan==False) or (number<0 and is_rongquan==True):
				if stock_cost == 0:
					stock_cost = price
				else:
					total = stock_count + number
					if total == 0:
						print is_rongquan
						for p in self.position_flow:
							print p
						print '!!! total stock number is 0, date %s stock_code %s stock_name %s number %d' % (date, self.stock_code, self.stock_name, number)
					stock_cost = float(stock_count*stock_cost + number*price) / (stock_count + number)
				stock_count += number
			elif (number<0 and is_rongquan==False) or (number>0 and is_rongquan==True):
				profit = (price - stock_cost)*(-number)
				profit_total += profit
				stock_count += number
			profit_list.append( (date, self.stock_code, self.stock_name, number, price, stock_count, stock_cost, profit, profit_total, op_type) )
		if stock_count != 0:
			last_price = SinaQuote.getLastClosePrice(self.stock_code, last_date)
			profit = stock_count * ( last_price - stock_cost)
			profit_total += profit
			profit_list.append( (last_date, self.stock_code, self.stock_name, 0, last_price, stock_count, stock_cost, profit, profit_total, 3) ) # 操作类型3是持仓
		columns = [u'日期', u'代码', u'名称', u'数量', u'价格', u'当前总持仓', u'当前成本', u'本次盈亏', u'总盈亏', u'操作类型']
		return (profit_total, profit_list, columns)
Exemple #3
0
	def stock_in(self, date, stock_code, stock_name, stock_number, price, actual_amount):  # 担保划入
		#print 'stock in:', stock_name,stock_number
		if math.isnan(price) or price == 0:
			price = SinaQuote.getLastClosePrice(stock_code, date)
		if math.isnan(actual_amount) or actual_amount == 0:
			actual_amount = stock_number * price
		self.bank_in_money(actual_amount)
		self.buy_stock(date, stock_code, stock_name, stock_number, price, actual_amount)
Exemple #4
0
	def getValue(self, date):
		closePrice = SinaQuote.getLastClosePrice(self.stock_code, date)
		value = 0;
		if closePrice == -1:
			print "!!!error: getClosePrice %s %s error" % (self.stock_code, date)
		else:
			value = closePrice * self.stock_number
		return value
Exemple #5
0
	def stock_out(self, date, stock_code, stock_name, stock_number, price, actual_amount):
		#print 'stock out:', stock_name,stock_number
		if price == 0:
			price = SinaQuote.getLastClosePrice(stock_code, date)
		if actual_amount == 0:
			actual_amount = stock_number * price
		self.sell_stock(date, stock_code, stock_name, stock_number, price, actual_amount)
		self.calculate_nv()  # !!! need this because after sell stock nv will change !!!
		self.bank_out_money(actual_amount)
Exemple #6
0
	def get_position_dataframe(self, date):
		position_list = []
		for stock_code, s in self.stock.items():
			if s.stock_number != 0:
				price = SinaQuote.getLastClosePrice(stock_code, date)
				position_list.append([date, s.stock_code, s.stock_name, s.stock_number, price, s.stock_number*price])
		df = pd.DataFrame()
		if len(position_list) > 0:
			df = pd.DataFrame(data=position_list, columns=[u'日期', u'代码', u'名称', u'数量', u'价格', u'市值'])
		return df
Exemple #7
0
def get_value(date):
	total_value = 0
	for line in open(argv[1]):
		r = line.split(',')
		#print r[1][4:-1]
		stock_number = int(r[6])
		stock_cd = r[1][4:-1]
		last_price = SinaQuote.getLastClosePrice(stock_cd, date)
		print stock_cd, stock_number, last_price
		value = stock_number * last_price 
		total_value += value
	return total_value
Exemple #8
0
def handle_row(row, account, is_first_row):
	#print row
	deal_date = get_row_date(row)
	deal_time = row[u'deal_time']
	stock_code = norm_stock_code(row[u'stock_code'])
	if stock_code == '000nan' and row['operation'] in ['buy','sell','earn stock']:
		print row
	stock_name = row[u'stock_name']
	operation = row[u'operation']
	stock_price = row[u'stock_price']
	if stock_price != u'---':
		stock_price = float(stock_price)
	else:
		stock_price = 0
	stock_number = row[u'stock_number']  # number should always be positive
	if stock_number != u'---':
		#print stock_number, type(stock_number)
		stock_number = abs(float(stock_number))
	else:
		stock_number = 0

	deal_amount = 0
	if 'deal_amount' in row.index:
		deal_amount = float(row[u'deal_amount']) # optional
	elif operation not in ['buy (bond repurchase)', 'sell (bond repurchase)']:
		deal_amount = stock_number * stock_price

	actual_amount = 0
	if 'actual_amount' in row.index:
		if not math.isnan(row[u'actual_amount']):
			actual_amount = float(row[u'actual_amount'])
	else:
		actual_amount = deal_amount

	remain_amount = 0
	if 'remain_amount' in row.index:
		remain_amount = float(row[u'remain_amount'])
	else:
		account.has_remain_amount_column = False


	#print deal_date, deal_time, stock_code, stock_name, operation, stock_price, stock_number, actual_amount, remain_amount

	if is_first_row:
		init_cash = remain_amount-actual_amount
		if row['operation'] == 'stock in':  # 如果第一笔操作是担保转入,直接用剩余金额,不能减去发生金额
			init_cash = remain_amount
		account.set_init_cash(init_cash)
		account.set_init_date(deal_date)
		print 'first row, init_cash %f, init_date %s' % (init_cash, deal_date)

	account.set_date(deal_date)

	if 'remain_stock' in row.index:
		remain_stock = row[u'remain_stock']  # number should always be positive
		if remain_stock != u'---':
			#print stock_number, type(stock_number)
			remain_stock = abs(float(remain_stock))
		else:
			remain_stock = 0
		process_init_position(account, remain_stock, stock_number, stock_code, stock_name, operation)

	if operation == 'buy':
		account.buy_stock(deal_date, stock_code, stock_name, stock_number, stock_price, actual_amount)
	elif operation == 'buy (new stock)':
		account.buy_new_stock(deal_date, stock_code, stock_name, stock_number, stock_price, actual_amount)
	elif operation == 'buy (bond repurchase)':
		#print '===========',row
		SinaQuote.setGC001Price(stock_code, stock_number, deal_amount, actual_amount)
		stock_price = SinaQuote.getGC001Price(stock_code)
		#print stock_code,stock_number,stock_price
		account.buy_stock(deal_date, stock_code, stock_name, stock_number, stock_price, actual_amount)		
	elif operation == 'sell':
		account.sell_stock(deal_date, stock_code, stock_name, stock_number, stock_price, actual_amount)
	elif operation == 'sell (bond repurchase)':
		#print '===========',row
		SinaQuote.setGC001Price(stock_code, stock_number, deal_amount, actual_amount)
		stock_price = SinaQuote.getGC001Price(stock_code)
		#print stock_code,stock_number,stock_price
		account.sell_stock(deal_date, stock_code, stock_name, stock_number, stock_price, actual_amount)
	
	elif operation == 'short':
		account.short_stock(deal_date, stock_code, stock_name, stock_number, stock_price, actual_amount)
	elif operation == 'cover':
		account.cover_stock(deal_date, stock_code, stock_name, stock_number, stock_price, actual_amount)
	
	elif operation == 'direct return':
		account.direct_return(deal_date, stock_code, stock_name, stock_number, stock_price)
	elif operation == 'stock in':
		account.stock_in(deal_date, stock_code, stock_name, stock_number, stock_price, actual_amount)
	elif operation == 'stock out':
		account.stock_out(deal_date, stock_code, stock_name, stock_number, stock_price, actual_amount)

	elif operation == 'fenji fund split':
		stock_price = SinaQuote.getLastClosePrice(stock_code, deal_date)
		actual_amount = float(stock_price * stock_number)
		account.stock_in(deal_date, stock_code, stock_name, stock_number, stock_price, actual_amount)
	elif operation in ['bank in money', 'bank out money', 'bank transfer']:
		account.bank_transfer(actual_amount)
	elif operation == 'borrow cash':
		account.borrow_cash(actual_amount)
	elif operation == 'return cash':
		account.return_cash(actual_amount)
	elif operation == 'pay interest':
		account.pay_interest(actual_amount)
	elif operation == 'pay tax':
		account.pay_tax(actual_amount)	
	elif operation == 'earn interest':
		account.earn_interest(actual_amount)
	elif operation == 'earn stock':
		account.earn_stock(deal_date, stock_code, stock_name, stock_number)
	elif operation == 'earn stock interest':
		if stock_number != 0 and actual_amount == 0:
			account.earn_stock(deal_date, stock_code, stock_name, stock_number)
		elif actual_amount != 0 and stock_number == 0:
			account.earn_interest(actual_amount)
		else:
			print '!!!! error earn stock interest, stock_number %f deal_amount %f' % (stock_number, actual_amount)
	elif operation == 'init position':
		account.add_init_position(stock_code, stock_name, stock_number)
	elif operation == 'init rongquan position':
		account.add_init_rongquan_position(stock_code, stock_name, stock_number)
	elif operation.startswith('todo:'):
		print '==== todo: row %s' % row
	elif operation.startswith('pass:'******'!!!! unknown operation %s row %s' % (operation, row)		
Exemple #9
0
	def stock_transfer(self, date, stock_code, stock_name, stock_number):  # 协议转让
		self.save_stock_code_name(stock_code, stock_name)
		price = SinaQuote.getLastClosePrice(stock_code, date)
		self.stock_position.minus(date, stock_code, stock_name, stock_number, price, 7) # 操作类型7是转出
		# 现金不增加,份额减少
		self.fund_units -= float(stock_number * price) / self.net_value