o
    !\i                     @   s   d Z ddlmZ ddlmZmZ ddlZddlZddlm	Z	 ddl
mZ ddlmZ ddlmZ dd	lmZ dd
lmZmZ ddlmZ ddlmZmZmZ eeZG dd de	ZdS )zBacktest agent    )Session)datetime	timedeltaN)	BaseAgent)MatchRepository)ModelRepository)MatchStatus)Feature)MatchPredictorprepare_target)FeatureBuilder)log_lossbrier_score_lossaccuracy_scorec                       sN   e Zd ZdZdef fddZdefddZdej	d	ej	de
fd
dZ  ZS )BacktestAgentzAgent for backtesting modelsdbc                    s0   t  |d t|| _t|| _t|| _d S )Nr   )super__init__r   
match_repor   
model_repor   feature_builder)selfr   	__class__ X/var/www/www-root/data/www/176.119.141.140/sports-predictor/app/agents/backtest_agent.pyr      s   

zBacktestAgent.__init__returnc              
   C   s  |   }zU| j }|sd}t| | jd|d W dS zt|j}W n% t	yK } zd| }t
| | jd|d W Y d}~W dS d}~ww t tdd }t tdd }| jj||d	d
}tdt| d dd |D }	| jttj|	 }
dd |
D }g }g }g }g }|D ]D}|j|vrq|jdu s|jdu rq||j }| j|}|| |t| t !|g}|"|d }|| |t #| qt|dkrd}t| | jd|d W dS t !|}t !|}t !|}t !|}t$t%||}t$t&||}t$t'|dk|dddf }| (||}||||t|d}td|  d|dd|dd|dd|dd	}| jd|d W dS  t	y } zt)|}tj
d| dd | jd|d W Y d}~dS d}~ww ) zRun backtestzNo active model foundF)successerrorzFailed to load model: NZ   )days   i  )	from_dateto_datelimitzBacktesting on z matchesc                 S   s   g | ]}|j qS r   )id).0mr   r   r   
<listcomp>>   s    z%BacktestAgent.run.<locals>.<listcomp>c                 S   s   i | ]}|j |qS r   )match_id)r&   fr   r   r   
<dictcomp>C   s    z%BacktestAgent.run.<locals>.<dictcomp>r   z(No matches with features for backtesting)accuracyloglossbrierroi_simulationmatches_testedzBacktest results: zBacktest Results:
Accuracy: z.4fz

LogLoss: z
Brier: z
ROI Simulation: z.2f%T)r   logszBacktest failed: )exc_info)*	start_runr   
get_latestloggerwarning
finish_runr
   loadartifact_path	Exceptionr   r   nowr   r   get_finishedinfolenr   queryr	   filterr)   in_allr%   
home_score
away_scorer   build_feature_vectorappendr   nparraypredict_probaargmaxfloatr   r   r   _simulate_roistr)r   run_idmodel_record	error_msg	predictorer"   r#   matches	match_idsfeaturesfeature_dictXypredictionsprobabilities_listmatchfeaturefeature_vectorX_singleprobaprobabilitiesr,   r-   r.   r/   resultsr2   r   r   r   run   s   













&zBacktestAgent.runy_truey_probac           
      C   s   d}d}t t|D ]%}t|| }|| | }|dkr/d}||7 }||| kr/||d 7 }q
|dkr6dS || | d }	|	S )z.Simple ROI simulation (for demonstration only)g        g?g      ?g      @r   d   )ranger?   rH   rK   )
r   rd   re   	total_bettotal_returni	predicted
confidence
bet_amountroir   r   r   rM      s   zBacktestAgent._simulate_roi)__name__
__module____qualname____doc__r   r   boolrc   rH   ndarrayrL   rM   __classcell__r   r   r   r   r      s
    "kr   )rr   sqlalchemy.ormr   r   r   loggingnumpyrH   app.agents.baser   )app.core.db.repositories.match_repositoryr   )app.core.db.repositories.model_repositoryr   app.core.db.models.matchr   app.core.db.models.featurer	   app.ml.modelsr
   r   app.ml.featuresr   sklearn.metricsr   r   r   	getLoggerro   r6   r   r   r   r   r   <module>   s    
