forked from 5ymph0en1x/SAAT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
analysis.py
708 lines (505 loc) · 26.8 KB
/
analysis.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
from UTILS.cointegration_analysis import estimate_long_run_short_run_relationships, \
engle_granger_two_step_cointegration_test
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# Name of the previously collected data
results_filename = "sample_.csv"
analysis_type = 1 # 1- Partial / 2- Full
# Start of the analysis
def read_data(filename):
'''
This function reads the .csv stored at the 'filename' location and returns a DataFrame
with two levels of column names. The first level column contains the Stock Name and the
second contains the type of market data, e.g. bid/ask, price/volume.
'''
df = pd.read_csv(filename, index_col=0)
df.columns = [df.columns.str[-3:], df.columns.str[:-4]]
print(df)
return df
market_data = read_data(results_filename)
# Show the First 5 Rows
print(market_data.head(5))
# Show the Stocks
stock_names = list(market_data.columns.get_level_values(0).unique())
print('The stocks available are', stock_names)
# Calculate mid-prices of each stock and add them to the DataFrame
for stock in stock_names:
market_data[stock, 'MidPrice'] = (market_data[stock, 'BidPrice'] + market_data[stock, 'AskPrice']) / 2
market_data = market_data.sort_index(axis=1)
print(market_data.head(5))
def mid_price_check(stock):
'''
Function that checks for different stocks if the MidPrice
is correctly specified.
'''
plt.figure(figsize=(20, 5))
plt.plot(market_data[stock, 'AskPrice'][:100])
plt.plot(market_data[stock, 'MidPrice'][:100])
plt.plot(market_data[stock, 'BidPrice'][:100])
plt.xticks([]) # Timestamp is not Important
plt.title('Ask, Bid and Mid Price Development of Stock ' + stock)
plt.legend(["Ask Price", "Mid Price", "Bid Price"], loc='lower left')
plt.show()
mid_price_check('BTC')
# Obtain the statistical parameters for each and every pair
data_analysis = {'Pairs': [],
'Constant': [],
'Gamma': [],
'Alpha': [],
'P-Value': []}
data_zvalues = {}
for stock1 in stock_names:
for stock2 in stock_names:
if stock1 != stock2:
if (stock2, stock1) in data_analysis['Pairs']:
continue
pairs = stock1, stock2
constant = estimate_long_run_short_run_relationships(np.log(
market_data[stock1, 'MidPrice']), np.log(market_data[stock2, 'MidPrice']))[0]
gamma = estimate_long_run_short_run_relationships(np.log(
market_data[stock1, 'MidPrice']), np.log(market_data[stock2, 'MidPrice']))[1]
alpha = estimate_long_run_short_run_relationships(np.log(
market_data[stock1, 'MidPrice']), np.log(market_data[stock2, 'MidPrice']))[2]
pvalue = engle_granger_two_step_cointegration_test(np.log(
market_data[stock1, 'MidPrice']), np.log(market_data[stock2, 'MidPrice']))[1]
zvalue = estimate_long_run_short_run_relationships(np.log(
market_data[stock1, 'MidPrice']), np.log(market_data[stock2, 'MidPrice']))[3]
data_analysis['Pairs'].append(pairs)
data_analysis['Constant'].append(constant)
data_analysis['Gamma'].append(gamma)
data_analysis['Alpha'].append(alpha)
data_analysis['P-Value'].append(pvalue)
data_zvalues[pairs] = zvalue
data_analysis = round(pd.DataFrame(data_analysis), 4).set_index('Pairs')
# Visualize the P-values
def plot_pvalues():
"""
This function plots all obtained P-values.
"""
plt.figure(figsize=(20, 5))
plt.hist(data_analysis['P-Value'], bins=100)
plt.xlabel('P-value')
plt.ylabel('Number of observations')
plt.title('All obtained P-values')
plt.show()
plot_pvalues()
# Show Top 10 and Bottom 10
print(data_analysis.sort_values('P-Value')[:10])
print(data_analysis.sort_values('P-Value')[-10:])
# Selecting tradable pairs where P-Value < 0.01 and create a seperate DataFrame containing these pairs
tradable_pairs_analysis = data_analysis[data_analysis['P-Value'] < 0.01].sort_values('P-Value')
# Get all the tradable stock pairs into a list
stock_pairs = list(tradable_pairs_analysis.index.values.tolist())
# Show the Pairs
print(stock_pairs)
# Create a list of unique tradable stocks
list_stock1 = [stock[0] for stock in stock_pairs]
list_stock2 = [stock[1] for stock in stock_pairs]
for stock in list_stock2:
list_stock1.append(stock)
unique_stock_list = list(set(list_stock1))
# Create a new DataFrame containing all market information for the tradable pairs
tradable_pairs_data = market_data[unique_stock_list]
print(tradable_pairs_data.head())
def Plot_Tradable_Z():
"""
This function plots the z-values of all pairs based on
the data_zvalues dataframe.
"""
for pair in stock_pairs:
zvalue = data_zvalues[pair]
plt.figure(figsize=(20, 5))
plt.title('Error-correction term stock pair {}'.format(pair))
zvalue.plot()
plt.xlabel('Time')
plt.ylabel('Magnitude')
xmin = 0
xmax = len(zvalue)
plt.hlines(0.005, xmin, xmax, 'g') # Note 0.005 is randomly chosen
plt.hlines(-0.005, xmin, xmax, 'r') # Note -0.005 is randomly chosen
plt.legend(['Z-Value', 'Positive Threshold', 'Negative Threshold'], loc='lower left')
plt.show()
Plot_Tradable_Z()
# Select randomly chosen pair from the tradable stock and visualize bid and ask prices, bid and ask volumes, and the z-values
import random
# Choose random stock
random_pair = random.choice(stock_pairs)
# Create a plot showing the bid and ask prices of a randomly chosen stock
def Plot_RandomPair_BidAskPrices():
"""
This function plots the bid and ask price of a randomly chosen tradable pair.
"""
plt.figure(figsize=(20, 5))
plt.title('Bid and ask prices of stock pair {} and {}'.format(random_pair[0], random_pair[1]))
plt.plot(tradable_pairs_data[random_pair[0], 'AskPrice'].iloc[:100], 'r')
plt.plot(tradable_pairs_data[random_pair[0], 'BidPrice'].iloc[:100], 'm')
plt.xlabel('Time')
plt.ylabel('Price stock {}'.format(random_pair[0]))
plt.legend(loc='lower left')
plt.twinx()
plt.plot(tradable_pairs_data[random_pair[1], 'AskPrice'].iloc[:100])
plt.plot(tradable_pairs_data[random_pair[1], 'BidPrice'].iloc[:100])
plt.xticks([])
plt.ylabel('Price stock {}'.format(random_pair[1]))
plt.legend(loc='upper right')
plt.show()
# Plot_RandomPair_BidAskPrices()
# Create a plot showing the bid and ask volumes of a randomly chosen stock
def Plot_RandomPair_BidAskVolumes(): # Plot not really clarifying, maybe other kind of plot?
"""
This function plots the bid and ask volumes of a randomly chosen tradable pair.
"""
plt.figure(figsize=(20, 5))
plt.title('Bid and ask volumes of stock pair {} and {}'.format(random_pair[0], random_pair[1]))
plt.plot(tradable_pairs_data[random_pair[0], 'AskVolume'].iloc[:100], 'r')
plt.plot(tradable_pairs_data[random_pair[0], 'BidVolume'].iloc[:100], 'm')
plt.xlabel('Time')
plt.ylabel('Volume stock {}'.format(random_pair[0]))
plt.legend(loc='lower left')
plt.twinx()
plt.plot(tradable_pairs_data[random_pair[1], 'AskVolume'].iloc[:100])
plt.plot(tradable_pairs_data[random_pair[1], 'BidVolume'].iloc[:100])
plt.xticks([])
plt.ylabel('Volume stock {}'.format(random_pair[1]))
plt.legend(loc='upper right')
plt.show()
# Plot_RandomPair_BidAskVolumes()
# Create a Dataframe containing information about the error-correction term of each pair
data_error_correction_term = {'Pair': [],
'CountZeroCrossings': [],
'TradingPeriod': [],
'LongRunMean': [],
'Std': []}
for pair in stock_pairs:
zvalue = data_zvalues[pair]
my_array = np.array(zvalue)
count = ((my_array[:-1] * my_array[1:]) < 0).sum()
trading_period = 1 / count
long_run_mean = zvalue.mean()
std = zvalue.std()
data_error_correction_term['Pair'].append(pair)
data_error_correction_term['CountZeroCrossings'].append(count)
data_error_correction_term['TradingPeriod'].append(trading_period)
data_error_correction_term['LongRunMean'].append(round(long_run_mean, 4))
data_error_correction_term['Std'].append(round(std, 4))
data_error_correction_term = pd.DataFrame(data_error_correction_term).set_index('Pair')
print(data_error_correction_term)
# Create a new column within the earlier defined DataFrame with Z-Values of all stock pairs
for pair in stock_pairs:
stock1 = pair[0]
stock2 = pair[1]
tradable_pairs_data[stock1 + stock2, 'Z-Value'] = data_zvalues[stock1, stock2]
# Create a Dictionary that saves all Gamma values of each pair
gamma_dictionary = {}
for pair, value in tradable_pairs_analysis.iterrows():
gamma_dictionary[pair] = value['Gamma']
print('Gamma:', gamma_dictionary)
# Create a Dictionary that saves all Standard Deviation values of each pair
std_dictionary = {}
for pair, value in data_error_correction_term.iterrows():
std_dictionary[pair] = value['Std']
print('Deviation:', std_dictionary)
positions = {}
limit = 100
for pair in stock_pairs:
stock1 = pair[0]
stock2 = pair[1]
gamma = gamma_dictionary[stock1, stock2]
for i in np.linspace(0.05, 1.0, 10):
threshold = float(i * std_dictionary[stock1, stock2])
current_position_stock1 = 0
current_position_stock2 = 0
column_name_stock1 = stock1 + ' Pos - Thres: ' + str(threshold)
# print(tradable_pairs_data[stock1, 'BidVolume'].iloc[0])
BidPrice_Stock1 = tradable_pairs_data[stock1, 'BidVolume'].iloc[0]
AskPrice_Stock1 = tradable_pairs_data[stock1, 'AskVolume'].iloc[0]
BidPrice_Stock2 = tradable_pairs_data[stock2, 'BidVolume'].iloc[0]
AskPrice_Stock2 = tradable_pairs_data[stock1, 'AskVolume'].iloc[0]
positions[column_name_stock1] = []
for time, data_at_time in tradable_pairs_data.iterrows():
BidVolume_Stock1 = data_at_time[stock1, 'BidVolume']
AskVolume_Stock1 = data_at_time[stock1, 'AskVolume']
BidVolume_Stock2 = data_at_time[stock2, 'BidVolume']
AskVolume_Stock2 = data_at_time[stock2, 'AskVolume']
zvalue = float(data_at_time[stock1 + stock2, 'Z-Value'])
# If the zvalues of (BB,DD) are high the spread diverges, i.e. sell BB (=stock1=y) and buy DD (=stock2=x)
if zvalue >= threshold:
hedge_ratio = float(gamma * (BidPrice_Stock1 / AskPrice_Stock2))
if hedge_ratio >= 1:
max_order_stock1 = current_position_stock1 + limit
max_order_stock2 = max_order_stock1 / hedge_ratio
trade = np.floor(
min((BidVolume_Stock1 / hedge_ratio), AskVolume_Stock2, max_order_stock1, max_order_stock2))
positions[column_name_stock1].append((- trade * hedge_ratio) + current_position_stock1)
current_position_stock1 = ((- trade * hedge_ratio) + current_position_stock1)
elif hedge_ratio < 1:
max_order_stock1 = current_position_stock1 + limit
max_order_stock2 = max_order_stock1 * hedge_ratio
trade = np.floor(
min((BidVolume_Stock1 * hedge_ratio), AskVolume_Stock2, max_order_stock1, max_order_stock2))
positions[column_name_stock1].append((- trade / hedge_ratio) + current_position_stock1)
current_position_stock1 = ((- trade / hedge_ratio) + current_position_stock1)
elif zvalue <= -threshold:
hedge_ratio = float(gamma * (AskPrice_Stock1 / BidPrice_Stock2))
if hedge_ratio >= 1:
max_order_stock1 = abs(current_position_stock1 - limit)
max_order_stock2 = max_order_stock1 / hedge_ratio
trade = np.floor(
min((AskVolume_Stock1 / hedge_ratio), BidVolume_Stock2, max_order_stock1, max_order_stock2))
positions[column_name_stock1].append((+ trade * hedge_ratio) + current_position_stock1)
current_position_stock1 = (+ trade * hedge_ratio) + current_position_stock1
elif hedge_ratio < 1:
max_order_stock1 = abs(current_position_stock1 - limit)
max_order_stock2 = max_order_stock1 * hedge_ratio
trade = np.floor(
min((AskVolume_Stock1 * hedge_ratio), BidVolume_Stock2, max_order_stock1, max_order_stock2))
positions[column_name_stock1].append((+ trade / hedge_ratio) + current_position_stock1)
current_position_stock1 = (+ trade / hedge_ratio) + current_position_stock1
BidPrice_Stock1 = data_at_time[stock1, 'BidPrice']
AskPrice_Stock1 = data_at_time[stock1, 'AskPrice']
BidPrice_Stock2 = data_at_time[stock2, 'BidPrice']
AskPrice_Stock2 = data_at_time[stock2, 'AskPrice']
else:
positions[column_name_stock1].append(current_position_stock1)
column_name_stock2 = stock2 + ' Pos - Thres: ' + str(threshold)
print(column_name_stock2)
if hedge_ratio >= 1:
# print(positions[column_name_stock1])
positions[column_name_stock1] = np.array(positions[column_name_stock1])
positions[column_name_stock2] = positions[column_name_stock1] / hedge_ratio * -1
elif hedge_ratio < 1:
positions[column_name_stock1] = np.array(positions[column_name_stock1])
positions[column_name_stock2] = positions[column_name_stock1] / (1 / hedge_ratio) * -1
# Create a seperate dataframe (to keep the original dataframe intact) with rounding
# Also insert the timestamp, as found in the tradeable_pairs_data DataFrame
positions_final = np.ceil(pd.DataFrame(positions))
positions_final['Timestamp'] = tradable_pairs_data.index
positions_final = positions_final.set_index('Timestamp')
# The difference between the positions
positions_diff = positions_final.diff()[1:]
# Positions_diff first rows
print(positions_diff.head())
# OPTIONAL to Excel to Save the Amount of Trades
# positions_diff[(positions_diff != 0)].count().to_excel('Thresholds.xlsx')
positions_diff[-1:] = -positions_final[-1:]
pnl_dataframe = pd.DataFrame()
for pair in stock_pairs:
stock1 = pair[0]
stock2 = pair[1]
Stock1_AskPrice = tradable_pairs_data[stock1, 'AskPrice'][1:]
Stock1_BidPrice = tradable_pairs_data[stock1, 'BidPrice'][1:]
Stock2_AskPrice = tradable_pairs_data[stock2, 'AskPrice'][1:]
Stock2_BidPrice = tradable_pairs_data[stock2, 'BidPrice'][1:]
for i in np.linspace(0.05, 1.0, 10):
threshold = i * std_dictionary[stock1, stock2]
column_name_1 = stock1 + ' Pos - Thres: ' + str(threshold)
column_name_2 = stock2 + ' Pos - Thres: ' + str(threshold)
pnl_dataframe[stock1 + str(threshold)] = np.where(positions_diff[column_name_1] > 0,
positions_diff[column_name_1] * -Stock1_BidPrice, positions_diff[column_name_1] * -Stock1_AskPrice)
pnl_dataframe[stock2 + str(threshold)] = np.where(positions_diff[column_name_2] > 0,
positions_diff[column_name_2] * -Stock2_BidPrice, positions_diff[column_name_2] * -Stock2_AskPrice)
print(pnl_dataframe.head())
# Create Columns for the pnl_threshold dataframe
pairs = []
thresholds = []
for pair in stock_pairs:
stock1 = pair[0]
stock2 = pair[1]
for i in np.linspace(0.05, 1.0, 10):
threshold = i * std_dictionary[stock1, stock2]
pair = stock1, stock2
pairs.append(pair)
thresholds.append(threshold)
# Include columns and append PnLs
pnl_threshold = {'Pairs': pairs,
'Thresholds': thresholds,
'PnLs': []}
for pair in stock_pairs:
stock1 = pair[0]
stock2 = pair[1]
for i in np.linspace(0.05, 1.0, 10):
threshold = i * std_dictionary[stock1, stock2]
pnl_threshold['PnLs'].append(
pnl_dataframe[stock1 + str(threshold)].sum() + pnl_dataframe[stock2 + str(threshold)].sum())
pnl_threshold = pd.DataFrame(pnl_threshold)
pnl_threshold = pnl_threshold.set_index('Pairs')
pnl_threshold.to_excel('Thresholds.xlsx')
# Find Highest PnLs
highest_pnls = pnl_threshold.groupby(by='Pairs').agg({'PnLs' : max})
highest_pnls.sort_values('PnLs', ascending=False)
print(highest_pnls)
# Plot error-correction term (z-value) to observe what the spread looks like (see slide for comparison plot cointegrated pair)
def Plot_Thresholds(stock1, stock2):
zvalue = tradable_pairs_data[stock1 + stock2, 'Z-Value']
plt.figure(figsize=(20, 15))
plt.xticks([])
plt.title('Error-correction term stock pair ' + stock1 + ' and ' + stock2)
zvalue.plot(alpha=0.5)
plt.xlabel('Time')
plt.ylabel('Magnitude')
xmin = 0
xmax = len(zvalue)
# Boundries chosen to give an approximate good fit
plt.hlines(pnl_threshold['Thresholds'][10:20], xmin, xmax, 'g')
plt.hlines(-pnl_threshold['Thresholds'][10:20], xmin, xmax, 'r')
plt.legend(['Z-Value', 'Positive Threshold', 'Negative Threshold'])
plt.show()
Plot_Thresholds('ETH', 'BAB')
if analysis_type == 2:
# Create a Plot that displays the Profitability of the Thresholds
def profitability_of_the_thresholds(stock1, stock2):
pnl_threshold[(pnl_threshold.index == (stock1, stock2))].plot(x='Thresholds', y='PnLs', figsize=(10,10))
plt.title('Profitability of the Thresholds for ' + stock1 + ' and ' + stock2)
plt.xlabel('Amount of Sigma away from the Mean')
plt.ylabel('Profits and Losses')
plt.legend(['Profits and Losses'])
plt.grid()
plt.show()
profitability_of_the_thresholds('ETH', 'BAB')
profitability_of_the_thresholds('BSV', 'ETH')
profitability_of_the_thresholds('BSV', 'BTC')
profitability_of_the_thresholds('BTC', 'LTC')
# Determine the pairs and the threshold, manually chosen based on pnl_threshold and ensuring no overlap.
threshold_dictionary = {('ETH', 'BAB'): 0.000183,
('BSV', 'BTC'): 0.000075,
('BSV', 'ETH'): 0.000050,
('BTC', 'LTC'): 0.000394}
print(threshold_dictionary)
# Selection of the final pairs for this trading strategy
stock_pairs_final = [('ETH', 'BAB'),
('BSV', 'BTC'),
('BSV', 'ETH'),
('BTC', 'LTC')]
print(stock_pairs_final)
positions_strategy_1 = {}
limit = 100
for pair in stock_pairs_final:
stock1 = pair[0]
stock2 = pair[1]
gamma = gamma_dictionary[stock1, stock2]
threshold = threshold_dictionary[stock1, stock2]
current_position_stock1 = 0
current_position_stock2 = 0
positions_strategy_1[stock1] = []
for time, data_at_time in tradable_pairs_data.iterrows():
BidPrice_Stock1 = data_at_time[stock1, 'BidPrice']
AskPrice_Stock1 = data_at_time[stock1, 'AskPrice']
BidPrice_Stock2 = data_at_time[stock2, 'BidPrice']
AskPrice_Stock2 = data_at_time[stock2, 'AskPrice']
BidVolume_Stock1 = data_at_time[stock1, 'BidVolume']
AskVolume_Stock1 = data_at_time[stock1, 'AskVolume']
BidVolume_Stock2 = data_at_time[stock2, 'BidVolume']
AskVolume_Stock2 = data_at_time[stock2, 'AskVolume']
zvalue = data_at_time[stock1 + stock2, 'Z-Value']
if zvalue >= threshold:
hedge_ratio = gamma * (BidPrice_Stock1 / AskPrice_Stock2)
if hedge_ratio >= 1:
max_order_stock1 = current_position_stock1 + limit
max_order_stock2 = max_order_stock1 / hedge_ratio
trade = np.floor(
min((BidVolume_Stock1 / hedge_ratio), AskVolume_Stock2, max_order_stock1, max_order_stock2))
positions_strategy_1[stock1].append((- trade * hedge_ratio) + current_position_stock1)
current_position_stock1 = ((- trade * hedge_ratio) + current_position_stock1)
elif hedge_ratio < 1:
max_order_stock1 = current_position_stock1 + limit
max_order_stock2 = max_order_stock1 * hedge_ratio
trade = np.floor(
min((BidVolume_Stock1 * hedge_ratio), AskVolume_Stock2, max_order_stock1, max_order_stock2))
positions_strategy_1[stock1].append((- trade / hedge_ratio) + current_position_stock1)
current_position_stock1 = ((- trade / hedge_ratio) + current_position_stock1)
elif zvalue <= -threshold:
hedge_ratio = gamma * (AskPrice_Stock1 / BidPrice_Stock2)
if hedge_ratio >= 1:
max_order_stock1 = abs(current_position_stock1 - limit)
max_order_stock2 = max_order_stock1 / hedge_ratio
trade = np.floor(
min((AskVolume_Stock1 / hedge_ratio), BidVolume_Stock2, max_order_stock1, max_order_stock2))
positions_strategy_1[stock1].append((+ trade * hedge_ratio) + current_position_stock1)
current_position_stock1 = (+ trade * hedge_ratio) + current_position_stock1
elif hedge_ratio < 1:
max_order_stock1 = abs(current_position_stock1 - limit)
max_order_stock2 = max_order_stock1 * hedge_ratio
trade = np.floor(
min((AskVolume_Stock1 * hedge_ratio), BidVolume_Stock2, max_order_stock1, max_order_stock2))
positions_strategy_1[stock1].append((+ trade / hedge_ratio) + current_position_stock1)
current_position_stock1 = (+ trade / hedge_ratio) + current_position_stock1
else:
positions_strategy_1[stock1].append(current_position_stock1)
if hedge_ratio >= 1:
positions_strategy_1[stock2] = positions_strategy_1[stock1] / hedge_ratio * -1
elif hedge_ratio < 1:
positions_strategy_1[stock2] = positions_strategy_1[stock1] / (1 / hedge_ratio) * -1
# Set Ceiling (to prevent positions with not enough volume available) as well as define the timestamp
positions_strategy_1 = np.ceil(pd.DataFrame(positions_strategy_1))
positions_strategy_1['Timestamp'] = tradable_pairs_data.index
positions_strategy_1 = positions_strategy_1.set_index('Timestamp')
# The difference between the positions
positions_diff_strategy_1 = positions_strategy_1.diff()[1:]
# # Positions_diff first rows
# positions_diff_strategy_1.head()
#Used as mentioned earlier.
positions_diff_strategy_1[-1:] = -positions_strategy_1[-1:]
# Show Positions over Time
for pairs in stock_pairs_final:
stock1 = pairs[0]
stock2 = pairs[1]
plt.figure(figsize=(20, 5))
positions_strategy_1[stock1].plot()
positions_strategy_1[stock2].plot()
plt.title('Positions over Time for ' + stock1 + ' and ' + stock2)
plt.legend(["Position in " + stock1, "Position in " + stock2], loc='lower right')
plt.show()
pnl_dataframe_strategy_1 = pd.DataFrame()
for pair in stock_pairs_final:
stock1 = pair[0]
stock2 = pair[1]
Stock1_AskPrice = tradable_pairs_data[stock1, 'AskPrice'][1:]
Stock1_BidPrice = tradable_pairs_data[stock1, 'BidPrice'][1:]
Stock2_AskPrice = tradable_pairs_data[stock2, 'AskPrice'][1:]
Stock2_BidPrice = tradable_pairs_data[stock2, 'BidPrice'][1:]
pnl_dataframe_strategy_1[stock1] = np.where(positions_diff_strategy_1[stock1] > 0,
positions_diff_strategy_1[stock1] * -Stock1_BidPrice,
positions_diff_strategy_1[stock1] * -Stock1_AskPrice)
pnl_dataframe_strategy_1[stock2] = np.where(positions_diff_strategy_1[stock2] > 0,
positions_diff_strategy_1[stock2] * -Stock2_BidPrice,
positions_diff_strategy_1[stock2] * -Stock2_AskPrice)
print("The total profit is: €", round(pnl_dataframe_strategy_1.sum().sum()))
pnl_dataframe_strategy_1['Timestamp'] = tradable_pairs_data.index[1:]
pnl_dataframe_strategy_1 = pnl_dataframe_strategy_1.set_index('Timestamp')
pnl_dataframe_strategy_1['PnL'] = pnl_dataframe_strategy_1.sum(axis=1)
pnl_dataframe_strategy_1['Cum PnL'] = pnl_dataframe_strategy_1['PnL'].cumsum()
for pair in stock_pairs_final:
stock1 = pair[0]
stock2 = pair[1]
pnl_dataframe_strategy_1[stock1+stock2 + ' PnL'] = pnl_dataframe_strategy_1[stock1] + pnl_dataframe_strategy_1[stock2]
pnl_dataframe_strategy_1[stock1+stock2 + ' Cum PnL'] = pnl_dataframe_strategy_1[stock1+stock2 + ' PnL'].cumsum()
print(pnl_dataframe_strategy_1.tail())
# All Pairs's PnL
for pair in stock_pairs_final:
stock1 = pair[0]
stock2 = pair[1]
pnl_dataframe_strategy_1[stock1 + stock2 + ' Cum PnL'].plot(figsize=(10, 10))
plt.title('Cumulative PnL of ' + stock1 + ' and ' + stock2)
plt.ylabel('Profit and Loss')
plt.xlabel("")
plt.grid()
plt.xticks(rotation=20)
plt.show()
# All Pairs's PnLs (including total) in one graph
pnl_dataframe_strategy_1['Cum PnL'].plot()
for pair in stock_pairs_final:
stock1 = pair[0]
stock2 = pair[1]
pnl_dataframe_strategy_1[stock1 + stock2 + ' Cum PnL'].plot(figsize=(10, 10))
plt.legend(['Cum PnL', 'BB and JJ Cum PnL', 'FF and MM Cum PnL', 'DD and HH Cum PnL', 'AA and II Cum PnL'])
plt.title('Cumulative PnLs of the Trading Strategy')
plt.ylabel('Profit and Loss')
plt.xlabel("")
plt.grid()
plt.xticks(rotation=20)
plt.show()
# Send to CSV
pnl_dataframe_strategy_1.to_csv('Algorithm PnL Results.csv')
# Send to Excel (in case CSV is incorrect)
# pnl_dataframe_strategy_1.to_excel('Algorithm PnL Results.xlsx')