@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();
}
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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.
@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:
Please or to participate in this conversation.