-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPricer.py
1357 lines (1213 loc) · 59.9 KB
/
Pricer.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
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Pricer Window - Launches the pricer.
Written by Alexandre Almosni [email protected]
(C) 2015-2017 Alexandre Almosni
Released under Apache 2.0 license. More info at http://www.apache.org/licenses/LICENSE-2.0
**Steps to disable tabs for debugging:
In PricerMenu.__init__, look for #grid_labels = ['Africa']# used for testing
and uncomment - this will reduce the number of bonds loaded for testing
Classes:
PricingGrid
RunsGrid
PricerWindow
Functions:
send_mail_via_com()
"""
import wx
import wx.grid as gridlib
import pandas
import datetime
import win32com.client
import time
import wx.lib.colourdb
import wx.lib.pubsub
from wx.lib.pubsub import pub
#import warnings
#warnings.filterwarnings('error', category=UnicodeWarning)
#warnings.filterwarnings('error', message='*equal comparison failed*')
import inforalgo
import inforalgopanel
from subprocess import Popen
from win32api import GetUserName
from StaticDataImport import bonds, DEFPATH, APPPATH, bondRuns, frontToEmail, SPECIALBONDS, colFormats, runTitleStr, regsToBondName, tabList, columnListByTrader
from BondDataModel import BondDataModel
class MessageContainer():
def __init__(self, data):
self.data = data
def send_mail_via_com(text, subject, recipient, a1=None, a2=None):
"""Function to send email to bloomberg when users click on 'send' in the runs menu.
Function is called by RunsGrid.sendRun()
Keyword arguments:
text : Text message
subject : Email subject
recipient : Recipient of email
a1 : attachment (False by default)
a2 : attachment (False by default)
"""
# s = win32com.client.Dispatch("Mapi.Session") works for Outlook 2003
o = win32com.client.Dispatch("Outlook.Application")
# s.Logon('Outlook') works for Outlook 2003
# Msg = o.CreateItem(0) works for Outlook 2003
Msg = o.CreateItem(0x0) # works for Outlook 2007
Msg.To = recipient
Msg.Subject = subject
Msg.Body = text
if a1 is not None:
Msg.Attachments.Add(a1)
if a2 is not None:
Msg.Attachments.Add(a2)
Msg.Send()
pass
class TextDisplayWindow(wx.Frame):
def __init__(self, title, filename):
wx.Frame.__init__(self, None, wx.ID_ANY, title, size=(800, 600))
panel = wx.Panel(self)
multiText = wx.TextCtrl(panel, -1,"",size=(775, 575), style=wx.TE_MULTILINE|wx.TE_READONLY)#775 and 575 to leave space for the scrollbar
f = open(APPPATH+filename, 'r')
multiText.SetValue(f.read()) #open the file from location as read
f.close()
multiText.SetFont(wx.Font(multiText.GetFont().GetPointSize(), wx.TELETYPE, wx.NORMAL, wx.NORMAL))
self.Show()
class AxeGrid(wx.Frame):
def __init__(self, title, bdm, bondList):
wx.Frame.__init__(self, None, wx.ID_ANY, title, size=(800, 600))
panel = wx.Panel(self)
sizer = wx.BoxSizer()
self.grid = gridlib.Grid(panel)
self.grid.Bind(wx.EVT_KEY_DOWN, self.onKeyDown)
sizer.Add(self.grid, proportion=1, flag=wx.EXPAND)
#Attributes creation
self.fontBold = self.grid.GetDefaultCellFont()
self.fontBold.SetWeight(wx.FONTWEIGHT_BOLD)
defattr = wx.grid.GridCellAttr()
defattr.SetReadOnly(True)
bidaskinputattr = wx.grid.GridCellAttr()
bidaskinputattr.SetReadOnly(False)
bidaskinputattr.SetAlignment(wx.ALIGN_RIGHT, wx.ALIGN_CENTRE)
bidaskinputattr.SetFont(self.fontBold)
bidaskinputattr.SetTextColour(wx.BLUE)
bidasksizeinputattr = wx.grid.GridCellAttr()
bidasksizeinputattr.SetReadOnly(False)
bidasksizeinputattr.SetAlignment(wx.ALIGN_RIGHT, wx.ALIGN_CENTRE)
bidasksizeinputattr.SetFont(self.fontBold)
self.columnList = ['Security', 'B Axe', 'B Sz(M)', 'B Px', 'A Axe', 'A Sz(M)', 'A Px']
self.grid.CreateGrid(200, len(self.columnList))
bidasksizeinputattr.SetTextColour(wx.BLUE)
self.grid.SetColAttr(0, defattr)
self.grid.SetColSize(0, 100)
self.grid.SetColAttr(1, defattr)
self.grid.SetColSize(1, 50)
self.grid.SetColAttr(2, bidasksizeinputattr)
self.grid.SetColSize(2, 50)
self.grid.SetColAttr(3, bidaskinputattr)
self.grid.SetColSize(3, 50)
self.grid.SetColAttr(4, defattr)
self.grid.SetColSize(4, 50)
self.grid.SetColAttr(5, bidasksizeinputattr)
self.grid.SetColSize(5, 50)
self.grid.SetColAttr(6, bidaskinputattr)
self.grid.SetColSize(6, 50)
self.grid.SetRowLabelSize(100)
for (j, header) in enumerate(self.columnList):
self.grid.SetColLabelValue(j, header)
i = 0
for bond in bondList:
if bond not in bdm.df.index:
continue
if bdm.df.at[bond,'POSITION']==0:
continue
self.grid.SetRowLabelValue(i, bond)
self.grid.SetCellValue(i, 0, bdm.df.at[bond, 'ISIN'])
if bdm.df.at[bond,'POSITION']<0:
self.grid.SetCellValue(i, 1, 'Y')
self.grid.SetCellValue(i, 2, '{:.0f}'.format(-bdm.df.at[bond, 'POSITION']/1000.))
self.grid.SetCellValue(i, 3, '{:,.3f}'.format(bdm.df.at[bond, 'BID']))
if bdm.df.at[bond,'POSITION']>0:
self.grid.SetCellValue(i, 4, 'Y')
self.grid.SetCellValue(i, 5, '{:.0f}'.format(bdm.df.at[bond, 'POSITION']/1000.))
self.grid.SetCellValue(i, 6, '{:,.3f}'.format(bdm.df.at[bond, 'ASK']))
i = i + 1
panel.SetSizerAndFit(sizer)
self.Show()
pass
def onCopySelection(self):
# Number of rows and cols
if self.grid.GetSelectionBlockTopLeft() == []:
rows = 1
cols = 1
iscell = True
else:
rows = self.grid.GetSelectionBlockBottomRight()[0][0] - self.grid.GetSelectionBlockTopLeft()[0][0] + 1
cols = self.grid.GetSelectionBlockBottomRight()[0][1] - self.grid.GetSelectionBlockTopLeft()[0][1] + 1
iscell = False
# data variable contain text that must be set in the clipboard
data = ''
# For each cell in selected range append the cell value in the data variable
# Tabs '\t' for cols and '\r' for rows
for r in range(rows):
for c in range(cols):
if iscell:
data += str(self.grid.GetCellValue(self.grid.GetGridCursorRow() + r, self.grid.GetGridCursorCol() + c))
else:
data += str(self.grid.GetCellValue(self.grid.GetSelectionBlockTopLeft()[0][0] + r, self.grid.GetSelectionBlockTopLeft()[0][1] + c))
if c < cols - 1:
data += '\t'
data += '\n'
# Create text data object
clipboard = wx.TextDataObject()
# Set data object value
clipboard.SetText(data)
# Put the data in the clipboard
if wx.TheClipboard.Open():
wx.TheClipboard.SetData(clipboard)
wx.TheClipboard.Close()
else:
wx.MessageBox("Can't open the clipboard", "Error")
def onKeyDown(self, event):
keycode = event.GetKeyCode()
if keycode == 67 and event.ControlDown():
self.onCopySelection()
else:
pass
event.Skip() # important, otherwise one would need to define all possible events
class RunsGrid(gridlib.Grid):
"""RunsGrid Class: Class to define the RunsGrid tab.
Also creates top row to allow users to send runs for specific bonds. When values in the cells are changed,
an event is sent out to add bonds into self.df.
Attributes:
self.df : Pandas DataFrame consisting of the run definitions.
self.bdm : Class instance of BondDataModel.
Methods:
__init__()
onReloadRunDefinitions() : Reload run definitions when the 'Reload run definitions' button is clicked.
fillGrid() : Populates wx.Grid.
onDoubleClick() : Event handler when user doubleclicks.
sendRun() : Function to send run to user's Bloomberg's email.
addBondsToRuns() : Registers a custom bond when users enters a bond in the top row of the runs tab.
"""
def __init__(self, panel, df, bdm, pricerwindow):
"""
Keyword arguments:
panel : wx.Panel object
df : pandas.DataFrame
bdm : BondDataModel class instance
By default the grid will have 100 lines and 60 rows (max 60 runs, 100 securities per run).
"""
gridlib.Grid.__init__(self, panel)
self.CreateGrid(60,100)
self.defaultFont = self.GetDefaultCellFont()
self.fontBold = self.GetDefaultCellFont()
self.fontBold.SetWeight(wx.FONTWEIGHT_BOLD)
self.df = df
self.bdm = bdm
self.pricerwindow = pricerwindow
self.SetRowLabelSize(80)
self.SetColLabelSize(50)
self.SetColLabelValue(0,'Double click \n to send')
self.SetColSize(1,200)
self.SetColLabelValue(1, 'Header: ')
self.SetColLabelValue(2, 'Daily change: ')
self.SetColLabelValue(3, 'Autoforward: ')
self.fillGrid()
self.Bind(wx.grid.EVT_GRID_CELL_LEFT_DCLICK, self.onDoubleClick)
def onReloadRunDefinitions(self, event):
'''
Reload the run definitions (DEFPATH+'run.csv') when the 'Reload run definitions' is clicked.
'''
self.df = pandas.read_csv(DEFPATH+'runs.csv', index_col=0)
self.ClearGrid()
self.fillGrid()
wx.CallAfter(self.ForceRefresh)
def fillGrid(self):
'''
Function to populate the wx.Grid with data in self.df. fillGrid detects the inputs selected by the user,
namely:
1) Price/ Yield/ Z-Spread
2) True/ False (for autoforwarding)
Fonts in blue represents fields that can be changed by the user.
'''
self.df = pandas.read_csv(DEFPATH+'runs.csv', index_col=0)
maxCol = max (self.df.iloc[i].count() for i in range(len(self.df.index)))
#Sets columns
for (j, header) in enumerate(self.df.columns):
self.SetColLabelValue(j + 1, header)
#Sets row
for (k, header) in enumerate(self.df.index): #K=Row
self.SetRowLabelValue(k, header) #Bond Labels
self.SetCellValue(k, 0, 'SEND') #Send button
self.SetReadOnly(k, 0, True)
self.SetCellFont(k, 0, self.fontBold)
self.SetCellTextColour(k,0,wx.BLUE)
for i in range(maxCol): #F = Col
if pandas.isnull(self.df.iloc[k,i]):
value = ''
else:
if (i != 1) and (i != 2):
self.SetReadOnly(k, i + 1, True)
else:
self.SetReadOnly(k, i + 1, False)
self.SetCellTextColour(k, i + 1, wx.BLUE)
if i == 1: #Drop down list. Options: Price/ Yield/ Z-Spread
self.SetCellEditor(k, i + 1, wx.grid.GridCellChoiceEditor(['Price','Yield','Spread'],True))
if i == 2: #Drop down list. Options: True/ False
self.SetCellEditor(k, i + 1, wx.grid.GridCellChoiceEditor(['True','False'],True))
value = str(self.df.iloc[k,i])
self.SetCellValue(k, i + 1, value)
if value == 'START' or value == 'END':
self.SetCellFont(k, i + 1, self.fontBold)
else:
self.SetCellFont(k, i + 1, self.defaultFont)
if k==0: #For the first row => where user specify individual bonds.
#Sends out an event when the values in the cells are changed.
self.Bind(gridlib.EVT_GRID_CELL_CHANGE, self.addBondsToRuns)
self.SetReadOnly(k, 1, False)
self.SetCellTextColour(k, 1, wx.BLUE)
for i in range(5, len(self.df.iloc[0])):
self.SetReadOnly(k, i, False)
self.SetCellTextColour(k, i, wx.BLUE)
def addBondsToRuns(self,event):
"""
Function to add bonds to self.df when user inputs bonds in the top row. Bolds the value if it == START or END.
"""
row = 0
col = event.GetCol()
value = str(self.GetCellValue(row,col))
#Adds the value to self.df
self.df.iloc[0,col-1] = value
#If value == START or END, make it bold
if value == 'START' or value == 'END':
self.SetCellFont(row,col,self.fontBold)
else:
self.SetCellFont(row,col,self.defaultFont)
def onDoubleClick(self,event):
'''
ActionHandler for a double click event. Triggers the sendRun() function.
Parameters passed to sendRun:
dailyChange: Sets daily change field to the 2 column of the clicked row
autoFwd: Sets True/False for autoforwarding to the value in the 3 column of the clicked row
bondCol: Pandas Series. List of bonds to be queried and sent emailed to user.
'''
row = event.GetRow()
col = event.GetCol()
if col == 0:
self.SetCellBackgroundColour(row, col, wx.RED)
self.ForceRefresh()
dailyChange = self.GetCellValue(row, 2)
if dailyChange == 'Spread':
#tdelta = datetime.datetime.now() - self.bdm.USDswapRate.lastRefreshTime
if (datetime.datetime.now() - self.bdm.lastRefreshTime).seconds >= 7200: # HARD-CODING TWO HOURS IN SECONDS
dlg = wx.MessageDialog(self, 'Z-spreads last full update >2h ago. Do you want to refresh first?', 'Z-spread update alert', style=wx.YES_NO)
#answer = dlg.ShowModal()
if dlg.ShowModal() == wx.ID_YES:
self.pricerwindow.onRefreshSwapRates(event)
autoFwd = self.GetCellValue(row, 3)
if row == 0:
bondCol=pandas.Series(index=self.df.iloc[0].index)
for i in range(0, self.df.iloc[0].count()):
bondCol.iloc[i] = self.GetCellValue(row,i+1)
else:
bondCol = self.df.iloc[row]
self.sendRun(bondCol, autoFwd, dailyChange)
wx.CallLater(500, self.SetCellBackgroundColour, row, col, wx.WHITE)
wx.CallLater(600, self.ForceRefresh)
else:
pass
def sendRun(self, bondCol, autoFwd, dailyChange):
'''
Function to extract the information queried, then triggers send_mail_via_com() to send the information
to the user. sendRun is called by onDoubleClick().
Keyword arguments:
bondCol: list of bonds to be queried
autoFwd: True/False for autoforwarding
dailyChange: Can either be Price/ Yield/ or Spread
'''
strHeader = 'Ccy Security B Px A Px B YTM A YTM B ZS A ZS'
if dailyChange == 'Price':
strHeader = strHeader + " PChgD PChgW"
elif dailyChange == 'Spread':
strHeader = strHeader + " ZChgD ZChgW"
else:
strHeader = strHeader + " YChgD YChgW"
strHeader = strHeader + '\n' + "-------------------------------------------------------------------------------" + '\n'
strRunOutput = ''
for i in range(4, bondCol.shape[0]):
bond = bondCol.iloc[i]
#The line below makes it works regardless whether user types 'END' in the top columns.
if bond == 'END' or type(bond) == float:
break
strPrice = '{:>7.3f}'.format(self.bdm.df.at[bond, 'BID']) + "-" + '{:<7.3f}'.format(
self.bdm.df.at[bond, 'ASK'])
strYield = '{:>5.2f}'.format(self.bdm.df.at[bond, 'YLDB']) + "/" + '{:<5.2f}'.format(
self.bdm.df.at[bond, 'YLDA'])
strBidAskZ = '{:>4.0f}'.format(self.bdm.df.at[bond, 'ZB']) + "/" + '{:<4.0f}'.format(
self.bdm.df.at[bond, 'ZA'])
if len(strYield) > 11:
strYield = ' nan/nan '
if len(strBidAskZ) > 9:
strBidAskZ = ' nan/nan '
strLine = self.bdm.df.at[bond, 'CRNCY'] + ' ' + self.bdm.df.at[bond, 'SECURITY_NAME'].ljust(
23) + strPrice + ' ' + strYield + ' ' + strBidAskZ + ' '
if dailyChange == 'Price':
strChange = '{: >+5.2f}'.format(self.bdm.df.at[bond, 'DP1D']) + "/" + '{: <+5.2f}'.format(
self.bdm.df.at[bond, 'DP1W'])
elif dailyChange == 'Spread':
strChange = '{: >+5.0f}'.format(self.bdm.df.at[bond, 'DISP1D']) + "/" + '{: <+5.0f}'.format(
self.bdm.df.at[bond, 'DISP1W'])
else: #Yield
strChange = '{: >+5.0f}'.format(self.bdm.df.at[bond, 'DY1D']) + "/" + '{: <+5.0f}'.format(
self.bdm.df.at[bond, 'DY1W'])
if len(strChange) > 11:
strChange = ' nan/nan '
strLine = strLine + strChange
strLine = strLine + '\n'
strRunOutput = strRunOutput + strLine
strRunOutput = strHeader + strRunOutput + '\n\n\n'
if autoFwd == 'TRUE' or autoFwd == 'True': ##Excel will mess it up when updating the file
strRunOutput = strRunOutput + '#autoforward' + '\n\n\n'
strRunOutput = strRunOutput + '#icbcsrun' + '\n\n\n'
send_mail_via_com(strRunOutput, runTitleStr + ' - ' + bondCol['Header'],
frontToEmail[self.bdm.mainframe.front_username])
class GenericPricingGrid(gridlib.Grid):
def __init__(self):
pass
class PricingGrid(gridlib.Grid):
"""PricingGrid class : Class to define the pricing grid
Attributes:
self.tab : pandas.DataFrame containing the names of the tabs to be created
self.bondList : list of bonds
self.columnList : list of columns
self.bondsWithBenchmark : list of bonds with Benchmarks
self.bdm : BondDataModel class instance
self.daysToCouponWarning : warning threshhold for days to coupon
Methods:
__init__()
initialPaint() : Function to paint the background colour orange when Pricer is first loaded.
showPopUpMenu() : Create and display a popup menu on right-click event:
showTradeHistory() : Shows the TradeHistory
copyLine() : Copies the selected line
copyISIN() : Copies the ISIN of the selected bond
showDES() : Shows the description on Bloomberg
showCN() : Shows the company news on bloomberg
showGP() : Shows the price graph on bloomberg
showALLQ() : Shows ALLQ on bloomberg
bbgScreenSendKeys() : Sends shell command to bloomberg.
updateBenchmarks() : updates benchmarks
updateOneBenchmark() : Updates single benchmark
singleBenchmarkUpdate(): Updates bond in the benchmark
updatePositions() : Holding function to only update positions after thread has died.
updateAllPositions() : Updates all the position
updateLine() : Holding function to only update line after thread has died.
updateLineAction() : Updates each line
createField() : Creates the fields to be displayed
---------------------
Back to RunsGrid
Back to PricerWindow
---------------------
"""
def __init__(self, panel, tab, columnList, bdm, pricer):
"""
Init function defines columns attributes and binds right click event to the grids.
Keyword arguments:
panel : wx.Panel object
tab : pandas.DataFrame containing the names of the tabs to be created
columnList : list of columns
bdm : BondDataModel class instance
"""
gridlib.Grid.__init__(self, panel)
#Attributes creation
self.fontBold = self.GetDefaultCellFont()
self.fontBold.SetWeight(wx.FONTWEIGHT_BOLD)
defattr = wx.grid.GridCellAttr()
defattr.SetReadOnly(True)
bidaskattr = wx.grid.GridCellAttr()
bidaskattr.SetAlignment(wx.ALIGN_RIGHT, wx.ALIGN_CENTRE)
bidaskattr.SetFont(self.fontBold)
bidaskattr.SetReadOnly(True)
rightalignattr = wx.grid.GridCellAttr()
rightalignattr.SetAlignment(wx.ALIGN_RIGHT, wx.ALIGN_CENTRE)
rightalignattr.SetReadOnly(True)
centrealignattr = wx.grid.GridCellAttr()
centrealignattr.SetAlignment(wx.ALIGN_CENTRE, wx.ALIGN_CENTRE)
centrealignattr.SetReadOnly(True)
bidaskinputattr = wx.grid.GridCellAttr()
bidaskinputattr.SetReadOnly(False)
bidaskinputattr.SetAlignment(wx.ALIGN_RIGHT, wx.ALIGN_CENTRE)
bidaskinputattr.SetFont(self.fontBold)
bidaskinputattr.SetTextColour(wx.BLUE)
bidasksizeinputattr = wx.grid.GridCellAttr()
bidasksizeinputattr.SetReadOnly(False)
bidasksizeinputattr.SetAlignment(wx.ALIGN_RIGHT, wx.ALIGN_CENTRE)
bidasksizeinputattr.SetFont(self.fontBold)
sendattr = wx.grid.GridCellAttr()
sendattr.SetTextColour(wx.BLUE)
sendattr.SetAlignment(wx.ALIGN_RIGHT, wx.ALIGN_CENTRE)
sendattr.SetReadOnly(True)
sendattr.SetFont(self.fontBold)
self.daysToCouponWarning = 10
self.clickedISIN = ''
self.clickedBond = ''
self.tabKeyCounter = 0
pub.subscribe(self.updateLine, "BOND_PRICE_UPDATE")
pub.subscribe(self.updatePositions, "POSITION_UPDATE")
pub.subscribe(self.updateBGNPrices, "BGN_PRICE_UPDATE")
self.tab = tab
self.bondList = list(self.tab['Bonds'])
self.columnList = columnList
self.bondsWithBenchmark = list(self.tab[self.tab['Benchmarks'].notnull()]['Bonds'])
self.bondToBenchmark = self.tab.loc[self.tab['Benchmarks'].notnull(),['Bonds','Benchmarks']].set_index('Bonds')['Benchmarks'].to_dict()
self.bdm = bdm
self.pricer = pricer
self.CreateGrid(len(self.bondList), len(self.columnList))
colFormats['wxFormat'] = pandas.np.nan
if self.pricer.mainframe is None or self.pricer.mainframe.isTrader:
colFormats.loc[colFormats['Format']=='BIDASK', 'wxFormat'] = bidaskinputattr
bidasksizeinputattr.SetTextColour(wx.BLUE)
else:
colFormats.loc[colFormats['Format']=='BIDASK', 'wxFormat'] = bidaskattr
colFormats.loc[colFormats['Format']=='CENTRE', 'wxFormat'] = centrealignattr
colFormats.loc[colFormats['Format']=='RIGHT', 'wxFormat'] = rightalignattr
colFormats.loc[colFormats['Format']=='DEFAULT', 'wxFormat'] = defattr
colFormats.loc[colFormats['Format']=='BIDASKINPUT', 'wxFormat'] = bidaskinputattr
colFormats.loc[colFormats['Format']=='BIDASKSIZEINPUT', 'wxFormat'] = bidasksizeinputattr
for c in self.columnList:
if c in colFormats.index:
self.SetColAttr(self.columnList.index(c), colFormats.loc[c, 'wxFormat'])
self.SetColSize(self.columnList.index(c), colFormats.loc[c, 'Width'])
self.SetRowLabelSize(1)
self.Bind(gridlib.EVT_GRID_CELL_RIGHT_CLICK, self.showPopUpMenu)
self.Bind(gridlib.EVT_GRID_CELL_CHANGE, self.onEditCell)
self.Bind(wx.EVT_KEY_DOWN, self.onKeyDown)
self.Bind(gridlib.EVT_GRID_SELECT_CELL, self.onSingleSelection)
self.Bind(gridlib.EVT_GRID_RANGE_SELECT, self.onSelection)
self.showAllqID = wx.NewId()
self.showTradeHistoryID = wx.NewId()
self.showDESID = wx.NewId()
self.showCNID = wx.NewId()
self.showGPID = wx.NewId()
self.buyRegsID = wx.NewId()
self.sellRegsID = wx.NewId()
self.buy144AID = wx.NewId()
self.sell144AID = wx.NewId()
self.copyLineID = wx.NewId()
self.copyISINID = wx.NewId()
self.pastePricesID = wx.NewId()
self.Bind(wx.EVT_MENU, self.showALLQ, id=self.showAllqID)
self.Bind(wx.EVT_MENU, self.showTradeHistory, id=self.showTradeHistoryID)
self.Bind(wx.EVT_MENU, self.showDES, id=self.showDESID)
self.Bind(wx.EVT_MENU, self.showCN, id=self.showCNID)
self.Bind(wx.EVT_MENU, self.showGP, id=self.showGPID)
self.Bind(wx.EVT_MENU, self.buyRegs, id=self.buyRegsID)
self.Bind(wx.EVT_MENU, self.sellRegs, id=self.sellRegsID)
self.Bind(wx.EVT_MENU, self.buy144A, id=self.buy144AID)
self.Bind(wx.EVT_MENU, self.sell144A, id=self.sell144AID)
self.Bind(wx.EVT_MENU, self.copyLine, id=self.copyLineID)
self.Bind(wx.EVT_MENU, self.copyISIN, id=self.copyISINID)
self.Bind(wx.EVT_MENU, self.onPastePrices, id=self.pastePricesID)
self.selected_row_number = 0
self.selected_col_number = 0
self.previousSingleSelection = True
self.singleSelection = True
self.askCol = self.columnList.index('ASK')
def initialPaint(self):
"""
Function to paint the background colour orange when Pricer is first loaded. Function is called by
PricerWindow.
Salespeople only see positions up to 1mm absolute size.
"""
wx.lib.colourdb.updateColourDB()
headerlineattr = wx.grid.GridCellAttr()
headerlineattr.SetBackgroundColour(wx.NamedColour('CORNFLOWERBLUE'))
headerlineattr.SetFont(self.fontBold)
headerlineattr.SetReadOnly(True)
self.oddLineColour = wx.NamedColour('GAINSBORO')
self.oddlineattr = wx.grid.GridCellAttr()
self.oddlineattr.SetBackgroundColour(self.oddLineColour)
for (j, header) in enumerate(self.columnList):
self.SetColLabelValue(j, header)
for (i, bond) in enumerate(self.bondList):
if bond in self.bdm.df.index:
if i % 2:
self.SetRowAttr(i,self.oddlineattr.Clone())#this clone thing is needed in wxPython 3.0 (worked fine without in 2.8)
if header in self.bdm.df.columns:
value = self.bdm.df.at[bond, header]
if header == 'POSITION':
if self.bdm.mainframe is None or self.bdm.mainframe.isTrader:
value = '{:,.0f}'.format(value)
else:
if value > 1000000:
value = '>1MM'
elif value < -1000000:
value = '<-1MM'
else:
value = '{:,.0f}'.format(value)
elif header == 'SIZE':
value = '{:,.0f}'.format(value / 1000000) + 'm'
else:
value = str(value)
self.SetCellValue(i, j, value)
if header == 'D2CPN' and self.bdm.df.at[bond, header] <= self.daysToCouponWarning:
self.SetCellBackgroundColour(i, j, wx.RED)
if header in ['BID_S', 'ASK_S']:
self.SetCellValue(i,j,'{:,.0f}'.format(self.bdm.df.at[bond, header + 'IZE']/1000.))
else:
if j == 0:
self.SetCellValue(i, j, bond)
if bond != '':
self.SetRowAttr(i, headerlineattr)
def onKeyDown(self, event):
keycode = event.GetKeyCode()
if keycode == wx.WXK_TAB:
self.tabKeyCounter = self.tabKeyCounter + 1
elif keycode == wx.WXK_NUMPAD_ENTER or keycode == wx.WXK_RETURN:
for i in range(0,self.tabKeyCounter):
self.MoveCursorLeft(False)
self.tabKeyCounter = 0
elif keycode == wx.WXK_WINDOWS_MENU:
self.clickedBond = self.GetCellValue(self.GetGridCursorRow(), self.columnList.index('BOND'))
self.showPopUpMenu(event, True)
elif keycode == 67 and event.ControlDown():
self.onCopySelection()
else:
pass
event.Skip() # important, otherwise one would need to define all possible events
def onSingleSelection(self, event):
if not (self.pricer.mainframe is None):
bond = self.GetCellValue(event.GetRow(), 1)
if bond in self.bdm.df.index and self.bdm.mainframe.isTrader:
postxt = 'REGS: ' + '{:,.0f}'.format(self.bdm.df.at[bond, 'REGS']) + ' 144A: '+ '{:,.0f}'.format(self.bdm.df.at[bond, '144A'])
risktxt = 'SPV01: ' + '{:,.0f}'.format(self.bdm.df.at[bond, 'RISK'])
wx.CallAfter(self.writeToStatusBar, bond + ': ' + postxt + ' ' + risktxt)
event.Skip()
def onSelection(self, event):
# issue is this and onSingleSelection are both fired concurrently
self.previousSingleSelection = self.singleSelection
if self.GetSelectionBlockTopLeft() == []:
self.singleSelection = True
else:
self.singleSelection = False
self.selected_row_number = self.GetSelectionBlockBottomRight()[0][0] - self.GetSelectionBlockTopLeft()[0][0] + 1
self.selected_col_number = self.GetSelectionBlockBottomRight()[0][1] - self.GetSelectionBlockTopLeft()[0][1] + 1
if self.selected_col_number == 1 and self.GetGridCursorCol() == self.columnList.index('POSITION') and self.bdm.mainframe.isTrader:
rowstart = self.GetGridCursorRow()
bondlist = [self.GetCellValue(rowstart + r, 1) for r in range(self.selected_row_number)]
postxt = 'Position: ' + '{:,.0f}'.format(self.bdm.df.loc[bondlist,'POSITION'].sum())
risktxt = 'SPV01: ' + '{:,.0f}'.format(self.bdm.df.loc[bondlist,'RISK'].sum())
wx.CallAfter(self.writeToStatusBar, 'Sum:' + ' ' + postxt + ' ' + risktxt)
pass
def writeToStatusBar(self,txt):
self.pricer.statusbar.SetStatusText(txt, 3)
def onCopySelection(self):
# Number of rows and cols
#print self.GetSelectionBlockBottomRight()
#print self.GetGridCursorRow()
#print self.GetGridCursorCol()
if self.GetSelectionBlockTopLeft() == []:
rows = 1
cols = 1
iscell = True
else:
rows = self.GetSelectionBlockBottomRight()[0][0] - self.GetSelectionBlockTopLeft()[0][0] + 1
cols = self.GetSelectionBlockBottomRight()[0][1] - self.GetSelectionBlockTopLeft()[0][1] + 1
iscell = False
# data variable contain text that must be set in the clipboard
data = ''
# For each cell in selected range append the cell value in the data variable
# Tabs '\t' for cols and '\r' for rows
for r in range(rows):
for c in range(cols):
if iscell:
data += str(self.GetCellValue(self.GetGridCursorRow() + r, self.GetGridCursorCol() + c))
else:
data += str(self.GetCellValue(self.GetSelectionBlockTopLeft()[0][0] + r, self.GetSelectionBlockTopLeft()[0][1] + c))
if c < cols - 1:
data += '\t'
data += '\n'
# Create text data object
clipboard = wx.TextDataObject()
# Set data object value
clipboard.SetText(data)
# Put the data in the clipboard
if wx.TheClipboard.Open():
wx.TheClipboard.SetData(clipboard)
wx.TheClipboard.Close()
else:
wx.MessageBox("Can't open the clipboard", "Error")
def onEditCell(self,event):
if self.previousSingleSelection:
self.onEditSingleCell(event)
else:
rowstart = event.GetRow()
col = event.GetCol()
colID = self.GetColLabelValue(col)
strNewValue = self.GetCellValue(rowstart, col)
self.onEditSingleCell(event)
if self.selected_col_number == 1 and colID == 'BID':
for r in range(self.selected_row_number):
if r==0:
continue#already done above
row = rowstart + r
bond = self.GetCellValue(row,1)
oldValue = float(self.GetCellValue(row,col))
newValue = self.readInput(oldValue,strNewValue)
self.SetCellValue(row,col,'{:,.3f}'.format(newValue))
try:
oldOffer = float(self.GetCellValue(row, self.askCol))
except:
oldOffer = 0
self.SetCellValue(row,self.askCol,'{:,.3f}'.format(newValue + oldOffer - oldValue))
self.sendUpdateToInforalgo(row)
def onEditSingleCell(self,event):
row = event.GetRow()
col = event.GetCol()
bond = self.GetCellValue(row,1)
colID = self.GetColLabelValue(col)
try:
oldValue = float(event.GetString())
except:
oldValue = 0
strNewValue = self.GetCellValue(row,col)
newValue = self.readInput(oldValue,strNewValue)
if colID == 'BID' or colID == 'ASK':
self.SetCellValue(row, col, '{:,.3f}'.format(newValue))
if colID == 'BID_S' or colID == 'ASK_S':
self.SetCellValue(row, col, '{:,.0f}'.format(newValue))
if colID == 'BID':
try:
oldOffer = float(self.GetCellValue(row, self.askCol))
except:
oldOffer = 0
self.SetCellValue(row, self.askCol, '{:,.3f}'.format(newValue + oldOffer - oldValue))
self.sendUpdateToInforalgo(row)
def sendUpdateToInforalgo(self, row):
wx.CallAfter(self.dataSentWarning,row)
bbg_sec_id = self.GetCellValue(row,0)
bid_price = float(self.GetCellValue(row, self.columnList.index('BID')))
ask_price = float(self.GetCellValue(row, self.columnList.index('ASK')))
try:
bid_size = int(self.GetCellValue(row, self.columnList.index('BID_S')).replace(',',''))
ask_size = int(self.GetCellValue(row, self.columnList.index('ASK_S')).replace(',',''))
except:
bid_size = 0
ask_size = 0
try:
self.pricer.uat_table.send_price(bbg_sec_id, bid_price, ask_price, bid_size*1000, ask_size*1000)
except:
print 'Failed to send data to UAT server'
try:
self.pricer.prd_table.send_price(bbg_sec_id, bid_price, ask_price, bid_size*1000, ask_size*1000)
except:
print 'Failed to send data to PRD server'
pass
def basisPointShift(self,bond,oldValue,strNewValue):
delta = float(strNewValue[:-1])
pass
@staticmethod
def readInput(oldValue, strNewValue):
'''
Takes float and string as input, returns float
Parser to understand +1, -0.5, +18 as +1/8 etc.
Will also return original value if it doesn't understand input
'''
if strNewValue[0] == '+' or strNewValue[0] == '-':
try:
delta = float(strNewValue[1:])
except:
delta = 0
delta_dic = {116: 0.063, 18: 0.125, 316: 0.188, 14: 0.25, 516: 0.313, 38: 0.375, 716: 0.438, 12: 0.5, 916: 0.563, 58: 0.625, 1116: 0.688, 34: 0.75, 1316: 0.813, 78: 0.875, 1516: 0.938}
if delta in delta_dic:
delta = delta_dic[delta]
if strNewValue[0] == '+':
newValue = oldValue + delta
else:
newValue = oldValue - delta
newValue = round(16*newValue) / 16 #solves issues with 1/16th increments
else:
try:
newValue = float(strNewValue)
except:
newValue = oldValue
return newValue
def dataSentWarning(self, row):
for cell in ['BID', 'ASK', 'BID_S', 'ASK_S']:
self.SetCellBackgroundColour(row, self.columnList.index(cell), wx.YELLOW)
def onPastePrices(self, event):
if not wx.TheClipboard.IsOpened():
clipboard = wx.TextDataObject()
if wx.TheClipboard.Open():
wx.TheClipboard.GetData(clipboard)
wx.TheClipboard.Close()
data = clipboard.GetText()
rowstart = self.GetGridCursorRow()
colstart = self.GetGridCursorCol()
for y, r in enumerate(data.splitlines()):
# Convert c in a array of text separated by tab
row = rowstart + y
for x, c in enumerate(r.split('\t')):
self.SetCellValue(row, colstart + x, c)
bbg_sec_id = self.GetCellValue(row,0)
bid_price = float(self.GetCellValue(row, self.columnList.index('BID')))
ask_price = float(self.GetCellValue(row, self.columnList.index('ASK')))
try:
bid_size = int(self.GetCellValue(row, self.columnList.index('BID_S')).replace(',',''))
ask_size = int(self.GetCellValue(row, self.columnList.index('ASK_S')).replace(',',''))
except:
bid_size = 0
ask_size = 0
wx.CallAfter(self.dataSentWarning,row)
self.pricer.uat_table.send_price(bbg_sec_id, bid_price, ask_price, bid_size*1000, ask_size*1000)
self.pricer.prd_table.send_price(bbg_sec_id, bid_price, ask_price, bid_size*1000, ask_size*1000)
def showPopUpMenu(self, event, fromWindowsMenu=False):
"""
Create and display a popup menu on right-click event. Function is called by __init__() when user
right clicks on a grid.
"""
menu = wx.Menu()
if not fromWindowsMenu:
self.clickedBond = self.GetCellValue(event.GetRow(), self.columnList.index('BOND'))
try:
self.clickedISIN = self.bdm.df.at[self.clickedBond, 'ISIN']
except:
self.clickedISIN = ''
showAllqItem = wx.MenuItem(menu, self.showAllqID, "ALLQ")
menu.AppendItem(showAllqItem)
showTradeHistoryItem = wx.MenuItem(menu, self.showTradeHistoryID, "Trade history")
menu.AppendItem(showTradeHistoryItem)
showDESItem = wx.MenuItem(menu, self.showDESID, "DES")
menu.AppendItem(showDESItem)
showCNItem = wx.MenuItem(menu, self.showCNID, "CN")
menu.AppendItem(showCNItem)
showGPItem = wx.MenuItem(menu, self.showGPID, "GP")
menu.AppendItem(showGPItem)
menu.AppendSeparator()
buyRegsItem = wx.MenuItem(menu, self.buyRegsID, "Buy REGS")
menu.AppendItem(buyRegsItem)
sellRegsItem = wx.MenuItem(menu, self.sellRegsID, "Sell REGS")
menu.AppendItem(sellRegsItem)
buy144AItem = wx.MenuItem(menu, self.buy144AID, "Buy 144A")
menu.AppendItem(buy144AItem)
sell144AItem = wx.MenuItem(menu, self.sell144AID, "Sell 144A")
menu.AppendItem(sell144AItem)
menu.AppendSeparator()
copyLineItem = wx.MenuItem(menu, self.copyLineID, "Copy line")
menu.AppendItem(copyLineItem)
copyISINItem = wx.MenuItem(menu, self.copyISINID, "Copy ISIN")
menu.AppendItem(copyISINItem)
pastePricesItem = wx.MenuItem(menu, self.pastePricesID, "Paste prices")
menu.AppendItem(pastePricesItem)
self.PopupMenu(menu)
menu.Destroy()
def showTradeHistory(self, event):
"""
Shows the TradeHistory.
"""
self.bdm.mainframe.onBondQuerySub(self.clickedBond)
wx.CallAfter(self.bdm.mainframe.Raise)
pass
def copyLine(self, event):
"""
Copies the selected line to the clipboard.
"""
self.bdm.df.loc[self.clickedBond].to_clipboard()
def copyISIN(self, event):
"""Copies the ISIN of the selected bond to the clipboard.
"""
if wx.TheClipboard.Open():
wx.TheClipboard.SetData(wx.TextDataObject(self.clickedISIN))
wx.TheClipboard.Close()
def showDES(self, event):
self.bbgScreenSendKeys(self.clickedISIN, 'DES')
def showCN(self, event):
self.bbgScreenSendKeys(self.clickedISIN, 'CN')
def showGP(self, event):
self.bbgScreenSendKeys(self.clickedISIN, 'GP')
def showALLQ(self, event):
self.bbgScreenSendKeys(self.clickedISIN, 'ALLQ')
def buyRegs(self, event):
self.bbgScreenSendKeys(self.clickedISIN, 'B')
def sellRegs(self, event):
self.bbgScreenSendKeys(self.clickedISIN, 'S')
def buy144A(self, event):
self.bbgScreenSendKeys(bonds.loc[regsToBondName[self.clickedISIN],'144A'], 'B')
def sell144A(self, event):
self.bbgScreenSendKeys(bonds.loc[regsToBondName[self.clickedISIN],'144A'], 'S')
def bbgScreenSendKeys(self, isin, strCommand):
"""Sends command to Bloomberg.
"""
shell = win32com.client.Dispatch("WScript.Shell")
shell.AppActivate('1-BLOOMBERG')
try:
shell.SendKeys(isin + '{F3}' + strCommand + '{ENTER}')
except:
print 'Failed to send command to Bloomberg'
def updateOneBenchmark(self, bond):
"""Checks if bond behchmarked or benchmarker
"""
for b, bc in self.bondToBenchmark.iteritems():
if bond == bc or bond == b:
self.singleBenchmarkUpdate(b)
def updateBenchmarks(self):
"""Called by the bond data model on first pass
"""
for bond in self.bondsWithBenchmark:
self.singleBenchmarkUpdate(bond)
def singleBenchmarkUpdate(self, bond):
"""Updates single benchmark.
"""
i = self.bondList.index(bond)
j = self.columnList.index('BENCHMARK')
try:
#bench = self.tab[self.tab['Bonds'] == bond]['Benchmarks'].iloc[0]
bench = self.bondToBenchmark[bond]
value = self.bdm.df.at[bond, 'ZB'] - self.bdm.df.at[bench, 'ZB']
self.SetCellBackgroundColour(i, j, wx.RED)
self.SetCellValue(i, j, '{:,.0f}'.format(value) + ' vs ' + bench)
except:
self.SetCellValue(i, j, 'FAIL')
if i % 2:
wx.CallLater(1000, self.SetCellBackgroundColour, i, j, self.oddLineColour)
else:
wx.CallLater(1000, self.SetCellBackgroundColour, i, j, wx.WHITE)
wx.CallLater(1100, self.ForceRefresh)
def updatePositions(self, message=None):
"""Holding function that listens to the POSITION_UPDATE event and calls updateAllPositions() after
the parent thread dies.
"""
wx.CallAfter(self.updateAllPositions, message)
def updateAllPositions(self, message):
"""Updates all the position. Function is called by updatePositions().
No need for sales logic here as they only see SOD positions.
"""
positions = message.data
j = self.columnList.index('POSITION')
for (i, bond) in enumerate(self.bondList):
if bond in self.bdm.df.index and bond in positions.index:
value = '{:,.0f}'.format(positions.at[bond, 'Qty'])
if value != self.GetCellValue(i, j):
self.SetCellBackgroundColour(i, j, wx.RED)
self.SetCellValue(i, j, value)
if i % 2:
wx.CallLater(1000, self.SetCellBackgroundColour, i, j, self.oddLineColour)
else:
wx.CallLater(1000, self.SetCellBackgroundColour, i, j, wx.WHITE)
wx.CallLater(1100, self.ForceRefresh)
def updateLine(self, message=None):
"""Holding function that listens to the BOND_PRICE_UPDATE event and calls updateLineAction() after
the parent thread dies.
"""
wx.CallAfter(self.updateLineAction, message)