Table of Contents
Flat Files โ Calculating Returns & Identifying Top Movers from OHLCV Data
This tutorial shows you exactly how to go from a CSV file โ to analysis โ to output files.
You will learn how to:
- Run Python properly
- Load Flat Files data
- Calculate returns
- Identify top gainers and losers
- Save results into new files
๐ฆ Step 0: Prepare Your Environment
โ What you need
- Your CSV file, example:
1D-20260202.csv๐ We also have a tutorial on how to extract Flat Files here: Flat Files S3 API โ Complete Guide: Authentication, Discovery, Download & Extraction
- Python installed
- Check if Python is installed:
1python--version
2
3or:
4
5python3--version- Install Pandas
1pip install pandas
2
3or:
4
5pip3 install pandas๐ Step 1: Set Up Your Working Folder
Create a folder, for example:
1flatfiles/Put your CSV inside:
1flatfiles/
2โโโ D-20260202.csv๐งพ Step 2: Create Your Python Script
Inside the same folder, create a file:
1returns_analysis.pyโ๏ธ Paste This Code (Complete Script)
1import pandas as pd
2
3# Step 1: Load the CSV file into memory (DataFrame)
4df=pd.read_csv("D-20260202.csv",delimiter=";")
5
6# Step 2: Calculate returns
7df["return"]= (df["price_close"]-df["price_open"])/df["price_open"]
8
9# Step 3: Show preview in terminal
10print("\nPreview of data with returns:")
11print(df[["exchange_symbol","price_open","price_close","return"]].head())
12
13# Step 4: Identify top gainers
14top_gainers=df.sort_values("return",ascending=False).head(5)
15
16# Step 5: Identify top losers
17top_losers=df.sort_values("return",ascending=True).head(5)
18
19# Step 6: Print results
20print("\nTop Gainers:")
21print(top_gainers[["exchange_symbol","return"]])
22
23print("\nTop Losers:")
24print(top_losers[["exchange_symbol","return"]])
25
26# Step 7: Save results to files
27df.to_csv("with_returns.csv",index=False)
28top_gainers.to_csv("top_gainers.csv",index=False)
29top_losers.to_csv("top_losers.csv",index=False)
30
31print("\nโ
Done! Files created:")
32print("- with_returns.csv")
33print("- top_gainers.csv")
34print("- top_losers.csv")๐ฅ๏ธ Step 3: What You Will See
Terminal Output
1Preview of data with returns:
2SWVL 3.03 โ 3.17 โ 0.046
3TSM 329.44 โ 329.69 โ 0.0007
4...
5
6Top Gainers:
7SWVL ...
8GLL ...
9
10Top Losers:
11...New Files Created
In your folder:
1flatfiles/
2โโโ D-20260202.csv
3โโโ returns_analysis.py
4โโโ with_returns.csv โ
(full dataset + return column)
5โโโ top_gainers.csv โ
6โโโ top_losers.csv โ
โ ๏ธ Common Beginner Mistakes (Avoid These)
โ Running Python code in terminal
Wrong:
1print(df.head())Correct:
1python returns_analysis.pyโ Forgetting delimiter
Wrong:
1pd.read_csv("file.csv")Correct:
1pd.read_csv("file.csv",delimiter=";")๐ฏ Final Takeaway
You now have a complete workflow:
1CSV file โ Python (DataFrame) โ Calculate โ Rank โ Save results