0% found this document useful (0 votes)
285 views

Forex Python

This document summarizes the analysis of foreign exchange data using Python. It processed over 35,000 data entries from an Excel file containing price and trade data into Pandas dataframes. It then analyzed the data under two scenarios: 1) with no hedging of client trades, and 2) hedging at the bid/ask price at each time interval. Key outputs included the net position, profit/loss over time, pip values, and the difference in pip values between the two scenarios to help identify opportunities for adopting a hedging strategy.

Uploaded by

api-223061586
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
285 views

Forex Python

This document summarizes the analysis of foreign exchange data using Python. It processed over 35,000 data entries from an Excel file containing price and trade data into Pandas dataframes. It then analyzed the data under two scenarios: 1) with no hedging of client trades, and 2) hedging at the bid/ask price at each time interval. Key outputs included the net position, profit/loss over time, pip values, and the difference in pip values between the two scenarios to help identify opportunities for adopting a hedging strategy.

Uploaded by

api-223061586
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Data Analytics Example with Foreign Exchange Data Processed in Python

The starting point is an Excel file containing nearly 35,000 data entries presented as shown in
the snapshot figures below. All data was imported into Python and pandas was used for writing
computational algorithms.

Sheet 1 - Prices Sheet 2 - Trades

When plotted, the bid-ask spread and the trades look as shown in these graphs:

The request was to process the data under different scenarios:

Scenario 1
Consider the situation where we hedge none of these client trades. Our position
begins flat (zero) and we are put into positions as the clients buy and sell to us.
Work out what our position and PnL looks like over the day.
Answer: The time index for the data in the two original sheets does not match. For the
two sheets to be processed together, the data was resampled so that time indices in the
two sheets match exactly. The resampling was done every minute even if the original
time index was spaced in milliseconds. Some granularity was lost - some transactions
that occurred within one minute are no longer recorded individually, but the total amount
traded in one minute is still there. The two sheets were then matched in one file
(dataframe). An excerpt of the code is shown below:

rng=pd.date_range('2012-09-30 23:12:00', '2012-10-01 20:14:00', freq ='1min')


df1 = pd.DataFrame(prices,index = rng, columns=['bid', 'offer','spread'])
df2 = pd.DataFrame(trades,index = rng, columns=['amount'])
A mid price column was calculated by averaging the bid and offer price for each time
index position. By multiplying the mid price column with the traded amount column, an
unrealized PnL column was created. For each positive amount that was traded, the
position long was noted, while for each negative traded amount, the position short was
noted. Position is a separate column. The number of pips were calculated by noting the
change in mid price from one time index to the next. The result of this computation was
recorded in column pip1. The value of one pip for each time index was calculated by
multiplying pip1 by unrealized PnL. Results are shown in a newly created column
pip_value1 which indicates whether a profit was made or if there was a loss.

Scenario 2
Assume you can hedge our position at any time buy buying or selling in the
market at the price at that time. You are aggressing (crossing the spread), so will
buy on the offer price or sell on the bid price. Without using any information about
the client, repeat the PnL analysis from Scenario 1.
Answer: In the second case, the unrealized PnL was calculated similarly as in Case 1,
except that if traded amounts were negative at a time index posiiton, unrealized PnL
was obtained by multiplying the bid price by amount traded. When the traded amount
was positive, the offer price was multiplied by the amount to calculate the unrealized
PnL. As in Scenario 1, for positive traded amounts a position of long was noted, with
the reverse true for negative amounts that were labeled short. Excerpt code:

N = len(trans3)
for k in range(N):
if unrealized_PnL_1[k] < 0:
position1[k] = 'short'

The number of pips were calculated by noting the change in either bid price (for negative
traded amounts) or offer price (for positive traded amounts) from one time index to the
next. Results are shown in a column called pip2. The value of one pip for each time
index was calculated by multiplying pip2 by unrealized PnL for this Scenario 2. Results
are shown in a column called pip_value2. Excerpt code:

for y in range(N):
if amount[y]<0:
pip2[y] = bid[y] - bid[y-1]
else:
pip2[y] = offer[y] - offer[y-1]

trans3['pip2']=pip2
trans3['pip_value2'] = pd.DataFrame(trans3.pip2 * trans3.unrealized_PnL_2,
index = trans3.index, columns=['pip_value2'])

A difference in pip values between Scenario 1 and 2 was calculated by simple


subtraction for each time index. Results are shown in a column called
diff_pip_values_1n2. For this Scenario 2, an extra column was calculated by noting the
change in pip value from one time index position to the next. Results are shown in
pip_value2_change. Some dramatic changes are noticed when going down the column
and these values could be used to adopt a hedging strategy for those large negative pip
value changes.

You might also like