brighter-trading/static/trade.js

59 lines
2.7 KiB
JavaScript

class Trade {
constructor() {
this.name = 'trade';
}
initialize(){
const price_input = document.getElementById('price');
const cp = document.getElementById('current_price');
window.UI.data.price_history.then((value) => { price_input.value = value[value.length-1].close;cp.value = price_input.value });
const qty_input = document.getElementById('quantity');
const tv_input = document.getElementById('trade_value');
tv_input.value = 0;
const orderType = document.getElementById('orderType');
orderType.addEventListener('change', function(){
if(this.value == 'MARKET'){
cp.style.display = "inline-block";
price_input.style.display = "none";
tv_input.value = qty_input.value * cp.value;
} else if(this.value == 'LIMIT') {
cp.style.display = "none";
price_input.style.display = "inline-block";
tv_input.value = qty_input.value * price_input.value;
}
});
price_input.addEventListener('change', function(){
if (orderType.value!='MARKET'){
tv_input.value = qty_input.value * price_input.value;
}else{
tv_input.value = qty_input.value * cp.value;
}
});
qty_input.addEventListener('change', function(){
if (orderType.value!='MARKET'){
tv_input.value = qty_input.value * price_input.value;
}else{
tv_input.value = qty_input.value * cp.value;
}
});
}
// Call to display the 'Create new trade' dialog.
open_tradeForm() { document.getElementById("new_trade_form").style.display = "grid"; }
// Call to hide the 'Create new signal' dialog.
close_tradeForm() { document.getElementById("new_trade_form").style.display = "none"; }
submitNewTrade(){
// Collect all the input fields.
var target = document.getElementById('trade_target').value; // The target to trade ['exchange'|'backtester'].
var symbol = window.UI.data.trading_pair; // The symbol to trade at.
var price = document.getElementById('price').value; // The price to trade at.
var side = document.getElementById('side').value; // The side to trade at.
var orderType = document.getElementById('orderType').value; // The orderType for the trade.
var quantity = document.getElementById('quantity').value; // The base quantity to trade.
var data = {target, symbol, price, side, orderType, quantity};
window.UI.data.comms.send_to_app( "new_trade", data);
this.close_tradeForm();
}
}