Skip to content

Commit 8b1fc84

Browse files
committed
Add EMA & SMA
1 parent e232a46 commit 8b1fc84

File tree

4 files changed

+104
-0
lines changed

4 files changed

+104
-0
lines changed

finplotWindow.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
from indicators import rsi
77
from indicators import stochastic
88
from indicators import stochasticRsi
9+
from indicators import sma
10+
from indicators import ema
911

1012
sys.path.append('../finplot')
1113
import finplot as fplt
@@ -66,6 +68,16 @@ def drawCandles(self):
6668
#self.createControlPanel(self.ax0.ax_widget)
6769
pass
6870

71+
def drawSma(self, period):
72+
self.sma_indicator = sma.Sma(self.data, period)
73+
self.sma_indicator.draw(self.ax0)
74+
pass
75+
76+
def drawEma(self, period):
77+
self.ema_indicator = ema.Ema(self.data, period)
78+
self.ema_indicator.draw(self.ax0)
79+
pass
80+
6981
def drawRsi(self, period):
7082
self.rsi_indicator = rsi.Rsi(self.data, period)
7183
self.rsi_indicator.draw(self.ax_rsi)

indicators/ema.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env python3
2+
import sys
3+
4+
sys.path.append('../finplot')
5+
import finplot as fplt
6+
7+
from common import calc_rsi
8+
9+
class Ema():
10+
11+
def __init__(self, dataFrames, ema_periods=9):
12+
self.ema_df = dataFrames["Close"].ewm(span=ema_periods, adjust=False).mean()
13+
pass
14+
15+
def draw(self, ax, ema_color = "yellow"):
16+
self.ema_plot = fplt.plot(self.ema_df, ax = ax, color=ema_color, width=1 )
17+
pass
18+
19+
def clear(self):
20+
pass

indicators/sma.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env python3
2+
import sys
3+
4+
sys.path.append('../finplot')
5+
import finplot as fplt
6+
7+
from common import calc_rsi
8+
9+
class Sma():
10+
11+
def __init__(self, dataFrames, sma_periods=14):
12+
self.sma_df = dataFrames["Close"].rolling(window=sma_periods).mean()
13+
pass
14+
15+
def draw(self, ax, sma_color = "green"):
16+
self.sma_plot = fplt.plot(self.sma_df, ax = ax, color=sma_color, width=1 )
17+
pass
18+
19+
def clear(self):
20+
pass

userInterface.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,26 @@ def createControlPanel(self):
497497
spacer = QtWidgets.QSpacerItem(50,20,QtWidgets.QSizePolicy.Minimum)
498498
layout.addSpacerItem(spacer)
499499

500+
# SMA
501+
self.SmaPB = QtWidgets.QPushButton(self.panel)
502+
self.SmaPB.setText("SMA")
503+
self.SmaPB.setCheckable(True)
504+
self.SmaPB.setMaximumWidth(100)
505+
self.SmaPB.toggled.connect(self.addSma)
506+
layout.addWidget(self.SmaPB)
507+
508+
# EMA
509+
self.EmaPB = QtWidgets.QPushButton(self.panel)
510+
self.EmaPB.setText("EMA")
511+
self.EmaPB.setCheckable(True)
512+
self.EmaPB.setMaximumWidth(100)
513+
self.EmaPB.toggled.connect(self.addEma)
514+
layout.addWidget(self.EmaPB)
515+
516+
# Spacer
517+
spacer = QtWidgets.QSpacerItem(50,20,QtWidgets.QSizePolicy.Minimum)
518+
layout.addSpacerItem(spacer)
519+
500520
# RSI
501521
self.RsiPB = QtWidgets.QPushButton(self.panel)
502522
self.RsiPB.setText("RSI")
@@ -568,6 +588,38 @@ def resetChart(self):
568588
self.fpltWindow.updateChart()
569589
pass
570590

591+
# On chart indicators
592+
def addSma(self):
593+
594+
# Show indicator parameter dialog
595+
paramDialog = indicatorParametersUI.IndicatorParametersUI(self.dock_chart)
596+
paramDialog.setWindowFlags(QtCore.Qt.CustomizeWindowHint)
597+
paramDialog.setTitle("SMA Indicator parameters")
598+
paramDialog.addParameter("SMA Period", 14)
599+
paramDialog.adjustSize()
600+
601+
if (paramDialog.exec() == QtWidgets.QDialog.Accepted ):
602+
period = paramDialog.getValue("SMA Period")
603+
self.fpltWindow.drawSma( period )
604+
605+
pass
606+
607+
# On chart indicators
608+
def addEma(self):
609+
610+
# Show indicator parameter dialog
611+
paramDialog = indicatorParametersUI.IndicatorParametersUI(self.dock_chart)
612+
paramDialog.setWindowFlags(QtCore.Qt.CustomizeWindowHint)
613+
paramDialog.setTitle("EMA Indicator parameters")
614+
paramDialog.addParameter("EMA Period", 9)
615+
paramDialog.adjustSize()
616+
617+
if (paramDialog.exec() == QtWidgets.QDialog.Accepted ):
618+
period = paramDialog.getValue("EMA Period")
619+
self.fpltWindow.drawEma( period )
620+
621+
pass
622+
571623
# indicators in external windows
572624
def toogleRsi(self):
573625

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy