tarf_monte_carlo 3.23 → 3.28

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e31ee8faa0b4ae02a93252953f455286d901887af6983874cb32c388842d9038
4
- data.tar.gz: 7ea19ee7948374d024720267a5cb4aa0d532d1acfb0ed7e56fa4401b006af2d7
3
+ metadata.gz: 61612dbfd673a328672091d8e64ec0ebd5251f0e1d0904938e4fcfa1e083ff84
4
+ data.tar.gz: 22c0461dc088058378702be27ad055f600f97eac6c5392008d9c5783ff15cea0
5
5
  SHA512:
6
- metadata.gz: 60943ac5835b69779f2c3fc89536113b3c99a85413e4e51c6bbbc60d2d3c8266cf1d2a27798c89ae6f8e759ad8eace8825233945173f6c3c57ba06a53fdcbca8
7
- data.tar.gz: c9ff7608e59e0d94b2c2d4d585e5b2a0e28d9064d4e28c547634d2e72da3d7196b912cf89820c594f7fffcf6c3b2f0e910788f1bd41fda1228092ae1c70f332f
6
+ metadata.gz: 0f3224cfb1535727083cb0223bf06eca402e43d50f4bd6eb3adccc7cf44cd47f5ddf16cf8882b57ca7ae564014f805cb8a3c5e3137edd967862e5b5653d9e0dd
7
+ data.tar.gz: 77e3ed4c219a3a797f1f03b8fa663eea5bb954672824a5154610fe1012fe25c1c0a9981881b272e678ae1579ea32ced99566539bb5f016255d1b06f8815d1a2e
@@ -0,0 +1,264 @@
1
+
2
+ SHELL = /bin/sh
3
+
4
+ # V=0 quiet, V=1 verbose. other values don't work.
5
+ V = 0
6
+ Q1 = $(V:1=)
7
+ Q = $(Q1:0=@)
8
+ ECHO1 = $(V:1=@ :)
9
+ ECHO = $(ECHO1:0=@ echo)
10
+ NULLCMD = :
11
+
12
+ #### Start of system configuration section. ####
13
+
14
+ srcdir = .
15
+ topdir = /Users/vivektripathi/.rvm/rubies/ruby-2.5.1/include/ruby-2.5.0
16
+ hdrdir = $(topdir)
17
+ arch_hdrdir = /Users/vivektripathi/.rvm/rubies/ruby-2.5.1/include/ruby-2.5.0/x86_64-darwin18
18
+ PATH_SEPARATOR = :
19
+ VPATH = $(srcdir):$(arch_hdrdir)/ruby:$(hdrdir)/ruby
20
+ prefix = $(DESTDIR)/Users/vivektripathi/.rvm/rubies/ruby-2.5.1
21
+ rubysitearchprefix = $(rubylibprefix)/$(sitearch)
22
+ rubyarchprefix = $(rubylibprefix)/$(arch)
23
+ rubylibprefix = $(libdir)/$(RUBY_BASE_NAME)
24
+ exec_prefix = $(prefix)
25
+ vendorarchhdrdir = $(vendorhdrdir)/$(sitearch)
26
+ sitearchhdrdir = $(sitehdrdir)/$(sitearch)
27
+ rubyarchhdrdir = $(rubyhdrdir)/$(arch)
28
+ vendorhdrdir = $(rubyhdrdir)/vendor_ruby
29
+ sitehdrdir = $(rubyhdrdir)/site_ruby
30
+ rubyhdrdir = $(includedir)/$(RUBY_VERSION_NAME)
31
+ vendorarchdir = $(vendorlibdir)/$(sitearch)
32
+ vendorlibdir = $(vendordir)/$(ruby_version)
33
+ vendordir = $(rubylibprefix)/vendor_ruby
34
+ sitearchdir = $(sitelibdir)/$(sitearch)
35
+ sitelibdir = $(sitedir)/$(ruby_version)
36
+ sitedir = $(rubylibprefix)/site_ruby
37
+ rubyarchdir = $(rubylibdir)/$(arch)
38
+ rubylibdir = $(rubylibprefix)/$(ruby_version)
39
+ sitearchincludedir = $(includedir)/$(sitearch)
40
+ archincludedir = $(includedir)/$(arch)
41
+ sitearchlibdir = $(libdir)/$(sitearch)
42
+ archlibdir = $(libdir)/$(arch)
43
+ ridir = $(datarootdir)/$(RI_BASE_NAME)
44
+ mandir = $(datarootdir)/man
45
+ localedir = $(datarootdir)/locale
46
+ libdir = $(exec_prefix)/lib
47
+ psdir = $(docdir)
48
+ pdfdir = $(docdir)
49
+ dvidir = $(docdir)
50
+ htmldir = $(docdir)
51
+ infodir = $(datarootdir)/info
52
+ docdir = $(datarootdir)/doc/$(PACKAGE)
53
+ oldincludedir = $(SDKROOT)/usr/include
54
+ includedir = $(prefix)/include
55
+ localstatedir = $(prefix)/var
56
+ sharedstatedir = $(prefix)/com
57
+ sysconfdir = $(prefix)/etc
58
+ datadir = $(datarootdir)
59
+ datarootdir = $(prefix)/share
60
+ libexecdir = $(exec_prefix)/libexec
61
+ sbindir = $(exec_prefix)/sbin
62
+ bindir = $(exec_prefix)/bin
63
+ archdir = $(rubyarchdir)
64
+
65
+
66
+ CC = gcc
67
+ CXX = g++
68
+ LIBRUBY = $(LIBRUBY_SO)
69
+ LIBRUBY_A = lib$(RUBY_SO_NAME)-static.a
70
+ LIBRUBYARG_SHARED = -l$(RUBY_SO_NAME)
71
+ LIBRUBYARG_STATIC = -l$(RUBY_SO_NAME)-static -framework Foundation
72
+ empty =
73
+ OUTFLAG = -o $(empty)
74
+ COUTFLAG = -o $(empty)
75
+ CSRCFLAG = $(empty)
76
+
77
+ RUBY_EXTCONF_H =
78
+ cflags = $(optflags) $(debugflags) $(warnflags)
79
+ cxxflags = $(optflags) $(debugflags) $(warnflags)
80
+ optflags = -O3
81
+ debugflags = -ggdb3
82
+ warnflags = -Wall -Wextra -Wno-unused-parameter -Wno-parentheses -Wno-long-long -Wno-missing-field-initializers -Wno-tautological-compare -Wno-parentheses-equality -Wno-constant-logical-operand -Wno-self-assign -Wunused-variable -Wimplicit-int -Wpointer-arith -Wwrite-strings -Wdeclaration-after-statement -Wshorten-64-to-32 -Wimplicit-function-declaration -Wdivision-by-zero -Wdeprecated-declarations -Wextra-tokens
83
+ CCDLFLAGS = -fno-common
84
+ CFLAGS = $(CCDLFLAGS) $(cflags) -fno-common -pipe $(ARCH_FLAG)
85
+ INCFLAGS = -I. -I$(arch_hdrdir) -I$(hdrdir)/ruby/backward -I$(hdrdir) -I$(srcdir)
86
+ DEFS =
87
+ CPPFLAGS = -I/usr/local/opt/libyaml/include -I/usr/local/opt/libksba/include -I/usr/local/opt/readline/include -I/usr/local/opt/zlib/include -I/usr/local/opt/openssl@1.1/include -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE -D_DARWIN_UNLIMITED_SELECT -D_REENTRANT $(DEFS) $(cppflags)
88
+ CXXFLAGS = $(CCDLFLAGS) $(cxxflags) $(ARCH_FLAG)
89
+ ldflags = -L. -fstack-protector -L/usr/local/lib -L/usr/local/opt/libyaml/lib -L/usr/local/opt/libksba/lib -L/usr/local/opt/readline/lib -L/usr/local/opt/zlib/lib -L/usr/local/opt/openssl@1.1/lib
90
+ dldflags = -Wl,-undefined,dynamic_lookup -Wl,-multiply_defined,suppress -L/usr/local/opt/libyaml/lib -L/usr/local/opt/libksba/lib -L/usr/local/opt/readline/lib -L/usr/local/opt/zlib/lib -L/usr/local/opt/openssl@1.1/lib
91
+ ARCH_FLAG =
92
+ DLDFLAGS = $(ldflags) $(dldflags) $(ARCH_FLAG)
93
+ LDSHARED = $(CC) -dynamic -bundle
94
+ LDSHAREDXX = $(CXX) -dynamic -bundle
95
+ AR = libtool -static
96
+ EXEEXT =
97
+
98
+ RUBY_INSTALL_NAME = $(RUBY_BASE_NAME)
99
+ RUBY_SO_NAME = ruby.2.5.1
100
+ RUBYW_INSTALL_NAME =
101
+ RUBY_VERSION_NAME = $(RUBY_BASE_NAME)-$(ruby_version)
102
+ RUBYW_BASE_NAME = rubyw
103
+ RUBY_BASE_NAME = ruby
104
+
105
+ arch = x86_64-darwin18
106
+ sitearch = $(arch)
107
+ ruby_version = 2.5.0
108
+ ruby = $(bindir)/$(RUBY_BASE_NAME)
109
+ RUBY = $(ruby)
110
+ ruby_headers = $(hdrdir)/ruby.h $(hdrdir)/ruby/backward.h $(hdrdir)/ruby/ruby.h $(hdrdir)/ruby/defines.h $(hdrdir)/ruby/missing.h $(hdrdir)/ruby/intern.h $(hdrdir)/ruby/st.h $(hdrdir)/ruby/subst.h $(arch_hdrdir)/ruby/config.h
111
+
112
+ RM = rm -f
113
+ RM_RF = $(RUBY) -run -e rm -- -rf
114
+ RMDIRS = rmdir -p
115
+ MAKEDIRS = /usr/local/opt/coreutils/bin/gmkdir -p
116
+ INSTALL = /usr/local/opt/coreutils/bin/ginstall -c
117
+ INSTALL_PROG = $(INSTALL) -m 0755
118
+ INSTALL_DATA = $(INSTALL) -m 644
119
+ COPY = cp
120
+ TOUCH = exit >
121
+
122
+ #### End of system configuration section. ####
123
+
124
+ preload =
125
+ libpath = . $(libdir) /usr/local/opt/libyaml/lib /usr/local/opt/libksba/lib /usr/local/opt/readline/lib /usr/local/opt/zlib/lib /usr/local/opt/openssl@1.1/lib
126
+ LIBPATH = -L. -L$(libdir) -L/usr/local/opt/libyaml/lib -L/usr/local/opt/libksba/lib -L/usr/local/opt/readline/lib -L/usr/local/opt/zlib/lib -L/usr/local/opt/openssl@1.1/lib
127
+ DEFFILE =
128
+
129
+ CLEANFILES = mkmf.log
130
+ DISTCLEANFILES =
131
+ DISTCLEANDIRS =
132
+
133
+ extout =
134
+ extout_prefix =
135
+ target_prefix = /tarf_monte_carlo
136
+ LOCAL_LIBS =
137
+ LIBS = $(LIBRUBYARG_SHARED) -lpthread -ldl -lobjc
138
+ ORIG_SRCS = tarf_monte_carlo (1).c tarf_monte_carlo.c
139
+ SRCS = $(ORIG_SRCS)
140
+ OBJS = tarf_monte_carlo (1).o tarf_monte_carlo.o
141
+ HDRS =
142
+ LOCAL_HDRS =
143
+ TARGET = tarf_monte_carlo
144
+ TARGET_NAME = tarf_monte_carlo
145
+ TARGET_ENTRY = Init_$(TARGET_NAME)
146
+ DLLIB = $(TARGET).bundle
147
+ EXTSTATIC =
148
+ STATIC_LIB =
149
+
150
+ TIMESTAMP_DIR = .
151
+ BINDIR = $(bindir)
152
+ RUBYCOMMONDIR = $(sitedir)$(target_prefix)
153
+ RUBYLIBDIR = $(sitelibdir)$(target_prefix)
154
+ RUBYARCHDIR = $(sitearchdir)$(target_prefix)
155
+ HDRDIR = $(rubyhdrdir)/ruby$(target_prefix)
156
+ ARCHHDRDIR = $(rubyhdrdir)/$(arch)/ruby$(target_prefix)
157
+ TARGET_SO_DIR =
158
+ TARGET_SO = $(TARGET_SO_DIR)$(DLLIB)
159
+ CLEANLIBS = $(TARGET_SO)
160
+ CLEANOBJS = *.o *.bak
161
+
162
+ all: $(DLLIB)
163
+ static: $(STATIC_LIB)
164
+ .PHONY: all install static install-so install-rb
165
+ .PHONY: clean clean-so clean-static clean-rb
166
+
167
+ clean-static::
168
+ clean-rb-default::
169
+ clean-rb::
170
+ clean-so::
171
+ clean: clean-so clean-static clean-rb-default clean-rb
172
+ -$(Q)$(RM) $(CLEANLIBS) $(CLEANOBJS) $(CLEANFILES) .*.time
173
+
174
+ distclean-rb-default::
175
+ distclean-rb::
176
+ distclean-so::
177
+ distclean-static::
178
+ distclean: clean distclean-so distclean-static distclean-rb-default distclean-rb
179
+ -$(Q)$(RM) Makefile $(RUBY_EXTCONF_H) conftest.* mkmf.log
180
+ -$(Q)$(RM) core ruby$(EXEEXT) *~ $(DISTCLEANFILES)
181
+ -$(Q)$(RMDIRS) $(DISTCLEANDIRS) 2> /dev/null || true
182
+
183
+ realclean: distclean
184
+ install: install-so install-rb
185
+
186
+ install-so: $(DLLIB) $(TIMESTAMP_DIR)/.sitearchdir.-.tarf_monte_carlo.time
187
+ $(INSTALL_PROG) $(DLLIB) $(RUBYARCHDIR)
188
+ clean-static::
189
+ -$(Q)$(RM) $(STATIC_LIB)
190
+ install-rb: pre-install-rb do-install-rb install-rb-default
191
+ install-rb-default: pre-install-rb-default do-install-rb-default
192
+ pre-install-rb: Makefile
193
+ pre-install-rb-default: Makefile
194
+ do-install-rb:
195
+ do-install-rb-default:
196
+ pre-install-rb-default:
197
+ @$(NULLCMD)
198
+ $(TIMESTAMP_DIR)/.sitearchdir.-.tarf_monte_carlo.time:
199
+ $(Q) $(MAKEDIRS) $(@D) $(RUBYARCHDIR)
200
+ $(Q) $(TOUCH) $@
201
+
202
+ site-install: site-install-so site-install-rb
203
+ site-install-so: install-so
204
+ site-install-rb: install-rb
205
+
206
+ .SUFFIXES: .c .m .cc .mm .cxx .cpp .o .S
207
+
208
+ .cc.o:
209
+ $(ECHO) compiling $(<)
210
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $(CSRCFLAG)$<
211
+
212
+ .cc.S:
213
+ $(ECHO) translating $(<)
214
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -S $(CSRCFLAG)$<
215
+
216
+ .mm.o:
217
+ $(ECHO) compiling $(<)
218
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $(CSRCFLAG)$<
219
+
220
+ .mm.S:
221
+ $(ECHO) translating $(<)
222
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -S $(CSRCFLAG)$<
223
+
224
+ .cxx.o:
225
+ $(ECHO) compiling $(<)
226
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $(CSRCFLAG)$<
227
+
228
+ .cxx.S:
229
+ $(ECHO) translating $(<)
230
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -S $(CSRCFLAG)$<
231
+
232
+ .cpp.o:
233
+ $(ECHO) compiling $(<)
234
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $(CSRCFLAG)$<
235
+
236
+ .cpp.S:
237
+ $(ECHO) translating $(<)
238
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -S $(CSRCFLAG)$<
239
+
240
+ .c.o:
241
+ $(ECHO) compiling $(<)
242
+ $(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -c $(CSRCFLAG)$<
243
+
244
+ .c.S:
245
+ $(ECHO) translating $(<)
246
+ $(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -S $(CSRCFLAG)$<
247
+
248
+ .m.o:
249
+ $(ECHO) compiling $(<)
250
+ $(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -c $(CSRCFLAG)$<
251
+
252
+ .m.S:
253
+ $(ECHO) translating $(<)
254
+ $(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -S $(CSRCFLAG)$<
255
+
256
+ $(TARGET_SO): $(OBJS) Makefile
257
+ $(ECHO) linking shared-object tarf_monte_carlo/$(DLLIB)
258
+ -$(Q)$(RM) $(@)
259
+ $(Q) $(LDSHARED) -o $@ $(OBJS) $(LIBPATH) $(DLDFLAGS) $(LOCAL_LIBS) $(LIBS)
260
+ $(Q) $(POSTLINK)
261
+
262
+
263
+
264
+ $(OBJS): $(HDRS) $(ruby_headers)
@@ -41,6 +41,9 @@
41
41
  #define COLLAR_LEGS 11
42
42
  #define DOUBLE_STRIKE_LEGS 12
43
43
 
44
+ #define FX_AMERICAN_BARRIER_KNOCKIN_DISCRETE 13
45
+ #define FX_AMERICAN_BARRIER_KNOCKOUT_DISCRETE 14
46
+
44
47
  #define DATAPOINTS 200 // data for plotting
45
48
  #define INTERVAL 50
46
49
  #define SIM_LIMIT 196 // 196 + 4 = 200 simulations nedded
@@ -53,6 +56,9 @@ VALUE cTarfMonteCarlo = Qnil;
53
56
 
54
57
  // Prototype for the initialization method - Ruby calls this, not you
55
58
  void Init_tarf_monte_carlo();
59
+ double european_payoff(double, double, int, int, double);
60
+ double get_equivalent_notional(int, double, float);
61
+ double get_equivalent_rebate(int, double, float);
56
62
 
57
63
  // Prototype for our methods - methods are prefixed by 'method_' here
58
64
  VALUE method_box_muller( VALUE );
@@ -77,11 +83,25 @@ VALUE method_box_muller( VALUE self ) {
77
83
  return DBL2NUM(eps);
78
84
  }
79
85
 
86
+ double european_payoff(double strike, double spot, int cp_sign, int dir_sign, double notional){
87
+ double diff = (spot - strike) * cp_sign;
88
+ diff = diff > 0 ? diff : 0;
89
+ return (diff * notional * dir_sign);
90
+ }
91
+
92
+ double get_equivalent_notional(int conversion_sign, double notional, float rate){
93
+ return (conversion_sign == 1) ? (notional / rate) : notional
94
+ }
95
+
96
+ double get_equivalent_rebate(int conversion_sign, double rebate, float rate){
97
+ return (conversion_sign == 1) ? (rebate * rate) : rebate
98
+ }
99
+
80
100
  // main method for running monte carlo simulation from sidekiq worker/outside method
81
101
  VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
82
102
  VALUE MCInputs = rb_ary_shift(args);
83
103
  // rb_p(rb_str_new2("MC Inputs:"));
84
- rb_p(MCInputs);
104
+ // rb_p(MCInputs);
85
105
 
86
106
  // seed value for rand() function
87
107
  srand( time(0) );
@@ -99,6 +119,9 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
99
119
  double S = NUM2DBL( rb_hash_aref(MCInputs, rb_str_new2("spot_rate")) );
100
120
  int Ko_compare_mult = NUM2INT( rb_hash_aref(MCInputs, rb_str_new2("multiplier")) );
101
121
  int ConvertNotional = NUM2INT( rb_hash_aref(MCInputs, rb_str_new2("convert_notional")) );
122
+ int dir_sign = NUM2INT( rb_hash_aref(MCInputs, rb_str_new2("direction_sign")) );
123
+ int dir2_sign = NUM2INT( rb_hash_aref(MCInputs, rb_str_new2("direction2_sign")) );
124
+ int cp_sign = NUM2INT( rb_hash_aref(MCInputs, rb_str_new2("callput_sign")) );
102
125
 
103
126
  // assign leg specific attributes
104
127
  double *pvs_pos = ( double* ) malloc( SCount * sizeof(double) );
@@ -114,6 +137,9 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
114
137
  double *CFs_array = ( double* ) malloc( NL * sizeof(double) );
115
138
  double *LSts_array = ( double* ) malloc( NL * sizeof(double) );
116
139
  double *USts_array = ( double* ) malloc( NL * sizeof(double) );
140
+ double *TempNs_array = ( double* ) malloc( NL * sizeof(double) );
141
+ double *Barrier_array = ( double* ) malloc( NL * sizeof(double) );
142
+ double *Rebate_array = ( double* ) malloc( NL * sizeof(double) );
117
143
 
118
144
  VALUE Ls = rb_hash_aref(MCInputs, rb_str_new2("leverage_ratios") );
119
145
  VALUE Ts = rb_hash_aref(MCInputs, rb_str_new2("expiration_times") );
@@ -126,6 +152,8 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
126
152
  VALUE CFs = rb_hash_aref(MCInputs, rb_str_new2("caps") );
127
153
  VALUE LSts = rb_hash_aref(MCInputs, rb_str_new2("lower_strikes") );
128
154
  VALUE USts = rb_hash_aref(MCInputs, rb_str_new2("upper_strikes") );
155
+ VALUE Brs = rb_hash_aref(MCInputs, rb_str_new2("barriers") );
156
+ VALUE Rbts = rb_hash_aref(MCInputs, rb_str_new2("rebates") );
129
157
 
130
158
  for (leg = 0; leg < NL; ++leg) {
131
159
  Ls_array[leg] = NUM2DBL( rb_ary_entry(Ls, leg) );
@@ -135,6 +163,7 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
135
163
  vs_array[leg] = NUM2DBL( rb_ary_entry(vs, leg) );
136
164
  bs_array[leg] = NUM2DBL( rb_ary_entry(bs, leg) );
137
165
  DFs_array[leg] = NUM2DBL( rb_ary_entry(DFs, leg) );
166
+ TempNs_array[leg] = *(Ns_array + leg);
138
167
  }
139
168
  // extra tarf structures
140
169
  if ( KType == PIVOT_POINTS || KType == PIVOT_ABSOLUTE || KType == PIVOT_LEGS ) {
@@ -151,6 +180,11 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
151
180
  LSts_array[leg] = NUM2DBL( rb_ary_entry(LSts, leg) );
152
181
  USts_array[leg] = NUM2DBL( rb_ary_entry(USts, leg) );
153
182
  }
183
+ } else if (KType == FX_AMERICAN_BARRIER_KNOCKIN_DISCRETE || KType == FX_AMERICAN_BARRIER_KNOCKOUT_DISCRETE) {
184
+ for (leg = 0; leg < NL; ++leg) {
185
+ Barrier_array[leg] = NUM2DBL( rb_ary_entry(Brs, leg) );
186
+ Rebate_array[leg] = NUM2DBL( rb_ary_entry(Rbts, leg) );
187
+ }
154
188
  }
155
189
 
156
190
  //
@@ -182,6 +216,8 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
182
216
  sim_dash_neg[leg] = 0.0;
183
217
  }
184
218
 
219
+ int knockedLeg = -1;
220
+ int knockedLeg_dash = -1;
185
221
  // legs loop start
186
222
  for( leg = 0; leg < NL; ++leg ) {
187
223
  double eps, eps_dash, drift, vSqrdt, profit_loss, profit_loss_dash;
@@ -198,8 +234,20 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
198
234
  Spot = Spot * exp( drift + vSqrdt * eps );
199
235
  Spot_dash = Spot_dash * exp( drift + vSqrdt * eps_dash );
200
236
 
201
- // Payoff of Dual strike points is independent of direction
202
- if ( KType == DOUBLE_STRIKE_POINTS || KType == DOUBLE_STRIKE_ABSOLUTE || KType == DOUBLE_STRIKE_LEGS ) {
237
+ if(KType == FX_AMERICAN_BARRIER_KNOCKIN_DISCRETE || KType == FX_AMERICAN_BARRIER_KNOCKOUT_DISCRETE){
238
+ profit_loss = Spot;
239
+ profit_loss_dash = Spot_dash;
240
+
241
+ double knockin = (Spot - *(Barrier_array + leg)) * dir2_sign;
242
+ double knockin_dash = (Spot_dash - *(Barrier_array + leg)) * dir2_sign;
243
+
244
+ if(knockedLeg == -1 && knockin > 0){
245
+ knockedLeg = leg;
246
+ }
247
+ if(knockedLeg_dash == -1 && knockin_dash > 0){
248
+ knockedLeg_dash = leg;
249
+ }
250
+ } else if ( KType == DOUBLE_STRIKE_POINTS || KType == DOUBLE_STRIKE_ABSOLUTE || KType == DOUBLE_STRIKE_LEGS ) {
203
251
  if ( Spot < *( LSts_array + leg ) ) {
204
252
  profit_loss = Spot - (*( LSts_array + leg ));
205
253
  } else if(Spot > *( USts_array + leg )){
@@ -211,10 +259,10 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
211
259
  }
212
260
 
213
261
  if (ConvertNotional == 1){
214
- if(Spot < *( Ps_array + leg )){
215
- Ns_array[leg] = *(Ns_array + leg) / *( LSts_array + leg );
262
+ if(Spot < *( Ps_array + leg ) || Spot_dash < *( Ps_array + leg )){
263
+ Ns_array[leg] = *(TempNs_array + leg) / *( LSts_array + leg );
216
264
  } else {
217
- Ns_array[leg] = *(Ns_array + leg) / *( USts_array + leg );
265
+ Ns_array[leg] = *(TempNs_array + leg) / *( USts_array + leg );
218
266
  }
219
267
  }
220
268
 
@@ -246,6 +294,10 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
246
294
  profit_loss = 0.0;
247
295
  } else if(Spot <= *( CFs_array + leg )){
248
296
  profit_loss = Spot - (*( CFs_array + leg ));
297
+
298
+ if (ConvertNotional == 1){
299
+ Ns_array[leg] = *(TempNs_array + leg) / *( CFs_array + leg );
300
+ }
249
301
  } else {
250
302
  profit_loss = Spot - (*( Xs_array + leg ));
251
303
  }
@@ -254,6 +306,10 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
254
306
  profit_loss_dash = 0.0;
255
307
  } else if(Spot_dash <= *( CFs_array + leg )){
256
308
  profit_loss_dash = Spot_dash - (*( CFs_array + leg ));
309
+
310
+ if (ConvertNotional == 1){
311
+ Ns_array[leg] = *(TempNs_array + leg) / *( CFs_array + leg );
312
+ }
257
313
  } else {
258
314
  profit_loss_dash = Spot_dash - (*( Xs_array + leg ));
259
315
  }
@@ -281,6 +337,10 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
281
337
  profit_loss = 0.0;
282
338
  } else if(Spot >= *( CFs_array + leg )){
283
339
  profit_loss = ( ( *( CFs_array + leg ) ) - Spot );
340
+
341
+ if (ConvertNotional == 1){
342
+ Ns_array[leg] = *(TempNs_array + leg) / *( CFs_array + leg );
343
+ }
284
344
  } else {
285
345
  profit_loss = ( ( *( Xs_array + leg ) ) - Spot );
286
346
  }
@@ -289,6 +349,10 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
289
349
  profit_loss_dash = 0.0;
290
350
  } else if(Spot_dash >= *( CFs_array + leg )){
291
351
  profit_loss_dash = ( ( *( CFs_array + leg ) ) - Spot_dash );
352
+
353
+ if (ConvertNotional == 1){
354
+ Ns_array[leg] = *(TempNs_array + leg) / *( CFs_array + leg );
355
+ }
292
356
  } else {
293
357
  profit_loss_dash = ( ( *( Xs_array + leg ) ) - Spot_dash );
294
358
  }
@@ -322,7 +386,37 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
322
386
  // legs loop end
323
387
  // start from the Knock value
324
388
  double ko_so_far = K, ko_so_far_dash = K;
325
- if( KType == ABSOLUTE || KType == PIVOT_ABSOLUTE || KType == COLLAR_ABSOLUTE || KType == DOUBLE_STRIKE_ABSOLUTE ) {
389
+ if(KType == FX_AMERICAN_BARRIER_KNOCKIN_DISCRETE){
390
+ if(knockedLeg >= 0){
391
+ double equivalent_notional = get_equivalent_notional(conversion_sign, ( *( Ns_array + knockedLeg ) ), sim[NL-1]);
392
+ sim_pos[NL-1] = european_payoff(( *( Xs_array + (knockedLeg) ) ), sim[NL-1], cp_sign, dir_sign, equivalent_notional);
393
+ }
394
+ else{
395
+ sim_pos[NL-1] = get_equivalent_rebate(conversion_sign, ( *( Rebate_array + 0 ) ), sim[NL-1]);
396
+ }
397
+ if(knockedLeg_dash >= 0 ){
398
+ double equivalent_notional = get_equivalent_notional(conversion_sign, ( *( Ns_array + knockedLeg_dash ) ), sim_dash[NL-1]);
399
+ sim_dash_pos[NL-1] = european_payoff(( *( Xs_array + (knockedLeg_dash) ) ), sim_dash[NL-1], cp_sign, dir_sign, equivalent_notional);
400
+ }
401
+ else{
402
+ sim_dash_pos[NL-1] = get_equivalent_rebate(conversion_sign, ( *( Rebate_array + 0 ) ), sim_dash[NL-1]);
403
+ }
404
+ } else if(KType == FX_AMERICAN_BARRIER_KNOCKOUT_DISCRETE){
405
+ if(knockedLeg >= 0){
406
+ sim_pos[knockedLeg] = get_equivalent_rebate(conversion_sign, ( *( Rebate_array + knockedLeg ) ), sim[knockedLeg]);
407
+ }
408
+ else{
409
+ double equivalent_notional = get_equivalent_notional(conversion_sign, ( *( Ns_array + 0 ) ), sim[NL-1]);
410
+ sim_pos[NL-1] = european_payoff(( *( Xs_array + 0 ) ), sim[NL-1], cp_sign, dir_sign, equivalent_notional);
411
+ }
412
+ if(knockedLeg_dash >= 0 ){
413
+ sim_dash_pos[knockedLeg_dash] = get_equivalent_rebate(conversion_sign, ( *( Rebate_array + knockedLeg_dash ) ), sim[knockedLeg_dash]);
414
+ }
415
+ else{
416
+ double equivalent_notional = get_equivalent_notional(conversion_sign, ( *( Ns_array + 0 ) ), sim_dash[NL-1]);
417
+ sim_dash_pos[NL-1] = european_payoff(( *( Xs_array + 0 ) ), sim_dash[NL-1], cp_sign, dir_sign, equivalent_notional);
418
+ }
419
+ } else if( KType == ABSOLUTE || KType == PIVOT_ABSOLUTE || KType == COLLAR_ABSOLUTE || KType == DOUBLE_STRIKE_ABSOLUTE ) {
326
420
  for( leg = 0; leg < NL; ++leg ) {
327
421
  // simulation normal
328
422
  if( ko_so_far > 0.0 ) {
@@ -365,7 +459,7 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
365
459
  } else if( KType == POINTS || KType == PIVOT_POINTS || KType == COLLAR_POINTS || KType == DOUBLE_STRIKE_POINTS ) {
366
460
  for( leg = 0; leg < NL; ++leg ) {
367
461
  // simulation normal
368
- rb_p(DBL2NUM(*(Ns_array + leg )));
462
+ // rb_p(DBL2NUM(*(Ns_array + leg )));
369
463
  if ( ko_so_far > 0.0 ) {
370
464
  if( sim[leg] >= 0.0 ) {
371
465
  // knock out condition
@@ -529,4 +623,4 @@ void Init_tarf_monte_carlo() {
529
623
  // void rb_define_singleton_method(VALUE klass, const char *name, VALUE (*func)(), int argc)
530
624
  rb_define_singleton_method(cTarfMonteCarlo, "box_muller", method_box_muller, 0);
531
625
  rb_define_singleton_method(cTarfMonteCarlo, "run_monte_carlo", method_run_monte_carlo, -2);
532
- }
626
+ }
@@ -3,5 +3,5 @@
3
3
  # gem yank tarf_monte_carlo -v 2.3
4
4
 
5
5
  module TarfMonteCarlo
6
- VERSION = "3.23"
6
+ VERSION = "3.28"
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tarf_monte_carlo
3
3
  version: !ruby/object:Gem::Version
4
- version: '3.23'
4
+ version: '3.28'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vivek Routh
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-09-24 00:00:00.000000000 Z
11
+ date: 2020-10-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -114,6 +114,7 @@ files:
114
114
  - Rakefile
115
115
  - bin/console
116
116
  - bin/setup
117
+ - ext/tarf_monte_carlo/Makefile
117
118
  - ext/tarf_monte_carlo/extconf.rb
118
119
  - ext/tarf_monte_carlo/tarf_monte_carlo.c
119
120
  - lib/.DS_Store