const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
const httpRequest = require('https');
var querystring = require('querystring');
var mongoose = require('mongoose');
var fs = require('fs');
var util = require('util');
var async = require('async');

const transactionMongoose = require('../models/Transaction.js');
const stockMongoose = require('../models/Stock.js');

/**
 * POST /transactions
 * Create a new transaction
 */
exports.simpleTest = (req, res, next) => {

    // var codeSource = "ABCA";
    // var isinSource = "FR0004040608";

    var codeSource = "BLC";
    var isinSource = "FR0000035370";

    var deltas = [[]];
    var startStart = 2;
    var startEnd = 7;
    var endStart = 4;
    var endEnd = 50;
    for (s = startStart; s < startEnd; s++) {
        var temp = [];
        for (e = endStart; e < endEnd; e++) {
            temp.push(0);
        }
        deltas.push(temp);
    }

    stockMongoose.findOne({ 'code': codeSource }, function (err, stock) {
        transactionMongoose.find({ 'isin': isinSource, 'nature': 'Acquisition' }, function (err, transactions) {
            async.each(transactions, function (transaction, callback) {
                testTransaction(transaction, stock);
                callback(null);
            }, logDeltas);
        });
    });

    function testTransaction(transaction, stock) {
        console.log(transaction.date);
        var dateToFind = transaction.date;
        var dates = stock.close.map(function (value, index) { return value[0]; });
        var closes = stock.close.map(function (value, index) { return value[1]; });
        var len = dates.length;
        for (s = startStart; s < startEnd; s++) {
            for (e = endStart; e < endEnd; e++) {
                if (e > s) {
                    for (i = 0; i < len; i++) {
                        if (s+e+i < len) {
                            var d = new Date(dates[i]);
                            var delta = 0;
                            if (dateToFind.getTime() == d.getTime()) {
                                var firstClose = Number(closes[i + s]);
                                var lastClose = Number(closes[i + e + s]);
                                delta = (lastClose - firstClose) / firstClose;
                                deltas[s - startStart][e - endStart] = deltas[s - startStart][e - endStart] + delta;
                                break;
                            }
                        }
                    }
                }
            }
        }
    }

    function logDeltas() {
        var min = 999999999999;
        var max = -999999999999;
        var sMin = 0;
        var eMin = 0;
        var sMax = 0;
        var eMax = 0;
        for (s = startStart; s < startEnd; s++) {
            for (e = endStart; e < endEnd; e++) {
                var val = deltas[s - startStart][e - endStart];
                if (val > max) {
                    max = val;
                    sMax = s;
                    eMax = e;
                } else if (val < min) {
                    min = val;
                    sMin = s;
                    eMin = e;
                }
            }
        }
        console.log("min: " + min.toPrecision(2) + " ( " + sMin + " - " + eMin + " )");
        console.log("max: " + max.toPrecision(2) + " ( " + sMax + " - " + eMax + " )");
    }

};
@@ -917,16 +917,539 @@
   %7D%0A%0A   
+ var nTransactions = 0;%0A    var nFailures = 0;%0A%0A    transactionMongoose.find(%7B 'nature': 'Acquisition' %7D, function (err, transactions) %7B%0A        var trs = transactions.slice(300, transactions.length);%0A        nTransactions = trs.length;%0A        async.each(trs, function (transaction, callback) %7B%0A            stockMongoose.findOne(%7B 'isin': transaction.isin %7D, function (err, stock) %7B%0A                testTransaction(transaction, stock);%0A                callback(null);%0A            %7D);%0A        %7D, logDeltas);%0A    %7D);%0A%0A    //
  stockMo
@@ -1515,20 +1515,23 @@
 ) %7B%0A    
+//
     
+ 
 transact
@@ -1622,32 +1622,35 @@
 nsactions) %7B%0A   
+ //
          async.e
@@ -1697,32 +1697,35 @@
  callback) %7B%0A   
+ //
              tes
@@ -1753,32 +1753,35 @@
 ion, stock);%0A   
+ //
              cal
@@ -1788,32 +1788,35 @@
 lback(null);%0A   
+ //
          %7D, logD
@@ -1822,24 +1822,27 @@
 Deltas);%0A   
+ //
      %7D);%0A   
@@ -1837,24 +1837,27 @@
      %7D);%0A   
+ //
  %7D);%0A%0A    fu
@@ -1909,28 +1909,84 @@
         
-console.log(
+if (stock == null) return;%0A        console.log(stock.code + %22 - %22 + 
 transact
@@ -2230,32 +2230,155 @@
 = dates.length;%0A
+        if (len == 0) %7B%0A            console.log(%22Pas de donnees pour : %22 + stock.code);%0A            nFailures++;%0A        %7D%0A
         for (s =
@@ -3893,32 +3893,186 @@
     %7D%0A        %7D%0A
+        var nTFinal = nTransactions-nFailures;%0A        min = min / nTFinal;%0A        max = max / nTFinal;%0A        console.log(%22Transactions: %22 + nTFinal);%0A
         console.

test on all data