def __init__(self): QtGui.QWidget.__init__(self) # This is always the same self.ui = Ui_chartWidget() self.ui.setupUi(self) self.indicators = [] ## Create a new GraphicsScene and set GraphicsView (chart) to scene chartViews.append(ChartView("nok")) self.chartView = chartViews[0] self.ui.chartTabs.addTab(self.chartView, "nok") ## Connect buttons self.connect(self.ui.chartLength, QtCore.SIGNAL("valueChanged(int)"), self.onZoomChart) self.connect(self.ui.nextDay, QtCore.SIGNAL("clicked()"), self.onNextDay) self.connect(self.ui.prevDay, QtCore.SIGNAL("clicked()"), self.onPrevDay) self.connect(self.ui.next30, QtCore.SIGNAL("clicked()"), self.onNext30) self.connect(self.ui.prev30, QtCore.SIGNAL("clicked()"), self.onPrev30) self.connect(self.ui.buy, QtCore.SIGNAL("clicked()"), self.onBuy) self.connect(self.ui.sell, QtCore.SIGNAL("clicked()"), self.onSell) ## Indicators self.connect(self.ui.addIndicator, QtCore.SIGNAL("clicked()"), self.onAddIndicator) ## Chart Styles self.connect(self.ui.chartStyle, QtCore.SIGNAL("currentIndexChanged(int)"), self.onChartStyleChange) ## Chart and Tab Controls self.connect(self.ui.symbolEntry, QtCore.SIGNAL("returnPressed()"), self.onNewTab) self.connect(self.ui.newTab, QtCore.SIGNAL("clicked()"), self.onNewTab) self.connect(self.ui.chartTabs, QtCore.SIGNAL("currentChanged(int)"), self.onChangeTab) self.connect(self.ui.chartTabs, QtCore.SIGNAL("tabCloseRequested(int)"), self.onCloseTab) ## Maximize screen #self.setWindowState(QtCore.Qt.WindowMaximized) ## Defaults #self.ui.chartTabs.setTabsClosable(True) self.update()
class Main(QtGui.QWidget): def __init__(self): QtGui.QWidget.__init__(self) # This is always the same self.ui = Ui_chartWidget() self.ui.setupUi(self) self.indicators = [] ## Create a new GraphicsScene and set GraphicsView (chart) to scene chartViews.append(ChartView("nok")) self.chartView = chartViews[0] self.ui.chartTabs.addTab(self.chartView, "nok") ## Connect buttons self.connect(self.ui.chartLength, QtCore.SIGNAL("valueChanged(int)"), self.onZoomChart) self.connect(self.ui.nextDay, QtCore.SIGNAL("clicked()"), self.onNextDay) self.connect(self.ui.prevDay, QtCore.SIGNAL("clicked()"), self.onPrevDay) self.connect(self.ui.next30, QtCore.SIGNAL("clicked()"), self.onNext30) self.connect(self.ui.prev30, QtCore.SIGNAL("clicked()"), self.onPrev30) self.connect(self.ui.buy, QtCore.SIGNAL("clicked()"), self.onBuy) self.connect(self.ui.sell, QtCore.SIGNAL("clicked()"), self.onSell) ## Indicators self.connect(self.ui.addIndicator, QtCore.SIGNAL("clicked()"), self.onAddIndicator) ## Chart Styles self.connect(self.ui.chartStyle, QtCore.SIGNAL("currentIndexChanged(int)"), self.onChartStyleChange) ## Chart and Tab Controls self.connect(self.ui.symbolEntry, QtCore.SIGNAL("returnPressed()"), self.onNewTab) self.connect(self.ui.newTab, QtCore.SIGNAL("clicked()"), self.onNewTab) self.connect(self.ui.chartTabs, QtCore.SIGNAL("currentChanged(int)"), self.onChangeTab) self.connect(self.ui.chartTabs, QtCore.SIGNAL("tabCloseRequested(int)"), self.onCloseTab) ## Maximize screen #self.setWindowState(QtCore.Qt.WindowMaximized) ## Defaults #self.ui.chartTabs.setTabsClosable(True) self.update() ############################################################################# ## Event Handlers ############################################################################# def update(self): self.updateAccounts() self.updateTime() self.chartView.drawChart() self.updateIndicators() for ii in self.indicators: apply(ii[0], ii[1:]) ############################################################################# ## Mulit-Chart View and Symbol Loading ############################################################################# def onNewTab(self): t = str(self.ui.symbolEntry.text()) self.ui.symbolEntry.clear() c = ChartView(t) chartViews.append(c) self.ui.chartTabs.addTab(c, t) def onCloseTab(self, num): chartViews.pop(num-1) self.ui.chartTabs.removeTab(num) def onChangeTab(self, x): self.chartView = chartViews[self.ui.chartTabs.currentIndex()-1] self.chartView.drawChart() self.update() ############################################################################# ## Chart Controls ## Zoom In/Out, Chart Style (ohlc, candlestick, etc), Normal/Log Scale, etc ############################################################################# def onZoomChart(self, i): self.chartView.chartLength = i self.chartView.drawChart() self.update() def onChartStyleChange(self, i): choices = [self.chartView.drawCandlesticks, self.chartView.drawOHLC, self.chartView.drawHLC, self.chartView.drawBar, self.chartView.drawDot, self.chartView.drawClose] self.chartView.chartStyle = choices[i] self.chartView.drawChart() self.update() ############################################################################# ## Current Day Display ## Date, Open, High, Low, Close ############################################################################# def updateCurrentDayDisplay(self): todayData = self.chartView.data.currentDay(time.currentDay) self.ui.currentDayDate.setText(todayData[0]) self.ui.currentDayOpen.setText(str(todayData[1])) self.ui.currentDayHigh.setText(str(todayData[2])) self.ui.currentDayLow.setText(str(todayData[3])) self.ui.currentDayClose.setText(str(todayData[4])) self.ui.currentDayVolume.setText(str(todayData[5])) ############################################################################# ## Time Controls ## Next/Prev Day ############################################################################# def updateTime(self): self.updateCurrentDayDisplay() self.ui.daysLeft.display(time.currentDay) def onNextDay(self): if time.currentDay > 0: time.currentDay -= 1 self.chartView.drawChart() self.update() def onPrevDay(self): time.currentDay += 1 self.chartView.drawChart() self.update() def onNext30(self): for ii in range(30): if time.currentDay > 0: time.currentDay -=1 self.update() self.chartView.drawChart() self.update() def onPrev30(self): for ii in range(30): time.currentDay +=1 self.update() self.chartView.drawChart() self.update() ############################################################################# ## Account Controls ## Buy/Sell, Stop/Limit, % Gain/Loss of last trade, % Gain/Loss Total, etc. ############################################################################# def updateAccounts(self): account.update() self.ui.showBalance.setText(str(account.balance)) self.ui.showPortfolio.setText(str(account.portfolio)) self.ui.showPortfolioValue.setText(str(account.portfolioValue())) p = account.portfolioProfit() pp = account.portfolioPercentage() c = "green" if p >= 0 else "red" self.ui.showProfit.setText(str(p)) self.ui.showProfitPercent.setText(str(pp)+"%") self.ui.showProfit.setStyleSheet("QLabel { color : " + c + ";}") self.ui.showProfitPercent.setStyleSheet("QLabel { color : " + c + ";}") def onBuy(self): account.buy(self.chartView.symbol, self.ui.buyShares.text()) self.ui.buyShares.clear() # TODO: Add code in to view pending orders, this should be cleared #self.ui.buyStop.clear() self.update() def onSell(self): if self.ui.stopLoss.text(): account.setStop(self.chartView.symbol, float(self.ui.stopLoss.text()), self.ui.sellShares.text()) else: account.sell(self.chartView.symbol, self.ui.sellShares.text()) self.ui.sellShares.clear() # TODO: Add code in to view pending orders, this should be cleared #self.ui.sellStop.clear() self.update() ############################################################################# ## Technical Indicators ## Moving Averages, Bollinger Bands, RSI, Volume, etc ## TODO: ## Add "your stop loss" indicator that plots the stop loss price ############################################################################# def updateIndicators(self): for ii in self.indicators: apply(ii[0], ii[1:]) def drawSMA(self, line): self.chartView.drawLine(self.chartView.data.sma(line.value(), time.currentDay, self.chartView.chartLength), line.color()) def drawWMA(self, line): self.chartView.drawLine(self.chartView.data.wma(line.value(), time.currentDay, self.chartView.chartLength), line.color()) def drawEMA(self, line): self.chartView.drawLine(self.chartView.data.ema(line.value(), time.currentDay, self.chartView.chartLength), line.color()) def drawBollingerBands(self, top, sma, bottom): d = self.chartView.data.bollingerBands(sma.value(), time.currentDay, self.chartView.chartLength) self.chartView.drawLine(d[0], top.color()) self.chartView.drawLine(d[1], sma.color()) self.chartView.drawLine(d[2], bottom.color()) def drawDonchianChannel(self, top, bottom): d = self.chartView.data.donchianChannel(top.value(), time.currentDay, self.chartView.chartLength) self.chartView.drawLine(d[0], top.color()) self.chartView.drawLine(d[1], bottom.color()) def onAddIndicator(self): i = self.ui.newIndicator.currentIndex() if i == 0: IndicatorWidget(self, "Simple Moving Average", self.drawSMA, [Line(self, "Period:")]) elif i == 1: IndicatorWidget(self, "Weighted Moving Average", self.drawWMA, [Line(self, "Period:")]) elif i == 2: IndicatorWidget(self, "Exponential Moving Average", self.drawEMA, [Line(self, "Period:")]) elif i == 3: IndicatorWidget(self, "Bollinger Bands", self.drawBollingerBands, [Line(self, "Top:", entryBox=False, defaultColor="green"), Line(self, "SMA:", defaultColor="blue"), Line(self, "Bottom:", entryBox=False, defaultColor="red")]) elif i == 4: IndicatorWidget(self, "Donchian Channel", self.drawDonchianChannel, [Line(self, "Top:", defaultColor="green"), Line(self, "Bottom", entryBox=False, defaultColor="red")]) self.update()
class Main(QtGui.QWidget): def __init__(self): QtGui.QWidget.__init__(self) # This is always the same self.ui = Ui_chartWidget() self.ui.setupUi(self) self.indicators = [] ## Create a new GraphicsScene and set GraphicsView (chart) to scene chartViews.append(ChartView("nok")) self.chartView = chartViews[0] self.ui.chartTabs.addTab(self.chartView, "nok") ## Connect buttons self.connect(self.ui.chartLength, QtCore.SIGNAL("valueChanged(int)"), self.onZoomChart) self.connect(self.ui.nextDay, QtCore.SIGNAL("clicked()"), self.onNextDay) self.connect(self.ui.prevDay, QtCore.SIGNAL("clicked()"), self.onPrevDay) self.connect(self.ui.next30, QtCore.SIGNAL("clicked()"), self.onNext30) self.connect(self.ui.prev30, QtCore.SIGNAL("clicked()"), self.onPrev30) self.connect(self.ui.buy, QtCore.SIGNAL("clicked()"), self.onBuy) self.connect(self.ui.sell, QtCore.SIGNAL("clicked()"), self.onSell) ## Indicators self.connect(self.ui.addIndicator, QtCore.SIGNAL("clicked()"), self.onAddIndicator) ## Chart Styles self.connect(self.ui.chartStyle, QtCore.SIGNAL("currentIndexChanged(int)"), self.onChartStyleChange) ## Chart and Tab Controls self.connect(self.ui.symbolEntry, QtCore.SIGNAL("returnPressed()"), self.onNewTab) self.connect(self.ui.newTab, QtCore.SIGNAL("clicked()"), self.onNewTab) self.connect(self.ui.chartTabs, QtCore.SIGNAL("currentChanged(int)"), self.onChangeTab) self.connect(self.ui.chartTabs, QtCore.SIGNAL("tabCloseRequested(int)"), self.onCloseTab) ## Maximize screen #self.setWindowState(QtCore.Qt.WindowMaximized) ## Defaults #self.ui.chartTabs.setTabsClosable(True) self.update() ############################################################################# ## Event Handlers ############################################################################# def update(self): self.updateAccounts() self.updateTime() self.chartView.drawChart() self.updateIndicators() for ii in self.indicators: apply(ii[0], ii[1:]) ############################################################################# ## Mulit-Chart View and Symbol Loading ############################################################################# def onNewTab(self): t = str(self.ui.symbolEntry.text()) self.ui.symbolEntry.clear() c = ChartView(t) chartViews.append(c) self.ui.chartTabs.addTab(c, t) def onCloseTab(self, num): chartViews.pop(num - 1) self.ui.chartTabs.removeTab(num) def onChangeTab(self, x): self.chartView = chartViews[self.ui.chartTabs.currentIndex() - 1] self.chartView.drawChart() self.update() ############################################################################# ## Chart Controls ## Zoom In/Out, Chart Style (ohlc, candlestick, etc), Normal/Log Scale, etc ############################################################################# def onZoomChart(self, i): self.chartView.chartLength = i self.chartView.drawChart() self.update() def onChartStyleChange(self, i): choices = [ self.chartView.drawCandlesticks, self.chartView.drawOHLC, self.chartView.drawHLC, self.chartView.drawBar, self.chartView.drawDot, self.chartView.drawClose ] self.chartView.chartStyle = choices[i] self.chartView.drawChart() self.update() ############################################################################# ## Current Day Display ## Date, Open, High, Low, Close ############################################################################# def updateCurrentDayDisplay(self): todayData = self.chartView.data.currentDay(time.currentDay) self.ui.currentDayDate.setText(todayData[0]) self.ui.currentDayOpen.setText(str(todayData[1])) self.ui.currentDayHigh.setText(str(todayData[2])) self.ui.currentDayLow.setText(str(todayData[3])) self.ui.currentDayClose.setText(str(todayData[4])) self.ui.currentDayVolume.setText(str(todayData[5])) ############################################################################# ## Time Controls ## Next/Prev Day ############################################################################# def updateTime(self): self.updateCurrentDayDisplay() self.ui.daysLeft.display(time.currentDay) def onNextDay(self): if time.currentDay > 0: time.currentDay -= 1 self.chartView.drawChart() self.update() def onPrevDay(self): time.currentDay += 1 self.chartView.drawChart() self.update() def onNext30(self): for ii in range(30): if time.currentDay > 0: time.currentDay -= 1 self.update() self.chartView.drawChart() self.update() def onPrev30(self): for ii in range(30): time.currentDay += 1 self.update() self.chartView.drawChart() self.update() ############################################################################# ## Account Controls ## Buy/Sell, Stop/Limit, % Gain/Loss of last trade, % Gain/Loss Total, etc. ############################################################################# def updateAccounts(self): account.update() self.ui.showBalance.setText(str(account.balance)) self.ui.showPortfolio.setText(str(account.portfolio)) self.ui.showPortfolioValue.setText(str(account.portfolioValue())) p = account.portfolioProfit() pp = account.portfolioPercentage() c = "green" if p >= 0 else "red" self.ui.showProfit.setText(str(p)) self.ui.showProfitPercent.setText(str(pp) + "%") self.ui.showProfit.setStyleSheet("QLabel { color : " + c + ";}") self.ui.showProfitPercent.setStyleSheet("QLabel { color : " + c + ";}") def onBuy(self): account.buy(self.chartView.symbol, self.ui.buyShares.text()) self.ui.buyShares.clear() # TODO: Add code in to view pending orders, this should be cleared #self.ui.buyStop.clear() self.update() def onSell(self): if self.ui.stopLoss.text(): account.setStop(self.chartView.symbol, float(self.ui.stopLoss.text()), self.ui.sellShares.text()) else: account.sell(self.chartView.symbol, self.ui.sellShares.text()) self.ui.sellShares.clear() # TODO: Add code in to view pending orders, this should be cleared #self.ui.sellStop.clear() self.update() ############################################################################# ## Technical Indicators ## Moving Averages, Bollinger Bands, RSI, Volume, etc ## TODO: ## Add "your stop loss" indicator that plots the stop loss price ############################################################################# def updateIndicators(self): for ii in self.indicators: apply(ii[0], ii[1:]) def drawSMA(self, line): self.chartView.drawLine( self.chartView.data.sma(line.value(), time.currentDay, self.chartView.chartLength), line.color()) def drawWMA(self, line): self.chartView.drawLine( self.chartView.data.wma(line.value(), time.currentDay, self.chartView.chartLength), line.color()) def drawEMA(self, line): self.chartView.drawLine( self.chartView.data.ema(line.value(), time.currentDay, self.chartView.chartLength), line.color()) def drawBollingerBands(self, top, sma, bottom): d = self.chartView.data.bollingerBands(sma.value(), time.currentDay, self.chartView.chartLength) self.chartView.drawLine(d[0], top.color()) self.chartView.drawLine(d[1], sma.color()) self.chartView.drawLine(d[2], bottom.color()) def drawDonchianChannel(self, top, bottom): d = self.chartView.data.donchianChannel(top.value(), time.currentDay, self.chartView.chartLength) self.chartView.drawLine(d[0], top.color()) self.chartView.drawLine(d[1], bottom.color()) def onAddIndicator(self): i = self.ui.newIndicator.currentIndex() if i == 0: IndicatorWidget(self, "Simple Moving Average", self.drawSMA, [Line(self, "Period:")]) elif i == 1: IndicatorWidget(self, "Weighted Moving Average", self.drawWMA, [Line(self, "Period:")]) elif i == 2: IndicatorWidget(self, "Exponential Moving Average", self.drawEMA, [Line(self, "Period:")]) elif i == 3: IndicatorWidget(self, "Bollinger Bands", self.drawBollingerBands, [ Line(self, "Top:", entryBox=False, defaultColor="green"), Line(self, "SMA:", defaultColor="blue"), Line(self, "Bottom:", entryBox=False, defaultColor="red") ]) elif i == 4: IndicatorWidget( self, "Donchian Channel", self.drawDonchianChannel, [ Line(self, "Top:", defaultColor="green"), Line(self, "Bottom", entryBox=False, defaultColor="red") ]) self.update()