Using Python to Create an Innovative Trading Strategy and Achieve Better Results

·

In today's fast-paced financial markets, standing out requires more than just following conventional wisdom. Traders are increasingly turning to data-driven approaches and algorithmic models to gain a competitive edge. One of the most powerful tools in this evolution is Python, a versatile programming language that enables the creation and testing of sophisticated trading strategies.

This article explores how to combine two well-known technical indicators — the Stochastic Oscillator and the Moving Average Convergence/Divergence (MACD) — into a cohesive, rule-based trading strategy. We'll walk through the logic, implement it step-by-step in Python, backtest performance using real historical data, and compare results against a market benchmark.

By the end, you'll understand how to build an innovative trading system that minimizes false signals and enhances profitability — all through code you can replicate and adapt.


Understanding the Stochastic Oscillator

The Stochastic Oscillator is a momentum-based leading indicator used to identify overbought and oversold conditions in financial markets. As prices move, this oscillator helps traders anticipate potential turning points by comparing a security’s closing price to its price range over a specific period.

Values of the Stochastic Oscillator range from 0 to 100. Traditionally:

While widely used, relying solely on these thresholds can generate false signals. That’s why we’re enhancing its application by pairing it with another robust indicator: MACD.

The Stochastic Oscillator consists of two primary lines:

%K Line (Fast Stochastic)

This line reflects the current momentum. It's calculated as:

%K = 100 * ((Current Close - Lowest Low) / (Highest High - Lowest Low))

Using a standard 14-period lookback, this value shows where the current close stands relative to recent price action.

%D Line (Slow Stochastic)

The %D line is a 3-period moving average of the %K line, providing a smoothed signal that reduces noise and improves reliability.

👉 Discover how algorithmic strategies can refine momentum signals like the Stochastic Oscillator.

Together, these components act as a market edge oscillator, helping traders detect shifts in momentum before they fully manifest in price trends — especially valuable for intraday trading applications.


Demystifying the MACD Indicator

The Moving Average Convergence/Divergence (MACD) is a trend-following momentum indicator that reveals relationships between two moving averages of a security’s price. Unlike lagging indicators, MACD provides early signals about trend strength and direction.

It comprises three core elements:

MACD Line

Calculated by subtracting the 26-period Exponential Moving Average (EMA) from the 12-period EMA:

MACD Line = 12-day EMA - 26-day EMA

This line captures short-term momentum shifts.

Signal Line

A 9-day EMA of the MACD line, acting as a trigger for buy and sell signals. When the MACD line crosses above or below the Signal line, it indicates potential entry or exit points.

Histogram

Represents the difference between the MACD and Signal lines. Expanding green bars suggest increasing bullish momentum; shrinking or red bars point to weakening trends.

Used together with the Stochastic Oscillator, MACD adds confirmation power — reducing false entries caused by volatility or temporary pullbacks.


Building the Combined Trading Strategy

Our goal is to create a high-conviction strategy that only triggers when both indicators align, minimizing risk and improving accuracy.

Entry Rules

We generate a buy signal when:

This combination confirms oversold conditions with strong downward momentum exhaustion.

We trigger a sell (short) signal when:

These conditions reflect overbought territory with sustained bullish momentum — ideal for contrarian entries.

This dual-filter approach leverages both oscillators' strengths: Stochastic identifies extreme price levels, while MACD validates momentum context.


Implementing the Strategy in Python

Let’s now translate theory into practice using Python. We’ll follow an eight-step implementation process for clarity and reproducibility.

Step 1: Import Required Libraries

import pandas as pd
import numpy as np
import requests
import matplotlib.pyplot as plt
from math import floor
from termcolor import colored as cl

plt.style.use('fivethirtyeight')
plt.rcParams['figure.figsize'] = (20,10)

These libraries handle data manipulation (pandas, numpy), API requests (requests), visualization (matplotlib), and formatting (termcolor).

Step 2: Fetch Historical Stock Data

We retrieve Apple (AAPL) historical data from 2010 onward using a financial data API. This ensures accurate, split-adjusted OHLC data for reliable backtesting.

def get_historical_data(symbol, start_date):
    api_key = 'YOUR_API_KEY'
    url = f'https://eodhistoricaldata.com/api/technical/{symbol}?from={start_date}&function=splitadjusted&api_token={api_key}'
    raw_data = requests.get(url).json()
    df = pd.DataFrame(raw_data)
    df['date'] = pd.to_datetime(df['date'])
    df.set_index('date', inplace=True)
    return df

aapl = get_historical_data('AAPL', '2010-01-01')

Step 3: Calculate the Stochastic Oscillator

def get_stoch_osc(high, low, close, k_period, d_period):
    lowest_low = low.rolling(k_period).min()
    highest_high = high.rolling(k_period).max()
    k_line = ((close - lowest_low) / (highest_high - lowest_low)) * 100
    d_line = k_line.rolling(d_period).mean()
    return k_line, d_line

aapl['%k'], aapl['%d'] = get_stoch_osc(aapl['high'], aapl['low'], aapl['close'], 14, 3)

Step 4: Compute MACD Values

def get_macd(price, slow, fast, smooth):
    exp1 = price.ewm(span=fast, adjust=False).mean()
    exp2 = price.ewm(span=slow, adjust=False).mean()
    macd_line = exp1 - exp2
    signal_line = macd_line.ewm(span=smooth, adjust=False).mean()
    histogram = macd_line - signal_line
    return macd_line, signal_line, histogram

aapl['macd'], aapl['macd_signal'], aapl['macd_hist'] = get_macd(aapl['close'], 26, 12, 9)
aapl.dropna(inplace=True)

Step 5: Define the Trading Logic

def implement_stoch_macd_strategy(prices, k, d, macd, signal):
    buy_price = []
    sell_price = []
    signal_list = []
    position = 0

    for i in range(len(prices)):
        if k[i] < 30 and d[i] < 30 and macd[i] < -2 and signal[i] < -2:
            if position == 0:
                buy_price.append(prices[i])
                sell_price.append(np.nan)
                signal_list.append(1)
                position = 1
            else:
                buy_price.append(np.nan)
                sell_price.append(np.nan)
                signal_list.append(0)
        elif k[i] > 70 and d[i] > 70 and macd[i] > 2 and signal[i] > 2:
            if position == 1:
                buy_price.append(np.nan)
                sell_price.append(prices[i])
                signal_list.append(-1)
                position = 0
            else:
                buy_price.append(np.nan)
                sell_price.append(np.nan)
                signal_list.append(0)
        else:
            buy_price.append(np.nan)
            sell_price.append(np.nan)
            signal_list.append(0)

    return buy_price, sell_price, signal_list

Step 6: Generate Position Signals

Tracks whether we’re long (holding stock) or flat (out of market), based on generated signals.

Step 7: Backtest Performance

We simulate investing $100,000 into this strategy over ~13.5 years:

investment_value = 100000
number_of_stocks = floor(investment_value / aapl['close'].iloc[0])
portfolio_returns = []

for i in range(len(aapl)):
    if not pd.isna(strategy['stoch_macd_position'].iloc[i]):
        daily_return = (aapl['close'].iloc[i] - aapl['close'].iloc[i-1]) * strategy['stoch_macd_position'].iloc[i]
        portfolio_returns.append(daily_return)

total_return = sum(portfolio_returns) * number_of_stocks
profit_percentage = floor((total_return / investment_value) * 100)

print(cl(f"Profit from STOCH-MACD Strategy: ${total_return:,.2f}", attrs=['bold']))
print(cl(f"Profit Percentage: {profit_percentage}%", attrs=['bold']))

Result:
$313,585 profit
313% return

👉 Learn how advanced trading tools can help validate your own algorithmic models.

Step 8: Compare Against SPY ETF Benchmark

We repeat the process with SPY (S&P 500 ETF) without any strategy — simply buy-and-hold:

spy_data = get_historical_data('SPY', '2010-01-01')
benchmark_return = (spy_data['close'].iloc[-1] - spy_data['close'].iloc[0]) / spy_data['close'].iloc[0]
benchmark_profit = floor(benchmark_return * 100)  # ≈159%

Our strategy outperformed SPY by 154 percentage points — a significant alpha generation.


Frequently Asked Questions (FAQ)

Q: Can this strategy be applied to other stocks or assets?

Yes. While tested on AAPL, the logic works across equities, ETFs, and even cryptocurrencies when adjusted for volatility and liquidity characteristics.

Q: Is Python necessary for algorithmic trading?

While not mandatory, Python offers unmatched flexibility for data analysis, visualization, and automation — making it the top choice among quant traders.

Q: How do I reduce false signals further?

Add filters like volume confirmation, RSI divergence, or machine learning classifiers to increase precision.

Q: What are the risks involved in backtesting?

Backtesting assumes perfect execution and ignores slippage, fees, and emotional bias. Always paper-trade first before live deployment.

Q: Can I use this for intraday trading?

Absolutely. The Stochastic Oscillator is particularly effective for intraday trading, especially on 15-minute or hourly charts.

Q: Do I need expensive tools to get started?

No. Free tools like Yahoo Finance APIs, Google Colab, and open-source libraries make it accessible to start building strategies today.


Final Thoughts

Creating an innovative trading strategy isn't about reinventing the wheel — it's about combining proven tools in new ways. By integrating the Stochastic Oscillator, MACD, and Python programming, we’ve built a system that delivers superior returns compared to passive investing.

More importantly, this framework is adaptable. You can tweak parameters, add risk controls, or integrate additional indicators to suit your trading style.

The future of trading belongs to those who embrace technology. Whether you're analyzing raw stochastic values or exploring deeper market edges, coding gives you control — and clarity — in uncertain markets.

👉 Start applying data science to your trading decisions with powerful analytics platforms.