################################################
##
## This script calculates training window regressions
##
################################################

use warnings;
use strict;
use Finance::EventDayChain;
use Finance::EventBackpack;
use Finance::SingleTradingDay;
use Finance::SingleDecisionEvent;
use Data::Dumper;
use DBI;
use DBD::mysql;

################################################

# Window sizes
my $training_upstream = 140;
my $training_offset   = 90;

# Training set
my $training_bourse = "NYSE";
my $training_index  = "DRG";
my $FF_bourse1 = "famafrench";
my $FF_index1  = "HML";
my $FF_bourse2 = "famafrench";
my $FF_index2  = "SMB";

################################################

###
# Open DB connection
###

my $db  = "DBI:mysql:thesis_economie:athos.ugent.be";
my $us  = "ewthesis";
my $pw  = "thesisew";
my $dbh = DBI->connect($db,$us,$pw) || die "Cannot connect: " . $DBI::errstr;

###
# Query events
###

# Init event backpack
my $eventBackpack_obj = Finance::EventBackpack->new();

# Generate query
my $option1;
$option1->{"duplicate"} = 0;
$option1->{"hist"}      = 0;  # For now ignore historical data

my $option2;
$option2->{"Xchange"}    = "NYSE";

# Do query
$eventBackpack_obj->query_events("FDA",$option1,$option2,$dbh);

###
# Process queried events
###

my @rsquares;

foreach my $event (@{$eventBackpack_obj->get_backpack_content()}){

	print $event->get_ticker()."\t".$event->get_date()."\t";

	# Get reference index data
	my $dayChainRef1 = Finance::EventDayChain->new();
	$dayChainRef1->retrieve_SingleTradingDays_around_window($event->get_date(),$training_bourse,$training_index,$training_upstream,-$training_offset,$dbh);
	
	# Get reference HML data
	my $dayChainRef2 = Finance::EventDayChain->new();
	$dayChainRef2->retrieve_SingleTradingDays_around_window($event->get_date(),$FF_bourse1,$FF_index1,$training_upstream,-$training_offset,$dbh);	
	
	# Get reference SMB data
	my $dayChainRef3 = Finance::EventDayChain->new();
	$dayChainRef3->retrieve_SingleTradingDays_around_window($event->get_date(),$FF_bourse2,$FF_index2,$training_upstream,-$training_offset,$dbh);
	
	# Get asset data
	my $dayChainAsset = Finance::EventDayChain->new();
	$dayChainAsset->retrieve_SingleTradingDays_around_window($event->get_date(),$event->get_Xchange(),$event->get_ticker(),$training_upstream,-$training_offset,$dbh);
	
	# Attach and check reference
	$dayChainAsset->attach_index_reference($dayChainRef1);
	$dayChainAsset->attach_HML_reference($dayChainRef2);
	$dayChainAsset->attach_SMB_reference($dayChainRef3);
	$dayChainAsset->reference_sanity_check(1);	# 1 for Fama French sanity check / 0 for other
	
	# Calculate Fama French model
	$dayChainAsset->calculate_Fama_French();
	#$dayChainAsset->calculate_alfa_beta();
	
	# Print
	my $crit0 = 2.5758;	# 1%
	my $crit1 = 1.96;	# 5%
	my $crit2 = 1.6449;	# 10%
	
	my $int_star = "";
	my $slope_star = "";
	my $HML_star = "";
	my $SMB_star = "";
	if (abs($dayChainAsset->get_ref_intercept_t) > $crit0){
		$int_star = "***";
	}
	elsif (abs($dayChainAsset->get_ref_intercept_t) > $crit1){
		$int_star = "**";
	}	
	elsif (abs($dayChainAsset->get_ref_intercept_t) > $crit2){
		$int_star = "*";
	}
	
	if (abs($dayChainAsset->get_ref_index_slope_t) > $crit0){
		$slope_star = "***";
	}
	elsif (abs($dayChainAsset->get_ref_index_slope_t) > $crit1){
		$slope_star = "**";
	}	
	elsif (abs($dayChainAsset->get_ref_index_slope_t) > $crit2){
		$slope_star = "*";
	}
	
	if (abs($dayChainAsset->get_ref_HML_slope) > $crit0){
		$HML_star = "***";
	}
	elsif (abs($dayChainAsset->get_ref_HML_slope) > $crit1){
		$HML_star = "**";
	}	
	elsif (abs($dayChainAsset->get_ref_HML_slope) > $crit2){
		$HML_star = "*";
	}
	
	if (abs($dayChainAsset->get_ref_SMB_slope) > $crit0){
		$SMB_star = "***";
	}
	elsif (abs($dayChainAsset->get_ref_SMB_slope) > $crit1){
		$SMB_star = "**";
	}	
	elsif (abs($dayChainAsset->get_ref_SMB_slope) > $crit2){
		$SMB_star = "*";
	}
	
	printf("%.4f", $dayChainAsset->get_ref_intercept);
	print $int_star;
	print "\t";
	printf("%.4f", $dayChainAsset->get_ref_index_slope);
	print $slope_star;
	print "\t";
	printf("%.4f", $dayChainAsset->get_ref_HML_slope);
	print $HML_star;
	print "\t";
	printf("%.4f", $dayChainAsset->get_ref_SMB_slope);
	print $SMB_star;
	print "\t";
	printf("%.4f", $dayChainAsset->get_ref_r2);
	print "\n";
	
	print "\t\t(";
	printf("%.4f", $dayChainAsset->get_ref_intercept_t);
	print ")\t(";
	printf("%.4f", $dayChainAsset->get_ref_index_slope_t);
	print ")\t(";
	printf("%.4f", $dayChainAsset->get_ref_HML_slope_t);
	print ")\t(";
	printf("%.4f", $dayChainAsset->get_ref_SMB_slope_t);
	print ")\n";
			
	push(@rsquares,$dayChainAsset->get_ref_r2);
	
}

foreach(@rsquares){
	print $_.",";
}

# Disconnect
$dbh->disconnect();



	
