🌐
alpharithms
alpharithms.com › home › tutorials › using the stochastic oscillator in python for algorithmic trading
Using the Stochastic Oscillator in Python for Algorithmic Trading - αlphαrithms
April 10, 2023 - In this article, we’ll cover the basics of the stochastic oscillator, how it’s calculated, how to interpret the results, and how to create a Stochastic Oscillator chart in Python using Plotly.
🌐
GitHub
github.com › Abhay64 › Stochastic-Oscillator
GitHub - Abhay64/Stochastic-Oscillator: The Stochastic Oscillator has two lines, the %K and %D. The %D line is more important to produce better trading signals.
The Stochastic Oscillator has two lines, the %K and %D. The %D line is more important to produce better trading signals. Python 3.6 · Libraries (pandas, numpy, matplotlib) The stochastic oscillator is one of the most recognized momentum indicators in technical analysis.
Starred by 10 users
Forked by 6 users
Languages   Python 100.0% | Python 100.0%
🌐
AskPython
askpython.com › home › stochastic indicator: python implementation
Stochastic Indicator: Python Implementation - AskPython
April 10, 2025 - In the next segment, we will discuss how this concept can be implemented in Python. In the code below, we have coded the stochastic indicator. Essentially, we randomly generate 500 price data points. Thereafter we plot the stochastic oscillator on the price movement to generate different types of signals for trading.
🌐
Steemit
steemit.com › utopian-io › @imwatsi › technical-analysis-using-python-stochastic-oscillator-basic
Technical Analysis Using Python: Stochastic Oscillator (Basic) — Steemit
May 6, 2019 - https://github.com/imwatsi/crypto-market-samples Find the complete Python script on GitHub: ta_stoch.py · You can find code used in this tutorial as well as other tools and examples in the GitHub repositories under my profile: ... How to use the Stochastic Oscillator class from the open source technical analysis library from Bitfinex
🌐
Medium
medium.com › codex › algorithmic-trading-with-stochastic-oscillator-in-python-7e2bec49b60d
Algorithmic Trading with Stochastic Oscillator in Python | by Nikhil Adithyan | CodeX | Medium
September 19, 2023 - There are a bunch of technical indicators that can be considered for research and analysis but the one we are going to discuss today is one of the most popular indicators used among traders for trading purposes. It’s none other than the Stochastic Oscillator technical indicator. In this article, we will use python to create a Stochastic Oscillator-based trading strategy and backtest the strategy to see how well it performs in the real-world market.
🌐
EODHD
eodhd.com › home › using python to create an innovative trading strategy and achieve better results
Python Trading Strategy: Synergizing Stochastic Oscillator and MACD Indicator | EODHD APIs Academy
February 5, 2025 - In this article, we will utilize Python to create an innovative trading strategy by synergizing two potent indicators: the Stochastic Oscillator and the Moving Average Convergence/Divergence (MACD) indicator. This strategy will explore how to use the Stochastic Oscillator for intraday trading, focusing on the raw stochastic value, the stochastic K and D line, and its role as a market edge oscillator.
Find elsewhere
🌐
Raposa
raposa.trade › blog › beginners-guide-to-trading-with-the-stochastic-oscillator-indicator
Beginner’s Guide to Trading with the Stochastic Oscillator Indicator — Raposa
July 27, 2021 - To do this, we’ll import our standard packages into Python. import pandas as pd import numpy as np import matplotlib.pyplot as plt import yfinance as yf · The next step is to build a function to calculate the stochastic oscillator and test it on some data.
🌐
Medium
rbdundas.medium.com › calculate-stochastic-oscillator-in-python-and-pandas-and-chart-with-matplotlib-aafde26b4a1f
Calculate Stochastic Oscillator in Python and Pandas and Chart with Matplotlib | by Rob Dundas | Medium
May 7, 2022 - Once through the 14-day period, it will set that row’s “best_high” and “best_low” values to be the current low and high. Finally, for that row, the calculation to get the Stochastic Oscillator (“fast_k”) is completed (100*(close-low)/(high-low)).
Top answer
1 of 1
1

A few comments:

  • Do not use global variables. Include the variables that are needed as input in the arguments of the function (between the two brackets on the def line). For the output, use the return keyword. That would make it a lot easier for other people to read and understand your code.

  • highest = int(max(data)): That line is highly suspicious. You're converting the result to int because data is a list of strings rather than a list of integers. But if it's a list of strings, how is max going to be able to identify the maximum value correctly? You need to convert to int before calling max, not after. String comparison is lexicographical: for instance, with strings '934' > '5169', whereas with ints 934 < 5169.

  • It appears your function manipulates several lists: data, k_arr, stoch. The formula that you're trying to implement is only expecting one list. The code you've shown us appears to do a lot more than just calculate the value for that formula.

  • Regarding "the result is always 100": you should expect the result to be 100 when the most recent closing price is the highest price of the 14 previous sessions, and to be less than 100 when the most recent closing price is not the highest price.

  • When L14 == H14, you cannot divide by (H14 - L14) because that would be a division by 0. Currently, your code handles that with a try / except block, which chooses the arbitrary value 0 as the indicator in this case. Note that the value 0 is also the value of the indicator when C == L14. I think a distinction should be made and since I see no reason, according to your formula, to return 0 when L14 == H14, your function could return "not a number" when the indicator cannot be calculated because of the division by 0.

With all that in mind, I suggest to rewrite your function so that it accepts a list of int as argument; extracts the last 14 values from that list; calculates L14 and H14 and C; calculates K; and returns K with the return keyword.

def stochastic_indicator(data):
    last_14 = data[-14:]
    l14 = min(last_14)
    h14 = max(last_14)
    c = last_14[-1]
    if h14 > l14:
        return 100 * (c - l14) / (h14 - l14)
    else:
        return float('nan')

# TESTING

str_data = ['5169', '5169', '5168', '5168', '5167', '5166', '5165', '5165', '5165', '5165', '5165', '5165', '5165', '5165', '5164', '5165', '5164', '5163', '5162', '5163', '5162', '5162', '5162', '5163', '5162', '5162', '5160', '5160', '5162', '5162', '5160', '5160', '5160', '5160', '5160', '5160', '5160', '5159', '5159', '5159', '5159', '5158', '5156', '5155', '5154', '5154', '5154', '5153', '5153', '5153', '5153', '5153', '5154', '5154', '5153', '5152', '5152', '5152', '5151', '5150', '5150', '5150', '5149', '5150', '5150', '5150', '5150', '5150', '5151', '5151', '5151', '5151', '5151', '5152', '5152', '5152', '5152', '5152', '5152', '5152', '5153', '5152', '5151', '5151', '5152', '5151', '5150', '5148', '5148', '5148', '5148', '5148', '5147', '5147', '5147', '5147', '5146', '5146', '5145', '5144', '5145', '5145', '5145', '5145', '5144', '5144', '5144', '5144', '5143', '5143', '5143', '5143', '5143', '5143', '5143', '5143', '5142', '5142', '5141', '5141', '5140', '5140', '5140', '5140', '5139', '5139', '5139', '5139', '5140', '5140', '5140', '5139',  '5008', '5007', '5007', '5006', '5004', '5004', '5003', '5002', '5002', '5002', '5002', '5003', '5002', '5002', '5000', '5000', '5000', '5000', '5000', '5000', '4999', '4999', '4999', '4999', '4999', '4999', '4999', '4997', '4997', '4998', '4996', '4996', '4997', '4996', '4996', '4996', '4996', '4997', '4997', '4997', '4996', '4996', '4996', '4995', '4995', '4993', '4993', '4994', '4994', '4994', '4993', '4993', '4992', '4992', '4992', '4992', '4990', '4990', '4988', '4989', '4990', '4991', '4991', '4992', '4993', '4993', '4993', '4993', '4993', '4993', '4993', '4991', '4991', '4991', '4991', '4992', '4991', '4989', '4989', '4989', '4988', '4987', '4986', '4985', '4985', '4985', '4985', '4985', '4985', '4985', '4987', '4987', '4986', '4985', '4985', '4985', '4985', '4985', '4984', '4984', '4984', '4984', '4984', '4983', '4983', '4983', '4983', '4982', '4980', '4980', '4980', '4981', '4979', '4978', '4977', '4976', '4977', '4977', '4976', '4977', '4977', '4976', '4974', '4973', '4971', '4971', '4970', '4970', '4970', '4969', '4969', '4968', '4968', '4967', '4967', '4967', '4967', '4967', '4967', '4967', '4965', '4963', '4964', '4965', '4965', '4965', '4964', '4964', '4962', '4962', '4962', '4961', '4960', '4960', '4959', '4959', '4959', '4960', '4959', '4959', '4958', '4958', '4958', '4958', '4958', '4956', '4955', '4955', '4954', '4954', '4954', '4954']

int_data = list(map(int, str_data))
#        = [5169, 5169, 5168, 5168, 5167, 5166, 5165, 5165, 5165, 5165, 5165, 5165, 5165, 5165, 5164, 5165, 5164, 5163, 5162, 5163, 5162, 5162, 5162, 5163, 5162, 5162, 5160, 5160, 5162, 5162, 5160, 5160, 5160, 5160, 5160, 5160, 5160, 5159, 5159, 5159, 5159, 5158, 5156, 5155, 5154, 5154, 5154, 5153, 5153, 5153, 5153, 5153, 5154, 5154, 5153, 5152, 5152, 5152, 5151, 5150, 5150, 5150, 5149, 5150, 5150, 5150, 5150, 5150, 5151, 5151, 5151, 5151, 5151, 5152, 5152, 5152, 5152, 5152, 5152, 5152, 5153, 5152, 5151, 5151, 5152, 5151, 5150, 5148, 5148, 5148, 5148, 5148, 5147, 5147, 5147, 5147, 5146, 5146, 5145, 5144, 5145, 5145, 5145, 5145, 5144, 5144, 5144, 5144, 5143, 5143, 5143, 5143, 5143, 5143, 5143, 5143, 5142, 5142, 5141, 5141, 5140, 5140, 5140, 5140, 5139, 5139, 5139, 5139, 5140, 5140, 5140, 5139, 5008, 5007, 5007, 5006, 5004, 5004, 5003, 5002, 5002, 5002, 5002, 5003, 5002, 5002, 5000, 5000, 5000, 5000, 5000, 5000, 4999, 4999, 4999, 4999, 4999, 4999, 4999, 4997, 4997, 4998, 4996, 4996, 4997, 4996, 4996, 4996, 4996, 4997, 4997, 4997, 4996, 4996, 4996, 4995, 4995, 4993, 4993, 4994, 4994, 4994, 4993, 4993, 4992, 4992, 4992, 4992, 4990, 4990, 4988, 4989, 4990, 4991, 4991, 4992, 4993, 4993, 4993, 4993, 4993, 4993, 4993, 4991, 4991, 4991, 4991, 4992, 4991, 4989, 4989, 4989, 4988, 4987, 4986, 4985, 4985, 4985, 4985, 4985, 4985, 4985, 4987, 4987, 4986, 4985, 4985, 4985, 4985, 4985, 4984, 4984, 4984, 4984, 4984, 4983, 4983, 4983, 4983, 4982, 4980, 4980, 4980, 4981, 4979, 4978, 4977, 4976, 4977, 4977, 4976, 4977, 4977, 4976, 4974, 4973, 4971, 4971, 4970, 4970, 4970, 4969, 4969, 4968, 4968, 4967, 4967, 4967, 4967, 4967, 4967, 4967, 4965, 4963, 4964, 4965, 4965, 4965, 4964, 4964, 4962, 4962, 4962, 4961, 4960, 4960, 4959, 4959, 4959, 4960, 4959, 4959, 4958, 4958, 4958, 4958, 4958, 4956, 4955, 4955, 4954, 4954, 4954, 4954]

for t in range(25, 304, 25):
    print('# Stochastic indicator at time t={:3d}: {:6.2f}'.format(t, stochastic_indicator(int_data[:t])))
# Stochastic indicator at time t= 25:   0.00
# Stochastic indicator at time t= 50:   0.00
# Stochastic indicator at time t= 75: 100.00
# Stochastic indicator at time t=100:   0.00
# Stochastic indicator at time t=125:   0.00
# Stochastic indicator at time t=150:   0.00
# Stochastic indicator at time t=175:   0.00
# Stochastic indicator at time t=200: 100.00
# Stochastic indicator at time t=225:  25.00
# Stochastic indicator at time t=250:  14.29
# Stochastic indicator at time t=275:  16.67
# Stochastic indicator at time t=300:   0.00
🌐
Insider Finance
wire.insiderfinance.io › how-to-calculate-stochastic-oscillator-in-python-39caaa7dac0b
How to Calculate Stochastic Oscillator in Python | by Huzaifa Zahoor | InsiderFinance Wire
September 5, 2024 - def stochastic_oscillator(data, periodK=14, smoothK=1, periodD=3): # Find lowest low and highest high data['Lowest Low'] = data['Low'].rolling(window=periodK).min() data['Highest High'] = data['High'].rolling(window=periodK).max() # Calculate ...
🌐
Medium
medium.datadriveninvestor.com › the-bollinger-stochastic-indicator-in-python-2bd7c7c2898e
The Bollinger Stochastic Indicator in Python. | by Sofien Kaabar, CFA | DataDrivenInvestor
August 9, 2021 - The Bollinger Stochastic Indicator in Python. Creating & Charting the Bollinger Stochastic Indicator Using Python. What if we fuse together two known powerful technical indicators? What would that …
🌐
GitHub
github.com › topics › stochastic-oscillator
stochastic-oscillator · GitHub Topics · GitHub
technical-analysis cci technical-indicators rsi stock-analysis stock-trading exponential-moving-average moving-average macd bollinger-bands stochastic-oscillator ... A comprehensive Python library for calculating popular technical indicators used in financial markets.
🌐
EODHD
eodhd.com › home › combining bollinger bands & stochastic oscillator into a killer python’s trading strategy
Powerful Python Trading Strategy: Bollinger Bands Stochastic Oscillator | EODHD APIs Academy
March 12, 2024 - Enhance your trading strategy with Bollinger Bands and Stochastic Oscillator indicators in Python. Learn how to filter false trading signals and backtest your strategy
🌐
InsightBig
insightbig.com › post › combining-bollinger-bands-and-stochastic-oscillator-to-create-a-killer-trading-strategy-in-python
Combining Bollinger Bands and Stochastic Oscillator to create a Killer Trading Strategy in Python
July 12, 2021 - IF PREV_ST_COM > 30 AND CUR_ST_COMP < 30 AND CL < LOWER_BB ==> BUY IF PREV_ST_COM > 70 AND CUR_ST_COMP < 70 AND CL < UPPER_BB ==> SELL where, PRE_ST_COM = Previous Day Stochastic Oscillator components' readings CUR_ST_COM = Current Day Stochastic Oscillator components' readings CL = Last Closing Price LOWER_BB = Current Day Lower Band reading UPPER_BB = Current Day Upper Band reading · That’s it! This concludes our theory part and let’s move on to the programming part where we will use Python to first build the indicators from scratch, construct the discussed trading strategy, backtest the strategy on Apple stock data, and finally compare the results with that of SPY ETF.