<?php

//entrada dos parametro via GET (moedid/perid)
$moedid = filter_input(INPUT_GET, 'moedid', FILTER_DEFAULT);
$perid = filter_input(INPUT_GET, 'perid', FILTER_VALIDATE_INT);

//montando as datas de acordo com o periodo
$data_ini = date("Y-m-d");
$data_fim = date('Y-m-d', strtotime("-{$perid} month", strtotime($data_ini)));

//////////////////////// grafico DIF /////////////////////////////
$Read = new Read;
$Read->ExeRead("tp_indice", "WHERE CFTC_Contract_Market_Code = :codigo AND indice_data BETWEEN :data_fim AND :data_ini ORDER BY indice_data DESC", "codigo={$moedid}&data_ini={$data_ini}&data_fim={$data_fim}");

////maximo e minimo do perido/////
foreach ($Read->getResult() as $mm):
    extract($mm);

    if ($moedid == '095741' || $moedid == '092741' || $moedid == '090741' || $moedid == '097741'):
        $NonComm_Positions_Long_All = $long;
        $NonComm_Positions_Short_All = $short;
    endif;

    $mmNetNonComm = $NonComm_Positions_Long_All - $NonComm_Positions_Short_All;
    $mmNetComm = $Comm_Positions_Long_All - $Comm_Positions_Short_All;
    $mmCover = $mmNetNonComm / ($Open_Interest_All - $NonComm_Postions_Spread_All);
    $mmFlip = $Pct_of_OI_NonComm_Long_All - $Pct_of_OI_NonComm_Short_All;

    $arrayNonComm[] = $mmNetNonComm;
    $arrayComm[] = $mmNetComm;
    $arrayCover[] = $mmCover;
    $arrayFlip[] = $mmFlip;
endforeach;
/////maximo e minimo NET
$maxNetNonComm = max($arrayNonComm);
$minNetNonComm = min($arrayNonComm);
$maxNetComm = max($arrayComm);
$minNetComm = min($arrayComm);
$maxCover = max($arrayCover);
$minCover = min($arrayCover);
$maxFlip = max($arrayFlip);
$minFlip = min($arrayFlip);
///////////////fim do max e mim///////////////

foreach ($Read->getResult() as $indices):
    $long = $indices['NonComm_Positions_Short_All'];
    $short = $indices['NonComm_Positions_Long_All'];

    extract($indices);

    if ($moedid == '095741' || $moedid == '092741' || $moedid == '090741' || $moedid == '097741'):
        $NonComm_Positions_Long_All = $long;
        $NonComm_Positions_Short_All = $short;
    endif;

    $Dados['data'][] = "'" . date("d/m/Y", strtotime($indice_data)) . "'";

    $NetNonComm = $NonComm_Positions_Long_All - $NonComm_Positions_Short_All;
    $NetComm = $Comm_Positions_Long_All - $Comm_Positions_Short_All;
    $Dados['dif'][] = $NetNonComm - $NetComm;

    ///Calcular a media dos ultimos 12 semanas     
    $dias = 7 * 12;

    $data_dif_ini = $indice_data;
    $data_dif_fim = date('Y-m-d', strtotime("-{$dias} day", strtotime($data_dif_ini)));

    $ReadMedia = new Read;
    $ReadMedia->ExeRead("tp_indice", "WHERE CFTC_Contract_Market_Code = :codigo AND indice_data BETWEEN :data_fim AND :data_ini ORDER BY indice_data DESC LIMIT 12", "codigo={$moedid}&data_ini={$data_dif_ini}&data_fim={$data_dif_fim}");

    unset($arrDif);
    foreach ($ReadMedia->getResult() as $M):
        $mNetNComm = $M['NonComm_Positions_Long_All'] - $M['NonComm_Positions_Short_All'];
        $mNetComm = $M['Comm_Positions_Long_All'] - $M['Comm_Positions_Short_All'];
        $Dif = $mNetNComm - $mNetComm;
        $arrDif[] = round($Dif);
        $arrCover[] = $mNetNComm / ($M['Open_Interest_All'] - $M['NonComm_Postions_Spread_All']);
    endforeach;
    /////// fim do calculo da média ///////////

    $Dados['dif_media'][] = array_sum($arrDif) / 12;

    $NetNonCommNormalizado = ($NetNonComm - $minNetNonComm) / ($maxNetNonComm - $minNetNonComm) * 100;

    $Dados['NetPosicNonComm'][] = round(($NetNonComm - $minNetNonComm) / ($maxNetNonComm - $minNetNonComm) * 100);
    $Dados['NetPosicComm'][] = round(($NetComm - $minNetComm) / ($maxNetComm - $minNetComm) * 100);

    $Cover = $NetNonComm / ($Open_Interest_All - $NonComm_Postions_Spread_All);
    $Moviment = ($Cover - $minCover) / ($maxCover - $minCover) * 100;
    $MoIndex = ($arrCover[5] - $minCover) / ($maxCover - $minCover) * 100;
    $MovimentIndex = $Moviment - $MoIndex;

    $Dados['Index'][] = number_format($MovimentIndex, 2, '.', ',');

    $Flip = $Pct_of_OI_NonComm_Long_All - $Pct_of_OI_NonComm_Short_All;
    $FlipNC = ($Flip - $minFlip) / ($maxFlip - $minFlip) * 100;
    $Dados['FlipNc'][] = number_format($FlipNC, 2, ',', '.');

    $Dados['Moeda1'][] = number_format($NetNonComm, 2, ',', '.');

    $Dados['Aberto'][] = $NonComm_Positions_Long_All + $NonComm_Postions_Spread_All + $Comm_Positions_Long_All + $NonRept_Positions_Long_All;
    $Dados['Volume'][] = $NonComm_Positions_Long_All + $NonComm_Positions_Short_All;

    $Dados['Long'][] = $NonComm_Positions_Long_All;
    $Dados['Short'][] = $NonComm_Positions_Short_All;

endforeach;

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////Moeda Base (USD)///////////////////
//////////////////////// grafico /////////////////////////////
$moedaBase = '098662';
$Read = new Read;
$Read->ExeRead("tp_indice", "WHERE CFTC_Contract_Market_Code = :codigo AND indice_data BETWEEN :data_fim AND :data_ini ORDER BY indice_data DESC", "codigo={$moedaBase}&data_ini={$data_ini}&data_fim={$data_fim}");

////maximo e minimo do perido/////
foreach ($Read->getResult() as $mm):
    extract($mm);

    if ($moedid == '095741' || $moedid == '092741' || $moedid == '090741' || $moedid == '097741'):
        $NonComm_Positions_Long_All = $long;
        $NonComm_Positions_Short_All = $short;
    endif;

    $mmNetNonComm = $NonComm_Positions_Long_All - $NonComm_Positions_Short_All;

    $arrayNonComm[] = $mmNetNonComm;
endforeach;
/////maximo e minimo NET
$maxNetNonComm = max($arrayNonComm);
$minNetNonComm = min($arrayNonComm);
///////////////fim do max e mim///////////////

foreach ($Read->getResult() as $indices):
    $long = $indices['NonComm_Positions_Short_All'];
    $short = $indices['NonComm_Positions_Long_All'];

    extract($indices);

    if ($moedid == '095741' || $moedid == '092741' || $moedid == '090741' || $moedid == '097741'):
        $NonComm_Positions_Long_All = $long;
        $NonComm_Positions_Short_All = $short;
    endif;

    $NetNonComm = $NonComm_Positions_Long_All - $NonComm_Positions_Short_All;
    $NetNonCommNormalizado = ($NetNonComm - $minNetNonComm) / ($maxNetNonComm - $minNetNonComm) * 100;
    $Dados['Moeda2'][] = number_format($NetNonComm, 2, ',', '.');

endforeach;

////////////////////////MOEDA//////////////
for ($i = 0; $i < count($Dados['Moeda2']); $i++) {
    $Dados['Moedas'][$i] = $Dados['Moeda1'][$i] - $Dados['Moeda2'][$i];
}

$maxMoedas = max($Dados['Moedas']);
$minMoedas = min($Dados['Moedas']);

for ($i = 0; $i < count($Dados['Moedas']); $i++) {
    $Dados['MoedasNormalizado'][$i] = ($Dados['Moedas'][$i] - $minMoedas) / ($maxMoedas - $minMoedas) * 100;
}
//////////////////////////////////////////////

$difDat = implode(', ', array_reverse($Dados['data']));
$difVal = implode(', ', array_reverse($Dados['dif']));
$difMed = implode(', ', array_reverse($Dados['dif_media']));

$nNetNonCommStr = implode(', ', array_reverse($Dados['NetPosicNonComm']));
$nNetCommStr = implode(', ', array_reverse($Dados['NetPosicComm']));
$CoverOiStr = implode(', ', array_reverse($Dados['Index']));
$FlipNcStr = implode(', ', array_reverse($Dados['FlipNc']));
$MoeLiquidezStr = implode(', ', array_reverse($Dados['Moedas']));
$MoeNormalizadoStr = implode(', ', array_reverse($Dados['MoedasNormalizado']));
$VolumeStr = implode(', ', array_reverse($Dados['Volume']));
$AbertoStr = implode(', ', array_reverse($Dados['Aberto']));
$NLongStr = implode(', ', array_reverse($Dados['Long']));
$NShortStr = implode(', ', array_reverse($Dados['Short']));
/////////////////////////// Fim do gráfico DIF///////////////////////


echo '<pre>';
print_r($Dados);
echo '</pre>';
