Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
1042 views


)�\Y`2�@s�ddlZddlZddlZddlZddlZddljZddl	m
Z
ddlmZm
Z
mZmZmZddlmZddlmZdd�Zddd	�Zd
dd�Zd
d�Zdd�Zdd�Zddddd�Zdd�Zdd�Zdd�Zdd�Zdd�Z d d!�Z!d"d#�Z"d$d%�Z#d&d'�Z$d(d)�Z%d*d+�Z&d,d-d.d/�Z'd,d-d0d1�Z(d2d3d4�Z)d2d5d6�Z*dS)7�N)�SVR)�model_selection�tree�	neighbors�ensemble�utils)�mean_squared_error)�KernelRidgecCs
t||�S)N)r)�x�y�r�A/projects/3572606d-aafc-4285-a345-c223c56d1457/ML/Main/utility.py�error_mean_square_distance
src
Cs�d}tjd�}d}d}d}	tjdddd�}
tjd*ddd�}tjd	d
dgdd
dgd
ddgdddgi�}|dkrddgd|
iddgd|
d|iddgd|
d|ig}
tjt�ddd|
�}n�|dkr\dddgddgd d!gig}tjt	j
�d|�}n�|d"krwtj�}n�|d#krddgd$d%d&|
iddgd$d%d&|
d|iddgd$d%d&|
d|ig}tjt
�ddd|�}nd}x�|D]�}tjd'|d(||d)d+i��}|j||�|j|�}t||�}|	r�|	|kr�|}	|}|}|	dkr|}	|}|}qW|dk	r�t|||	|j||�f�|S),z0Check regression for various parameter settings.Nrz2type: %s;
        mse: %f;
        Score of %0.2f;g�������?g@�@�num�g@Zmax_samplesg�?g�?Zmax_features�	bootstrapTFZbootstrap_featuresr�kernel�linear�CZrbf�gammaZsigmoid�cvZ
param_gridZKNR�weights�uniform�distance�	algorithm�auto�p�ZDTRZKRR�alpha�g@�base_estimatorZrandom_stateZn_jobsg������)rZcheck_random_state�np�linspace�logspacerZ
ParameterGridZGridSearchCVrrZKNeighborsRegressorrZDecisionTreeRegressorr	rZBaggingRegressor�fitZpredictr�print�score)�x_train�y_train�x_test�y_test�	model_fitZmod_pred�rngZmsg_info�y_predZ
error_pred�cr�gridZgrid_svrr Zgrid_knrZgrid_krr�paramsZ
mod_pred_tempZy_pred_tempZerror_pred_temprrr
�test_regressionsV!' &!
'
#r2z.xlsxcCsOg}xBtj|�D]1}|j|�r|jtjj||��qW|S)N)�os�listdir�endswith�append�path�join)�root_dir�	extension�	file_list�filerrr
�get_file_list_from_root_dirCs
 r=cCs�i}x�|D]�}tjj|�}tjj|�d}y8tjt|d�ddd|�jdd�||<Wq
tk
r�}zt	||�WYdd}~Xq
Xq
W|S)Nr�rbZ	index_colZ	sheetnameZorient�index)
r3r7�basename�splitext�pandasZ
read_excel�open�to_dict�	Exceptionr&)r;�
sheet_name�datar7Zbase_name_without_ext�errr
�collect_data_from_file_listKs

rIcCspg}t|�}xW|D]O\}}|dkrQ|jt||||��qt||||�}PqW|S)Nr)�lenr6�#extract_vectors_of_specific_data_1d)rG�st_name_t_stamps�markZvecsZst_name_t_stamps_len�
stock_name�t_stamprrr
� extract_vectors_of_specific_dataYs rPcCs0g}x#|D]}|j|||�q
W|S)N)r6)rGrLrM�vecrOrrr
rKes
rK�cCsUtjdd�tj|�tj|�tj|�tj||�tj�dS)N�figsize�r)rTr)�plt�figure�title�xlabel�ylabel�plot�show)r
rrWrXrYrrr
�	plot_datals


r\cCs�||ks|dkr�yLtj�jjj}djd|d|d|df�}t|��WnJtk
r�}z*tdt	|�d�t
jd
�WYdd}~XnXdS)Nr�
zFunction name: zLength of data is: %dz
Period is: %dz@We need period to be grather then 0 and less then length of datazError occured: z
EXITrr!)�inspect�currentframe�f_back�f_code�co_namer8�
ValueErrorrEr&�str�sys�exit)�period�lengthZcaller_nameZ
error_massage�errorrrr
�period_errorusrjcCsSg}xF|D]>}tj|�r;|jt||��q
t||�}Pq
W|S)u�
    Relative Strength Index (RSI): A technical momentum indicator that compares
    the magnitude of recent gains to recent losses in an attempt to determine
    overbought and oversold conditions of an asset. The formula for computing
    the Relative Strength Index is as follows.
    :return: [RSI = 100-[100/(1+RS)]]
    where  RS = Avg. of x days’ up closes / Average of x days’ down closes.
    )r"�shaper6�relative_strength_index_1d)�close_prices_vecsrg�rsiZ
prices_vecrrr
�relative_strength_index�s	
rocCs5t|�}t||�tj|�}|d|�}||dkj�|}||dkj�|}dg||}x�td||�D]�}|||d}	||d|	dkr�|	nd|}||d|	dkr�|	nd|}|dkr||nd}
ddd|
||<q�W|S)Nrr�dgY@g�?)rJrjr"�diff�sum�range)�close_prices_vecrg�len_cl_prices_vec�deltas�seed�up�downrn�i�deltaZrsrrr
rl�s
()rlcCshg}x[t||�D]J\}}tj|�rM|jt|||��qt|||�}PqW|S)u
    Money Flow Index (MFI): This one measures the strength of money in and out
    of a security. The formula for MFI is as follows:
    Money Flow (MF) = Typical Price * Volume.
    Money Ratio (MR) = (Positive MF / Negative MF).
    :return: MFI = 100 – (100/ (1+MR)).
    )�zipr"rkr6�money_flow_index_1d)Ztypc_prices_vecsZvolume_data_vecsrg�mfi�typical_prices_vec�volume_data_vecrrr
�money_flow_index�sr�cCs t|�}t||�tj|�}tjdd�t|dd�|�D��}dg||}x�td||�D]�}||||�}||||�|dkj�|}	||||�|dkj�|}
|
dkr�|	|
nd}ddd|||<q|W|S)NcSs g|]\}}||�qSrr)�.0�typZvolrrr
�
<listcomp>�s	z'money_flow_index_1d.<locals>.<listcomp>rrrpgY@g�?)rJrjr"rq�arrayr|rsrr)rr�rgZlen_ty_prices_vecrvZraw_money_flowr~rzrwrxry�mrrrr
r}�s
/((r}cCsng}xat|||�D]M\}}}tj|�rS|jt|||��qt|||�}PqW|S)N)r|r"rkr6�typical_prices_vecs_1d)�price_list_high�price_list_low�price_list_closer��high�low�closerrr
�typical_prices_vecs�s"r�cCsdd�t|||�D�S)NcSs1g|]'\}}}tj|||g��qSr)r"�mean)r�r�r�r�rrr
r��s	z*typical_prices_vecs_1d.<locals>.<listcomp>)r|)r�r�r�rrr
r��s	r�cCsSg}xF|D]>}tj|�r;|jt||��q
t||�}Pq
W|S)u!
    Exponential Moving Average (EMA): This indicator
    returns the exponential moving average of a field over a
    given period of time. EMA formula is as follows.
    :return: EMA = [alpha *T Close] + [1-alpha *Y close]
    Where T is Today’s close and Y is Yesterday’s close
    )r"rkr6�exponential_moving_average_1d)Zclose_price_vecsrg�emaZclose_price_vecrrr
�exponential_moving_average�s
r�cCs�t|�}t||�tj|�}tjtjdd|��}||j�}tj||dd�dt|��}||d�}|S)Ng�?g�mode�fullg�)rJrjr"�asarray�expr#rr�convolve)rtrgrurr�rrr
r��s
(r�cCstg}xgt|||�D]S\}}}tj|�rV|jt||||��qt||||�}PqW|S)u�
    Stochastic Oscillator (SO): The stochastic oscillator
    defined as a measure of the difference between the
    current closing price of a security and its lowest low
    price, relative to its highest high price for a given period
    of time. The formula for this computation is as follows:
    :return: %K = [(Close price – Lowest price) / (Highest Price – Lowest Price)] * 100
    )r|r"rkr6�stochastic_oscillator_1d)�high_prices_vecs�low_prices_vecsrmrg�so�high_prices_vec�low_prices_vecrtrrr
�stochastic_oscillator�s	"r�c	s�t��}t�|���fdd�td|��D�}��fdd�td|��D�}tj|�d�|�}tj||�}dd�t||�D�S)Ncs.g|]$}t�||�d���qS)r)�max)r�rz)r�rgrr
r�
s	z,stochastic_oscillator_1d.<locals>.<listcomp>rcs.g|]$}t�||�d���qS)r)�min)r�rz)r�rgrr
r�s	cSs6g|],\}}|dkr,||dnd�qS)rrpr)r�Z
curr_low_sZ
high_min_srrr
r�s	)rJrjrsr"�subtractr|)	r�r�rtrgZlen_high_prices_vecZmax_high_vecZmin_low_vecZcurr_low_subZhigh_min_subr)r�r�rgr
r�
s
))	r���cCsYg}xL|D]D}tj|�r>|jt|||��q
t|||�}Pq
W|S)a
    Moving Average Convergence/Divergence (MACD):
    This function calculates difference between a short and a long term moving average for a field.
    The formulas for calculating MACD.
    :return: MACD = [EMA of Closing prices] - [EMA of closing prices]
    )r"rkr6�(moving_average_convergence_divergence_1d)rm�slow_period�fast_periodZmacdrtrrr
�%moving_average_convergence_divergences
r�cCsnt|�}t||�t||�t||�}t||�}tj|t|�t|�d�|�S)N)rJrjr�r"r�)rtr�r�ruZemaslowZemafastrrr
r�&s

r��	cCsSg}xF|D]>}tj|�r;|jt||��q
t||�}Pq
W|S)z7
    :return: Signal Line = period day EMA of MACD
    )r"rkr6�signal_line_1d)Zmacd_data_vecsrg�sl�
macd_data_vecrrr
�signal_line/s
r�cCs&t|�}t||�t||�S)N)rJrjr�)r�rgZlen_macd_data_vecrrr
r�=s
r�)+�numpyr"rer^r3rB�matplotlib.pyplot�pyplotrUZsklearn.svmr�sklearnrrrrrZsklearn.metricsrZsklearn.kernel_ridger	rr2r=rIrPrKr\rjrorlr�r}r�r�r�r�r�r�r�r�r�r�rrrr
�<module>s>(2