Advertisement
z1rix

All .pyx file

Feb 9th, 2023 (edited)
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.50 KB | Software | 0 0
  1. # cython: language_level=3
  2. # cython: profile=True
  3.  
  4. # cythonize -a -b test_cy.pyx
  5. # python setup.py build_ext --inplace
  6.  
  7. import numpy as np
  8. cimport cython
  9. cimport numpy as np
  10. np.import_array()
  11.  
  12.  
  13. @cython.wraparound(False)
  14. @cython.boundscheck(False)
  15. @cython.cdivision(True)
  16. cdef tp_sl_back_test(double[:, :] data, double tp, double sl):
  17.  
  18.     cdef double balance = 100
  19.     cdef double balance_copy
  20.  
  21.     cdef Py_ssize_t  i
  22.  
  23.     cdef int right = 0
  24.     cdef int total = 0
  25.  
  26.     cdef double entry_price
  27.     cdef double close_price
  28.     cdef double high_price
  29.     cdef double low_price
  30.  
  31.     cdef double tp_price
  32.     cdef double sl_price
  33.  
  34.     for i in xrange(data.shape[0]):
  35.  
  36.         balance_copy = balance
  37.  
  38.         entry_price = data[i, 0]
  39.         high_price = data[i, 1]
  40.         low_price = data[i, 2]
  41.         close_price = data[i, 3]
  42.  
  43.         tp_price = entry_price + ((entry_price * 0.01) * tp)
  44.         sl_price = entry_price - ((entry_price * 0.01) * sl)
  45.  
  46.         if (sl_price < low_price) and (tp_price > high_price):
  47.             pass
  48.  
  49.         elif sl_price >= low_price:
  50.             close_price = sl_price
  51.  
  52.         elif tp_price <= high_price:
  53.             close_price = tp_price
  54.  
  55.         else:
  56.             close_price = sl_price
  57.  
  58.         balance *= 0.9996
  59.         balance *= close_price / entry_price
  60.         balance *= 0.9996
  61.  
  62.         if balance_copy < balance:
  63.             right += 1
  64.             total += 1
  65.         else:
  66.             total += 1
  67.  
  68.     return balance, right, total
  69.  
  70.  
  71. @cython.wraparound(False)
  72. @cython.boundscheck(False)
  73. cpdef tp_sl_loop(np.ndarray[np.float64_t, ndim=2] data_np, np.ndarray[np.float64_t, ndim=2] tp_sl_mash, str pair):
  74.  
  75.     cdef int n = tp_sl_mash.shape[0]
  76.     cdef int i
  77.     cdef int right
  78.     cdef int total
  79.  
  80.     cdef double[:, ::1] data = np.ascontiguousarray(data_np)
  81.  
  82.     cdef double tp
  83.     cdef double sl
  84.     cdef double balance
  85.  
  86.     cdef np.ndarray[np.float64_t, ndim=1] X = np.zeros(n)
  87.     cdef np.ndarray[np.float64_t, ndim=1] Y = np.zeros(n)
  88.     cdef np.ndarray[np.float64_t, ndim=1] Z = np.zeros(n)
  89.     cdef np.ndarray[np.float64_t, ndim=2] tp_sl_list = np.zeros((n, 3))
  90.  
  91.     for i in xrange(n):
  92.         tp = tp_sl_mash[i, 0]
  93.         sl = tp_sl_mash[i, 1]
  94.  
  95.         balance, right, total = tp_sl_back_test(data, tp, sl)
  96.  
  97.         X[i] = tp * 100
  98.         Y[i] = sl * 100
  99.         Z[i] = balance
  100.  
  101.         tp_sl_list[i, 0] = tp
  102.         tp_sl_list[i, 1] = sl
  103.         tp_sl_list[i, 2] = balance
  104.  
  105.     return X, Y, Z, tp_sl_list
  106.  
Tags: python cythom
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement