getting last alert when attaching the indicator - page 2

 

A A Adhioermawan #:

   //--- main loop
for (int i = limit - 1; i >= 0; i--)
{
    //...
        if (Close[i] < Open[i]) // If the candle is bearish, change to Sell signal
        {
            Buffer1[i] = 0; // Clear Buy signal
            Buffer2[i] = High[i] + iATR(NULL, PERIOD_CURRENT, 14, i); // Generate Sell signal
            
                if (time[0] != last_alert_time){Alert("Sell Signal Detected"); // Alert for Sell
                last_alert_time  = time[0];}

What does time[0] have to do with the bar with index i that you analyze at each individual iteration?


I asked you for the following:

Vladislav Boyko #:
describe in ordinary human words when you need an alert

But you dodged this and sent the code. So I give up. Perhaps one of the other users will be able to guess when you want alerts.

 

Your (like many others) problem is that you start writing code first and think later.

Do you need notification about signals on bar 0 and 1? Then leave the “main loop” alone and write separate logic for alerts. You have ready-made signals in buffers, analyze them. Once you try to do this, you will be faced with the question "when exactly do I want to receive alerts?"

 
Vladislav Boyko #:

Your (like many others) problem is that you start writing code first and think later.

Do you need notification about signals on bar 0 and 1? Then leave the “main loop” alone and write separate logic for alerts. You have ready-made signals in buffers, analyze them. Once you try to do this, you will be faced with the question "when exactly do I want to receive alerts?"

No, Mr Vladislav I didn't mean to dodge your advice. The condition for alert at my code are fine and clear to me just like I said, I learn write this code from several platform include asking chatGPT then backtested it. The problem is about alert always triggered when I attach the indicator to the chart/changing timeframe. That is why I asked if anyone has similar problem with me. I shared my full code to you in case you may want to try the indicator and see my problem, I am really sorry if this action inappropriate and offense you
 
Vladislav Boyko #:
Do you need notification about signals on bar 0 and 1? Then leave the “main loop” alone and write separate logic for alerts. You have ready-made signals in buffers, analyze them.

This is the path to object-oriented thinking.

The code for generating signals should not know anything about alerts.

The code for sending alerts should not know anything about generating signals. The code for alerts should simply take the single current signal and analyze whether an alert should be sent. This may involve storing information about which signal the previous alert was sent for. A little later, you will want to combine all the alert functions and data into a single object. Because the alert functions and data are independent and self-contained, you don't want them getting in the way while you work on other code. As a bonus, you will find that your code is more portable, less cumbersome, and identifiers are shorter and more obvious.

 
A A Adhioermawan #:
I am really sorry if this action inappropriate and offense you

This is not inappropriate or offensive. You have nothing to apologize for.

It's just that I asked you for a "task" and you gave me a broken implementation with no explanation of how it's supposed to work. I see that it works strangely, but I don't know how it's supposed to work.

 
  • You can send alerts for the signal on bar #1 at the moment of opening of a new bar. That is, alerts about a new signal that has just been formed and will not be redrawn. It's quite simple and doesn't require any complicated logic.
  • You can send alerts for bar #0 (current unformed bar). On this bar, signals are redrawn and flicker. If you send an alert every time the signal on bar #0 changes, you will receive too many alerts (because the signal will flicker). If you want to avoid a huge number of alerts during the life of one bar, then you need to think about what event should lead to an alert.
 
A A Adhioermawan #:
No, Mr Vladislav I didn't mean to dodge your advice. The condition for alert at my code are fine and clear to me just like I said, I learn write this code from several platform include asking chatGPT then backtested it. The problem is about alert always triggered when I attach the indicator to the chart/changing timeframe. That is why I asked if anyone has similar problem with me. I shared my full code to you in case you may want to try the indicator and see my problem, I am really sorry if this action inappropriate and offense you

did you see my advice?

 
@Michael Charles Schefe #: i would just put in OnInit() a line "time_alert = iTime(Symbol(),PERIOD_CURRENT,0)+PeriodSeconds();"

During the OnInit() you should not depend on any function that requests data from the trade server, because it may not be connected yet.

Instead, you should wait for the first tick or valid data in the OnCalculate() and only then initialise any required variables that is dependant on that data.