Add pattern recognition to technical analysis

This commit is contained in:
didier
2021-05-02 02:28:41 +01:00
parent e30f724a2f
commit c7daf13529
2 changed files with 214 additions and 0 deletions

View File

@@ -0,0 +1,206 @@
import argparse
from typing import List
import requests
import yfinance as yf
import mplfinance as mpf
import pandas as pd
import math
from gamestonk_terminal import config_terminal as cfg
from gamestonk_terminal.helper_funcs import (
parse_known_args_and_warn,
plot_autoscale,
)
from datetime import datetime
def get_pattern_recognition(ticker: str, resolution: str) -> pd.DataFrame:
"""Get pattern recognition data
Parameters
----------
ticker : str
Ticker to get pattern recognition data
resolution : str
Resolution of data to get pattern recognition from
Returns
-------
pd.DataFrame
Get datapoints corresponding to pattern signal data
"""
response = requests.get(
f"https://finnhub.io/api/v1/scan/pattern?symbol={ticker}&resolution={resolution}&token={cfg.API_FINNHUB_KEY}"
)
if response.status_code == 200:
d_data = response.json()
if "points" in d_data:
return pd.DataFrame(d_data["points"]).T
return pd.DataFrame()
def plot_pattern_recognition(ticker: str, pattern: pd.DataFrame, segments: List):
"""Plot pattern recognition signal
Parameters
----------
ticker : str
Ticker to display pattern recognition on top of the data
pattern : pd.DataFrame
Pattern recognition signal data
segments : List
Segment lines that indicates some pattern signal in data
"""
start_time = 999999999999
for i in pattern:
if pattern[i]["atime"] < start_time:
start_time = pattern[i]["atime"]
df_stock = yf.download(
ticker,
start=datetime.utcfromtimestamp(start_time).strftime("%Y-%m-%d"),
progress=False,
)
df_stock["date_id"] = (df_stock.index.date - df_stock.index.date.min()).astype(
"timedelta64[D]"
)
df_stock["date_id"] = df_stock["date_id"].dt.days + 1
df_stock["OC_High"] = df_stock[["Open", "Close"]].max(axis=1)
df_stock["OC_Low"] = df_stock[["Open", "Close"]].min(axis=1)
mc = mpf.make_marketcolors(
up="green", down="red", edge="black", wick="black", volume="in", ohlc="i"
)
s = mpf.make_mpf_style(marketcolors=mc, gridstyle=":", y_on_right=False)
mpf.plot(
df_stock,
type="candle",
volume=False,
title=f"\n{ticker}",
alines=segments,
xrotation=10,
style=s,
figratio=(10, 7),
figscale=1.10,
figsize=plot_autoscale(),
update_width_config=dict(
candle_linewidth=1.0, candle_width=0.8, volume_linewidth=1.0
),
)
def pattern_recognition_view(other_args: List[str], ticker: str):
"""Display pattern recognition signals on the data
Parameters
----------
other_args : List[str]
Command line arguments to be processed with argparse
ticker : str
Ticker to display pattern recognition on top of the data
"""
parser = argparse.ArgumentParser(
add_help=False,
prog="pr",
description="""
Display pattern recognition signals on the data. [Source: https://finnhub.io]
""",
)
parser.add_argument(
"-r",
"--resolution",
action="store",
dest="resolution",
type=str,
default="D",
choices=["1", "5", "15", "30", "60", "D", "W", "M"],
help="Plot resolution to look for pattern signals",
)
try:
ns_parser = parse_known_args_and_warn(parser, other_args)
if not ns_parser:
return
df_pattern = get_pattern_recognition(ticker, ns_parser.resolution)
if df_pattern.empty:
print("No pattern identified in this data", "\n")
return
l_segments = list()
for i in df_pattern:
a_part = ""
if "aprice" in df_pattern[i]:
if df_pattern[i]["aprice"] != 0 and not math.isnan(
df_pattern[i]["aprice"]
):
a_part = (
datetime.utcfromtimestamp(df_pattern[i]["atime"]).strftime(
"%Y-%m-%d"
),
df_pattern[i]["aprice"],
)
b_part = ""
if "bprice" in df_pattern[i]:
if df_pattern[i]["bprice"] != 0 and not math.isnan(
df_pattern[i]["bprice"]
):
b_part = (
datetime.utcfromtimestamp(df_pattern[i]["btime"]).strftime(
"%Y-%m-%d"
),
df_pattern[i]["bprice"],
)
c_part = ""
if "cprice" in df_pattern[i]:
if df_pattern[i]["cprice"] != 0 and not math.isnan(
df_pattern[i]["cprice"]
):
c_part = (
datetime.utcfromtimestamp(df_pattern[i]["ctime"]).strftime(
"%Y-%m-%d"
),
df_pattern[i]["cprice"],
)
d_part = ""
if "dprice" in df_pattern[i]:
if df_pattern[i]["dprice"] != 0 and not math.isnan(
df_pattern[i]["dprice"]
):
d_part = (
datetime.utcfromtimestamp(df_pattern[i]["dtime"]).strftime(
"%Y-%m-%d"
),
df_pattern[i]["dprice"],
)
segment = (a_part, b_part, c_part, d_part)
l_segment = list(segment)
while "" in l_segment:
l_segment.remove("")
segment = tuple(l_segment)
l_segments.append(segment)
plot_pattern_recognition(ticker, df_pattern, l_segments)
for ix in range(len(df_pattern.columns)):
print(f"From {l_segments[ix][0][0]} to {l_segments[ix][-1][0]}")
print(
f"Pattern: {df_pattern[0]['patternname']} ({df_pattern[0]['patterntype']})"
)
print("")
except Exception as e:
print(e, "\n")

View File

@@ -20,6 +20,7 @@ from gamestonk_terminal.technical_analysis import volume as ta_volume
from gamestonk_terminal.technical_analysis import finbrain_view
from gamestonk_terminal.technical_analysis import tradingview_view
from gamestonk_terminal.technical_analysis import finviz_view
from gamestonk_terminal.technical_analysis import finhub_view
class TechnicalAnalysisController:
@@ -33,6 +34,7 @@ class TechnicalAnalysisController:
"view",
"summary",
"recom",
"pr",
"ema",
"sma",
"vwap",
@@ -88,6 +90,8 @@ class TechnicalAnalysisController:
print(
" recom recommendation based on Technical Indicators [Tradingview API]"
)
print(" pr pattern recognition [Finhub]")
print("")
print("overlap:")
print(" ema exponential moving average")
print(" sma simple moving average")
@@ -157,6 +161,10 @@ class TechnicalAnalysisController:
"""Process recom command"""
tradingview_view.print_recommendation(other_args, self.ticker)
def call_pr(self, other_args: List[str]):
"""Process pr command"""
finhub_view.pattern_recognition_view(other_args, self.ticker)
# OVERLAP
def call_ema(self, other_args: List[str]):
"""Process ema command"""