mirror of
https://github.com/yongghongg/stock-screener.git
synced 2022-03-09 00:26:31 +03:00
Created using Colaboratory
This commit is contained in:
128
simple_technical_analysis_stock_screener_demo.ipynb
Normal file
128
simple_technical_analysis_stock_screener_demo.ipynb
Normal file
@@ -0,0 +1,128 @@
|
||||
{
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0,
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"name": "simple-technical-analysis-stock-screener-demo.ipynb",
|
||||
"provenance": [],
|
||||
"authorship_tag": "ABX9TyOv5Rg0eovcgPOOu//hyU/J",
|
||||
"include_colab_link": true
|
||||
},
|
||||
"kernelspec": {
|
||||
"name": "python3",
|
||||
"display_name": "Python 3"
|
||||
},
|
||||
"language_info": {
|
||||
"name": "python"
|
||||
}
|
||||
},
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "view-in-github",
|
||||
"colab_type": "text"
|
||||
},
|
||||
"source": [
|
||||
"<a href=\"https://colab.research.google.com/github/yongghongg/stock-screener/blob/main/simple_technical_analysis_stock_screener_demo.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "vnhzHjCEeKmK"
|
||||
},
|
||||
"source": [
|
||||
"A demo Colab Notebook for my article: \n",
|
||||
"https://levelup.gitconnected.com/automate-your-stock-screening-using-python-9107dda724c3\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"metadata": {
|
||||
"id": "t_3iZkcseOYp"
|
||||
},
|
||||
"source": [
|
||||
"# install required libraries (on colab)\n",
|
||||
"!pip install bs4\n",
|
||||
"!pip install requests\n",
|
||||
"# import required libraries \n",
|
||||
"from bs4 import BeautifulSoup\n",
|
||||
"import ast\n",
|
||||
"import pandas as pd\n",
|
||||
"import re\n",
|
||||
"import requests\n",
|
||||
"from datetime import datetime"
|
||||
],
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"metadata": {
|
||||
"id": "FUUL-MhDd_xd"
|
||||
},
|
||||
"source": [
|
||||
"def get_stock_price(ticker):\n",
|
||||
" # pass a ticker name to i3investor website url\n",
|
||||
" url = \"https://klse.i3investor.com/servlets/stk/chart/{}.jsp\". format(ticker)\n",
|
||||
" # get response from the site and extract the price data\n",
|
||||
" response = requests.get(url, headers={'User-Agent':'test'})\n",
|
||||
" soup = BeautifulSoup(response.content, \"html.parser\")\n",
|
||||
" script = soup.find_all('script')\n",
|
||||
" data_tag = script[19].contents[0] #changed to 19 from 20\n",
|
||||
" chart_data = ast.literal_eval(re.findall('\\[(.*)\\]', data_tag.split(';')[0])[0])\n",
|
||||
" # tabulate the price data into a dataframe\n",
|
||||
" chart_df = pd.DataFrame(chart_data, columns = ['Date', 'Open', 'High', 'Low', 'Close', 'Volume'])\n",
|
||||
" # convert timestamp into readable date\n",
|
||||
" chart_df['Date'] = chart_df['Date'].apply(lambda x: \\\n",
|
||||
" datetime.utcfromtimestamp(int(x)/1000).strftime('%Y-%m-%d'))\n",
|
||||
" return chart_df\n",
|
||||
"\n",
|
||||
"def add_EMA(price, day):\n",
|
||||
" return price.ewm(span=day).mean()\n",
|
||||
"\n",
|
||||
"def get_stock_list():\n",
|
||||
" # this is the website we're going to scrape from\n",
|
||||
" url = \"https://www.malaysiastock.biz/Stock-Screener.aspx\"\n",
|
||||
" response = requests.get(url, headers={'User-Agent':'test'})\n",
|
||||
" soup = BeautifulSoup(response.content, \"html.parser\")\n",
|
||||
" table = soup.find(id = \"MainContent2_tbAllStock\")\n",
|
||||
" # return the result in a list\n",
|
||||
" return [stock.getText() for stock in table.find_all('a')]\n",
|
||||
"\n",
|
||||
"# function to check for EMA crossing\n",
|
||||
"def check_EMA_crossing(df):\n",
|
||||
" # condition 1: EMA18 is higher than EMA50 at the last trading day\n",
|
||||
" cond_1 = df.iloc[-1]['EMA18'] > df.iloc[-1]['EMA50']\n",
|
||||
" # condition 2: EMA18 is lower than EMA50 the previous day\n",
|
||||
" cond_2 = df.iloc[-2]['EMA18'] < df.iloc[-2]['EMA50']\n",
|
||||
" # condition 3: to filter out stocks with less than 50 candles\n",
|
||||
" cond_3 = len(df.index) > 50 \n",
|
||||
" # will return True if all 3 conditions are met\n",
|
||||
" return (cond_1 and cond_2 and cond_3)\n",
|
||||
"\n",
|
||||
"# main program\n",
|
||||
"\n",
|
||||
"# a list to store the screened results\n",
|
||||
"screened_list = [] \n",
|
||||
"# get the full stock list\n",
|
||||
"stock_list = get_stock_list()\n",
|
||||
"for each_stock in stock_list:\n",
|
||||
" print(each_stock)\n",
|
||||
" # Step 1: get stock price for each stock\n",
|
||||
" price_chart_df = get_stock_price(each_stock)\n",
|
||||
" # Step 2: add technical indicators (in this case EMA)\n",
|
||||
" price_chart_df['EMA18']=add_EMA(price_chart_df['Close'],18)\n",
|
||||
" price_chart_df['EMA50']=add_EMA(price_chart_df['Close'],50)\n",
|
||||
" price_chart_df['EMA100']=add_EMA(price_chart_df['Close'],100)\n",
|
||||
" # if all 3 conditions are met, add stock into screened list\n",
|
||||
" if check_EMA_crossing(price_chart_df):\n",
|
||||
" screened_list.append(each_stock)\n",
|
||||
"print(screened_list)"
|
||||
],
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
}
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user