################################################
##
## This script calculates distributional day effects
##
################################################

use warnings;
use strict;
use Finance::EventDayChain;
use Finance::EventBackpack;
use Finance::SingleTradingDay;
use Finance::SingleDecisionEvent;
use Finance::Functions;
use Statistics::Distributions::Bartlett;
use Statistics::Test::WilcoxonRankSum;
use Data::Dumper;
use DBI;
use DBD::mysql;

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

# Window sizes
my $window_start = 140;
my $window_end   = -92;
my $event_start  = 0;
my $event_end    = 1;

# Reference set
my $reference_bourse = "NASDAQ";
my $reference_index1 = "NBI";

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

###
# 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
$option1->{"status"} = "AUTHORISED";	# AUTHORISED/REFUSED
##$option1->{"generic"} = "NO";
#$option1->{"biosimilar"} = "NO";
#$option1->{"orphan"} = "NO";
#$option1->{"type"}      = ;
#$option1->{"NDA_type"}  = 1;
#$option1->{"review_classification"} = "S";
my $option2;
$option2->{"Xchange"} = "NASDAQ";
#$option2->{"name"}    = "AFFYMAX";
#$option2->{"ticker"} = "LLY|PFE|MRK|ABT|GSK|BMY|JNJ|NVS|SNY";

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

###
# Process queried events
###

# Init
my @assets;
my @refs;

my @final_bag_normalized_returns;

# Process data
foreach my $event (@{$eventBackpack_obj->get_backpack_content()}){
	
	### TRAINING ###
	
	# Get reference index data
	my $dayChainRef1 = Finance::EventDayChain->new();
	$dayChainRef1->retrieve_SingleTradingDays_around_window($event->get_date(),$reference_bourse,$reference_index1,$window_start,$window_end,$dbh);

	# Calculate normal returns
	$dayChainRef1->calculate_chain_normal_return();
	my $refreturnsref = $dayChainRef1->get_normal_return();
	my @refreturns;
	foreach (keys %$refreturnsref){
		push(@refreturns,$refreturnsref->{$_});
	}
	
	# Get asset data
	my $dayChainAsset = Finance::EventDayChain->new();
	$dayChainAsset->retrieve_SingleTradingDays_around_window($event->get_date(),$event->get_Xchange(),$event->get_ticker(),$window_start,$window_end,$dbh);
	
	# Calculate normal returns
	$dayChainAsset->calculate_chain_normal_return();
	my $assetreturnsref = $dayChainAsset->get_normal_return();
	my @assetreturns;
	foreach (keys %$assetreturnsref){
		push(@assetreturns,$assetreturnsref->{$_});
	}
	
	### EVENT ###
	
	# Get reference index data
	my $dayChainRef2 = Finance::EventDayChain->new();
	$dayChainRef2->retrieve_SingleTradingDays_around_window($event->get_date(),$event->get_Xchange(),$reference_index1,$event_start,$event_end,$dbh);	
	$dayChainRef2->calculate_chain_normal_return();
	my $eventreturnsref = $dayChainRef2->get_normal_return();
	
	# Get asset data
	my $dayChainAsset2 = Finance::EventDayChain->new();
	$dayChainAsset2->retrieve_SingleTradingDays_around_window($event->get_date(),$event->get_Xchange(),$event->get_ticker(),$event_start,$event_end,$dbh);	
	$dayChainAsset2->calculate_chain_normal_return();
	my $eventreturnsasset = $dayChainAsset2->get_normal_return();
	
	# Fetch
	my $pos1 = rank_pos(\@assetreturns,$eventreturnsasset->{0});
	my $pos2 = rank_pos(\@assetreturns,$eventreturnsasset->{1});
	
	#if ($pos2<$pos1){
	#	$pos1 = $pos2;
	#}

	push(@assets, $pos2);

	# Fetch
	my $pos3 = rank_pos(\@refreturns,$eventreturnsref->{0});
	my $pos4 = rank_pos(\@refreturns,$eventreturnsref->{1});
	
	#if ($pos4<$pos3){
	#	$pos3 = $pos4;
	#}

	push(@refs, $pos4);
	
	# Store all returns normalized in a bag
	my $normalized_bag = SENormalize(\@assetreturns);
	foreach(@$normalized_bag){
		push(@final_bag_normalized_returns,$_);
	}
}

# Print

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

print "\n";

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

my $wilcox_test = Statistics::Test::WilcoxonRankSum->new();
$wilcox_test->load_data(\@assets, \@refs);
my $prob = $wilcox_test->probability();
$wilcox_test->summary();

print "\n";

foreach(@final_bag_normalized_returns){

	#print $_.",";
}

# Disconnect
$dbh->disconnect;