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
  • 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

Create a folder, for example:

1flatfiles/

Put your CSV inside:

1flatfiles/
2โ”œโ”€โ”€ D-20260202.csv

Inside the same folder, create a file:

1returns_analysis.py
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")
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...

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        โœ…

Wrong:

1print(df.head())

Correct:

1python returns_analysis.py

Wrong:

1pd.read_csv("file.csv")

Correct:

1pd.read_csv("file.csv",delimiter=";")

You now have a complete workflow:

1CSV file โ†’ Python (DataFrame) โ†’ Calculate โ†’ Rank โ†’ Save results