-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcfuncs.pyx
More file actions
936 lines (693 loc) · 26.1 KB
/
Copy pathcfuncs.pyx
File metadata and controls
936 lines (693 loc) · 26.1 KB
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
# cython: language_level=3
cimport cython
import numpy as np
cimport numpy as np
from libc.stdlib cimport rand
from cython.parallel import prange
from libc.math cimport sqrt, fmax, exp
from libc.math cimport pow as fpow
cdef extern from "limits.h":
int INT_MAX
cdef roll():
return rand() / float(INT_MAX)
"""
cdef extern from "<algorithm>" namespace "std":
cdef int imax[int](int x, int y)
cdef int imin[int](int x, int y)
"""
DTYPE = np.float
ctypedef np.float_t DTYPE_t
ctypedef np.uint8_t uint8
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.nonecheck(False)
def calculate_profit_potential_stoploss(
DTYPE_t[::1] ask_history,
DTYPE_t[::1] bid_history,
DTYPE_t stop_loss,
DTYPE_t commission
):
cdef Py_ssize_t n = ask_history.shape[0]
cdef Py_ssize_t i
cdef Py_ssize_t j
cdef Py_ssize_t ci
cdef Py_ssize_t m
cdef DTYPE_t p_long_
cdef DTYPE_t p_short_
cdef bint stop_long_hit
cdef bint stop_short_hit
cdef DTYPE_t a
cdef DTYPE_t b
cdef DTYPE_t a0
cdef DTYPE_t b0
cdef DTYPE_t pos_value
p_long = np.zeros(n, dtype=DTYPE)
p_short = np.zeros(n, dtype=DTYPE)
cdef DTYPE_t[::1] p_long_view = p_long
cdef DTYPE_t[::1] p_short_view = p_short
# outer loop
for i in prange(n, nogil=True, num_threads=8):
stop_long_hit = False
stop_short_hit = False
p_long_ = 0.0
p_short_ = 0.0
# the price at the opening of the position
a0 = ask_history[i]
b0 = bid_history[i]
m = n - i - 1
# inner loop from current tick to end of week
for j in range(m):
ci = i + j
# the current price
a = ask_history[ci]
b = bid_history[ci]
if not stop_long_hit:
pos_value = a - b0 - commission
p_long_ = max(pos_value, p_long_)
stop_long_hit = pos_value < - stop_loss
if not stop_short_hit:
pos_value = a0 - b - commission
p_short_ = max(pos_value, p_short_)
stop_short_hit = pos_value < - stop_loss
if stop_long_hit and stop_short_hit:
break
# accumulate p's
p_long_view[i] = p_long_
p_short_view[i] = p_short_
return p_long, p_short
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.nonecheck(False)
def calculate_profit_potential_stoploss_takeprofit(
DTYPE_t[::1] ask_history,
DTYPE_t[::1] bid_history,
DTYPE_t stop_loss,
DTYPE_t take_profit,
DTYPE_t commission
):
cdef Py_ssize_t n = ask_history.shape[0]
cdef Py_ssize_t i
cdef Py_ssize_t j
cdef Py_ssize_t ci
cdef Py_ssize_t m
cdef DTYPE_t p_long_
cdef DTYPE_t p_short_
cdef bint stop_long_hit
cdef bint stop_short_hit
cdef DTYPE_t a
cdef DTYPE_t b
cdef DTYPE_t a0
cdef DTYPE_t b0
cdef DTYPE_t pos_value
p_long = np.zeros(n, dtype=DTYPE)
p_short = np.zeros(n, dtype=DTYPE)
cdef DTYPE_t[::1] p_long_view = p_long
cdef DTYPE_t[::1] p_short_view = p_short
# outer loop
for i in prange(n, nogil=True, num_threads=8):
stop_long_hit = False
stop_short_hit = False
p_long_ = 0.0
p_short_ = 0.0
# the price at the opening of the position
a0 = ask_history[i]
b0 = bid_history[i]
m = n - i - 1
# inner loop from current tick to end of week
for j in range(m):
ci = i + j
# the current price
a = ask_history[ci]
b = bid_history[ci]
if not stop_long_hit:
pos_value = a - b0 - commission
p_long_ = max(pos_value, p_long_)
stop_long_hit = (pos_value < - stop_loss
or pos_value > take_profit)
if not stop_short_hit:
pos_value = a0 - b - commission
p_short_ = max(pos_value, p_short_)
stop_short_hit = (pos_value < - stop_loss
or pos_value > take_profit)
if stop_long_hit and stop_short_hit:
break
# accumulate p's
if p_long_ >= take_profit:
p_long_view[i] = p_long_
if p_short_ >= take_profit:
p_short_view[i] = p_short_
return p_long, p_short
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.nonecheck(False)
def regular_causal_conv(
DTYPE_t[::1] signal,
DTYPE_t[::1] kernel
):
cdef Py_ssize_t n = signal.shape[0]
cdef Py_ssize_t k = kernel.shape[0]
cdef Py_ssize_t i
cdef Py_ssize_t j
cdef Py_ssize_t pad = 1
cdef DTYPE_t accum
result = np.zeros(n, dtype=DTYPE)
cdef DTYPE_t[::1] result_view = result
# outer loop over signal
for i in range(n - k):
accum = 0.0
# inner loop over kernel elements
for j in range(k):
accum += kernel[k - j - pad] * signal[i + k - j]
result_view[i + k] = accum
return result
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.nonecheck(False)
@cython.cdivision(True)
def regular_sma(
DTYPE_t[::1] signal,
Py_ssize_t w
):
""" w is the window size """
cdef Py_ssize_t i
cdef Py_ssize_t n = signal.size
cdef DTYPE_t buffer = 0.0
result = np.zeros(n)
cdef DTYPE_t[::1] result_view = result
for i in range(n):
buffer += signal[i]
if i >= w:
buffer -= signal[i - w]
result_view[i] = buffer / w
return result
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.nonecheck(False)
def regular_ema(
DTYPE_t[::1] signal,
DTYPE_t N
):
""" N is the number of days in EMA. """
cdef DTYPE_t beta = 2 / (N + 1)
cdef DTYPE_t one_minus_beta = 1.0 - beta
cdef Py_ssize_t i
cdef Py_ssize_t n = signal.size
result = np.zeros(n)
cdef DTYPE_t[::1] result_view = result
result_view[0] = signal[0]
for i in range(n - 1):
result_view[i + 1] = signal[i + 1] * beta + result_view[i] * one_minus_beta
return result
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.nonecheck(False)
@cython.cdivision(True)
def regular_bbands(
DTYPE_t[::1] signal,
DTYPE_t N,
Py_ssize_t std_w
):
""" returns (ema(X, N), std(X, std_w)) where X is a regular time series """
cdef DTYPE_t beta = 2 / (N + 1)
cdef DTYPE_t one_minus_beta = 1.0 - beta
cdef DTYPE_t buffer = 0.0
cdef Py_ssize_t n = signal.size
cdef Py_ssize_t i
std = np.zeros(n)
mean = np.zeros(n)
cdef DTYPE_t[::1] std_view = std
cdef DTYPE_t[::1] mean_view = mean
mean_view[0] = signal[0]
for i in range(n - 1):
# calculate mean
mean_view[i + 1] = signal[i + 1] * beta + mean_view[i] * one_minus_beta
# calculate std
# TODO - implement subtract last, add next moving average calc
buffer += (mean_view[i + 1] - signal[i + 1]) ** 2
if i + 1 >= std_w:
buffer -= (mean_view[i + 1 - std_w] - signal[i + 1 - std_w]) ** 2
std_view[i + 1] = sqrt(buffer / std_w)
return mean, std
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.nonecheck(False)
@cython.cdivision(True)
def irregular_sma_eq(
DTYPE_t[::1] signal,
DTYPE_t [::1] times,
Py_ssize_t tau
):
"""
https://pdfs.semanticscholar.org/882e/93570eae184ae737bf0344cb50a2925e353d.pdf
left = 1; roll_sum = 0;
for (right in 1:N(X)) {
// Expand window on right end
roll_sum = roll_sum + values[right];
// Shrink window on left end
while (times[left] <= times[right] - tau) {
roll_sum = roll_sum - values[left];
left = left + 1;
}
// Save SMA value for current time window
out[right] = roll_sum / (right - left + 1);
}
"""
cdef Py_ssize_t n = signal.size
cdef Py_ssize_t right
cdef Py_ssize_t left = 0
cdef DTYPE_t roll_sum = 0.0
result = np.zeros(n)
cdef DTYPE_t[::1] result_view = result
for right in range(n):
roll_sum += signal[right]
while times[left] <= (times[right] - tau):
roll_sum -= signal[left]
left += 1
result_view[right] = roll_sum / (right - left + 1)
return result
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.nonecheck(False)
@cython.cdivision(True)
def irregular_sma(
DTYPE_t[::1] signal,
DTYPE_t [::1] times,
DTYPE_t tau
):
"""
https://pdfs.semanticscholar.org/882e/93570eae184ae737bf0344cb50a2925e353d.pdf
left = 1; roll_area = left_area = values[1] * tau; out[1] = values[1];
for (right in 2:N(X)) {
// Expand interval on right end
roll_area = roll_area + values[right-1] * (times[right] - times[right-1]);
// Remove truncated area on left end
roll_area = roll_area - left_area;
// Shrink interval on left end
t_left_new = times[right] - tau;
while (times[left] <= t_left_new) {
roll_area = roll_area - values[left] * (times[left+1] - times[left]);
left = left + 1;
}
// Add truncated area on left end
left_area = values[max(1, left-1)] * (times[left] - t_left_new)
roll_area = roll_area + left_area;
// Save SMA value for current time window
out[right] = roll_area / tau;
}
"""
cdef Py_ssize_t n = signal.size
cdef Py_ssize_t right
cdef Py_ssize_t left = 0
cdef DTYPE_t roll_area = signal[0] * tau
cdef DTYPE_t left_area = signal[0] * tau
cdef DTYPE_t roll_sum = 0.0
cdef DTYPE_t t_left_new
result = np.zeros(n)
cdef DTYPE_t[::1] result_view = result
result_view[0] = signal[0]
for right in np.arange(1, n):
roll_area += signal[right - 1] * (times[right] - times[right - 1])
roll_area -= left_area
t_left_new = times[right] - tau
while (times[left] <= t_left_new):
roll_area -= signal[left] * (times[left + 1] - times[left])
left += 1
left_area = signal[max(1, left - 1)] * (times[left] - t_left_new)
roll_area += left_area
result_view[right] = roll_area / tau
return result
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.nonecheck(False)
@cython.cdivision(True)
def irregular_ema(
DTYPE_t[::1] signal,
DTYPE_t[::1] times,
DTYPE_t tau
):
cdef Py_ssize_t n = signal.size
cdef Py_ssize_t i
cdef DTYPE_t w
result = np.zeros(n)
cdef DTYPE_t[::1] result_view = result
result_view[0] = signal[0]
for i in np.arange(1, n):
w = exp( - (times[i] - times[i - 1]) / tau )
result_view[i] = result_view[i - 1] * w + signal[i - 1] * (1 - w)
return result
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.nonecheck(False)
@cython.cdivision(True)
def irregular_max(
DTYPE_t[::1] signal,
DTYPE_t[::1] times,
DTYPE_t tau
):
cdef Py_ssize_t left = 0
cdef Py_ssize_t max_pos = 0
cdef Py_ssize_t max_pos_
cdef Py_ssize_t i
cdef Py_ssize_t k
cdef Py_ssize_t n = signal.size
result = np.zeros(n)
cdef DTYPE_t[::1] result_view = result
for right in range(n):
if signal[right] >= signal[max_pos]:
max_pos = right
while times[left] <= (times[right] - tau):
left += 1
if max_pos < left:
max_pos_ = left
for k in range(right - left):
if signal[left + k] > signal[max_pos_]:
max_pos_ = left + k
max_pos = max_pos_
result_view[right] = signal[max_pos]
return result
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.nonecheck(False)
@cython.cdivision(True)
def irregular_min(
DTYPE_t[::1] signal,
DTYPE_t[::1] times,
DTYPE_t tau
):
cdef Py_ssize_t left = 0
cdef Py_ssize_t min_pos = 0
cdef Py_ssize_t min_pos_
cdef Py_ssize_t i
cdef Py_ssize_t k
cdef Py_ssize_t n = signal.size
result = np.zeros(n)
cdef DTYPE_t[::1] result_view = result
for right in range(n):
if signal[right] <= signal[min_pos]:
min_pos = right
while times[left] <= (times[right] - tau):
left += 1
if min_pos < left:
min_pos_ = left
for k in range(right - left):
if signal[left + k] < signal[min_pos_]:
min_pos_ = left + k
min_pos = min_pos_
result_view[right] = signal[min_pos]
return result
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.nonecheck(False)
@cython.cdivision(True)
def irregular_minmax(
DTYPE_t[::1] signal,
DTYPE_t[::1] times,
DTYPE_t tau
):
cdef Py_ssize_t left = 0
cdef Py_ssize_t min_pos = 0
cdef Py_ssize_t min_pos_
cdef Py_ssize_t max_pos = 0
cdef Py_ssize_t max_pos_
cdef Py_ssize_t i
cdef Py_ssize_t k
cdef Py_ssize_t n = signal.size
mins = np.zeros(n)
maxes = np.zeros(n)
cdef DTYPE_t[::1] mins_view = mins
cdef DTYPE_t[::1] maxes_view = maxes
for right in range(n):
if signal[right] >= signal[max_pos]:
max_pos = right
if signal[right] <= signal[min_pos]:
min_pos = right
while times[left] <= (times[right] - tau):
left += 1
if max_pos < left:
max_pos_ = left
for k in range(right - left):
if signal[left + k] > signal[max_pos_]:
max_pos_ = left + k
max_pos = max_pos_
if min_pos < left:
min_pos_ = left
for k in range(right - left):
if signal[left + k] < signal[min_pos_]:
min_pos_ = left + k
min_pos = min_pos_
mins_view[right] = signal[min_pos]
maxes_view[right] = signal[max_pos]
return mins, maxes
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.nonecheck(False)
@cython.cdivision(True)
def irregular_roc(
DTYPE_t[::1] signal,
DTYPE_t[::1] times
):
cdef Py_ssize_t i
cdef Py_ssize_t n = signal.size
result = np.zeros(n)
cdef DTYPE_t[::1] result_view = result
result_view[0] = 0
for i in np.arange(1, n):
result_view[i] = (signal[i] - signal[i - 1]) / (times[i] - times[i - 1])
return result
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.nonecheck(False)
@cython.cdivision(True)
def square_signal(
DTYPE_t[::1] signal
):
"""
https://pdfs.semanticscholar.org/882e/93570eae184ae737bf0344cb50a2925e353d.pdf
sigma^2 = SMA(X^2, tau) - SMA(X, tau)^2
"""
cdef Py_ssize_t n = signal.size
cdef Py_ssize_t i
result = np.zeros(n)
cdef DTYPE_t[::1] result_view = result
result_view[0] = signal[0]
for i in range(n):
result_view[i] = fpow(signal[i], 2.0)
return result
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.nonecheck(False)
@cython.cdivision(True)
def backtest_simple_bbands_strategy(
DTYPE_t[::1] ask,
DTYPE_t[::1] bid,
DTYPE_t[::1] sma,
DTYPE_t[::1] std,
DTYPE_t bband_multiplier,
DTYPE_t vrr,
DTYPE_t pnl
):
cdef bint long_open = False
cdef bint short_open = False
cdef DTYPE_t position_value = 0.0
cdef DTYPE_t bband_higher
cdef DTYPE_t bband_lower
cdef DTYPE_t v_range
cdef DTYPE_t take_profit
cdef DTYPE_t stop_loss
cdef Py_ssize_t i
cdef Py_ssize_t n = ask.size
cdef Py_ssize_t trade_counter = 0
cdef Py_ssize_t opened_counter = 0
cdef Py_ssize_t closed_counter = 0
closed_value = np.zeros(np.round(len(ask) // 100))
opened_value = np.zeros(np.round(len(ask) // 100))
times_opened = np.zeros(np.round(len(ask) // 100))
times_closed = np.zeros(np.round(len(ask) // 100))
position_type = np.zeros(np.round(len(ask) // 100), dtype=np.uint8)
cdef DTYPE_t[::1] closed_view = closed_value
cdef DTYPE_t[::1] opened_view = opened_value
cdef DTYPE_t[::1] times_opened_view = times_opened
cdef DTYPE_t[::1] times_closed_view = times_closed
cdef uint8[::1] position_type_view = position_type
for i in range(n):
v_range = std[i] * bband_multiplier
bband_higher = sma[i] + v_range
bband_lower = sma[i] - v_range
if not (long_open or short_open):
# short pos
if ask[i] > bband_higher:
short_open = True
position_value = bid[i]
opened_view[trade_counter] = position_value
times_opened_view[trade_counter] = i
position_type_view[trade_counter] = 2
take_profit = v_range * vrr
stop_loss = v_range * pnl * vrr
# long pos
if bid[i] < bband_lower:
long_open = True
position_value = ask[i]
opened_view[trade_counter] = position_value
times_opened_view[trade_counter] = i
position_type_view[trade_counter] = 1
take_profit = v_range * vrr
stop_loss = v_range * pnl * vrr
elif short_open:
if (position_value - bid[i] >= take_profit
or position_value - bid[i] <= - stop_loss):
short_open = False
closed_view[trade_counter] = position_value - bid[i]
times_closed_view[trade_counter] = i
trade_counter += 1
elif long_open:
if (ask[i] - position_value >= take_profit
or ask[i] - position_value <= - stop_loss):
long_open = False
closed_view[trade_counter] = ask[i] - position_value
times_closed_view[trade_counter] = i
trade_counter += 1
return position_type, times_opened, times_closed, opened_value, closed_value
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.nonecheck(False)
@cython.cdivision(True)
def backtest_turnback_bbands_strategy(
DTYPE_t[::1] ask,
DTYPE_t[::1] bid,
DTYPE_t[::1] typ,
DTYPE_t[::1] sma,
DTYPE_t[::1] std,
DTYPE_t bband_multiplier,
DTYPE_t take_profit,
DTYPE_t stop_loss
):
""" typ is a smoothed typical price. it is tracked. if it goes outside the
bband and comes back, a position is opened. """
cdef bint long_open = False
cdef bint short_open = False
cdef bint short_pending = False
cdef bint long_pending = False
cdef DTYPE_t position_value = 0.0
cdef DTYPE_t bband_higher
cdef DTYPE_t bband_lower
cdef DTYPE_t v_range
cdef Py_ssize_t i
cdef Py_ssize_t n = ask.size
cdef Py_ssize_t trade_counter = 0
cdef Py_ssize_t opened_counter = 0
cdef Py_ssize_t closed_counter = 0
closed_value = np.zeros(np.round(len(ask) // 100))
opened_value = np.zeros(np.round(len(ask) // 100))
times_opened = np.zeros(np.round(len(ask) // 100))
times_closed = np.zeros(np.round(len(ask) // 100))
position_type = np.zeros(np.round(len(ask) // 100), dtype=np.uint8)
cdef DTYPE_t[::1] closed_view = closed_value
cdef DTYPE_t[::1] opened_view = opened_value
cdef DTYPE_t[::1] times_opened_view = times_opened
cdef DTYPE_t[::1] times_closed_view = times_closed
cdef uint8[::1] position_type_view = position_type
for i in range(n):
v_range = std[i] * bband_multiplier
bband_higher = sma[i] + v_range
bband_lower = sma[i] - v_range
if not (long_open or short_open):
# bband pierced upwards
if (not short_pending and
typ[i] > bband_higher):
short_pending = True
# bband pierced downwards
if (not long_pending and
typ[i] < bband_lower):
long_pending = True
# short pos
if (short_pending and
typ[i] < bband_higher):
short_open = True
short_pending = False
position_value = bid[i]
opened_view[trade_counter] = position_value
times_opened_view[trade_counter] = i
position_type_view[trade_counter] = 2
# long pos
if (long_pending and
bid[i] > bband_lower):
long_open = True
long_pending = False
position_value = ask[i]
opened_view[trade_counter] = position_value
times_opened_view[trade_counter] = i
position_type_view[trade_counter] = 1
elif short_open:
if (position_value - bid[i] >= take_profit
or position_value - bid[i] <= - stop_loss):
short_open = False
closed_view[trade_counter] = position_value - bid[i]
times_closed_view[trade_counter] = i
trade_counter += 1
elif long_open:
if (ask[i] - position_value >= take_profit
or ask[i] - position_value <= - stop_loss):
long_open = False
closed_view[trade_counter] = ask[i] - position_value
times_closed_view[trade_counter] = i
trade_counter += 1
return position_type, times_opened, times_closed, opened_value, closed_value
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.nonecheck(False)
@cython.cdivision(True)
def backtest_sma_crossover_strategy(
DTYPE_t[::1] ask,
DTYPE_t[::1] bid,
DTYPE_t[::1] sma_slow,
DTYPE_t[::1] sma_fast
):
cdef bint regime_long = False
cdef bint regime_long_new = False
cdef DTYPE_t position_value = 0.0
cdef Py_ssize_t i
cdef Py_ssize_t n = ask.size
cdef Py_ssize_t trade_counter = 0
closed_value = np.zeros(np.round(len(ask) / 100))
opened_value = np.zeros(np.round(len(ask) / 100))
times_opened = np.zeros(np.round(len(ask) // 100))
times_closed = np.zeros(np.round(len(ask) // 100))
position_type = np.zeros(np.round(len(ask) // 100), dtype=np.uint8)
cdef DTYPE_t[::1] closed_view = closed_value
cdef DTYPE_t[::1] opened_view = opened_value
cdef DTYPE_t[::1] times_opened_view = times_opened
cdef DTYPE_t[::1] times_closed_view = times_closed
cdef uint8[::1] position_type_view = position_type
regime_long = sma_fast[0] > sma_slow[0]
for i in range(n):
regime_long_new = sma_fast[i] < sma_slow[i]
# crossover
if regime_long_new != regime_long:
# open long, close short
if regime_long_new:
# open new pos
position_value_new = ask[i]
opened_view[trade_counter] = position_value_new
times_opened_view[trade_counter] = i
position_type_view[trade_counter] = 1
# close old short if exists
if trade_counter > 0:
closed_view[trade_counter] = position_value - bid[i]
times_closed_view[trade_counter] = i
position_value = position_value_new
trade_counter += 1
# open short, close long
else:
# open new pos
position_value_new = bid[i]
opened_view[trade_counter] = position_value_new
times_opened_view[trade_counter] = i
position_type_view[trade_counter] = 2
# close old long if exists
if trade_counter > 0:
closed_view[trade_counter] = ask[i] - position_value
times_closed_view[trade_counter] = i
position_value = position_value_new
trade_counter += 1
regime_long = regime_long_new
return position_type, times_opened, times_closed, opened_value, closed_value