+
diff --git a/js/predictions.js b/js/predictions.js
index f391df1..3b3e4ae 100644
--- a/js/predictions.js
+++ b/js/predictions.js
@@ -285,7 +285,7 @@ function* generate_pattern_1_with_peak(given_prices, peak_start) {
});
}
yield {
- pattern_description: "decreasing, high spike, random lows",
+ pattern_description: "decreasing middle, high spike, random low",
pattern_number: 1,
prices: predicted_prices
};
@@ -351,7 +351,7 @@ function* generate_pattern_2(given_prices) {
max_rate -= 300;
}
yield {
- pattern_description: "always decreasing",
+ pattern_description: "consistently decreasing",
pattern_number: 2,
prices: predicted_prices
};
@@ -543,26 +543,29 @@ function* generate_pattern_3(given_prices) {
}
}
-
-function* generate_possibilities(sell_prices) {
- if (!isNaN(sell_prices[0])) {
+function* generate_possibilities(sell_prices, first_buy) {
+ if (first_buy || isNaN(sell_prices[0])) {
+ for (var buy_price = 90; buy_price <= 110; buy_price++) {
+ sell_prices[0] = sell_prices[1] = buy_price;
+ if (first_buy) {
+ yield* generate_pattern_3(sell_prices);
+ } else {
+ yield* generate_pattern_0(sell_prices);
+ yield* generate_pattern_1(sell_prices);
+ yield* generate_pattern_2(sell_prices);
+ yield* generate_pattern_3(sell_prices);
+ }
+ }
+ } else {
yield* generate_pattern_0(sell_prices);
yield* generate_pattern_1(sell_prices);
yield* generate_pattern_2(sell_prices);
yield* generate_pattern_3(sell_prices);
- } else {
- for (var buy_price = 90; buy_price <= 110; buy_price++) {
- sell_prices[0] = sell_prices[1] = buy_price;
- yield* generate_pattern_0(sell_prices);
- yield* generate_pattern_1(sell_prices);
- yield* generate_pattern_2(sell_prices);
- yield* generate_pattern_3(sell_prices);
- }
}
}
-function analyze_possibilities(sell_prices) {
- generated_possibilities = Array.from(generate_possibilities(sell_prices));
+function analyze_possibilities(sell_prices, first_buy) {
+ generated_possibilities = Array.from(generate_possibilities(sell_prices, first_buy));
global_min_max = [];
for (var day = 0; day < 14; day++) {
diff --git a/js/scripts.js b/js/scripts.js
index 508ebb2..f0eeb19 100644
--- a/js/scripts.js
+++ b/js/scripts.js
@@ -9,10 +9,12 @@ const getSellFields = function () {
const sell_inputs = getSellFields()
const buy_input = $("#buy")
-
+const first_buy_field = $("#first_buy");
//Functions
-const fillFields = function (prices) {
+const fillFields = function (prices, first_buy) {
+ first_buy_field.prop("checked", first_buy);
+
buy_input.focus();
buy_input.val(prices[0] || '')
buy_input.blur();
@@ -33,11 +35,11 @@ const fillFields = function (prices) {
const initialize = function () {
try {
const prices = getPrices()
- console.log("Prices from Storage", prices)
+ const first_buy = getFirstBuyState();
if (prices === null) {
- return
+ fillFields([], first_buy)
} else {
- fillFields(prices)
+ fillFields(prices, first_buy)
}
$(document).trigger("input");
} catch (e) {
@@ -45,14 +47,16 @@ const initialize = function () {
}
$("#reset").on("click", function () {
+ first_buy_field.prop('checked', false);
$("input").val(null).trigger("input");
})
}
-const updateLocalStorage = function (data) {
+const updateLocalStorage = function (prices, first_buy) {
try {
- if (data.length !== 14) throw "The data array needs exactly 14 elements to be valid"
- localStorage.setItem("sell_prices", JSON.stringify(data))
+ if (prices.length !== 14) throw "The data array needs exactly 14 elements to be valid"
+ localStorage.setItem("sell_prices", JSON.stringify(prices))
+ localStorage.setItem("first_buy", JSON.stringify(first_buy));
} catch (e) {
console.error(e)
}
@@ -63,6 +67,10 @@ const isEmpty = function (arr) {
return filtered.length == 0
}
+const getFirstBuyState = function () {
+ return JSON.parse(localStorage.getItem('first_buy'))
+}
+
const getPrices = function () {
let prices = JSON.parse(localStorage.getItem("sell_prices"))
if (!prices || isEmpty(prices) || prices.length !== 14) {
@@ -79,13 +87,13 @@ const getSellPrices = function () {
})
}
-const calculateOutput = function (data) {
+const calculateOutput = function (data, first_buy) {
if (isEmpty(data)) {
$("#output").html("");
return;
}
let output_possibilities = "";
- for (let poss of analyze_possibilities(data)) {
+ for (let poss of analyze_possibilities(data, first_buy)) {
var out_line = "
" + poss.pattern_description + " | "
for (let day of poss.prices.slice(1)) {
if (day.min !== day.max) {
@@ -104,9 +112,13 @@ const calculateOutput = function (data) {
const update = function () {
const sell_prices = getSellPrices();
const buy_price = parseInt(buy_input.val());
- const data = [buy_price, buy_price, ...sell_prices];
- updateLocalStorage(data);
- calculateOutput(data);
+ const first_buy = first_buy_field.is(":checked");
+
+ buy_input.prop('disabled', first_buy);
+
+ const prices = [buy_price, buy_price, ...sell_prices];
+ updateLocalStorage(prices, first_buy);
+ calculateOutput(prices, first_buy);
}
$(document).ready(initialize);