AIToday
AI Coding AssistantsAI Stocks & MarketsQiita 機械学習Published: Oct 3, 2026, 13:00 JST

Python mean-reversion tutorial uses yfinance, SMA20/SMA50 on AAPL

Python mean-reversion tutorial uses yfinance, SMA20/SMA50 on AAPL

3 Key Points

  1. What happened

    A Qiita tutorial shows how to pull Apple (AAPL) price data with yfinance, generate buy and sell signals from SMA20 and SMA50 crossovers, backtest returns, then add a scikit-learn logistic regression model.

  2. Why it matters

    The walkthrough gives readers a copy-paste pipeline from raw Yahoo Finance data to a backtest and model output, without needing proprietary tools.

  3. What to watch

    The article stops at backtesting and prints a classification report, so whether the logistic regression actually lifts accuracy hinges on hyperparameter tuning or alternative models such as random forests or XGBoost, which it suggests but does not run.

WHO IT HITSRetail traders and Python learners following Japanese-language coding tutorials can replicate this workflow with free libraries, though the piece stops short of live trading.

Not sure about something? Ask the AI

Questions and answers are published on this page.

Summaries like this, in your inbox every morning.

Context & Analysis

The article is a hands-on walkthrough rather than a market call. It starts from a common assumption in mean-reversion investing — that asset prices tend to drift back toward a past average, making short-term counter-trades attractive when buying or selling has been excessive — and then demonstrates that idea end-to-end in code.

The build sequence is incremental. First, yfinance downloads Apple (AAPL) daily prices for a fixed window, and matplotlib charts the close. Next, pandas computes a 20-day and 50-day moving average, and the crossover points become the trade signals. A simple backtest then compounds the strategy's returns into a cumulative curve.

The second half layers machine learning on top of the same signal. A logistic regression from scikit-learn is trained on SMA20, SMA50, and a one-day price-change feature to predict whether the next close will be higher. The article prints a classification report and points readers toward hyperparameter tuning or models like random forests or XGBoost if they want more. It closes by suggesting real-time data from Binance through ccxt and the eventual construction of an automated trading system.

For readers, the practical value hinges on whether the added model actually improves on the plain crossover rule — something the tutorial leaves open, since it does not show a comparative accuracy figure. The genuine test is whether tuning or alternative models deliver a measurable edge before anyone moves from a notebook backtest to real orders.

FAQ
What data source and libraries does the tutorial use?
It pulls Apple (AAPL) price data from Yahoo Finance via yfinance, and uses pandas, matplotlib, scikit-learn, and ccxt across the workflow.
How does the strategy decide when to buy or sell?
It calculates a 20-day moving average (SMA20) and a 50-day moving average (SMA50) and treats their crossover points as entry signals.
Does the tutorial handle live trading?
It shows how to fetch real-time BTC/USDT price data from Binance using the ccxt library, but presents this as an example of data retrieval rather than a full live-trading system.
Qiita 機械学習Read Original Article

AI news that matters for your work, delivered every morning.

Pick your industry and the AI tools you use, and get news related to your work every day.

Free · 30 seconds with Google · unsubscribe anytimeWhat is AIToday? →

Ask AI

Ask AI anything about this article. The AI reads this article, earlier AIToday articles, and Wikipedia, and cites its sources. Q&As are published on this page for other readers too.

Questions and answers are published on this page.

Related Articles

Next articleApple restricts macOS full disk access over AI agent risks