def toCsv(data_path, result_path, stock=None, year=None): weak_path = os.path.join(data_path, 'csv', 'strategy', '', 'yesterday_red') if stock is None: stock = data.Stock(os.path.join(data_path, 'csv', 'stock')) stock.readAll() dates = stock.dates() if year is None: year = "*" else: year += "*" result = [] for dir_path in sorted(glob.glob(os.path.join(weak_path, year)), reverse=False): if os.path.isdir(dir_path) == False: continue for csv_path in sorted(glob.glob(os.path.join(dir_path, "*.csv")), reverse=False): date = os.path.basename(csv_path).split('.')[0] print(date) date = dates[dates.index(date) + 1] for index, value in pd.read_csv(csv_path).iterrows(): increase = value[f"y_{name.INCREASE}"] if increase >= 10.5: continue open = value[f"y_{name.OPEN}"] close = value[f"y_{name.CLOSE}"] code = value[name.CODE] [otp, ot] = tick(open) [ctp, ct] = tick(close) if ot == ct: t = round((close - open) / ot) else: t = round((otp - open) / ot) + round((close - otp) / ct) result.append([ code, date, increase, value[f"y_{name.AMPLITUDE}"], t, round(((close - open) / open) * 100, 2), open, close, value[f"y_{name.HIGH}"], value[f"y_{name.LOW}"], value[f"y_{name.VOLUME}"] ]) pd.DataFrame(result, columns=[ name.CODE, name.DATE, name.INCREASE, name.AMPLITUDE, 'tick', f"c_{name.INCREASE}", name.OPEN, name.CLOSE, name.HIGH, name.LOW, name.VOLUME ]).to_csv(result_path)
def __init__(self, dir, stock_path): self.dir = dir self.trends = {} self.stock = data.Stock(stock_path) trend_files = glob.glob(os.path.join(dir, '*.json')) if len(trend_files) == 0: for dir in sorted(glob.glob(os.path.join(dir, '*')), reverse=True): if os.path.isdir(dir): self.trends[os.path.basename(dir)] = glob.glob( os.path.join(dir, '*.json')) else: self.trends[os.path.basename(dir)] = trend_files
def __init__(self, root, master, w, h, config=None): ui.process.__init__(self, master, w, h) self.start_now_date = tk.StringVar() self.end_date = tk.StringVar() self.stock = data.Stock(other.stock_csv_path(config)) tk.Label(master, text='日期:', font=ui.FONT).place(x=10, y=10) tk.Entry(master, textvariable=self.end_date, font=ui.FONT).place(x=self.ex * 1.5, y=10) tk.Label(master, text='現在開始年月:', font=ui.FONT).place(x=10, y=self.ey) tk.Entry(master, textvariable=self.start_now_date, font=ui.FONT).place(x=self.ex * 1.5, y=self.ey) tk.Button(master, text='K', font=ui.BTN_FONT, command=lambda: self.k()).place(x=10, y=self.h * 18) self.keyEvent(root)
def __init__(self, root, config=None, path=None): self.root = root self.size = pyautogui.size() self.width = self.size.width self.height = self.size.height self.dir = '' self.config = config self.stock = data.Stock(other.stock_csv_path(config)) self.root.state('zoomed') self._mainLayout() self._button_layout() self._list_layout() filename = os.path.join(path, datetime.now().strftime(f"%Y-%m-%d-watch.log")) logging.basicConfig(level=logging.INFO, format='%(asctime)s [%(levelname)s] %(message)s', datefmt='%Y-%m-%d %H:%M:%S', filename=filename)
def calendarXY(date, dir): if date in prevDays: return prevDays[date] else: prevDays[date] = [] prevDay = data.Stock(dir).afterDates(date).__len__() dateT = datetime.fromisoformat(date) nM = datetime.now().month nY = datetime.now().year prevMonth = 0 dayX = 1 dayY = dateT.isocalendar()[1] % 6 if nY != dateT.year: prevMonth += ((nY - dateT.year) * 12) if nM != dateT.month: prevMonth += abs(nM - dateT.month) if dateT.isocalendar()[2] != 7: dayX = dateT.isocalendar()[2] + 1 weeks = calendar.Calendar(calendar.SUNDAY).monthdayscalendar( dateT.year, dateT.month) for index in range(weeks.__len__()): if dateT.day in weeks[index]: dayY = index + 1 d = [prevDay, prevMonth, dayX, dayY] for v in d: prevDays[date].append(v) return d
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg import matplotlib.ticker as ticker import tkinter from xlsx import trend from stock import data, name, weak from ui import pattern from scipy.interpolate import UnivariateSpline from tkinter import ttk from matplotlib.lines import Line2D from pywinauto.application import Application import pyautogui import mplfinance as mpf from ui import k d = [] stock = data.Stock("D:\\data\\csv\\stock") trend = data.Trend("D:\\data\\csv") # paths = glob.glob( # os.path.join('D:\\data\\csv\\strategy\\weak\\yesterday_red_trend_high10_low11_left_head', '*', '*.csv')) # # for path in sorted(paths, reverse=True): # date = os.path.basename(path).split('.')[0] # # for i, v in pd.read_csv(path).iterrows(): # d.append([ # v['code'], # v['name'], # date, # v['increase'], # v['d_increase'],
# 統計弱勢股紅黑K比例 import pandas as pd import glob import configparser import os from stock import name, data NAME = 'weak_black_red' config = configparser.ConfigParser() config.read('../config.ini') data_path = os.path.abspath(dict(config['path'])['data']) weak_path = os.path.join(data_path, 'csv', 'strategy', '', 'all') stock = data.Stock(os.path.join(data_path, 'csv', 'stock')) stock.readAll() dates = stock.dates() result = [] for year in ['2020', '2019', '2018', '2017']: total = 0 red = 0 red_1_5 = 0 black = 0 for dir_path in sorted(glob.glob(os.path.join(weak_path, year + '*')), reverse=False): if os.path.isdir(dir_path) == False: continue for csv_path in sorted(glob.glob(os.path.join(dir_path, "*.csv")), reverse=False): date = os.path.basename(csv_path).split('.')[0]
import math import pyautogui import time import pandas as pd import numpy as np import os import glob from xlsx import cmoney, xq, csv from stock import data, name import matplotlib.pyplot as plt stock = data.Stock('D:\\data\\csv\\stock') trend = data.Trend("D:\\data\\csv") data.calendar_xy('2017-01-11', year=2020, month=12) # 47 4 3 data.calendar_xy('2019-01-02') # 24 4 1 data.calendar_xy('2020-01-02') # 12 5 1 data.calendar_xy('2019-01-02', year=2020, month=8) # 19 4 1 data.calendar_xy('2018-09-21', year=2020, month=8) # 23 6 4 d = {} d1 = [] def DirToCode(path): for p in glob.glob(f"{path}\\*.csv"): for i, v in pd.read_csv(p).iterrows(): d1.append( [v['code'], v['name'], os.path.basename(p).split('.')[0]])
def __init__(self, dir, csv_dir): Trend.__init__(self, dir) self.stock = data.Stock(csv_dir)