
"use strict";
let assert = require('assert');
let KeltnerChannels    = require('../../lib/volatility/KeltnerChannels').KeltnerChannels;

var input = {
  useSMA : false,
  maPeriod : 20,
  atrPeriod : 10,
  multiplier : 1,
  "high": [
    227.82,
    228.89,
    228.04,
    228.33,
    227.94,
    229.44,
    229.59,
    229.07,
    229.06,
    227.88,
    228.26,
    228.14,
    227.44,
    228.2,
    228.1,
    228.24,
    228.45,
    228.2,
    228.43,
    227.47,
    227.59,
    227.09,
    226.3,
    225.78,
    226.21,
    226.45,
    226.45,
    226,
    226.94,
    226.85,
    226.33,
    225.97,
    226.45,
    226.7,
    226.25,
    226.04,
    225.28,
    225.03,
    223.99,
    222.99,
    223.25,
    223.09,
    222.91,
    223.02,
    223.01,
    223.21,
    223.8,
    223.9,
    223.77,
    223.08,
    222.89,
    223.9,
    224.39,
    224.36,
    223.67,
    223.46,
    223.06,
    223.89,
    223.61,
    217.19,
    203.33,
    203.51,
    202.86,
    202.84,
    200.38,
    197.64,
    199.12,
    198.47,
    198.14,
    198.02,
    197.5,
    199.5,
    201.1,
    199.83,
    199.86,
    200.66,
    199.9,
    199.64,
    198.82,
    198.32,
    197.4,
    197.45,
    190.72,
    192.93,
    193,
    193.98,
    194.6,
    193.9,
    196.76,
    198.5,
    196.25,
    196,
    194.94,
    195.07,
    195.71,
    195.71,
    195.5,
    198,
    196.84,
    196.25,
    194.49,
    195.5,
    195.6
  ],
  "low": [
    226.96,
    227.42,
    227.07,
    227.3,
    227.29,
    227.37,
    228.69,
    228.24,
    227.7,
    227.57,
    227.5,
    226.5,
    226.8,
    227,
    227.67,
    228.09,
    227.7,
    226.95,
    226.82,
    226.77,
    226.92,
    225.59,
    225.12,
    225.06,
    225.06,
    226,
    225.63,
    225.6,
    225.99,
    226.2,
    225.5,
    225.28,
    225.9,
    225.83,
    225.81,
    224.37,
    224.93,
    223.55,
    222.99,
    221.59,
    221.52,
    222.32,
    222.22,
    222.6,
    222.52,
    222.69,
    222.95,
    223.36,
    223.01,
    221.73,
    221.89,
    222.79,
    223.38,
    222.79,
    223.29,
    222.93,
    222.73,
    222.66,
    216.41,
    195.1,
    197.51,
    200.07,
    201,
    198.52,
    192.5,
    194.43,
    197.19,
    196.64,
    196.94,
    194.62,
    195.59,
    193.49,
    198.87,
    197.43,
    197.79,
    198.76,
    197.94,
    197.87,
    194.44,
    196.92,
    195.55,
    184.81,
    187.15,
    189.92,
    191.63,
    190.77,
    192.61,
    192.62,
    192.65,
    195.2,
    195,
    193.3,
    193.49,
    193.3,
    193.98,
    194.5,
    194.54,
    194.26,
    195.86,
    193.52,
    193.6,
    193.89,
    195.02
  ],
  "close": [
    227.49,
    227.62,
    227.3,
    227.93,
    227.37,
    229.39,
    228.9,
    229.06,
    227.71,
    227.68,
    228.14,
    226.96,
    227.31,
    227.92,
    228.09,
    228.24,
    227.71,
    228.2,
    227.46,
    227.18,
    227.06,
    226.12,
    225.78,
    225.07,
    226.02,
    226.44,
    225.74,
    225.99,
    226.61,
    226.33,
    225.97,
    225.91,
    226.36,
    226.15,
    225.91,
    225.08,
    225.03,
    223.95,
    222.99,
    222.19,
    223.09,
    222.8,
    222.68,
    222.78,
    222.95,
    222.96,
    223.8,
    223.77,
    223.06,
    221.94,
    222.8,
    223.59,
    224.35,
    223.37,
    223.35,
    222.99,
    222.76,
    223.61,
    217.19,
    202.24,
    200.26,
    201.22,
    201.65,
    200.38,
    196.83,
    197.28,
    198.47,
    197.55,
    198.02,
    195.58,
    197.06,
    198.87,
    199.33,
    197.9,
    198.76,
    199.79,
    197.96,
    198.76,
    197.9,
    197.08,
    196.34,
    188.32,
    189.93,
    191.64,
    192.44,
    193.55,
    193.68,
    192.82,
    195.35,
    195.89,
    195.82,
    193.48,
    194.75,
    194.35,
    195.17,
    195.45,
    194.64,
    196.3,
    196.01,
    193.95,
    193.89,
    195.36,
    195.16
  ]
}


let expectResult = [
  {
    "middle": 227.88299999999998,
    "upper": 228.85501832015498,
    "lower": 226.91098167984498
  },
  {
    "middle": 227.80461904761904,
    "upper": 228.74643553575854,
    "lower": 226.86280255947955
  },
  {
    "middle": 227.644179138322,
    "upper": 228.64181397764753,
    "lower": 226.64654429899645
  },
  {
    "middle": 227.4666382680056,
    "upper": 228.48250962339858,
    "lower": 226.4507669126126
  },
  {
    "middle": 227.23838700438603,
    "upper": 228.22467122423973,
    "lower": 226.25210278453233
  },
  {
    "middle": 227.12235014682545,
    "upper": 228.12500594469378,
    "lower": 226.11969434895713
  },
  {
    "middle": 227.05736441855638,
    "upper": 228.00475463663787,
    "lower": 226.10997420047488
  },
  {
    "middle": 226.93190114059863,
    "upper": 227.86655233687196,
    "lower": 225.9972499443253
  },
  {
    "middle": 226.84219627006541,
    "upper": 227.72338234671142,
    "lower": 225.9610101934194
  },
  {
    "middle": 226.820082339583,
    "upper": 227.7081498085644,
    "lower": 225.9320148706016
  },
  {
    "middle": 226.7734078310513,
    "upper": 227.63766855313455,
    "lower": 225.90914710896803
  },
  {
    "middle": 226.6968927995226,
    "upper": 227.55772744939753,
    "lower": 225.83605814964767
  },
  {
    "middle": 226.6219506281395,
    "upper": 227.46570181302695,
    "lower": 225.77819944325205
  },
  {
    "middle": 226.59700294926907,
    "upper": 227.41137901566776,
    "lower": 225.78262688287037
  },
  {
    "middle": 226.55443123981487,
    "upper": 227.3743696995737,
    "lower": 225.73449278005606
  },
  {
    "middle": 226.493056836023,
    "upper": 227.27500144980593,
    "lower": 225.71111222224005
  },
  {
    "middle": 226.358479994497,
    "upper": 227.22923014690164,
    "lower": 225.48772984209236
  },
  {
    "middle": 226.23195809025918,
    "upper": 227.05063322742336,
    "lower": 225.413282953095
  },
  {
    "middle": 226.01462874832973,
    "upper": 226.89943637177748,
    "lower": 225.12982112488197
  },
  {
    "middle": 225.72656886753643,
    "upper": 226.6228957286394,
    "lower": 224.83024200643345
  },
  {
    "middle": 225.38975278491392,
    "upper": 226.33644695990662,
    "lower": 224.44305860992122
  },
  {
    "middle": 225.1707287101602,
    "upper": 226.19575346765362,
    "lower": 224.1457039526668
  },
  {
    "middle": 224.94494502347828,
    "upper": 225.94446730522236,
    "lower": 223.9454227417342
  },
  {
    "middle": 224.72923597362322,
    "upper": 225.69780602719288,
    "lower": 223.76066592005355
  },
  {
    "middle": 224.54359445232578,
    "upper": 225.4573075005385,
    "lower": 223.62988140411306
  },
  {
    "middle": 224.39182355210428,
    "upper": 225.26316529549572,
    "lower": 223.52048180871284
  },
  {
    "middle": 224.25545940428484,
    "upper": 225.0916669733371,
    "lower": 223.41925183523256
  },
  {
    "middle": 224.21208231816246,
    "upper": 225.04966913030952,
    "lower": 223.3744955060154
  },
  {
    "middle": 224.1699792402422,
    "upper": 224.97780737117458,
    "lower": 223.36215110930985
  },
  {
    "middle": 224.06426693164772,
    "upper": 224.86731224948684,
    "lower": 223.2612216138086
  },
  {
    "middle": 223.86195579530033,
    "upper": 224.71969658135555,
    "lower": 223.00421500924512
  },
  {
    "middle": 223.76081714812887,
    "upper": 224.63278385557857,
    "lower": 222.88885044067916
  },
  {
    "middle": 223.74454884830706,
    "upper": 224.6403188850118,
    "lower": 222.84877881160233
  },
  {
    "middle": 223.802210862754,
    "upper": 224.70940389578826,
    "lower": 222.89501782971976
  },
  {
    "middle": 223.7610479234441,
    "upper": 224.73452165317494,
    "lower": 222.78757419371325
  },
  {
    "middle": 223.7219005021637,
    "upper": 224.63602685892144,
    "lower": 222.80777414540594
  },
  {
    "middle": 223.65219569243382,
    "upper": 224.5279094135158,
    "lower": 222.77648197135184
  },
  {
    "middle": 223.5672246741068,
    "upper": 224.38836702308058,
    "lower": 222.74608232513302
  },
  {
    "middle": 223.57129851466806,
    "upper": 224.43332662874445,
    "lower": 222.70927040059166
  },
  {
    "middle": 222.9635557989854,
    "upper": 224.45938110165415,
    "lower": 221.46773049631665
  },
  {
    "middle": 220.98988381812964,
    "upper": 224.54512659053154,
    "lower": 217.43464104572774
  },
  {
    "middle": 219.01560916878395,
    "upper": 222.79732766394565,
    "lower": 215.23389067362226
  },
  {
    "middle": 217.32078924794737,
    "upper": 221.0683358935929,
    "lower": 213.57324260230186
  },
  {
    "middle": 215.82833312909526,
    "upper": 219.38712511017624,
    "lower": 212.26954114801427
  },
  {
    "middle": 214.35706330727666,
    "upper": 217.99197609024955,
    "lower": 210.72215052430377
  },
  {
    "middle": 212.68781918277412,
    "upper": 216.7472406874497,
    "lower": 208.62839767809854
  },
  {
    "middle": 211.22040783203371,
    "upper": 215.19488718624174,
    "lower": 207.2459284778257
  },
  {
    "middle": 210.00608327660194,
    "upper": 213.77611469538917,
    "lower": 206.23605185781472
  },
  {
    "middle": 208.81978963121128,
    "upper": 212.3958179081198,
    "lower": 205.24376135430276
  },
  {
    "middle": 207.7912382377626,
    "upper": 211.12966368698025,
    "lower": 204.45281278854495
  },
  {
    "middle": 206.62826316749948,
    "upper": 209.97284607179537,
    "lower": 203.2836802632036
  },
  {
    "middle": 205.71700000869,
    "upper": 208.9191246225563,
    "lower": 202.51487539482372
  },
  {
    "middle": 205.06490476976714,
    "upper": 208.5478169222468,
    "lower": 201.58199261728748
  },
  {
    "middle": 204.51872336312266,
    "upper": 207.87634430035436,
    "lower": 201.16110242589096
  },
  {
    "middle": 203.88836875711098,
    "upper": 207.1502276006195,
    "lower": 200.62650991360246
  },
  {
    "middle": 203.39995268500516,
    "upper": 206.54262564416285,
    "lower": 200.25727972584747
  },
  {
    "middle": 203.05614766738563,
    "upper": 206.07455333062754,
    "lower": 200.03774200414372
  },
  {
    "middle": 202.57080027049176,
    "upper": 205.48336536740948,
    "lower": 199.65823517357404
  },
  {
    "middle": 202.2078669113973,
    "upper": 205.00617549862326,
    "lower": 199.40955832417134
  },
  {
    "middle": 201.7975938722166,
    "upper": 204.75407160071995,
    "lower": 198.84111614371324
  },
  {
    "middle": 201.34829921771978,
    "upper": 204.1491291733728,
    "lower": 198.54746926206676
  },
  {
    "middle": 200.8713183398417,
    "upper": 203.57706529992942,
    "lower": 198.165571379754
  },
  {
    "middle": 199.67595468842822,
    "upper": 203.37512695250717,
    "lower": 195.97678242434927
  },
  {
    "middle": 198.74776852762554,
    "upper": 202.4340235652966,
    "lower": 195.06151348995448
  },
  {
    "middle": 198.0708381916612,
    "upper": 201.68946772556515,
    "lower": 194.45220865775727
  },
  {
    "middle": 197.53456788769347,
    "upper": 200.92833446820703,
    "lower": 194.1408013071799
  },
  {
    "middle": 197.15508523172267,
    "upper": 200.53047515418487,
    "lower": 193.77969530926046
  },
  {
    "middle": 196.82412473346338,
    "upper": 200.06097566367936,
    "lower": 193.5872738032474
  },
  {
    "middle": 196.44277952075257,
    "upper": 199.48394535794694,
    "lower": 193.4016136835582
  },
  {
    "middle": 196.3387052806809,
    "upper": 199.48675453415584,
    "lower": 193.19065602720596
  },
  {
    "middle": 196.29597144442556,
    "upper": 199.459215772553,
    "lower": 193.13272711629813
  },
  {
    "middle": 196.25064083067073,
    "upper": 199.22256072598543,
    "lower": 193.27872093535603
  },
  {
    "middle": 195.98677027536877,
    "upper": 198.931498181152,
    "lower": 193.04204236958554
  },
  {
    "middle": 195.86898263009556,
    "upper": 198.66523774530046,
    "lower": 193.07272751489066
  },
  {
    "middle": 195.7243176177055,
    "upper": 198.4179472213899,
    "lower": 193.0306880140211
  },
  {
    "middle": 195.67152546363832,
    "upper": 198.2687921069543,
    "lower": 193.07425882032234
  },
  {
    "middle": 195.65042780043467,
    "upper": 198.10896777941906,
    "lower": 193.19188782145028
  },
  {
    "middle": 195.55419658134565,
    "upper": 197.8628825624316,
    "lower": 193.2455106002597
  },
  {
    "middle": 195.62522547836036,
    "upper": 198.0770428613377,
    "lower": 193.17340809538302
  },
  {
    "middle": 195.66187067089746,
    "upper": 197.96650631557708,
    "lower": 193.35723502621784
  },
  {
    "middle": 195.4988353689072,
    "upper": 197.84600744911887,
    "lower": 193.15166328869554
  },
  {
    "middle": 195.3456129528208,
    "upper": 197.54706782501128,
    "lower": 193.14415808063032
  },
  {
    "middle": 195.34698314779024,
    "upper": 197.48929253276168,
    "lower": 193.2046737628188
  },
  {
    "middle": 195.32917522895306,
    "upper": 197.31525367542736,
    "lower": 193.34309678247877
  }
]

describe('KeltnerChannels', function() {
  it('should calculate KeltnerChannels using the calculate method', function() {
    var out  = KeltnerChannels.calculate(input);
    require('fs').writeFileSync('keltner', JSON.stringify(out, null, 2))
    assert.deepEqual(out, expectResult, 'Wrong Results');
  });

  it('should be able to calculate KeltnerChannels by using getResult', function() {
    let keltnerchannels = new KeltnerChannels(input);
    assert.deepEqual(keltnerchannels.getResult(),  expectResult, 'Wrong Results while calculating next bar');
  });

  it('should be able to calculate KeltnerChannels for reversed input by using calculate method', function() {
    let myInput = Object.assign({}, input);
    myInput.reversedInput = true;
    myInput.high.reverse();
    myInput.low.reverse();
    myInput.close.reverse();
    assert.deepEqual(KeltnerChannels.calculate(myInput),  expectResult.slice().reverse(), 'Wrong Results while calculating next bar');
  });
})
