Skip to main content

Python

Python is widely used in finance and is well-suited for data analysis. Jupyter Notebook is recommended for interactive data analysis and exploration.

Jupyter

Jupyter enables exploratory data analysis and visualization. Install the required packages by running the following command:

pip install requests python mplfinance

Candlestick Queries

QuestDB supports data retrieval via REST API, enabling interactive analysis in Jupyter notebooks. Example query:

import requests
import time
import pandas as pd
import json
import mplfinance as mpf
from requests.models import PreparedRequest

baseUrl = "http://[Your Kmaster Server IP Address]:[Api exposed server port]>"
exchange = "OANDA"
symbol = "EUR/USD"
hoursBack = 24
timeframe = 15


startTime = (int(time.time()) - (hoursBack * 60 * 60)) * 1000
params = {"exchange": exchange,"symbol": symbol,"startEpoch": startTime,"endEpoch": int(time.time()) * 1000, "limit":10000, "timeframe": timeframe}
req = PreparedRequest()
req.prepare_url(f"{baseUrl}/api/markets/candlesticks", params)
response = requests.get(req.url)
data = response.json()
data["status"]
if data["status"] == "ok":
candlesticks = data["data"]
if len(candlesticks) > 0:
df = pd.DataFrame(candlesticks)
df.timestamp = pd.to_datetime(df.timestamp)
df = df.set_index("timestamp")
df = df.sort_values(by="timestamp")
mpf.plot(df,title=exchange+":"+symbol, type="candle",volume=True,tight_layout=True, mav=(50), style="yahoo")
else:
print("There is no data in the time range selected.")
else:
print(f"Error in request: {response.text}")