class SimpleQuantTransition(SimpleQuantSafeProcess):
    def __init__(self, stockSymbol, strategyName, startDate, endDate, feedbackQueue):
        SimpleQuantSafeProcess.__init__(self, feedbackQueue)
        self.strategy_manager = SimpleQuantStrategyManager()
        self.data_manager = SimpleQuantUIDataManager(stockSymbol, startDate, endDate)
        #TODO:fix crash cause by event engine
        #self.event_engine = SimpleQuantEventEngine()
        self.strategy_name = strategyName

    def getStockData(self):
        hqData = self.data_manager.getStockData()
        return hqData

    #TODO: remove runStrategy() when multi-process done
    def runStrategy(self, strategyName):
        MyLogger.info("Run Strategy")
        self.strategy_manager.setStrategyName(strategyName)
        self.stock_strategy = self.strategy_manager.getStrategyInstance()(self.data_manager)
        self.profitsData = self.stock_strategy.backTest()
        return self.profitsData

    def updateTransitionContext(self, startDate, endDate):
        self.data_manager.setStockContext(startDate, endDate)

    def getStrategyNameList(self):
        return self.strategy_manager.getStrategyNameList()

    def saferun(self):
        MyLogger.info("Start Transition")
        MyLogger.info("Run Strategy")
        self.strategy_manager.setStrategyName(self.strategy_name)
        self.stock_strategy = self.strategy_manager.getStrategyInstance()(self.data_manager)
        self.profitsData = self.stock_strategy.backTest()
        self.endUp()

    def endUp(self):
        MyLogger.info("End Up Transition")
        hqData = self.getStockData()
        profits = self.profitsData
        with open('app/static/json/hqData.json', 'w') as f:
            f.write(json.dumps(hqData))
        with open('app/static/json/profits.json', 'w') as f:
            f.write(json.dumps(profits))
Beispiel #2
0
class SimpleQuantUITransitionDialog(QtGui.QDialog):
    def __init__(self, stockSymbol):
        QtGui.QDialog.__init__(self)
        #today is endDate, startDate is earlier than endDate
        endDate = datetime.datetime.today()
        deltaDays = datetime.timedelta(days=-60)
        startDate = endDate + deltaDays
        self.strategy_manager = SimpleQuantStrategyManager()
        self.data_manager = SimpleQuantUIDataManager(stockSymbol, startDate, endDate)
        self.event_engine = SimpleQuantEventEngine()
        #self.stock_strategy = SimpleQuantStrategyMACD(self.data_manager)
        self.setGeometry(100, 100, 850, 650)
        self.main_widget = QtGui.QWidget(self)
        
        self.start_date = QtGui.QDateEdit(self)
        self.start_date.setCalendarPopup(True)
        startDateStr = startDate.strftime("%Y-%m-%d")
        self.start_date.setDate(QDate.fromString(startDateStr, 'yyyy-MM-dd'))
        startDateLabel = QtGui.QLabel('StartDate: ', self)
        
        endDateStr = endDate.strftime("%Y-%m-%d")
        self.end_date = QtGui.QDateEdit(self)
        self.end_date.setCalendarPopup(True)
        self.end_date.setDate(QDate.fromString(endDateStr, 'yyyy-MM-dd'))
        endDateLabel = QtGui.QLabel('EndDate: ', self)
        
        
        self.update_button = QtGui.QPushButton("Update")
        self.update_button.clicked.connect(self.updateButtonClicked)
        self.transition_layout = QtGui.QVBoxLayout(self.main_widget)
        self.stock_layout = QtGui.QHBoxLayout()
        self.stock_widget_layout = QtGui.QVBoxLayout()
        self.stock_widget_layout.addWidget(startDateLabel)
        self.stock_widget_layout.addWidget(self.start_date)
        self.stock_widget_layout.addWidget(endDateLabel)
        self.stock_widget_layout.addWidget(self.end_date)
        hqData = self.data_manager.getStockData()
        self.stock_canvas = SimpleQuantUIStrategyMplCanvas(hqData)
        self.stock_layout.addStretch(1)
        self.stock_layout.addWidget(self.stock_canvas)
        self.stock_layout.addLayout(self.stock_widget_layout)
        self.stock_layout.addWidget(self.update_button)
        
        self.strategy_layout = QtGui.QHBoxLayout()
        self.strategy_button = QtGui.QPushButton("Run")
        self.strategy_button.clicked.connect(self.strategyButtonClicked)
        self.strategy_combobox = QtGui.QComboBox()
        self.strategy_combobox.activated[str].connect(self.strategyChanged)
        [self.strategy_combobox.addItem(strategyName) for strategyName in self.strategy_manager.getStrategyNameList()]
        for entry in hqData:
            entry[1] = '0'
        self.strategy_canvas = SimpleQuantUIStrategyMplCanvas(hqData)
        self.strategy_layout.addStretch(1)
        self.strategy_layout.addWidget(self.strategy_canvas)
        self.strategy_layout.addWidget(self.strategy_button)
        self.strategy_layout.addWidget(self.strategy_combobox)
        
        self.online_radio_button = QtGui.QRadioButton('Online')
        self.offline_radio_button = QtGui.QRadioButton('Offline')
        self.online_radio_button.toggled.connect(self.onlineClicked)
        self.offline_radio_button.toggled.connect(self.offlineClicked)
        
        self.transition_layout.addLayout(self.stock_layout)
        self.transition_layout.addLayout(self.strategy_layout)
        self.transition_layout.addWidget(self.online_radio_button)
        self.transition_layout.addWidget(self.offline_radio_button)
        
    def updateButtonClicked(self):
        endDate = self.end_date.date().toPyDateTime()
        startDate = self.start_date.date().toPyDateTime()
        self.data_manager.setStockContext(startDate, endDate)
        stockData = self.data_manager.getStockData()
        self.stock_canvas.updateFigure(stockData)
        self.stock_canvas.draw()
        
    def strategyButtonClicked(self):
        self.stock_strategy = self.strategy_manager.getStrategyInstance()(self.data_manager)
        profitsData = self.stock_strategy.backTest()
        self.strategy_canvas.updateFigure(profitsData)
        self.strategy_canvas.draw()
        MyLogger.info("Done!")
        
    def strategyChanged(self, strategyName):
        self.strategy_manager.setStrategyName(strategyName)
        
    def onlineClicked(self):
        self.event_engine.start()
        self.event_engine.register(EVENT_TIMER, self.timerHandler)
        
    def offlineClicked(self):
        self.event_engine.unregister(EVENT_TIMER, self.timerHandler)
        self.event_engine.stop()
        
    def timerHandler(self, event):
        self.data_manager.retreiveRealTimeQuotes()