binance_bot/src/js/tradingbot/worker/DefaultWorker.js

60 lines
1.9 KiB
JavaScript

import TradingPair from "../../apiwrapper/assets/TradingPair.js";
import Transaction from "../../apiwrapper/transaction/Transaction.js";
import TransactionPhase from "../../apiwrapper/transaction/TransactionPhase.js";
import AssetData from "../data/asset/AssetData.js";
import AbstractStrategy from "../strategy/AbstractStrategy.js";
import AbstractWorker from "./AbstractWorker.js";
export default class DefaultWorker extends AbstractWorker{
/**
* @param {string} name
* @param {TradingPair} tradingPair
* @param {AbstractStrategy} strategy
*/
constructor(name, tradingPair, strategy){
super(name, tradingPair, strategy);
}
/**
* @param {string} dataPath path to data directory
*/
onInit(dataPath){
this.getLogger().debug("Worker " + this.getName() + " initialized!");
}
/**
* @param {AssetData} assetData
* @param {Transaction[]} transactions
*/
onUpdate(assetData, transactions){
this.getStrategy().setAssetData(assetData);
const price = this.getStrategy().getAssetData().getOrderBook().getBestBid().price;
if(this.getStrategy().shouldBuy()){
this.getApiProvider().requestBuy(this, Math.round(50 / price), price);
}
for(let transaction of transactions){
if(transaction.phase == TransactionPhase.BUY_DONE && this.getStrategy().shouldSell(transaction)){
this.getApiProvider().requestSell(this, transaction);
} else if(this.getStrategy().shouldCancle()){
this.getApiProvider().requestCancle(this, transaction);
}
}
}
/**
* @returns {string} string representation of this object
*/
toJson() {
return super.toJson();
}
/**
* @param {string} objString
* @returns {AbstractWorker}
*/
static fromJson(objString) {
return super.fromJson(objString, new DefaultWorker());
}
}