-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest2.py
More file actions
60 lines (51 loc) · 1.81 KB
/
test2.py
File metadata and controls
60 lines (51 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import airbyte as ab
import pandas as pd
import matplotlib.pyplot as plt
# Create and configure the source connector:
source = ab.get_source(
"source-coin-api",
config={
"api_key": ab.get_secret("COIN_API_KEY"),
"environment": "production",
"symbol_id": "COINBASE_SPOT_INDEX_USD",
"period": "1DAY",
"start_date": "2023-01-01T00:00:00",
},
streams="*",
)
# Verify the config and creds by running `check`:
source.check()
# Read data from the source into the default cache:
cache = ab.get_default_cache()
result = source.read(cache=cache)
# Read from the cache into a pandas Dataframe:
ohlcv_df = cache["ohlcv_historical_data"].to_pandas()
# Convert 'time_period_start' to datetime format and necessary columns to numeric
ohlcv_df["time_period_start"] = pd.to_datetime(ohlcv_df["time_period_start"])
numeric_columns = [
"price_open",
"price_high",
"price_low",
"price_close",
"volume_traded",
"trades_count",
]
ohlcv_df[numeric_columns] = ohlcv_df[numeric_columns].apply(
pd.to_numeric, errors="coerce"
)
# Calculate daily price movement
ohlcv_df["daily_movement"] = ohlcv_df["price_close"] - ohlcv_df["price_open"]
# Set the 'time_period_start' column as the index for plotting
ohlcv_df.set_index("time_period_start", inplace=True)
# Plotting the daily movement
plt.figure(figsize=(12, 6)) # Set the figure size
plt.plot(ohlcv_df["daily_movement"], marker="o", linestyle="-")
plt.title("Daily Price Movement")
plt.xlabel("Date")
plt.ylabel("Price Movement")
plt.grid(True)
plt.xticks(rotation=45) # Rotates the date labels for better readability
plt.tight_layout() # Adjusts the plot to ensure everything fits without overlapping
# Save the plot as an image file
plt.savefig("daily_price_movement.png")
print("Plot saved as 'daily_price_movement.png'")