Before you start
What this module changes in your trading process.
Set up a Python research environment with an interpreter, packages, and notebooks.
Python is the de facto language for quantitative research. Build practical foundations with environments, NumPy, Pandas, and price data.
Module outline
Before you start
Set up a Python research environment with an interpreter, packages, and notebooks.
Lesson 1
Menyiapkan lingkungan riset Python (interpreter, paket, notebook).
Riset kuantitatif modern berdiri di atas beberapa pustaka Python: • NumPy — array numerik & operasi vektor yang cepat. • Pandas — struktur data tabel (DataFrame) untuk deret waktu. • Matplotlib — visualisasi. • SciPy / statsmodels / scikit-learn — statistik & machine learning (dipakai di tingkat lanjut).
Menyiapkan lingkungan Gunakan virtual environment agar paket proyek terpisah dan reprodusibel: Example code: # Buat & aktifkan environment, lalu pasang paket python -m venv .venv . .venv/bin/activate # Windows: .venv\Scripts\activate pip install numpy pandas matplotlib jupyter Jupyter Notebook sangat populer untuk riset karena memungkinkan eksekusi sel demi sel, melihat hasil & grafik langsung — cocok untuk eksplorasi data.
Reprodusibilitas Catat versi paket (mis. pip freeze > requirements.txt ). Hasil riset yang tidak bisa direproduksi sulit dipercaya — prinsip yang sama berlaku untuk backtest.
Example
# Buat & aktifkan environment, lalu pasang paket python -m venv .venv . .venv/bin/activate # Windows: .venv\Scripts\activate pip install numpy pandas matplotlib jupyter
Key points
Practice checkpoint
Apply this lesson to one market you actually watch. Write the rule, the data needed, the risk check, and the condition that invalidates the idea.
Before continuing
Lesson 2
Menggunakan NumPy untuk komputasi numerik vektor yang efisien.
NumPy menyediakan ndarray , array yang memungkinkan operasi pada seluruh deret angka sekaligus (vectorized) — jauh lebih cepat dan ringkas daripada loop Python.
Example code: import numpy as np harga = np.array([100, 102, 101, 105, 107]) # Operasi vektor: hitung selisih harian tanpa loop selisih = np.diff(harga) # [2, -1, 4, 2] return_harian = selisih / harga[:-1] # persentase perubahan print(return_harian.mean()) # rata-rata return print(return_harian.std()) # volatilitas (simpangan baku) Konsep penting: • Vectorization: operasi pada array utuh, bukan elemen satu per satu.
Ini pola berpikir inti quant. • Broadcasting: NumPy otomatis menyesuaikan bentuk array yang kompatibel. • Fungsi agregat: mean , std , sum , cumsum — blok bangunan metrik. Mengapa ini penting Backtest pada ribuan baris data jadi cepat bila ditulis secara vektor. Loop manual seringkali lambat dan rawan bug indeks.
Example
import numpy as np harga = np.array([100, 102, 101, 105, 107]) # Operasi vektor: hitung selisih harian tanpa loop selisih = np.diff(harga) # [2, -1, 4, 2] return_harian = selisih / harga[:-1] # persentase perubahan print(return_harian.mean()) # rata-rata return print(return_harian.std()) # volatilitas (simpangan baku)
Key points
Practice checkpoint
Apply this lesson to one market you actually watch. Write the rule, the data needed, the risk check, and the condition that invalidates the idea.
Before continuing
Lesson 3
Memuat dan memanipulasi data harga dengan Pandas (Series, DataFrame, indexing waktu).
Pandas adalah alat utama untuk deret waktu keuangan. Dua objeknya: Series (satu kolom berindeks) dan DataFrame (tabel).
Example code: import pandas as pd # Muat data harga (CSV dengan kolom tanggal & close) df = pd.read_csv("harga.csv", parse_dates=["date"], index_col="date") df = df.sort_index() # Return harian sederhana & log return df["ret"] = df["close"].pct_change() df["log_ret"] = np.log(df["close"] / df["close"].shift(1)) # Rolling: rata-rata bergerak 20 hari df["ma20"] = df["close"].rolling(20).mean() print(df.tail()) Operasi yang sering dipakai: • Indexing waktu: df.loc["2024-01"] mengambil rentang tanggal. • pct_change() : return persentase. • shift() : menggeser data — krusial untuk menghindari look-ahead bias (dibahas di backtesting). • rolling() : jendela bergerak untuk indikator.
Awas look-ahead shift(1) memastikan keputusan hari ini hanya memakai informasi hingga kemarin. Lupa menggeser data adalah sumber bug backtest paling umum dan paling berbahaya. Visualisasi cepat: df["close"].plot() menampilkan grafik harga; df["ret"].hist() menampilkan distribusi return.
Example
import pandas as pd # Muat data harga (CSV dengan kolom tanggal & close) df = pd.read_csv("harga.csv", parse_dates=["date"], index_col="date") df = df.sort_index() # Return harian sederhana & log return df["ret"] = df["close"].pct_change() df["log_ret"] = np.log(df["close"] / df["close"].shift(1)) # Rolling: rata-rata bergerak 20 hari df["ma20"] = df["close"].rolling(20).mean() print(df.tail())
Key points
Practice checkpoint
Apply this lesson to one market you actually watch. Write the rule, the data needed, the risk check, and the condition that invalidates the idea.
Before continuing
Fieldwork
Build one worksheet for Python for Trading: Pandas and NumPy: state the market, the rule, the data input, the risk limit, the validation check, and the review note before using it live.
Glossary
Checkpoint quiz
Quiz results can add XP when you are signed in.
Progress action
Marking complete saves the module, updates streak activity, and awards XP only once per module.
Previous module
Before writing a strategy, understand the playing field: how orders meet in the order book, the role of brokers and APIs, and what each order type implies.
Next module
A strategy is only as good as its data. Learn price-data structure, timeframe resampling, and the quiet data traps that break backtests.
Risk note: Metavulus learning content is for education and market preparation only. It is not financial advice, investment advice, or a trading recommendation.