diff --git a/projects/forex_algo_trading.md b/projects/forex_algo_trading.md index 313f07e..5e9ac3a 100644 --- a/projects/forex_algo_trading.md +++ b/projects/forex_algo_trading.md @@ -51,6 +51,71 @@ swing_trading_project/ - Use `oandapyV20` in `data_fetcher.py` to request historical EUR/USD data. Consider H4 or D granularity. - Save the data to `data/` as CSV. +```python +import csv +import os +from oandapyV20 import API # The Oanda API wrapper +import oandapyV20.endpoints.instruments as instruments +from datetime import datetime +import pandas as pd + +# Configuration +ACCOUNT_ID = 'your_account_id_here' +ACCESS_TOKEN = 'your_access_token_here' +INSTRUMENT = 'EUR_USD' +GRANULARITY = 'H4' # 4-hour candles +OUTPUT_FILENAME = 'eur_usd_data.csv' + +# Directory for saving the data +DATA_DIR = 'data' +if not os.path.exists(DATA_DIR): + os.makedirs(DATA_DIR) + +def fetch_data(account_id, access_token, instrument, granularity): + """Fetch historical forex data for a specified instrument and granularity.""" + client = API(access_token=access_token) + params = { + "granularity": granularity, + "count": 5000 # Maximum data points to fetch in one request + } + + # Create a data request + data_request = instruments.InstrumentsCandles(instrument=instrument, params=params) + data = client.request(data_request) + + return data['candles'] + +def save_to_csv(data, filename): + """Save fetched forex data to a CSV file.""" + filepath = os.path.join(DATA_DIR, filename) + with open(filepath, mode='w', newline='') as file: + writer = csv.writer(file) + writer.writerow(['Time', 'Open', 'High', 'Low', 'Close', 'Volume']) + + for candle in data: + writer.writerow([ + candle['time'], + candle['mid']['o'], + candle['mid']['h'], + candle['mid']['l'], + candle['mid']['c'], + candle['volume'] + ]) + +def main(): + """Main function to fetch and save EUR/USD data.""" + print("Fetching data...") + data = fetch_data(ACCOUNT_ID, ACCESS_TOKEN, INSTRUMENT, GRANULARITY) + print(f"Fetched {len(data)} data points.") + + print("Saving to CSV...") + save_to_csv(data, OUTPUT_FILENAME) + print(f"Data saved to {os.path.join(DATA_DIR, OUTPUT_FILENAME)}") + +if __name__ == '__main__': + main() +``` + ## Step 4: Exploratory Data Analysis - Create a new Jupyter notebook in `notebooks/`.