Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

TutanRamon's avatar

Dynamic binding implementations

I am planning to write a (small) stock analyzer. I want to check several technical indicators using webservices.

My idea is to have a IndicatorInterface with a couple of methods like fetchData(), evaluate() and buyScore() or something like that.

I got 3 classes right now

class RSI implements IndicatorInterface{}    //Relative Strength Index
class SMA implements IndicatorInterface{}    //Simple Moving Average
class DMI implements IndicatorInterface{}    //Directional Movement Index

The plan is to have around 20 such class implementations. Thanks to the IndicatorInterface, each class has at least a method to collect the data, one to analyze it and one to give an buyScore.

The next step is to build strategies. A Strategy consists of one or more technical indicators (the classes above) with different thresholds etc. I want to store the strategies in the database and then run them.

So, for instance, strategy A: first run SMA, output should be 0.4 at least. If so, continue to DMI etc. etc.

In my Strategy Class, I now do this (part pseudo code):

public method run()
{
  $finalScore = 0;
 
  foreach ($this->getIndicators() as $indicator) {
    App::bind('IndicatorInterface', 'App\Indicators\' . $indicator);
    $indicator = App::make('IndicatorInterface');
    $finalScore += $indicator->buyScore();

  return $finalScore;
  }
}

I am wondering if this is the way to go. Or do I need to use a different approach?

Thanks.

0 likes
7 replies
CorvS's avatar

@tutanramon Why are you binding the existing indicator to the service container? What's wrong with invoking it directly?

foreach ($this->getIndicators() as $indicator) {
    $finalScore += $indicator->buyScore();
}
TutanRamon's avatar

Hi @corvs

I am new to the interface concept and thought i was a nice way to learn this. Thanks for helping me.

$this->getIndicators() gives a collection of indicators linked to the strategy.... How can I invoke the class related to the indicator?
CorvS's avatar

@tutanramon Just to clarify things, are we talking about an Eloquent relationship here? What does getIndicators() return? Just strings that represent the names (RSI, SMA, ...)?

TutanRamon's avatar

Hi,

Yes, i have a model Strategy and a model Indicator.

class Strategy extends Model{

public function indicators()
{
  return $this->hasMany("App\Indicator");
}

// etc.

}

So, it's not getIndicators(), but indicators().

And the Indicator model contains the name of the class, ie App\Indicators\RSI and some threshold values.

CorvS's avatar
CorvS
Best Answer
Level 27

@tutanramon Alright. You may consider using STI (single table inheritance) for that, since you already have a "type" column anyway. It's pretty straightforward and if you are done setting it up you are able to iterate a strategie's indicators as mentioned above. There is a well documented and easy to use package from Caleb:

https://github.com/calebporzio/parental

TutanRamon's avatar

Thanks again @CORVS.

I was reading last night about factory design pattern. I rewrote some of the code and now have a IndicatorFactory class which returns a new instance of a given indicator. So, the whole binding part is gone now. I just loop over the collection of Indicators, initiate new instances using the new factory and make some calculations.

Thanks to the contract (interface class) I now can easily add new indicators. I think this is a good approach, don't you?

I will also take a look at the package you suggested. Maybe that's even better. Thanks.

CorvS's avatar

@tutanramon

I think this is a good approach, don't you?

Yes. The STI approach mentioned above is kinda similar to a factory pattern.

Please or to participate in this conversation.