Python示例代码之按指定算法判断买卖点计算股票收益

      炒股票的小伙伴们一般都有一个神奇的愿景,总认为按照自己的方法选择股票的买卖点,就一定能赚钱,今天我用程序模拟了一把买卖点和收益的实测,结果显示,无论我使用什么买卖点判断方法,总是有些股票赚钱,有些股票赔钱。比如按20日线的价格作为买卖点、10日线、5日线,以及3日线,交易量递增法、交易量递减法,等等。

      如下为一种买卖点的判断示例,以及验证在某时间段内使用该方法的收益,有兴趣的小伙伴可以参考,欢迎大家有好的算法找我一起研究!

# Author Zhanhai

import tushare as ts
import datetime as dt
import json
import os
import time

class StockAnylize():

	def __init__(self, stock_code):
		self.stock_code = stock_code

	def _get_today(self):
		today = dt.datetime.today()
		return str(today.strftime("%Y-%m-%d"))

	def get_start_from_end(self, endday, day_duration):
		edate  = dt.datetime.strptime(endday, '%Y-%m-%d')
		delta = dt.timedelta(days = day_duration)
		sdate  = edate - delta
		return str(sdate.strftime("%Y-%m-%d"))

	def get_suggests(self, date_day):
		return "keep"

	def down_load_data(self, date_day_start, date_day_end):
		date_day_start = self.get_start_from_end(date_day_start, 20)
		stock_data = ts.get_hist_data(self.stock_code, start = date_day_start, end = date_day_end)
		if not stock_data is None:
			stock_data.to_json("d:\\stock/single_data/" + self.stock_code + ".json", orient = "index", force_ascii = False)

	def get_date_day_start(self, date_day_start, stock_info):
		if date_day_start in stock_info:
			return date_day_start
		else:
			for i in range(1, 8):
				early_day = self.get_start_from_end(date_day_start, - i)
				if early_day in stock_info:
					return early_day
		return None

	def get_days_duration_from_start_to_end(self, date_day_start, date_day_end):
		if date_day_start is None:
			return 0
		if date_day_end is None:
			return 0
		date1 = time.strptime(date_day_start, "%Y-%m-%d")
		date2 = time.strptime(date_day_end, "%Y-%m-%d")
		date1 = dt.datetime(date1[0],date1[1],date1[2])
		date2 = dt.datetime(date2[0],date2[1],date2[2])
		duration = date2 - date1
		return duration.days

	def get_early_day(self, cur_day, stock_info):
		for i in range(1, 20):
			early_day = self.get_start_from_end(cur_day, i)
			if early_day in stock_info:
				break
		if not early_day in stock_info:
			return None
		return early_day

	def get_next_day(self, cur_day, stock_info):
		for i in range(1, 20):
			early_day = self.get_start_from_end(cur_day, -i)
			if early_day in stock_info:
				break
		if not early_day in stock_info:
			return None
		return early_day

	def is_day_can_buy(self, judge_day, stock_info):
		if self.is_day_can_sale(judge_day, stock_info):
			return False
		cur_close_price = stock_info[judge_day].get("close")
		cur_open_price = stock_info[judge_day].get("open")
		ma_5_price = stock_info[judge_day].get("ma5")
		if cur_close_price > cur_open_price:
			if ((cur_close_price + cur_open_price) / 2) > ma_5_price:
				return True
		else:
			return False
		return False

	def get_price(self, day, stock_info):
		open_price = stock_info[day].get("open")
		close_price = stock_info[day].get("close")
		if open_price > close_price:
			return (close_price + open_price) / 2
		else:
			return close_price

	def is_day_can_sale(self, judge_day, stock_info):
		if judge_day == "2019-09-17":
			print(stock_info[judge_day])
		cur_price = self.get_price(judge_day, stock_info)
		ma5_price = stock_info[judge_day].get("ma20")
		#print("cur_price", cur_price, " ma5_price", ma5_price)
		if cur_price < ma5_price:
			return True
		return False

	def get_buy_date(self, date_day_start, date_day_end, stock_info):
		date_day_start = self.get_date_day_start(date_day_start, stock_info)
		duration = self.get_days_duration_from_start_to_end(date_day_start, date_day_end)
		if duration < 3:
			return None
		for i in range(duration):
			# 卖了不要马上买,至少要晚3天后再买
			judge_day = self.get_start_from_end(date_day_start, -i - 3)
			if not judge_day in stock_info:
				continue
			if self.is_day_can_buy(judge_day, stock_info):
				return judge_day
		return None

	def get_sale_date(self, buy_date, date_day_end, stock_info):
		date_day_start = self.get_date_day_start(buy_date, stock_info)
		duration = self.get_days_duration_from_start_to_end(date_day_start, date_day_end)
		for i in range(duration):
			# -2的原因是买了不能马上卖,必须至少隔一天
			judge_day = self.get_start_from_end(date_day_start, -2 - i)
			if not judge_day in stock_info:
				continue
			if self.is_day_can_sale(judge_day, stock_info):
				return judge_day
		return None

	def get_duration_income(self, buy_date, sale_date, stock_info):
		buy_action_day = self.get_next_day(buy_date, stock_info)
		sale_action_day = self.get_next_day(sale_date, stock_info)
		if sale_action_day is None:
			sale_action_day = sale_date
		buy_price = stock_info[buy_action_day].get("open")
		sale_price = stock_info[sale_action_day].get("open")
		print("buy_action_day:", buy_action_day, " sale_action_day:", sale_action_day, " buy_price", buy_price, " sale_price", sale_price)
		return sale_price - buy_price

	def get_detail_transaction_data(self, detail_transaction_data, date_day_start, date_day_end, stock_info):
		buy_date = self.get_buy_date(date_day_start, date_day_end, stock_info)
		if not buy_date is None:
			sale_date = self.get_sale_date(buy_date, date_day_end, stock_info)
			if not sale_date is None:
				income = self.get_duration_income(buy_date, sale_date, stock_info)
				detail_transaction_data[buy_date] = {"buy_date_flag":buy_date, "sale_date_flag":sale_date, "income":income}
				self.get_detail_transaction_data(detail_transaction_data, self.get_start_from_end(sale_date, -1), date_day_end, stock_info)

	def get_income(self, detail_transaction_data):
		total_income = 0
		for (k, v) in  detail_transaction_data.items():
			total_income = total_income + v.get("income")
		return total_income

	def caculate_income(self, date_day_start, date_day_end):
		self.down_load_data(date_day_start, date_day_end)
		detail_transaction_data = {}
		stock_file_name = stock_base_folder + "/single_data/" + self.stock_code + ".json"
		stock_info = json.load(open(stock_file_name))
		self.get_detail_transaction_data(detail_transaction_data, date_day_start, date_day_end, stock_info)
		income = self.get_income(detail_transaction_data)
		return income

if __name__ == "__main__":
	stock_base_folder = "D:\\stock"
	stock_list_file_name = stock_base_folder + '/stock_list.json'
	print(StockAnylize("600249").caculate_income("2019-05-06", "2019-08-30"))

 

 

如果你喜欢这篇文章,别忘了点赞和评论哦!

你可能感兴趣的:(Python集中营)