How to calculate `lib.ema` starting from the previous period? - javascript

Hi I need to calculate EMA in Zenbot, but starting from the previous period, how does one do that?
return function ema (s, key, length, source_key) {
if (!source_key) source_key = 'close'
if (s.lookback.length >= length) {
var prev_ema = s.lookback[0][key]
if (typeof prev_ema === 'undefined' || isNaN(prev_ema)) {
var sum = 0
s.lookback.slice(0, length).forEach(function (period) {
sum += period[source_key]
})
prev_ema = sum / length
}
var multiplier = 2 / (length + 1)
s.period[key] = (s.period[source_key] - prev_ema) * multiplier + prev_ema
}
}
}

Recently figured out the same thing, you can use this commit as a reference.
+++ b/extensions/strategies/wavetrend/strategy.js
## -0,0 +1,172 ##
...
+ onPeriod: function (s, cb) {
+ if (s.period.wto) {
+ s.signal = null // hold
+ let prev_wto = s.lookback[0].wto
+ let wto = s.period.wto
+ let prev_hcl3 = s.lookback[0].hcl3
+ let hcl3 = s.period.hcl3
+ let prev_ema = s.lookback[0].ema
+ let ema = s.period.ema
+
+ if (!s.sell_signal_close)
+ s.sell_signal_close = 0
+ if (!s.buy_signal_close)
+ s.buy_signal_close = 0
+ if (!s.sell_pct_orig)
+ s.sell_pct_orig = s.sell_pct
+ if (!s.buy_pct_orig)
+ s.buy_pct_orig = s.sell_pct

Related

How to replace NULL% by 0%

In my calculatePercents () method, I should get 0% instead of NULL%.
In my console.log, I get prints with the values NULL
calculatePercents() {
this.v.global.total.amount = this.v.global.cash.amount + this.v.global.sec.amount;
console.log("Cash => " + JSON.stringify(this.v.global.total.amount));
console.log('------------------------');
this.v.global.cash.percent = (this.v.global.cash.amount / (this.v.global.cash.amount + this.v.global.sec.amount)) * 100;
console.log(" Global cash " + JSON.stringify(this.v.global.cash.percent));
this.v.global.sec.percent = (this.v.global.sec.amount / (this.v.global.cash.amount + this.v.global.sec.amount)) * 100;
console.log(" NULL ???? ==> " + JSON.stringify(this.v.global.sec.percent));
var totPercent = (this.v.global.cash.percent + this.v.global.sec.percent);
this.v.global.total.percent = totPercent;
}
How I could get a 0 instead of NULL please.
you can use || operator to set zero
Eg:
this.v.global.total.percent = (totPercent||0);
for more https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_OR
with || if the first value is false (converted as boolean) then it will take the second, in that case it's used as a default value.
this.v.global.total.percent = totPercent || 0;
You can try this in your method;
if(this.v.global.cash.amount == null){
this.v.global.cash.amount = 0
}
if(this.v.global.sec.amount == null){
this.v.global.sec.amount = 0
}
And then you can do that; (your method)
calculatePercents() {
this.v.global.total.amount = this.v.global.cash.amount + this.v.global.sec.amount;
console.log("Cash => " + JSON.stringify(this.v.global.total.amount));
console.log('------------------------');
this.v.global.cash.percent = (this.v.global.cash.amount / (this.v.global.cash.amount + this.v.global.sec.amount)) * 100;
console.log(" Global cash " + JSON.stringify(this.v.global.cash.percent));
this.v.global.sec.percent = (this.v.global.sec.amount / (this.v.global.cash.amount + this.v.global.sec.amount)) * 100;
console.log(" NULL ???? ==> " + JSON.stringify(this.v.global.sec.percent));
var totPercent = (this.v.global.cash.percent + this.v.global.sec.percent);
this.v.global.total.percent = totPercent;
}
And your veriable type is can be "let".
Simple Code:
let a = null;
if(a==null){
a=0
}
console.log(a);
// output: 0

time diffrence in double digit

i want to display TravelTimeHoursDiff and TravelTimeMinutesDiff in double digit now my time is shown as 7:0 i want to display like 07:00
if ($scope.DispatchStatus.ArrivalTime != undefined){
var today = $rootScope.getSysDate().split(" ");
var timeArrival = new Date(today[0] + ' ' + $scope.DispatchStatus.ArrivalTime);
var TravelTime = new Date(today[0] + ' ' + $scope.Route.TravelTime);
var timeArrivalHours = timeArrival.getHours();
var TravelTimeHoursDiff = timeArrivalHours - TravelTime.getHours() ;
var TravelTimeMinutesDiff = (timeArrival.getMinutes() - TravelTime.getMinutes());
if(TravelTimeHoursDiff < 0 || (TravelTimeHoursDiff <= 0 && TravelTimeMinutesDiff < 0) || (TravelTimeHoursDiff == 0 && TravelTimeMinutesDiff == 0)){
$scope.formvalidationbit = $scope.DispatchStatusAddForm[fieldName].$invalid = true;
angular.element('#' + fieldName).addClass('ng-invalid');
angular.element('#' + fieldName).removeClass('ng-valid');
$scope.DispatchStatusAddForm.$valid = false;
var errorbit = 1;
}else{
if (isNaN(TravelTimeHoursDiff)) {
TravelTimeHoursDiff = '--';
}
if (isNaN(TravelTimeMinutesDiff)) {
TravelTimeMinutesDiff = '--';
}
if(TravelTimeMinutesDiff <0){
TravelTimeMinutesDiff = TravelTimeMinutesDiff * (-1);
}
$scope.TravelTime = TravelTimeHoursDiff + ':' + TravelTimeMinutesDiff;
}
}
Just add leading 0 to values smaller then 10, something like:
let addLeadingZero(v){
return v < 10 ? ("0" + v) : v;
}
$scope.TravelTime = addLeadingZero(TravelTimeHoursDiff) + ':' + addLeadingZero(TravelTimeMinutesDiff);

Is there a work around to reference a variable above where its declared

So I'm building a template with handlebars and I want to append const totalSum to const sumTextSidebar. But obviously it can't find it bc it hasn't been created yet. Is there a workaround for this? I included my code below. Thank you!
function multi(element) {
const responseSidebar = ($(element).data('sidebar-respond'));
const responseUnder = ($(element).data('response-under'));
const optionId = Number($(element).data('optionid')) + 1;
const questionId = $(element).data('questionid') + 1;
const responseUnderArea = '#quiz-alert-' + questionId;
const sidebarId = '#sidebar-' + (questionId - 1) + '-' + (optionId - 1);
const sidebarQuestion = '#sidebar-' + (questionId - 1);
const responseUnderID = "#" + (questionId - 1) + "-" + (optionId - 1);
const sumTextUnderQuestionID = '#sumTextUnder-' + (questionId - 1);
**const sumTextSidebar** = ($(element).data('sumTextSidebar')+ totalSum);
const sumSidebarElement = $('<li>' + sumTextSidebar + '</li>');
const values = Number($(element).val());
const sumSidebarId = '#sumTextID-' + questionId;
if ($(element).is(':checked')) {
$(sidebarId).removeClass('hidden');
$(responseUnderID).removeClass('hidden');
$(responseUnderArea).removeClass('hidden');
$(sumTextUnderQuestionID).removeClass('hidden');
if ($(sumSidebarId).length == 0) {
$(sidebarQuestion).append(sumSidebarElement).slideDown('slow');
sumSidebarElement.attr('id', 'sumTextID-' + questionId);
}
addValue.push(values)
} else {
$(sidebarId).addClass('hidden');
$(responseUnderID).addClass('hidden');
subtractValue.push(values)
}
function getSum(total, num) {
return total + num;
}
const addValueSum = addValue.reduce(getSum);
const subtractValueSum = subtractValue.reduce(getSum);
**const totalSum** = addValueSum - subtractValueSum;

Transform HH MM SS in seconds

I need to transform 3 form inputs (HH, MM, SS) in seconds with javascript.
I have this code but it has only with 1 form input in seconds : https://jsfiddle.net/94150148/hhomeLc3/
To do this I need a new javascript function.
window.onload = function () {generate()};
function generate() {
var width = 'width=\"' + document.getElementById('width').value + '\" ';
var height = 'height=\"' + document.getElementById('height').value + '\" ';
var ytid = "videoID";
var start = document.getElementById('start').value;
var end = document.getElementById('end').value;
if (start !== "") {
if(ytid === document.getElementById('ytid')) {
ytid += '?start=' + start;
}
else {
ytid += '&start=' + start;
}
}
if (end !== "") {
if (ytid === document.getElementById('ytid')) {
ytid += '?end=' + end;
}
else {
ytid += '&end=' + end;
}
}
document.getElementById('embedcode').value = '<iframe ' + width + height +
'src=\"https://www.youtube.com\/embed\/' + ytid +
'\" frameborder=\"0\"><\/iframe>';
}
function clearall() {
document.getElementById('width').value = 550;
document.getElementById('height').value = 315;
document.getElementById('start').value = "";
document.getElementById('end').value = "";
}
The jsFiddle to play with what I need : https://jsfiddle.net/94150148/ybmkcyyu/
https://jsfiddle.net/ybmkcyyu/3/
EDIT:
Do not display start and end when value is 0
https://jsfiddle.net/ybmkcyyu/6/
EDIT2:
https://jsfiddle.net/ybmkcyyu/7/
if (start !== "") {
ytid += '?start=' + start;
}
if (end !== "") {
if (start == "") {
ytid += '?end=' + end;
}
else {
ytid += '&end=' + end;
}
}
You just need to get value of every fields, as int, then add it with the formula: ((hours * 60) + minutes ) * 60 + secondes
And you might ensure that the result is a number. (if user enter a char instead of a number, it should not display something wrong)
var starth = parseInt(document.getElementById('starth').value);
var startm = parseInt(document.getElementById('startm').value);
var starts = parseInt(document.getElementById('starts').value);
var endh = parseInt(document.getElementById('endh').value);
var endm = parseInt(document.getElementById('endm').value);
var ends = parseInt(document.getElementById('ends').value);
var start = (((starth * 60) + startm) * 60) + starts;
if(isNaN(start) || start === 0)
start = "";
var end = (((endh * 60) + endm) * 60) + ends;
if(isNaN(end) || end === 0)
end = "";
/* (...) */
JS is generally quite good at math.
sHour = document.getElementById('starth').value,
sMin = document.getElementById('startm').value,
sSec = document.getElementById('starts').value,
sTime = (sHour * 3600) + (sMin * 60) + sSec;
https://jsfiddle.net/link2twenty/ybmkcyyu/4/

JavaScript addition issue

So, i'm adding 2 characters 4 levels together (hp, attack, strength and defense) and then comparing them. However I am having a problem. when the numbers are added together they're added together as a string so it outputs as follows. 9060951/99709940 instead of 246 (90+60+95+1)/308 (99+70+99+40). Here is what I am doing.
function calculate(player1, player2) {
var total1 = player1.getTotal();
var total2 = player2.getTotal();
var differencePercentage;
if(total1 > total2) {
differencePercentage = total2 + "/" + total1 + " = " + (total2/total1);
} else {
differencePercentage = total1 + "/" + total2 + " = " + (total1/total2);
}
var percentage = differencePercentage;
return percentage;
}
function Player(hp, attack, strength, defense) {
this.hp = parseInt(hp);
this.attack = parseInt(attack);
this.strength = parseInt(strength);
this.defense = parseInt(defense);
this.getTotal = function() {
var total = 0;
total = hp + attack + strength + defense;
return total;
}
}
Why is this happening?
You are parsing the Ints into this.hp, this.attack etc. in your Player function but not into the getTotal function
Try this
this.getTotal = function() {
var total = 0;
total = this.hp + this.attack + this.strength + this.defense;
return total;
}

Categories