Python code for markets: stocks, bitcoin and buy-or-sell sentiment (with disclaimer)

Python code for markets: stocks, bitcoin and buy-or-sell sentiment (with disclaimer)
This is the index of the code section. Every program is a Python file commented line by line, with sample data and with the output you should get when you run it: download it, unzip it, run it. There is nothing to install and no account to create, because all six use the Python standard library only.
They are written for people who run a small business and want to understand the numbers, not for people building a system: each file reads end to end in one sitting and can be changed without fear. All six were tested before publishing, and every article explains the code with the real fragments that ship inside the ZIP.
Disclaimer: this section is educational material. It is not a recommendation to buy or to sell. The sample prices are invented and the real historical data (bitcoin from CoinGecko and the S&P 500 from the Federal Reserve of St. Louis) is quoted only for practice. A past result guarantees nothing about the future: before investing, consult a licensed professional.
Earlier batches: six more programs for inventory, accounting and databases and the first six.
| batch | what it teaches | program |
|---|---|---|
| M1 | reading a price series and describing it: returns, volatility and a chart drawn in the console | Python code: read a price series and describe it (free download) |
| M2 | the 20- and 50-day moving average, the crossover, and the comparison against buy and hold | Python code: 20 and 50 moving averages and the crossover (free download) |
| M3 | the 14-day RSI: overbought above 70 and oversold below 30 | Python code: 14-period RSI from scratch (free download) |
| M4 | Bollinger bands: the 20-day average plus or minus two standard deviations | Python code: Bollinger bands with mean and standard deviation (free download) |
| M5 | measuring the tone of headlines with two hand-written word lists | Python code: measure headline sentiment with your own lexicon |
| M6 | combining the technical signal with sentiment and seeing, in numbers, whether it would win | Python code: combined moving-average and headline-sentiment signal |
Before you start: what this is and what it is not
The six programs are meant to understand how prices are read and how a rule is measured. None of them guesses the future and none of them says what to buy. In five of them the data is an invented series, built with a fixed seed so the result repeats identically on any computer; the first one also carries a file with real historical data, quoted with its source, to practise with prices that actually existed.
The order that works best
- M1 — the price series: what a daily return is and why volatility is mentioned.
- M2 — the moving average and its crossover, with the honest comparison against buy and hold.
- M3 — the RSI: the overbought and oversold thermometer.
- M4 — Bollinger bands: how far the price usually moves.
- M5 — headline sentiment, with its limit clearly marked.
- M6 — putting both ideas together and seeing the result in numbers.
If your interest is business and not the stock market, read every program as a calculation exercise: the moving average smooths your shop sales just as well, and headline sentiment is the same idea as reading your customers' comments.
What you need to run them
| You need | Detail |
|---|---|
| Python 3.11 or newer | Install it once from the official site and it is available for every program |
| A text editor | The one that ships with your system is enough; if you are going to change the data, one with code highlighting helps |
| Your own data | The CSV file inside the datos folder is enough: open it
in Excel, replace it with your movements and save it the same way |
| Five minutes | None of the six takes longer than that to show a result |
| Extra libraries | None: all six use only what ships with Python |
How to start today
Install Python from the official site, pick the program that solves this week's problem and run
it with your own data: in every case it is enough to replace the CSV file that ships in the
datos folder. If the result is useful, keep it; if not, the file stays where it is and
breaks nothing.
When this is not enough
A script answers one concrete question and answers it well. When the business has several warehouses, several users, invoicing and accounting at the same time, the natural next step is Kardex Tauro, which does that work with screens and with the data stored in a database. Until then, these programs help you understand what is being calculated and keep you away from a spreadsheet that only its author can read.
Questions that always come up
Do I need to know how to program? Not to run them: unzip and run one command. To change a formula or add a column you do need to read the program, and that is what the comment on every line is for.
Can I use them in a real business? Yes, and they work: they are tested before publishing. Keep in mind they are didactic internal-control material: check them against your own figures and the rules that apply to you before making decisions with them.
Why are there no screens like an inventory program? Because a screen costs much more than a file, and the point of this section is that you understand the numbers. When the business grows, the next step is a program with a database and several users, not a bigger script.
What happens to my data? Nothing leaves your computer: no servers, no accounts, no internet involved. The program reads and writes files in your own folder.
Disclaimer: this section is educational material. It is not a recommendation to buy or to sell. The sample prices are invented and the real historical data (bitcoin from CoinGecko and the S&P 500 from the Federal Reserve of St. Louis) is quoted only for practice. A past result guarantees nothing about the future: before investing, consult a licensed professional.
All six programs are didactic internal-control material: check them against your own figures before using them in a real business.