def sell_real_time(self, data): """ # 各种情况。。。。。卖出要排序 # 计算延时时间,如果可以获取当前卖二价,则挂单卖,如果有挂最新卖价按钮,再好不过 :return: DataFrame({'price':,'num':})。。。。 """ data_sell0 = pd.DataFrame([]) # 涨停止盈,9%以上卖出什么的10% if float(data.price[0]) >= self.df.close[0] * 1.09: price = float(data.a3_p[0]) df = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] num = self.obj_5.num_buy_process(coe=1, data=df) data_sell0 = pd.DataFrame({'price': price, 'num': num}, index=[0]) # 长上影线止盈,卖3价,1/10 data_sell1 = pd.DataFrame([]) close_ave5 = create_data.ave_data_create(self.df.close, 5) open_ave5 = create_data.ave_data_create(self.df.open, 5) high_ave5 = create_data.ave_data_create(self.df.high, 5) low_ave5 = create_data.ave_data_create(self.df.low, 5) if ((float(data.high[0]) - max(float(data.open[0]), float(data.price[0]))) / (float(data.high[0]) - float(data.low[0])) >= 0.5) or \ ((high_ave5[0] - max(open_ave5[0], close_ave5[0])) / (high_ave5[0] - low_ave5[0]) >= 0.5): if float(data.volume[0]) > self.df.volume[0:5].mean(): price = float(data.a3_p[0]) df = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] num = self.obj_5.num_buy_process(coe=1, data=df) data_sell1 = pd.DataFrame({'price': price, 'num': num}, index=[0]) # 跳空止盈,卖3价,1/10 data_sell2 = pd.DataFrame([]) if float(data.low[0]) > self.ave_price_5[0]: price = float(data.a3_p[0]) df = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] num = self.obj_5.num_buy_process(coe=1, data=df) data_sell2 = pd.DataFrame({'price': price, 'num': num}, index=[0]) data_sell_ = pd.concat([data_sell0, data_sell1, data_sell2]) # 因为目前买价和买量都是一样的,所以不需要分类 # 可能会有一个小bug,当一次性全部卖完的时候,也就是说,当vs的情况发生一个事件以上的时候,存量比卖出的数量要少。。。。 if data_sell_.empty is False: num = data_sell_.num.sum() price = data_sell_.price.drop_duplicates()[0] data_sell_ = pd.DataFrame({'price': price, 'num': num}, index=[0]) return data_sell_
def __init__(self, stock_id, date=time.strftime('%Y%m%d', time.localtime(time.time()))): self.date = date self.stock_id = stock_id self.df = data_process.Get_DataFrame(self.stock_id, self.date) if len(self.df.high) >= 196: dif = max(self.df.high[:196]) - min(self.df.low[:196]) # 根据196个交易日内,长宽比等于价格差与天数乘以系数(ef)比,为了计算自变量,来替换时间序列,好得出想要的角度 # 切记,要个股196至200天的最高与最低的数据 ef = round((36 / 12.5) * dif / 196, 4) else: # 如果数据不够的情况,为使角度变大 ef = 0.0000001 self.ef = ef # 不考虑数据不够的情况,比较少 self.ave_price_60 = create_data.ave_data_create(self.df.close, 60) self.ave_price_5 = create_data.ave_data_create(self.df.close, 5)
def angle_data(self, ave=60, stop_dot=50): """ :return: DataFrame (for create line_model below,60 for angle enough) """ date = [] for i in range(1, stop_dot + 1): date.append(self.ef * i) ave_price = create_data.ave_data_create(self.df.close, ave)[:stop_dot] ave_price.reverse() data = pd.DataFrame({'date': date, 'price': np.array(ave_price)}) # df = pd.DataFrame({'square_feet':[150,200,250,300,350,400,600],'price':[150,200,250,300,350,400,600]}) return data
def candle_k(data): """ 一定要更改均线值,不然均线会错误 存在的问题:1 x轴日期包含非交易日和停牌日,如果遇到节假日,或者分钟级数据遇到收市,时间就不会连续; 2 图为静态图; 3 不能交互 4 必须限定x轴日期范围 :param data: 某只股票的数据集,不包括停牌数据 :return: K线和成交量图 """ # drop the date index from the dateframe & make a copy datareshape = data.reset_index() # convert the datetime64 column in the dataframe to 'float days' datareshape['date'] = mdates.date2num(datareshape['date'].astype(dt.date)) # clean day data for candle view datareshape.drop(['volume', 'amount', 'factor', 'code'], axis=1, inplace=True) # 填缺失值的行为,是停牌缺失还是列缺失 datareshape = datareshape.reindex( columns=['date', 'open', 'high', 'low', 'close']) # 考虑不满足210天的股票 # av1,av2长度为150 av1 = create_data.ave_data_create(datareshape.close.values, MA1, date=len(datareshape)) # av1.reverse() av2 = create_data.ave_data_create(datareshape.close.values, MA2, date=len(datareshape)) # av2.reverse() sp = len(datareshape.date.values[MA2 - 1:]) fig = plt.figure(facecolor='#07000d', figsize=(15, 10)) ax1 = plt.subplot2grid((6, 4), (1, 0), rowspan=4, colspan=4, axisbg='#07000d') candlestick_ohlc(ax1, datareshape.values[-sp:], width=.6, colorup='#ff1717', colordown='#53c156') Label1 = str(MA1) + ' SMA' Label2 = str(MA2) + ' SMA' ax1.plot(datareshape.date.values[-sp:], av1[-sp:], '#e1edf9', label=Label1, linewidth=1.5) ax1.plot(datareshape.date.values[-sp:], av2[-sp:], '#4ee6fd', label=Label2, linewidth=1.5) ax1.grid(True, color='w') ax1.xaxis.set_major_locator(mticker.MaxNLocator(10)) ax1.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d')) ax1.yaxis.label.set_color("w") ax1.spines['bottom'].set_color("#5998ff") ax1.spines['top'].set_color("#5998ff") ax1.spines['left'].set_color("#5998ff") ax1.spines['right'].set_color("#5998ff") ax1.tick_params(axis='y', colors='w') plt.gca().yaxis.set_major_locator(mticker.MaxNLocator(prune='upper')) ax1.tick_params(axis='x', colors='w') plt.ylabel('Stock price and Volume') # 绘制成交量 volume_min = 0 ax1v = ax1.twinx() ax1v.fill_between(datareshape.date.values[-sp:], volume_min, data.volume.values[-sp:], facecolor='#00ffe8', alpha=.4) ax1v.axes.yaxis.set_ticklabels([]) ax1v.grid(False) # Edit this to 3, so it's a bit larger ax1v.set_ylim(0, 3 * data.volume.values.max()) ax1v.spines['bottom'].set_color("#5998ff") ax1v.spines['top'].set_color("#5998ff") ax1v.spines['left'].set_color("#5998ff") ax1v.spines['right'].set_color("#5998ff") ax1v.tick_params(axis='x', colors='w') ax1v.tick_params(axis='y', colors='w') # 修饰 添加股票代码以及指定位置添加文字与箭头 plt.suptitle(stock, color='w') # plt.setp(ax0.get_xticklabels(), visible=False) plt.setp(ax1.get_xticklabels(), visible=False) # Mark big event # TODO: Make a real case here ax1.annotate('BreakNews!', (datareshape.date.values[90], av1[90]), xytext=(0.8, 0.9), textcoords='axes fraction', arrowprops=dict(facecolor='white', shrink=0.05), fontsize=10, color='w', horizontalalignment='right', verticalalignment='bottom') plt.subplots_adjust(left=.09, bottom=.14, right=.94, top=.95, wspace=.20, hspace=0) plt.show()
def selling_3(self, first_sell_up, second_sell_up, third_sell_up, first_sell_down): global running_box # 支撑线压力线,波谷波峰数据集,变异系数,支撑点压力点,以及原因的获取 # df_s, cv_s, price_s = self.support_line() e_support, reason_s = self.support_dot() # df_p, cv_p, price_p = self.pressure_line() e_pressure, reason_p = self.pressure_dot() obj1 = Deal(self.stock_id) price_buy = 0 price_sell = 0 cash = 0 num = 0 # 涨停止盈 if self.df.low[0] <= self.df.close[1] * 1.09 <= self.df.high[0]: price_buy = 0 price_sell = self.df.close[1] * 1.09 num0 = list(running_box.loc[running_box.stock_id == self.stock_id, 'bought_num'])[0] / 5 num = math.floor(num0 * 100) * 100 commission_buy, commission_sell = obj1.order_cost( num, price_sell, close_tax=0.001, open_commission=0.0003, close_commission=0.0003, min_commission=5) cash = num * price_sell - commission_sell if num >= 200: print 'situation3_5' return price_buy, price_sell, cash, num else: num = 200 commission_buy, commission_sell = obj1.order_cost( num, price_sell, close_tax=0.001, open_commission=0.0003, close_commission=0.0003, min_commission=5) cash = num * price_sell - commission_sell print 'situation3_6' return price_buy, price_sell, cash, num # 长上影线止盈 close_ave5 = create_data.ave_data_create(self.df.close, 5) open_ave5 = create_data.ave_data_create(self.df.open, 5) high_ave5 = create_data.ave_data_create(self.df.high, 5) low_ave5 = create_data.ave_data_create(self.df.low, 5) if ((self.df.high[0] - max(self.df.close[0], self.df.open[0])) / (self.df.high[0] - self.df.low[0]) >= 0.5) or \ ((high_ave5[0] - max(open_ave5[0], close_ave5[0])) / (high_ave5[0] - low_ave5[0]) >= 0.5): if self.df.volume[0] > self.df.volums[0:5].mean(): price_buy = 0 price_sell = self.df.close[0] num0 = list( running_box.loc[running_box.stock_id == self.stock_id, 'bought_num'])[0] / 5 num = math.floor(num0 * 100) * 100 commission_buy, commission_sell = obj1.order_cost( num, price_sell, close_tax=0.001, open_commission=0.0003, close_commission=0.0003, min_commission=5) cash = num * price_sell - commission_sell if num >= 200: print 'situation3_7' return price_buy, price_sell, cash, num else: num = 200 commission_buy, commission_sell = obj1.order_cost( num, price_sell, close_tax=0.001, open_commission=0.0003, close_commission=0.0003, min_commission=5) cash = num * price_sell - commission_sell print 'situation3_8' return price_buy, price_sell, cash, num if self.df.high[0] < self.ave_price_60[0]: price_sell = min(e_support, first_sell_down) * 0.99 # 支撑位止损 if self.df.close[0] <= price_sell: num = list( running_box.loc[running_box.stock_id == self.stock_id, 'bought_num'])[0] commission_buy, commission_sell = obj1.order_cost( num, price_sell, close_tax=0.001, open_commission=0.0003, close_commission=0.0003, min_commission=5) cash = num * price_sell - commission_sell print 'situation3_9' return price_buy, price_sell, cash, num else: # 跳空止盈 if self.df.low[0] > self.ave_price_5[0]: price_buy = 0 price_sell = self.df.close[0] num0 = list( running_box.loc[running_box.stock_id == self.stock_id, 'bought_num'])[0] / 5 num = math.floor(num0 * 100) * 100 commission_buy, commission_sell = obj1.order_cost( num, price_sell, close_tax=0.001, open_commission=0.0003, close_commission=0.0003, min_commission=5) cash = num * price_sell - commission_sell if num >= 200: print 'situation3_10' return price_buy, price_sell, cash, num else: num = 200 commission_buy, commission_sell = obj1.order_cost( num, price_sell, close_tax=0.001, open_commission=0.0003, close_commission=0.0003, min_commission=5) cash = num * price_sell - commission_sell print 'situation3_11' return price_buy, price_sell, cash, num # 超过压力位止盈 # 对于cv_p没做限定,如果存在负角度的压力线,是否会低于60日线,已改pressure角度0到30 elif self.df.low[0] <= first_sell_up <= self.df.high[0]: price_buy = 0 price_sell = self.df.close[0] num0 = list( running_box.loc[running_box.stock_id == self.stock_id, 'bought_num'])[0] / 5 num = math.floor(num0 * 100) * 100 commission_buy, commission_sell = obj1.order_cost( num, price_sell, close_tax=0.001, open_commission=0.0003, close_commission=0.0003, min_commission=5) cash = num * price_sell - commission_sell if num >= 200: print 'situation3_12' return price_buy, price_sell, cash, num else: num = 200 commission_buy, commission_sell = obj1.order_cost( num, price_sell, close_tax=0.001, open_commission=0.0003, close_commission=0.0003, min_commission=5) cash = num * price_sell - commission_sell print 'situation3_13' return price_buy, price_sell, cash, num elif self.df.low[0] <= second_sell_up <= self.df.high[0]: price_buy = 0 price_sell = self.df.close[0] num0 = list( running_box.loc[running_box.stock_id == self.stock_id, 'bought_num'])[0] / 5 num = math.floor(num0 * 100) * 100 commission_buy, commission_sell = obj1.order_cost( num, price_sell, close_tax=0.001, open_commission=0.0003, close_commission=0.0003, min_commission=5) cash = num * price_sell - commission_sell if num >= 200: print 'situation3_14' return price_buy, price_sell, cash, num else: num = 200 commission_buy, commission_sell = obj1.order_cost( num, price_sell, close_tax=0.001, open_commission=0.0003, close_commission=0.0003, min_commission=5) cash = num * price_sell - commission_sell print 'situation3_15' return price_buy, price_sell, cash, num elif self.df.low[0] <= third_sell_up <= self.df.high[0]: price_buy = 0 price_sell = self.df.close[0] num0 = list( running_box.loc[running_box.stock_id == self.stock_id, 'bought_num'])[0] / 5 num = math.floor(num0 * 100) * 100 commission_buy, commission_sell = obj1.order_cost( num, price_sell, close_tax=0.001, open_commission=0.0003, close_commission=0.0003, min_commission=5) cash = num * price_sell - commission_sell if num >= 200: print 'situation3_16' return price_buy, price_sell, cash, num else: num = 200 commission_buy, commission_sell = obj1.order_cost( num, price_sell, close_tax=0.001, open_commission=0.0003, close_commission=0.0003, min_commission=5) cash = num * price_sell - commission_sell print 'situation3_17' return price_buy, price_sell, cash, num return price_buy, price_sell, cash, num
overlap.add(line) if render_path: overlap.render(render_path) return overlap # .render('k-line.html') if __name__ == '__main__': # 数据导入 name = '300317' period = '1' if period.isdigit(): period += 'day' data_raw = data_process.Get_DataFrame(name, '20171210') data_raw.sort_index(inplace=True) ma5 = create_data.ave_data_create(data_raw.close.values, 5)[:150] ma5.reverse() ma60 = create_data.ave_data_create(data_raw.close.values, 60)[:150] ma60.reverse() # 均线维度必须一致,data不能有空值 data_new = pd.DataFrame(data_raw, columns=['open', 'high', 'low', 'close', 'volume']) open = list(data_new.open)[:150] open.reverse() close = list(data_new.close)[:150] close.reverse() high = list(data_new.high)[:150] high.reverse() low = list(data_new.low)[:150] low.reverse() volume = list(data_new.volume)[:150]