numpy array of symbols and the quantization function

Calculation of net turnover

Key_Function:

np.sign (array): Returns the sign of each element in the array of

np.piecewise (array, list of conditions, the returned list): The condition list element in the array element index determination condition list, taking the index corresponding to the returned list, as the return value, the return value consisting of these a new array

Code:

Import numpy AS NP
 Import matplotlib.pyplot AS PLT 

C, V = np.loadtxt ( ' BHP.csv ' , DELIMITER = ' , ' , usecols = (6, 7 are), the unpack = True)
 # C day closing price, v is the daily volume 
'' ' 
C 
[93.3 93.72 95.64 94.56 93.93 92.39 92.11 92.36 91.76 93.91 
  94.6 93.27 94.43 96.02 95.76 94.47 94.34 92.22 88.31 89.59 
  89.02 86.95 84.88 87.38 88.56 89.59 88.71 90.02 91.26 90.67] 
v 
[1741900. 2620800. 2.4613 million. 3270900. 2650200. 4667300. 5359800. 
  7768400. 4799100. 3448300. 4719800. 3898900. 3,727,700.3379400.
  2463900. 3590900. 3805000. 3271700. 5507800. 2996800. 3434800. 
  5008300. 7809799. 3947100. 3809700. 3098200. 3500200. 4285600. 
  3918800. 3632200.] 

'' ' 
# calculates the daily closing price difference 
Change = np.diff ( C)
 Print (Change)
 '' ' 
[1.92 -1.08 -1.26 0.63 -1.54 -0.28 0.25 2.15 0.69 -1.33 -0.6 1.16 
  1.59 -0.26 -1.29 -0.13 -2.12 -3.91 -0.57 -2.07 -2.07 1.28 2.5 1.18 
  1.03 - 1.31 1.24 -0.59 0.88] 
'' ' 

Signs = np.sign (Change)
 Print (Signs)
 ' '' 
[1. -1. -1. 1. -1. -1. 1. -1. 1.. 1. -1 1 1 -1 -1 -1 -1 -1 
  1 -1 -1 -1 -1 1. 1. 1. 1.1. -1.]
 '' ' 
# by determining the array element meets the condition,Return Value Returns meet the conditions
= np.piecewise Pieces (Change, [Change <0, Change> 0], [-1,. 1 ])
 Print (Pieces)
 '' ' 
[1. -1. -1. 1. -1. -1.. 1 -1. 1. 1. 1. 1 1 -1 -1 -1 -1 -1 
  1 -1 -1 -1 1. 1. 1. 1. -1 1. 1..] 
'' ' 

Print (np.array_equal (Signs, Pieces))
 # True 

# calculated value OBV OBV value, is the day volume, but depends on the closing price of positive and negative change amount 
Print ( V [. 1:] * Signs)
 '' ' 
[-2.4613 million -3.2709 million 2650200. 2620800. 7768400. -4.6673 million -5.3598 million.... 
 -4.7991 million 3448300. 4719800. 3727700. 3379400. -2.4639 million -3.8989 million...  
 -3590900. -3.805 million. -3271700. -5507800. 2996800. -3434800. -5,008,300.
 -7809799. 3947100. 3809700. 3098200. -3500200. 4285600. 3918800. 
 -3632200.] 
'' '

 

Trading Process Simulation

Key_Function:

np.vectorize function: function map corresponding to the python, the vectorization function, i.e., into the input parameters from a single element vector

  Avoiding the use of looping through each element of the array

ndarray objects can be filled condition [], the elemental filtration

def myfunc(a, b):
    "Return a-b if a>b, otherwise return a+b"
    if a > b:
        return a - b
    else:
        return a + b
    

vfunc = np.vectorize(myfunc)

vfunc([1, 2, 3, 4], 2)
# array([3, 4, 1, 2])
       

Code:

Import numpy AS NP
 Import matplotlib.pyplot AS PLT 

O, H, L, C = np.loadtxt ( ' BHP.csv ' , DELIMITER = ' , ' , usecols = (3,4,5,6), the unpack = True)
 # open, high, low, close 

DEF calc_profit (Open, High, Low, use close):
     # at prices slightly lower than the opening price to buy 
    buy Open * = float (0.999 )
     # day price range 
    IF Low < Buy < High:
         return (Close - Buy) / Buy      # calculating profit day opposite 
    the else :
         return 0 
    
#np.vectorize () function to the quantized 
= np.vectorize (calc_profit) FUNC     # The four input array, output array into a 

Profits = FUNC (O, H, L, C)
 Print (Profits)
 '' ' 
[0.00755895 0.0123267 .00154302 of 0. The 0.00780612 0.0021668 
 -0.01006869 -0.00121617 -0.01774473 0.00568316 -0.00614746 .00560552 
  .00675817 .00225356 -0.00274807 -0.02015786 0.00762307 -0.00675369 
 -0.00675957 -0.01558377 of 0. The .01065112 .02904986 .00168882 
 -0.0098442 0.00540779 0.00376864 -0.00499634 -0.00783465 .00603003] 
'' ' 
    
real_trades = Profits [ Profits! = 0]      # filtered off elements equal to 0 
Print (len (real_trades),"   " , Round (100.0 * len (real_trades) / len (C), 2), " % " )
 # 28 93.33% 
# array element corresponding division division 

Print (round (np.mean (real_trades) * 100, 2)) # 2 represents taking two significant figures 
# 0.02 

winning_trades = Profits [Profits> 0]
 Print (len (winning_trades), round (100.0 * len (winning_trades) / len (C), 2), " % " )
 # 53.33% 16 
Print (round (np.mean (winning_trades) * 100, 2 ))
 # 0.72 


losing_trades = Profits [Profits < 0]
 Print(len(losing_trades), round(100.0 * len(losing_trades)/len(c), 2), "%")
# 12 40.0 %
print(round(np.mean(losing_trades) * 100, 2))
# -0.92

 

Guess you like

Origin www.cnblogs.com/draven123/p/11392021.html