Adding Events to your strategy
#1
There are a large number of Events with interesting data that are emitted by the Gekko plugins. Some would be quite useful in strategies but they are not available for the simple reason that strategies are not plugins and therefore cannot directly pick up the triggered events.

My quest was initially to be able to read live portfolio data as I have two strategies running in parallell and I need to know if I am exposed or not. I also want the strategy to react correctly to manual changes in the portfolio. I have since added more events that have proven quite useful.

Before using this guide, please be aware that it requires changes to two core Gekko plugins, meaning that if you update Gekko your changes are lost and the strategy will not work. It may also mean you break something else if you are not careful.

That being said, it is quite easy to do. Let's add the PortfolioChange event (~ is your Gekko root folder);
Edit ~/plugins/tradingAdvisor/tradingAdvisor.js and add:
Actor.prototype.processPortfolioChange = function(portfolio) {  // This must be the handler name. The parameter name can be made up (portfolio). Just be consistent.
  this.strategy.processPortfolioChange(portfolio);  // This will be executed in baseTradingMethod.js
}


Edit ~/plugins/tradingAdvisor/baseTradingMethod.js and add:
Base.prototype.processPortfolioChange = function(portfolio) {
  this.onPortfolioChange(portfolio);  // onPortfolioChange will be accessible in the strategy
}


In your strategy, onPortfolioChange will trigger when the portfolio has changed so you can read the asset and currency variables. This is independent of your candle size so this can trigger at any moment. Add, for example;
strat.onPortfolioChange = function (portfolio) {
 log.info('Portfolio has changed; Asset=', portfolio.asset.toFixed(3), 'and Currency=', portfolio.currency.toFixed(0) );
}


That's it! 

Follow the same logic for any other Events. This adds Balance updates;
Edit ~/plugins/tradingAdvisor/tradingAdvisor.js and add:
Actor.prototype.portfolioValueChange = function(balance) {
  this.strategy.portfolioValueChange(balance);
}

Edit ~/plugins/tradingAdvisor/baseTradingMethod.js and add:
Base.prototype.portfolioValueChange = function(balance) {
  this.onBalanceChange(balance);
}

In your strategy, add:
strat.onBalanceChange = function (balance) {
  log.info('Balance has changed; it is now', balance.balance.toFixed(1) );
}
  Reply


Messages In This Thread
Adding Events to your strategy - by Hallonstedt - 11-05-2019, 09:13 AM
RE: Adding Events to your strategy - by Xavier32 - 08-03-2021, 12:57 PM
RE: Adding Events to your strategy - by Xavier32 - 08-06-2021, 03:48 PM
RE: Adding Events to your strategy - by Xavier32 - 08-07-2021, 12:35 PM
RE: Adding Events to your strategy - by lucifar - 08-31-2021, 07:30 AM
RE: Adding Events to your strategy - by lucifar - 08-31-2021, 10:55 AM
RE: Adding Events to your strategy - by lucifar - 09-01-2021, 09:31 AM
RE: Adding Events to your strategy - by lucifar - 09-01-2021, 11:29 AM
RE: Adding Events to your strategy - by lucifar - 09-01-2021, 01:12 PM
RE: Adding Events to your strategy - by Xavier32 - 09-01-2021, 07:25 PM
RE: Adding Events to your strategy - by lucifar - 09-04-2021, 08:15 AM
RE: Adding Events to your strategy - by Xavier32 - 09-11-2021, 11:09 AM
RE: Adding Events to your strategy - by Xavier32 - 09-13-2021, 09:05 AM
RE: Adding Events to your strategy - by Xavier32 - 09-13-2021, 12:10 PM
RE: Adding Events to your strategy - by Xavier32 - 09-19-2021, 03:06 PM
RE: Adding Events to your strategy - by Xavier32 - 09-22-2021, 12:10 PM

Forum Jump:


Users browsing this thread: