Skip to content

Commit

Permalink
feat(stock-market): update exchange_rate endpoint in mock server
Browse files Browse the repository at this point in the history
  • Loading branch information
nprimo committed May 13, 2024
1 parent 6558757 commit 33deab1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
from flask import Flask, jsonify
from flask import Flask, jsonify, request
from flask_cors import CORS

from random import uniform
import time
from utils import load_historical_data
from utils import load_data

app = Flask(__name__)
CORS(app)

start_time = time.time()

historical_data = load_historical_data()
historical_data = load_data()


@app.route('/exchange_rate/<symbol>')
def get_stock_data(symbol):
if symbol not in list(historical_data.keys()):
return jsonify("Invalid symbol")
current_time = time.time()
last_value = historical_data[symbol].Close[-1]
step = (int(current_time * 10) - int(start_time * 10)
) % len(historical_data[symbol])
try:
return jsonify({
'symbol': 'USD',
'rate': float(historical_data[symbol][step]),
'timestamp': current_time
})
except:
return "Server Error"
return jsonify({
'symbol': 'USD',
'rate': last_value * (1 + uniform(0.05, -0.05) + step * 0.0005),
'timestamp': current_time
})


@app.route('/stocks_list')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@ itsdangerous==2.1.2
Jinja2==3.1.4
MarkupSafe==2.1.3
Werkzeug==3.0.3
numpy==1.26.4
pandas==2.2.2
python-dateutil==2.9.0.post0
pytz==2024.1
six==1.16.0
tzdata==2024.1
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import csv
import pandas as pd


def load_historical_data(directory_path='./sample-stocks/'):
Expand All @@ -19,6 +20,21 @@ def load_historical_data(directory_path='./sample-stocks/'):
return historical_data


def load_data(directory_path='./sample-stocks/'):
historical_data = {}

file_list = [filename for filename in os.listdir(
directory_path) if filename.endswith(".csv")]
for filename in file_list:
symbol = filename.replace(".csv", "")
file_path = os.path.join(directory_path, filename)
historical_data[symbol] = pd.read_csv(
file_path, index_col=0, parse_dates=True)

return historical_data


if __name__ == "__main__":
result = load_historical_data()
result = load_data()
print(f'keys: {result.keys()}')
print(result["AE"].Close[-1])

0 comments on commit 33deab1

Please sign in to comment.