290 lines
12 KiB
JavaScript
290 lines
12 KiB
JavaScript
class Strategy{
|
|
constructor(strat_type, name, side, take_profit, stop_loss, conditions) {
|
|
// The type of alert.
|
|
this.type = strat_type;
|
|
// The source of the alert.
|
|
this.name = name;
|
|
// buy|sell: string
|
|
this.side = side;
|
|
// Conditions to exit trade in profit. {on: ('profit'|'condition'), trig_val: 999}
|
|
this.take_profit = take_profit;
|
|
// Conditions to exit trade in loss. {on: ('loss'|'condition'), trig_val: 999}
|
|
this.stop_loss = stop_loss;
|
|
// The conditions to evaluate. { on: (signal_name: str), trig_val: (true|false|changed) }
|
|
this.conditions = conditions;
|
|
// html formatted representation.
|
|
this.display_output = this.format_html();
|
|
// any last results needed for comparison.
|
|
this.last_result = {};
|
|
}
|
|
format_html(){
|
|
let html ='<li>' + '<span>' + this.name + '</span>';
|
|
html +='<span>' + this.type + '</span>';
|
|
html +='<span>' + this.side + '</span>';
|
|
if (this.take_profit.on == 'profit'){
|
|
if(this.side == 'buy') {var then_do ='sell';}
|
|
else if(this.side == 'sell') {var then_do ='buy';}
|
|
html += '<span>' + 'If profit exceeds' + ': ' + this.take_profit.trig_val + ' '+ then_do + '.</span>';
|
|
}
|
|
if (this.stop_loss.on == 'loss'){
|
|
if(this.side == 'buy') {var then_do ='sell';}
|
|
else if(this.side == 'sell') {var then_do ='buy';}
|
|
html +='<span>' + 'If loss exceeds' + ': ' + this.stop_loss.trig_val + ' '+ then_do + '.</span>';
|
|
}
|
|
for(let cond of this.conditions){
|
|
html += '<span>If ' + cond.on + ' is ' + cond.trig_val + '</span>';
|
|
}
|
|
html += '</li>';
|
|
return html;
|
|
}
|
|
conditions_satisfied(signals){
|
|
let result = true;
|
|
for(let cond of this.conditions){
|
|
if (cond.trig_val == 'true'){
|
|
if (signals[cond.on] == true){
|
|
result = result && true;
|
|
}else{
|
|
result = false;
|
|
}
|
|
}
|
|
else if (cond.trig_val == 'false'){
|
|
if (signals[cond.on] == false){
|
|
result = result && true;
|
|
}else{
|
|
result = false;
|
|
}
|
|
}
|
|
else if (cond.trig_val == 'changed'){
|
|
// If no last result exists for this trigger, create one.
|
|
if ( !this.last_results[cond.on] ){
|
|
this.last_results[cond.on] = signals[cond.on];
|
|
}
|
|
if (signals[cond.on] != this.last_results[cond.on]){
|
|
result = result && true;
|
|
}else{
|
|
result = false;
|
|
}
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
name(){
|
|
return this.name;
|
|
}
|
|
type(){
|
|
return this.type;
|
|
}
|
|
strategy(){
|
|
return this.strategy;
|
|
}
|
|
print_html(){
|
|
return this.display_output;
|
|
}
|
|
}
|
|
class Strategies {
|
|
constructor(target_id) {
|
|
// The list of strategies.
|
|
this.strategies = [];
|
|
// The html element id that displays the strategies.
|
|
this.target_id = target_id;
|
|
// The html element that displays the the strategies.
|
|
this.target = null;
|
|
|
|
}
|
|
// Call to display Create new signal dialog.
|
|
open_form() { document.getElementById("new_strat_form").style.display = "grid"; }
|
|
// Call to hide Create new signal dialog.
|
|
close_form() { document.getElementById("new_strat_form").style.display = "none"; }
|
|
|
|
open_stg_form(){
|
|
this.open_form();
|
|
this.fill_field('strat_opt', 'take_profit');
|
|
// condition = { on: (signal_name: str), trig_val: (true|false|changed) }
|
|
let cond1 = {on: 'signal1', trig_val:'changed'}
|
|
// take_profit = {on: ('profit'|'condition'), trig_val: 999}
|
|
let take_profit = {on: 'signal2', trig_val: false}
|
|
// stop_loss = {on: ('loss'|'condition'), trig_val: 999}
|
|
let stop_loss = {on: 'loss', trig_val: '80%' }
|
|
let side='buy';
|
|
let conditions = []; //_str = side + condition + take_profit
|
|
conditions.push(cond1);
|
|
this.strategies.push( new Strategy('take_profit', 'strategy_#1', side, take_profit, stop_loss, conditions) );
|
|
this.update_html();
|
|
}
|
|
fill_field(field, value){
|
|
if (field == 'strat_opt'){
|
|
let options = document.getElementById('strat_opt');
|
|
|
|
if (value == 'take_profit'){
|
|
// Clear previous content.
|
|
options.innerHTML="";
|
|
|
|
//Create a drop down for buy/sell option
|
|
let side_select_label = document.createElement("label");
|
|
side_select_label.for = 'trade_in_side';
|
|
side_select_label.innerHTML = 'Side:';
|
|
let side_select = document.createElement("select");
|
|
side_select.id = "trade_in_side";
|
|
side_select.name = "trade_in_side";
|
|
side_select.innerHTML = '<option>buy</option><option>sell</option>';
|
|
options.appendChild(side_select_label);
|
|
options.appendChild(side_select);
|
|
|
|
//Create an input for margin trading value. (1X-100X)
|
|
let margin_label = document.createElement("label");
|
|
margin_label.for = 'margin';
|
|
margin_label.innerHTML = 'Margin:';
|
|
let margin_select = document.createElement("input");
|
|
margin_select.name = 'margin';
|
|
margin_select.type = 'number';
|
|
margin_select.min = 1;
|
|
margin_select.max = 100;
|
|
margin_select.value = 1;
|
|
options.appendChild(margin_label);
|
|
options.appendChild(margin_select);
|
|
|
|
// Create a un ordered list to hold the conditions of trading in.
|
|
let ul_in_cond = document.createElement('ul');
|
|
ul_in_cond.id ='trade_in_conditions';
|
|
|
|
// Create a submit button for the conditions.
|
|
let add_cond_btn = document.createElement('button');
|
|
add_cond_btn.setAttribute('id','add_cond_btn');
|
|
add_cond_btn.setAttribute('type','button');
|
|
add_cond_btn.innerHTML = "Add Condition";
|
|
add_cond_btn.onclick = function () {
|
|
let li = document.createElement('li');
|
|
li.innerHTML = 'Trigger:'+ trigger.value+' Value:' + trigVal.value;
|
|
ul_in_cond.appendChild(li);
|
|
};
|
|
|
|
// Add a horizontal rule.
|
|
let element = document.createElement('hr');
|
|
options.appendChild(element);
|
|
|
|
//Create a drop down for trigger options
|
|
let trigger_label = document.createElement("label");
|
|
trigger_label.for = 'trigger';
|
|
trigger_label.innerHTML = 'Trigger Signal:';
|
|
let trigger = document.createElement("select");
|
|
trigger.id = "trigger";
|
|
trigger.name = "trigger";
|
|
// Populate the signal selector.
|
|
for (let signal in window.UI.signals.signals){
|
|
let opt = document.createElement('option');
|
|
opt.value = window.UI.signals.signals[signal].name;
|
|
opt.innerHTML = window.UI.signals.signals[signal].name;
|
|
trigger.appendChild(opt);
|
|
}
|
|
// Add it to the dom.
|
|
options.appendChild(trigger_label);
|
|
options.appendChild(trigger);
|
|
|
|
//Create a drop down for trigger value.
|
|
let trigVal_label = document.createElement("label");
|
|
trigVal_label.for = 'trigVal';
|
|
trigVal_label.innerHTML = 'Trigger Value:';
|
|
let trigVal = document.createElement("select");
|
|
trigVal.id = "trigVal";
|
|
trigVal.name = "trigVal";
|
|
|
|
let opt = document.createElement('option');
|
|
opt.value = true;
|
|
opt.innerHTML = 'true';
|
|
trigVal.appendChild(opt);
|
|
|
|
opt = document.createElement('option');
|
|
opt.value = false;
|
|
opt.innerHTML = 'false';
|
|
trigVal.appendChild(opt);
|
|
|
|
opt = document.createElement('option');
|
|
opt.value = 'changed';
|
|
opt.innerHTML = 'changed';
|
|
trigVal.appendChild(opt);
|
|
|
|
// Add trigger Value select element to the dom.
|
|
options.appendChild(trigVal_label);
|
|
options.appendChild(trigVal);
|
|
|
|
// Add the submit btn and the list to the dom.
|
|
options.appendChild(add_cond_btn);
|
|
options.appendChild(ul_in_cond);
|
|
|
|
// Add a horizontal rule.
|
|
element = document.createElement('hr');
|
|
options.appendChild(element);
|
|
|
|
//Create an input for take profit value.
|
|
let prof_value_lbl = document.createElement("label");
|
|
prof_value_lbl.for = 'profit_val';
|
|
prof_value_lbl.innerHTML = 'Profit %:';
|
|
let profit_val = document.createElement("input");
|
|
profit_val.type='number';
|
|
profit_val.min=0; profit_val.max=100; profit_val.value = 50;
|
|
|
|
//Create a drop down for take profit type.
|
|
let prof_typ_label = document.createElement("label");
|
|
prof_typ_label.for = 'prof_typ';
|
|
prof_typ_label.innerHTML = 'Profit type:';
|
|
let prof_typ = document.createElement("select");
|
|
prof_typ.id = "prof_typ";
|
|
prof_typ.name = "prof_typ";
|
|
prof_typ.onchange= function(){
|
|
if (this.value == 'value'){
|
|
prof_value_lbl.style.display = 'inline-block';
|
|
profit_val.style.display = 'inline-block';
|
|
//profit_trig.style.display = 'none';
|
|
}
|
|
else if (this.value == 'conditional'){
|
|
prof_value_lbl.style.display = 'none';
|
|
profit_val.style.display = 'none';
|
|
//profit_trig.style.display = 'block';
|
|
}
|
|
};
|
|
|
|
opt = document.createElement('option');
|
|
opt.value = 'value';
|
|
opt.innerHTML = 'value';
|
|
prof_typ.appendChild(opt);
|
|
|
|
opt = document.createElement('option');
|
|
opt.value = 'conditional';
|
|
opt.innerHTML = 'conditional';
|
|
prof_typ.appendChild(opt);
|
|
|
|
// Add profit_type select element to the dom.
|
|
options.appendChild(prof_typ_label);
|
|
options.appendChild(prof_typ);
|
|
|
|
opt = document.createElement('br');
|
|
options.appendChild(opt);
|
|
|
|
// Add value input to the dom.
|
|
options.appendChild(prof_value_lbl);
|
|
options.appendChild(profit_val);
|
|
|
|
|
|
}
|
|
if (value == 'incremental_profits'){
|
|
options.innerHTML="Incremental_profits -> not done.";
|
|
}
|
|
if (value == 'swing'){
|
|
options.innerHTML="swing -> not done.";
|
|
}
|
|
}
|
|
}
|
|
set_target(){
|
|
// This is called after the html document has been parsed.
|
|
this.target = document.getElementById(this.target_id);
|
|
}
|
|
update_html(){
|
|
let strats ='';
|
|
for (let strat of this.strategies){
|
|
strats += strat.print_html();
|
|
}
|
|
this.target.innerHTML = strats;
|
|
}
|
|
|
|
}
|