# Flat Files – Calculating Returns & Identifying Top Movers from OHLCV Data

## 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:

```
D-20260202.csv
```

👉 We also have a tutorial on how to extract Flat Files here: [Flat Files S3 API – Complete Guide: Authentication, Discovery, Download & Extraction](https://www.finfeedapi.com/learn/academy/tutorials/s3-api-complete-guide)  


* Python installed
  *  Check if Python is installed:

```
python--version

or:

python3--version
```

* Install Pandas

```
pip install pandas

or:

pip3 install pandas
```

## 📁 Step 1: Set Up Your Working Folder

Create a folder, for example:

```
flatfiles/
```

Put your CSV inside:

```
flatfiles/
├── D-20260202.csv
```

## 🧾 Step 2: Create Your Python Script

Inside the same folder, create a file:

```
returns_analysis.py
```

### ✍️ Paste This Code (Complete Script)

```python
import pandas as pd

# Step 1: Load the CSV file into memory (DataFrame)
df=pd.read_csv("D-20260202.csv",delimiter=";")

# Step 2: Calculate returns
df["return"]= (df["price_close"]-df["price_open"])/df["price_open"]

# Step 3: Show preview in terminal
print("\nPreview of data with returns:")
print(df[["exchange_symbol","price_open","price_close","return"]].head())

# Step 4: Identify top gainers
top_gainers=df.sort_values("return",ascending=False).head(5)

# Step 5: Identify top losers
top_losers=df.sort_values("return",ascending=True).head(5)

# Step 6: Print results
print("\nTop Gainers:")
print(top_gainers[["exchange_symbol","return"]])

print("\nTop Losers:")
print(top_losers[["exchange_symbol","return"]])

# Step 7: Save results to files
df.to_csv("with_returns.csv",index=False)
top_gainers.to_csv("top_gainers.csv",index=False)
top_losers.to_csv("top_losers.csv",index=False)

print("\n✅ Done! Files created:")
print("- with_returns.csv")
print("- top_gainers.csv")
print("- top_losers.csv")
```

## 🖥️ Step 3: What You Will See

### Terminal Output

```
Preview of data with returns:
SWVL  3.03 → 3.17 → 0.046
TSM   329.44 → 329.69 → 0.0007
...

Top Gainers:
SWVL ...
GLL ...

Top Losers:
...
```

### New Files Created

In your folder:

```
flatfiles/
├── D-20260202.csv
├── returns_analysis.py
├── with_returns.csv      ✅ (full dataset + return column)
├── top_gainers.csv       ✅
├── top_losers.csv        ✅
```

## ⚠️ Common Beginner Mistakes (Avoid These)

### ❌ Running Python code in terminal

Wrong:

```
print(df.head())
```

Correct:

```
python returns_analysis.py
```

### ❌ Forgetting delimiter

Wrong:

```
pd.read_csv("file.csv")
```

Correct:

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

## 🎯 Final Takeaway

You now have a complete workflow:

```
CSV file → Python (DataFrame) → Calculate → Rank → Save results
```