-
Notifications
You must be signed in to change notification settings - Fork 0
/
daily.py
126 lines (94 loc) · 5.05 KB
/
daily.py
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
pip install evds --upgrade
pip install plotnine
# Import pandas and numpy
import pandas as pd
import numpy as np
# Import evds
from evds import evdsAPI
# Please put your CBRT API key {https://evds2.tcmb.gov.tr-Profile Page-API Key}
evds = evdsAPI('..........')
# We download datas in the range 2013-2023
# https://evds2.tcmb.gov.tr/index.php?/evds/DataGroupLink/2/bie_abanlbil/en
# Central Bank Analytical Balance Sheet (Thousand TRY)(Business)
# Foreign Assets | TP.AB.A02 Domestic Assets | TP.AB.A03 FX Revaluation Account | TP.AB.A08
# Total Foreign Liabilities | TP.AB.A10 Currency Issued | TP.AB.A17
# Extra Budgetary Funds | TP.AB.A21 Deposits of Non-Bank Sector | TP.AB.A22 Deposits of Public Sector | TP.AB.A25
# Deposits of Banking Sector | TP.AB.A18 Open Market Operations | TP.AB.A24
#You can change the frequency in the end of the following code [ default version is startdate="12-08-2023", enddate="24-08-2023" ]
df=evds.get_data(['TP.AB.A02','TP.AB.A03', 'TP.AB.A08','TP.AB.A10','TP.AB.A17','TP.AB.A18','TP.AB.A21','TP.AB.A24','TP.AB.A25','TP.AB.A22'], startdate="12-08-2023", enddate="24-08-2023")
# Rename columns
new_column_names = ['Date','Foreign Assets', 'Domestic Assets', 'Revaluation', 'Total Foreign Liabilities', 'Currency Issued', 'Banking Reserves', 'Extra Funds', 'OMO', 'Deposits of Public Sector', 'Deposits of Non-Bank Sector']
df.columns = new_column_names
# We choose Date column as the index column
df.set_index('Date', inplace=True)
# Drop the NAN rows
df=df.dropna()
df
# Calculate the difference between Foreign Assets and Total Foreign Liabilities. This is Net Foreign Assets.
df['Net Foreign Assets'] = df['Foreign Assets'] - df['Total Foreign Liabilities']
df
# Drop the Foreign Assets and Total Foreign Liabilities columns
df.drop(['Foreign Assets', 'Total Foreign Liabilities'], axis=1, inplace=True)
df
# Move the last column to the first position
last_column = df.pop('Net Foreign Assets')
df.insert(0, 'Net Foreign Assets', last_column)
df
# Calculate the first difference for all columns
df_diff = df.diff()
# Delete the first row
df_diff = df_diff .iloc[1:]
df_diff
# Calculate the liquidity Situation
df_diff['Liquidity'] = df_diff['Net Foreign Assets']+ df_diff['Domestic Assets']+ df_diff['Revaluation']- df_diff['Currency Issued']- df_diff['Extra Funds'] - df_diff['Deposits of Public Sector'] - df_diff['Deposits of Non-Bank Sector']
df_diff
# OMO account is normally asset account but CBRT puts it liabilities side. We need to multiply with -1
df_diff['OMO']=df_diff['OMO'] * (-1)
df_diff
# The sum of the Liquidity and OMO need to be equal a Banking Reserves
# We create new series for comparing with Banking Reserves
# Please control whether Banking Reserves is rougly equal to Banking Reserves 2
#df_diff['Banking Reserves 2']=df_diff['OMO'] + df_diff['Liquidity']
#df_diff
# OMO and Liquidity Visualization
# Import the required libraries
from plotnine import ggplot, aes, geom_col, theme_minimal, labs, scale_y_continuous, theme, element_text
# Create a new DataFrame to hold the necessary columns for plotting
plot_df = df_diff[['OMO', 'Liquidity']].reset_index()
# Convert values to millions for better readability
plot_df['OMO'] /= 1e6
plot_df['Liquidity'] /= 1e6
# Melt the DataFrame to create a format suitable for stacked column plotting
plot_df = pd.melt(plot_df, id_vars=['Date'], value_vars=['OMO', 'Liquidity'])
# Create the stacked column plot using plotnine
p = (
ggplot(plot_df, aes(x='Date', y='value', fill='variable')) +
geom_col(position='stack') +
labs(title='OMO and Liquidity', x='Date', y='Value (in millions)') +
theme_minimal() +
theme(figure_size=(10, 6)) # Adjust figure size
)
# Display the plot
print(p)
# Liquidity Components' Visualization
# Calculate the liquidity components for the same period
liquidity_components = df_diff[['Net Foreign Assets', 'Domestic Assets', 'Revaluation',
'Currency Issued', 'Extra Funds',
'Deposits of Public Sector', 'Deposits of Non-Bank Sector']].reset_index()
# Convert values to millions for better readability
liquidity_components = liquidity_components.apply(lambda col: col / 1e6 if col.name != 'Date' else col, axis=0)
# Melt the DataFrame to create a format suitable for stacked column plotting
liquidity_components = pd.melt(liquidity_components, id_vars=['Date'],
value_vars=['Net Foreign Assets', 'Domestic Assets', 'Revaluation',
'Currency Issued', 'Extra Funds',
'Deposits of Public Sector', 'Deposits of Non-Bank Sector'])
# Create a stacked column plot for liquidity components using plotnine
p_liquidity_components = (
ggplot(liquidity_components, aes(x='Date', y='value', fill='variable')) +
geom_col(position='stack') +
labs(title='Liquidity Components', x='Date', y='Value (in millions)') +
theme_minimal() +
theme(figure_size=(10, 6))
)
# Display the plot for liquidity components
print(p_liquidity_components)