Options:
- Clone CSignalMA and add a shift parameter to it to go through past bars (the cleanest option).
- Replicate the logic of the moving average crossover with iMA and CopyBuffer in a test script/EA.
This will allow you to print signals from the last 200 candles without using the Strategy Tester.
Options:
- Clone CSignalMA and add a shift parameter to it to go through past bars (the cleanest option).
- Replicate the logic of the moving average crossover with iMA and CopyBuffer in a test script/EA.
This will allow you to print signals from the last 200 candles without using the Strategy Tester.
The CSignalMa already has a shift parameter:
#include <Expert\ExpertSignal.mqh> // wizard description start //+------------------------------------------------------------------+ //| Description of the class | //| Title=Signals of indicator 'Moving Average' | //| Type=SignalAdvanced | //| Name=Moving Average | //| ShortName=MA | //| Class=CSignalMA | //| Page=signal_ma | //| Parameter=PeriodMA,int,12,Period of averaging | //| Parameter=Shift,int,0,Time shift | //| Parameter=Method,ENUM_MA_METHOD,MODE_SMA,Method of averaging | //| Parameter=Applied,ENUM_APPLIED_PRICE,PRICE_CLOSE,Prices series |
For example, I add a Shift of 200 (i.e. the bar start index is shifted from 0 to 200 so that the bar index 200 is used as the current bar). Subsequent bars are treated as future bars.
CSignalMA *signal=new CSignalMA; signal.Shift(200); // start from the bar at index -200 to generate signals
>This will allow you to print signals from the last 200 candles
How can I delegate it to the Print()-statements?
I don't understand how to test or run CSignalMa in my custom code. Do I have to go through the bars or something else?
for(int...}{ ??? // cannot pass bar index to LongCondition() Print("Long-signal: ", signal.LongCondition(); ??? }
What you propose with signal.Shift(200) does not work for scanning past bars: that parameter shifts the moving average, it does not indicate which bar to evaluate. LongCondition() always works on the current bar.
If you want to print historical signals, you need to make a bar loop and replicate the CSignalMA logic with iMA/CopyBuffer, or Inherit/edit CSignalMA to add an "evaluation shift" and thus evaluate each bar in the past.
or Inherit/edit CSignalMA to add an "evaluation shift" and thus evaluate each bar in the past.
I want to inherit/edit CSignalMa and add an "evaluation shift":
class MySignalMA : public CSignalMA { protected: CiMA m_ma; // object-indicator //--- adjusted parameters int evaluation_shift; // evaluates the signal from StartIndex() - evaluation_shift ... int CSignalMA::LongCondition(void) { int result=0; int idx = StartIndex() - evaluation_shift; // ok? .. }
But I don't know how to implement this specifically and then iterate to get the desired Print() statements.
Have you done this before? Can you give me an example signal implementation with such an evaluation_shift?
Or is there another source/tutorial that explains this approach in detail?
The issue with your attempt is that StartIndex() doesn't let you choose which bar to evaluate; it always works on the current bar.
To scan history you need a loop that walks through bar indices and compares each bar with the previous one. Very simplified (pseudo-code, not compilable):
for i = N ... 1: c_curr = Close(i) c_prev = Close(i+1) ma_curr = MA(i) ma_prev = MA(i+1) // if cross up -->> long signal // if cross down -->> short signal
That's the idea: "i" is the bar you want to evaluate, and i+1 is the previous bar. If you adjust your class to work like this (instead of subtracting an "evaluation_shift" from StartIndex()), you'll be able to print historical signals.
The issue with your attempt is that StartIndex() doesn't let you choose which bar to evaluate; it always works on the current bar.
To scan history you need a loop that walks through bar indices and compares each bar with the previous one. Very simplified (pseudo-code, not compilable):
That's the idea: "i" is the bar you want to evaluate, and i+1 is the previous bar. If you adjust your class to work like this (instead of subtracting an "evaluation_shift" from StartIndex()), you'll be able to print historical signals.
Is this really the only solution? It's not a clean solution because:
We always have to provide a custom implementation from the existing CSignal implementation and implement this so called "evaluation_shift".
There must be a better, simpler solution to this problem, since the MetaTrader StrategyTester also back-tests every CSignal strategy. Perhaps there's a dedicated API for this? I think such trivial things ("finding the strategy result for bar x") should be provided in the Standard library, without having to recreate such cumbersome solutions.You have already been given the two practical ways to achieve what you want, either loop through bars and replicate the logic, or extend the class and add an evaluation method.
Forum on trading, automated trading systems and testing trading strategies
CExpertSignal: get signal results from specified bar index
Miguel Angel Vico Alba, 2025.08.31 09:58
Options:
- Clone CSignalMA and add a shift parameter to it to go through past bars (the cleanest option).
- Replicate the logic of the moving average crossover with iMA and CopyBuffer in a test script/EA.
This will allow you to print signals from the last 200 candles without using the Strategy Tester.
The issue is not that the library is missing a function, the issue is that you are struggling to implement the logic yourself.
That in itself is not a problem, but it means you should be clear about your expectations. If you do not have the programming knowledge to code this, then say it clearly. You can hire someone in the Freelance section, or simply say, "I have no idea how to program, I need someone to do it for me".
What does not help is going in circles with partial attempts while expecting a ready-made copy/paste. The community can guide you, but you need to either make a serious attempt at coding it, or request it as a job.
Also, keep in mind that you are trying to make something that was designed to work inside the Strategy Tester behave the same way outside of it. What you call trivial is actually not trivial at all. Everything has its purpose. If you want something very specific like this, do not expect that libraries intended for the Tester will also work in the way you want outside of it.
the two practical ways to achieve what you want, either loop through bars and replicate the logic, or extend the class and add an evaluation method.
Following your suggestion, I extended SignalMA with evaluation_index, and then you say in https://www.mql5.com/en/forum/494373#comment_57924707 such approach does not work and I should better interate through bars. So what? Imagine, I use the approach to iterate through the bars:// Iterate through the last 200 bars
CSignalMA *myExpertSignal=new CSignalMA; int totalBars = Bars; for(int i = totalBars - 1; i >= totalBars - 200; i--) { // Access bar data using i index double closePrice = iClose(Symbol(), Period(), i); Print("Bar ", i, ": Close=", closePrice); myExpertSignal.checkSignal(i); // SUCH METHOD DOES NOT EXIST ! // I do not want to replicate the logic of the signal with iMA and CopyBuffer, it should use signal directly! }
How can I check the CSignalMA implementation on the specified bar index?
- 2025.08.31
- www.mql5.com
myExpertSignal.checkSignal(i) does not exist and will never work because the Standard Library is not designed that way.
The signal modules only evaluate the current bar in the Tester or in live trading. There is no function to query a past index.
If you want historical results you either replicate the logic in a bar loop or extend the class and implement your own evaluation method.
That has already been explained. If you cannot program it yourself and are expecting ready-made code, the right place is the Freelance section. This will be my final comment on the matter.
myExpertSignal.checkSignal(i) does not exist and will never work because the Standard Library is not designed that way.
The signal modules only evaluate the current bar in the Tester or in live trading. There is no function to query a past index.
If you want historical results you either replicate the logic in a bar loop or extend the class and implement your own evaluation method.
That has already been explained. If you cannot program it yourself and are expecting ready-made code, the right place is the Freelance section. This will be my final comment on the matter.
> myExpertSignal.checkSignal(i) does not exist and will never work because the Standard Library is not designed that way.
Yes, I know. I can read the code. My questions and posts are very clear and concise – your answers were not!At first, you suggested I extend CSignal and add a so-called evaluation_shift. I did that and asked you how exactly I should this shift be integrated to better understand the idea of this evaluation_shift. Then you say: It doesn't work because it always operates on the current bar. So you don't seem to know what you're talking about, giving me confusing advice. It's better to say nothing before giving users confusing advice and indirectly advertising yourself as a freelancer. This isn't an advertising platform for you!
> If you want historical results you either replicate the logic in a bar loop or extend the class and implement your own evaluation method
So I am right with my assumption that Metatrader does not have such trivial straight method like "finding the strategy result for bar x".
Your suggestion, to replicate the signal logic in combination with CopyBuffer and Indicator is a very messy, cumbersome solution.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
How can I test or use classes derived from CExpertSignal without using the StrategyTester?
For example, I have CSignalMA (a child from CExpertSignal, located in Expert/Signal).
I want to have something like this:
1. go to the last 200 bars
2. show entry, exit signals derived by CSignalMA in Print()-statement
I cannot find a method in CExpertSignal which does something like this:
Has anyone already implemented something like this or is there an easy way to do this?