@ -10,10 +10,12 @@ const getSellFields = function () {
const sell _inputs = getSellFields ( )
const sell _inputs = getSellFields ( )
const buy _input = $ ( "#buy" )
const buy _input = $ ( "#buy" )
const first _buy _field = $ ( "#first_buy" ) ;
const first _buy _field = $ ( "#first_buy" ) ;
const previous _pattern _input = $ ( "#previous_pattern" ) ;
//Functions
//Functions
const fillFields = function ( prices , first _buy ) {
const fillFields = function ( prices , first _buy , previous _pattern ) {
first _buy _field . prop ( "checked" , first _buy ) ;
first _buy _field . prop ( "checked" , first _buy ) ;
previous _pattern _input . val ( previous _pattern ) ;
buy _input . focus ( ) ;
buy _input . focus ( ) ;
buy _input . val ( prices [ 0 ] || '' )
buy _input . val ( prices [ 0 ] || '' )
@ -36,10 +38,11 @@ const initialize = function () {
try {
try {
const prices = getPrices ( )
const prices = getPrices ( )
const first _buy = getFirstBuyState ( ) ;
const first _buy = getFirstBuyState ( ) ;
const previous _pattern = getPreviousPatternState ( ) ;
if ( prices === null ) {
if ( prices === null ) {
fillFields ( [ ] , first _buy )
fillFields ( [ ] , first _buy , previous _pattern )
} else {
} else {
fillFields ( prices , first _buy )
fillFields ( prices , first _buy , previous _pattern )
}
}
$ ( document ) . trigger ( "input" ) ;
$ ( document ) . trigger ( "input" ) ;
} catch ( e ) {
} catch ( e ) {
@ -48,15 +51,19 @@ const initialize = function () {
$ ( "#reset" ) . on ( "click" , function ( ) {
$ ( "#reset" ) . on ( "click" , function ( ) {
first _buy _field . prop ( 'checked' , false ) ;
first _buy _field . prop ( 'checked' , false ) ;
$ ( "select" ) . val ( null ) ;
$ ( "input" ) . val ( null ) . trigger ( "input" ) ;
$ ( "input" ) . val ( null ) . trigger ( "input" ) ;
} )
} )
$ ( 'select' ) . formSelect ( ) ;
}
}
const updateLocalStorage = function ( prices , first _buy ) {
const updateLocalStorage = function ( prices , first _buy , previous _pattern ) {
try {
try {
if ( prices . length !== 14 ) throw "The data array needs exactly 14 elements to be valid"
if ( prices . length !== 14 ) throw "The data array needs exactly 14 elements to be valid"
localStorage . setItem ( "sell_prices" , JSON . stringify ( prices ) )
localStorage . setItem ( "sell_prices" , JSON . stringify ( prices ) )
localStorage . setItem ( "first_buy" , JSON . stringify ( first _buy ) ) ;
localStorage . setItem ( "first_buy" , JSON . stringify ( first _buy ) ) ;
localStorage . setItem ( "previous_pattern" , JSON . stringify ( previous _pattern ) ) ;
} catch ( e ) {
} catch ( e ) {
console . error ( e )
console . error ( e )
}
}
@ -71,6 +78,10 @@ const getFirstBuyState = function () {
return JSON . parse ( localStorage . getItem ( 'first_buy' ) )
return JSON . parse ( localStorage . getItem ( 'first_buy' ) )
}
}
const getPreviousPatternState = function ( ) {
return JSON . parse ( localStorage . getItem ( 'previous_pattern' ) )
}
const getPricesFromLocalstorage = function ( ) {
const getPricesFromLocalstorage = function ( ) {
try {
try {
const sell _prices = JSON . parse ( localStorage . getItem ( "sell_prices" ) ) ;
const sell _prices = JSON . parse ( localStorage . getItem ( "sell_prices" ) ) ;
@ -112,14 +123,15 @@ const getSellPrices = function () {
} )
} )
}
}
const calculateOutput = function ( data , first _buy ) {
const calculateOutput = function ( data , first _buy , previous _pattern ) {
if ( isEmpty ( data ) ) {
if ( isEmpty ( data ) ) {
$ ( "#output" ) . html ( "" ) ;
$ ( "#output" ) . html ( "" ) ;
return ;
return ;
}
}
let output _possibilities = "" ;
let output _possibilities = "" ;
for ( let poss of analyze _possibilities ( data , first _buy )) {
for ( let poss of analyze _possibilities ( data , first _buy , previous _pattern )) {
var out _line = "<tr><td>" + poss . pattern _description + "</td>"
var out _line = "<tr><td>" + poss . pattern _description + "</td>"
out _line += ` <td> ${ Number . isFinite ( poss . probability ) ? ( ( poss . probability * 100 ) . toPrecision ( 3 ) + '%' ) : '—' } </td> ` ;
for ( let day of poss . prices . slice ( 1 ) ) {
for ( let day of poss . prices . slice ( 1 ) ) {
if ( day . min !== day . max ) {
if ( day . min !== day . max ) {
out _line += ` <td> ${ day . min } .. ${ day . max } </td> ` ;
out _line += ` <td> ${ day . min } .. ${ day . max } </td> ` ;
@ -138,15 +150,17 @@ const update = function () {
const sell _prices = getSellPrices ( ) ;
const sell _prices = getSellPrices ( ) ;
const buy _price = parseInt ( buy _input . val ( ) ) ;
const buy _price = parseInt ( buy _input . val ( ) ) ;
const first _buy = first _buy _field . is ( ":checked" ) ;
const first _buy = first _buy _field . is ( ":checked" ) ;
const previous _pattern = parseInt ( previous _pattern _input . val ( ) ) ;
buy _input . prop ( 'disabled' , first _buy ) ;
buy _input . prop ( 'disabled' , first _buy ) ;
const prices = [ buy _price , buy _price , ... sell _prices ] ;
const prices = [ buy _price , buy _price , ... sell _prices ] ;
if ( ! window . price _from _query ) {
if ( ! window . price _from _query ) {
updateLocalStorage ( prices , first _buy );
updateLocalStorage ( prices , first _buy , previous _pattern );
}
}
calculateOutput ( prices , first _buy );
calculateOutput ( prices , first _buy , previous _pattern );
}
}
$ ( document ) . ready ( initialize ) ;
$ ( document ) . ready ( initialize ) ;
$ ( document ) . on ( "input" , update ) ;
$ ( document ) . on ( "input" , update ) ;
$ ( previous _pattern _input ) . on ( "change" , update ) ;