<file_sep>from PyQt5 import QtCore, QtGui, QtWidgets
from DataFrameModel import PandasModel
import pandas as pd


class FromInit(QtWidgets.QWidget):
    def __init__(self, parent=None):
        QtWidgets.QWidget.__init__(self, parent=None)
        self.setWindowTitle('AMS')
        self.resize(500,300)

        self.rptDate = ''
        self.rptAcct = ''

        vLayout = QtWidgets.QVBoxLayout(self)
        hLayout = QtWidgets.QHBoxLayout()

        lblDate = QtWidgets.QLabel(self)
        lblDate.setText('Date')
        hLayout.addWidget(lblDate)

        cal = QtWidgets.QCalendarWidget(self)
        dtEdit = QtWidgets.QDateEdit(QtCore.QDate.currentDate(), self)
        dtEdit.setCalendarPopup(True)
        dtEdit.setCalendarWidget(cal)
        dtEdit.setMaximumDate(QtCore.QDate.currentDate())
        dtEdit.setDate(cal.selectedDate())
        dtEdit.dateChanged.connect(self.saveDate)
        hLayout.addWidget(dtEdit)

        lblAcct = QtWidgets.QLabel(self)
        lblAcct.setText('Account')
        hLayout.addWidget(lblAcct)

        comboAcct = QtWidgets.QComboBox()
        comboAcct.addItems(['Citic', 'CMB', 'ALL'])
        comboAcct.currentTextChanged.connect(self.saveAcct)
        hLayout.addWidget(comboAcct)

        hLayout.addStretch(1)

        btnReport = QtWidgets.QPushButton("REPORT", self)
        btnReport.clicked.connect(self.loadReport)
        hLayout.addWidget(btnReport)

        vLayout.addLayout(hLayout)

        self.tblHold = QtWidgets.QTableView(self)
        
        vLayout.addWidget(self.tblHold)

        self.rptDate = dtEdit.date().toString('yyyyMMdd')
        self.rptAcct = comboAcct.currentText()

    def loadReport(self):
        df = pd.read_excel('A_Shares.xlsx',sheet_name='Trans')
        model = PandasModel(df)
        self.tblHold.setModel(model)
        self.tblHold.setSortingEnabled(True)
        self.tblHold.horizontalHeader().setStyleSheet("QHeaderView::section {background-color:lightblue;color: black;padding-left: 4px;border: 1px solid #6c6c6c;font: bold;}")

    def saveAcct(self, str):
        self.rptAcct = str

    def saveDate(self, date):
        self.rptDate = date.toString('yyyyMMdd')

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = FromInit()
    w.show()
    sys.exit(app.exec_())<file_sep># -*- coding: utf-8 -*-

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QWidget
from first import Ui_First
from second import Ui_Second
 
class Ui_MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        QtWidgets.QWidget.__init__(self, parent=None)
        
        # initialize format of main form
        self.setWindowTitle('AMS')
        self.setFixedSize(900,600)
        #self.resize(1200,600)

        self.first = First()
        self.second = Second()
        self.mainSplitter = QtWidgets.QSplitter(QtCore.Qt.Horizontal)

        # main form layout
        mainWidget = QtWidgets.QWidget()
        mainLayout = QtWidgets.QVBoxLayout()
        mainLayout.setContentsMargins(3, 0, 3, 1)
        mainWidget.setLayout(mainLayout)
        mainLayout.addWidget(self.mainSplitter)
        self.setCentralWidget(mainWidget)

        # initialize menu widgets
        menuSplitter = QtWidgets.QSplitter(QtCore.Qt.Vertical)
        menuSplitter.setHandleWidth(0)
        pixLogo = QtGui.QPixmap('logo.png')
        lblLogo = QtWidgets.QLabel()
        lblLogo.setScaledContents(True)
        lblLogo.setFixedSize(150,60)
        lblLogo.setPixmap(pixLogo)
        menuSplitter.addWidget(lblLogo)
        btnReport = QtWidgets.QPushButton('REPORT')
        btnReport.setFixedSize(150,30)
        menuSplitter.addWidget(btnReport)
        btnFunc = QtWidgets.QPushButton('FUNCTION')
        btnFunc.setFixedSize(150,30)
        menuSplitter.addWidget(btnFunc)
        #用frame占位，保持菜单按钮位置固定不变
        frame = QtWidgets.QFrame()
        menuSplitter.addWidget(frame)

        # initialize main splitter
        self.mainSplitter.addWidget(menuSplitter)
        frameTest = QtWidgets.QFrame()
        frameTest.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.mainSplitter.addWidget(frameTest)
        #菜单默认关闭
        self.mainSplitter.setSizes([1, 1]) 

        # format handle of main splitter
        self.mainSplitter.setHandleWidth(20)
        self.handleLayout(self.mainSplitter)

        self.mainSplitter.setStyleSheet('''
            QWidget{border-style:solid;border-width:2;border-color:red}
            ''')
        
        menuSplitter.setStyleSheet('''
            QPushButton{border:none;color:white;;background-color:black}
            QLabel{border:none;background-color:black}
            QFrame{border:none;background-color:black}
            ''')

        # connect button function
        btnReport.clicked.connect(lambda :self.changeUI('REPORT'))
        btnFunc.clicked.connect(lambda :self.changeUI('FUNCTION'))

    def handleLayout(self, splitter):
        handle = splitter.handle(1) 
        layout = QtWidgets.QVBoxLayout() 
        layout.setContentsMargins(0, 0, 0, 0) 
        button = QtWidgets.QToolButton(handle) 
        button.setArrowType(QtCore.Qt.LeftArrow) 
        button.setFixedSize(20,40)
        button.clicked.connect(lambda: self.handleSplitterButton(True)) 
        layout.addWidget(button)
        handle.setLayout(layout) 

    def handleSplitterButton(self, left=True): 
        if not all(self.mainSplitter.sizes()): 
            self.mainSplitter.setSizes([1, 1]) 
        elif left: 
            self.mainSplitter.setSizes([0, 1]) 

    def changeUI(self,name):
        if name == "REPORT":
            self.mainSplitter.widget(1).setParent(None)
            self.mainSplitter.insertWidget(1, self.first)
            self.handleLayout(self.mainSplitter)
 
        if name == "FUNCTION":
            self.mainSplitter.widget(1).setParent(None)
            self.mainSplitter.insertWidget(1, self.second)
            self.handleLayout(self.mainSplitter)
        
class First(QWidget, Ui_First):
    def __init__(self):
        super(First,self).__init__()
        # 子窗口初始化时实现子窗口布局
        self.initUI(self)
 
        # 设置子窗体最小尺寸
        self.setMinimumWidth(30)
        self.setMinimumHeight(30)
 
class Second(QWidget, Ui_Second):
    def __init__(self):
        super(Second,self).__init__()
        self.initUI(self)
        self.setMinimumWidth(30)
        self.setMinimumHeight(30)
 
if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    ui = Ui_MainWindow()
    ui.show()
    sys.exit(app.exec_())<file_sep># -*- coding: utf-8 -*-
 
# Form implementation generated from reading ui file 'index.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!
 
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QWidget
from first import Ui_First
from second import Ui_Second
 
 
class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.horizontalLayout = QtWidgets.QHBoxLayout(self.centralwidget)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.splitter = QtWidgets.QSplitter(self.centralwidget)
        self.splitter.setOrientation(QtCore.Qt.Horizontal)
        self.splitter.setObjectName("splitter")
        self.frame = QtWidgets.QFrame(self.splitter)
        self.frame.setMaximumSize(QtCore.QSize(200, 16777215))
        self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
        self.frame.setObjectName("frame")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.frame)
        self.verticalLayout.setObjectName("verticalLayout")
        self.pushButton = QtWidgets.QPushButton(self.frame)
        self.pushButton.setObjectName("pushButton")
        self.verticalLayout.addWidget(self.pushButton)
        self.pushButton_2 = QtWidgets.QPushButton(self.frame)
        self.pushButton_2.setObjectName("pushButton_2")
        self.verticalLayout.addWidget(self.pushButton_2)
 
        # 这里注释掉，因为不需要frame2。当初在qt 里面设计这个frame2的原因是为了占位，不至于到时候布局出现错乱。
        # self.frame_2 = QtWidgets.QFrame(self.splitter)
        # self.frame_2.setStyleSheet("background-color:white;")
        # self.frame_2.setFrameShape(QtWidgets.QFrame.StyledPanel)
        # self.frame_2.setFrameShadow(QtWidgets.QFrame.Raised)
        # self.frame_2.setObjectName("frame_2")
 
        self.horizontalLayout.addWidget(self.splitter)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 23))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
 
        # 初始化两个对象，并把 first对象 加入到 splitter 中
        self.first = First()
        self.second = Second()
        self.splitter.addWidget(self.first)
 
        self.pushButton.clicked.connect(lambda :self.change(self.pushButton.objectName()))
        self.pushButton_2.clicked.connect(lambda :self.change(self.pushButton_2.objectName()))
 
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)
 
    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "界面1"))
        self.pushButton_2.setText(_translate("MainWindow", "界面2"))
 
    def change(self,name):
        if name == "pushButton":
            self.splitter.widget(1).setParent(None)
            self.splitter.insertWidget(1, self.first)
 
        if name == "pushButton_2":
            self.splitter.widget(1).setParent(None)
            self.splitter.insertWidget(1, self.second)
 
class First(QWidget, Ui_First):
    def __init__(self):
        super(First,self).__init__()
        # 子窗口初始化时实现子窗口布局
        self.setupUi(self)
 
        # 设置子窗体最小尺寸
        self.setMinimumWidth(30)
        self.setMinimumHeight(30)
 
class Second(QWidget, Ui_Second):
    def __init__(self):
        super(Second,self).__init__()
        self.setupUi(self)
        self.setMinimumWidth(30)
        self.setMinimumHeight(30)
 
if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())<file_sep># -*- coding: utf-8 -*-
 
from PyQt5 import QtCore, QtGui, QtWidgets
from DataFrameModel import PandasModel
import pandas as pd
 
 
class Ui_DailySummary(object):
    def initUI(self, Ui_DailySummary):

        #初始化报告参数
        self.rptDate = ''
        self.rptAcct = ''

        vLayout = QtWidgets.QVBoxLayout(self)
        hLayout = QtWidgets.QHBoxLayout()
        hLayout2 = QtWidgets.QHBoxLayout()

        lblDate = QtWidgets.QLabel(self)
        lblDate.setText('Date')
        hLayout.addWidget(lblDate)

        cal = QtWidgets.QCalendarWidget(self)
        dtEdit = QtWidgets.QDateEdit(QtCore.QDate.currentDate(), self)
        dtEdit.setCalendarPopup(True)
        dtEdit.setCalendarWidget(cal)
        dtEdit.setMaximumDate(QtCore.QDate.currentDate())
        dtEdit.setDate(cal.selectedDate())
        dtEdit.dateChanged.connect(self.saveDate)
        #dtEdit.setFixedWidth(120)
        hLayout.addWidget(dtEdit)

        lblAcct = QtWidgets.QLabel(self)
        lblAcct.setText('Account')
        hLayout.addWidget(lblAcct)

        comboAcct = QtWidgets.QComboBox()
        comboAcct.setEditable(True)
        comboAcct.lineEdit().setAlignment(QtCore.Qt.AlignLeft)
        comboAcct.addItems(['Citic', 'CMB', 'ALL'])
        comboAcct.currentTextChanged.connect(self.saveAcct)
        comboAcct.setStyleSheet("background-color:white;")
        hLayout.addWidget(comboAcct)

        hLayout.addStretch(10)

        btnReport = QtWidgets.QPushButton("RUN REPORT", self)
        btnReport.clicked.connect(self.loadReport)
        hLayout.addWidget(btnReport)

        hLayout.addStretch(1)

        widget = QtWidgets.QWidget()
        widget.setLayout(hLayout)
        #widget.setFixedHeight(50)

        vLayout.addWidget(widget)

        hLayout.setContentsMargins(0, 0, 0, 0)
        hLayout2.setContentsMargins(0, 0, 0, 0)
        vLayout.setContentsMargins(0, 0, 0, 0)
        vLayout.setSpacing(0)

        self.tblHold = QtWidgets.QTableView(self)
        hLayout2.addWidget(self.tblHold)

        vLayout.addLayout(hLayout2)

        self.rptDate = dtEdit.date().toString('yyyyMMdd')
        self.rptAcct = comboAcct.currentText()

    def loadReport(self):
        df = pd.read_excel('A_Shares.xlsx',sheet_name='Trans')
        model = PandasModel(df)
        self.tblHold.setModel(model)
        self.tblHold.setSortingEnabled(True)
        self.tblHold.horizontalHeader().setStyleSheet("QHeaderView::section {background-color:lightblue;color: black;padding-left: 4px;border: 1px solid #6c6c6c;font: bold;}")

    def saveAcct(self, str):
        self.rptAcct = str

    def saveDate(self, date):
        self.rptDate = date.toString('yyyyMMdd')<file_sep># -*- coding: utf-8 -*-
 
# Form implementation generated from reading ui file 'second.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!
 
from PyQt5 import QtCore, QtGui, QtWidgets
 
class Ui_Second(object):
    def setupUi(self, Ui_Second):
        Ui_Second.setObjectName("MainWindow")
        Ui_Second.resize(800, 600)
        # self.centralwidget = QtWidgets.QWidget(Ui_Second)
        # self.centralwidget.setObjectName("centralwidget")
        self.horizontalLayout = QtWidgets.QHBoxLayout(Ui_Second)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.frame_2 = QtWidgets.QFrame(Ui_Second)
        self.frame_2.setStyleSheet("background-color:green;")
        self.frame_2.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.frame_2.setFrameShadow(QtWidgets.QFrame.Raised)
        self.frame_2.setObjectName("frame_2")
        self.label = QtWidgets.QLabel(self.frame_2)
        self.label.setGeometry(QtCore.QRect(300, 180, 181, 81))
        font = QtGui.QFont()
        font.setPointSize(20)
        font.setBold(True)
        font.setWeight(75)
        self.label.setFont(font)
        self.label.setObjectName("label")
        self.horizontalLayout.addWidget(self.frame_2)
        # Ui_Second.setCentralWidget(self.centralwidget)
        # self.menubar = QtWidgets.QMenuBar(Ui_Second)
        # self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 23))
        # self.menubar.setObjectName("menubar")
        # Ui_Second.setMenuBar(self.menubar)
        # self.statusbar = QtWidgets.QStatusBar(Ui_Second)
        # self.statusbar.setObjectName("statusbar")
        # Ui_Second.setStatusBar(self.statusbar)
 
        self.retranslateUi(Ui_Second)
        QtCore.QMetaObject.connectSlotsByName(Ui_Second)
 
    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.label.setText(_translate("MainWindow", "这是界面2"))
 <file_sep>import pandas as pd 
import time

tuple_cur = ('CNY', 'HKD', 'USD')

def get_trans(date, account):
    #读取交易数据
    df_trans_all = pd.read_excel('A_Shares.xlsx',sheet_name='Trans')
    df_trans_all['Date'] = df_trans_all[['Date','Symbol_Code']].astype(str)
    df_trans = df_trans_all[df_trans_all['Date'] <= date]
    df_trans = df_trans[df_trans['Account'].isin(account)]
    df_trans = df_trans.round(4)
    return df_trans

def get_cost(code, qt, df):
    df_code = df[df['Symbol_Code'].isin([code])]
    df_code.sort_values(by = ['Date'],axis = 0,ascending = False, inplace=True)
    
    #从当前持仓日往前取买入即分红送股数据获取成本金额
    #直至当前持仓数量扣除完整为止
    amt_cost = 0
    for i in df_code.index.tolist():
        if qt <= 0:
            break
        else:
            qt_trans = df_code.loc[i,'Quantity']
            amt_trans =  df_code.loc[i,'Settle_Amt']
            amt_cost = amt_cost + (amt_trans * (qt/qt_trans if qt_trans>qt else 1)) * -1
            qt = qt - qt_trans
    
    return amt_cost

     
def get_holding(date, df_trans):
    #取股票持仓
    df_trans_stock = df_trans[~df_trans['Symbol_Code'].isin(tuple_cur)]
    df_group_stock = df_trans_stock.loc[:, ['Symbol_Code', 'Symbol_Name', 'Cur', 'Quantity']]
    df_group_stock = df_group_stock.groupby(['Symbol_Code', 'Symbol_Name', 'Cur']).sum()
    df_group_stock = df_group_stock.reset_index()
    df_group_stock.drop(index=(df_group_stock.loc[(df_group_stock['Quantity']==0)].index), inplace=True)
    df_group_stock['Date'] = date
    
    #取持仓股票成本
    df_trans_buy = df_trans_stock[df_trans_stock['Quantity']>=0]
    df_group_stock['Cost_Amt'] = float(0)
    for i in df_group_stock.index.tolist():
        code = df_group_stock.loc[i,'Symbol_Code']
        hold_quantity = df_group_stock.loc[i,'Quantity']
        if hold_quantity > 0:
            cost_amt = get_cost(code, hold_quantity, df_trans_buy)
            df_group_stock.at[i, 'Cost_Amt'] = cost_amt

    #取现金持仓
    df_trans_cur = df_trans
    df_group_cur = df_trans_cur.loc[:, ['Cur', 'Settle_Amt']]
    df_group_cur = df_group_cur.groupby(['Cur']).sum()
    df_group_cur = df_group_cur.reset_index()
    df_group_cur['Date'] = date
    df_group_cur['Symbol_Code'] = df_group_cur['Cur']
    df_group_cur['Symbol_Name'] = df_group_cur['Cur']
    df_group_cur.rename(columns={'Settle_Amt':'Quantity'}, inplace=True)
    df_group_cur['Cost_Amt'] = df_group_cur['Quantity']
    
    #合并资金及股票持仓
    df_hold = df_group_stock.append(df_group_cur, ignore_index=True)
    df_hold = df_hold.round(4)
    df_hold.sort_values(by = ['Cur', 'Symbol_Code'],axis = 0,ascending = True, inplace=True)
    
    #获取行业信息
    df_sector = pd.read_excel('A_Shares.xlsx',sheet_name='Sector')
    df_hold = pd.merge(df_hold, df_sector, how='left')
    df_hold = df_hold[['Date','Symbol_Code', 'Symbol_Name', 'Sector', 'Cur', 'Quantity', 'Cost_Amt']]
    
    return df_hold


##############################################################################

trade_date = '20190814'
trade_date = time.strftime('%Y%m%d')

account_id = 'All'
if account_id == 'All':
    account_id = ['CMS', 'Citic']
else:
    account_id = [account_id]

trans_date = get_trans(trade_date, account_id)
hold = get_holding(trade_date, trans_date)
print(hold)


