Hello All
I'm attempting to get this indicator to update every 15 minutes, but it's not working, and switching the timeframe period manually makes it work. The objective is to print the line on the last previous LOW that is above the SMA, and the indication Print line once this LOW it come below the SMA . same vice versa for last high below SMA and print once this high it come above SMA Do you have any clue what's wrong with the code?
Thanks on advance . .
int limit = rates_total - prev_calculated; for(int i = limit-1; i >= 0; i--) if (( Low[i]) > iMA(NULL, PERIOD_CURRENT, 10, 0, MODE_SMA, PRICE_CLOSE, i) && ( Low[i]) < iMA(NULL, PERIOD_CURRENT, 10, 0, MODE_SMA, PRICE_CLOSE, 0) )
I'd say this is wrong. It works well when prev_calculated == 0, but won't handle new bars properly.
Let's break down the scenarios for limit and i:
Scenario | Result |
---|---|
when rates_total - prev_calculated == rates_total | limit works out to be Bars and i works out to Bars - 1. This is fine, although your while condition (i >= 0) means you'll be checking the currently open bar when i == 0. |
when rates_total - prev_calculated == 0 | limit works out to 0 and i works out to -1. Not necessarily a bad thing because it ignores the currently open bar on new ticks, which means no repainting. However, on a new bar, i == 0, and you're checking the new bar's value. Probably not your intention. |
when rates_total - prev_calculated == 1 | limit works out to 1 and i works out to 0. Here's the issue - you're only looking at the currently open bar, and it's only happening once on the close of a bar. Also, when i == 0, Low[i] means that you're checking the Low for the currently open bar. |
if (( Low[i]) > iMA(NULL, PERIOD_CURRENT, 10, 0, MODE_SMA, PRICE_CLOSE, i) && ( Low[i]) < iMA(NULL, PERIOD_CURRENT, 10, 0, MODE_SMA, PRICE_CLOSE, 0) )
Are you sure you want to compare every Low[i] value in your loop to the iMA(SMA) value of the currently open bar?
----
BTW, based on your code (lack of a double array, lack of SetIndexBuffer(0, array)), this seems unnecessary
#property indicator_buffers 1 #property indicator_color1 clrNONE
I'd say this is wrong. It works well when prev_calculated == 0, but won't handle new bars properly.
Let's break down the scenarios for limit and i:
Scenario | Result |
---|---|
when rates_total - prev_calculated == rates_total | limit works out to be Bars and i works out to Bars - 1. This is fine, although your while condition (i >= 0) means you'll be checking the currently open bar when i == 0. |
when rates_total - prev_calculated == 0 | limit works out to 0 and i works out to -1. Not necessarily a bad thing because it ignores the currently open bar on new ticks, which means no repainting. However, on a new bar, i == 0, and you're checking the new bar's value. Probably not your intention. |
when rates_total - prev_calculated == 1 | limit works out to 1 and i works out to 0. Here's the issue - you're only looking at the currently open bar, and it's only happening once on the close of a bar. Also, when i == 0, Low[i] means that you're checking the Low for the currently open bar. |
Are you sure you want to compare every Low[i] value in your loop to the iMA(SMA) value of the currently open bar?
----
BTW, based on your code (lack of a double array, lack of SetIndexBuffer(0, array)), this seems unnecessary
Hi Alexender
Thank you for responding so quickly. I'm a little perplexed by your example. Could you please provide a code example? Apart from your query, yes, I want the indicator to prints from the last low above the SMA line and meets the conditions like this low[i] is above SMA in previous and below SMA on current bar ..
Looking forward to hear from you . ..
Hi Alexender
Thank you for responding so quickly. I'm a little perplexed by your example. Could you please provide a code example? Apart from your query, yes, I want the indicator to prints from the last low above the SMA line and meets the conditions like this low[i] is above SMA in previous and below SMA on current bar ..
Looking forward to hear from you . ..
When | int i equals | Result |
---|---|---|
rates_total - prev_calculated == rates_total | rates_total - 1 | Your loops checks every bar, including the currently open candle |
rates_total - prev_calculated == 0 | -1 | Your loop checks nothing, since -1 < 0 |
rates_total - prev_calculated == 1 | 0 | Your loop checks 1 bar, the new bar that just opened. It will not check again until another new bar. |
Here's an example of how I would do it. I take it you want a repainting indicator.
int OnCalculate(const int rates_total, const int prev_calculated, const datetime &time[], const double &open[], const double &high[], const double &low[], const double &close[], const long &tick_volume[], const long &volume[], const int &spread[]) { const bool isFirstRun = prev_calculated == 0; if (isFirstRun) { scan(Bars - 2); //--- Bars - 2 because we have to check index of Bars - 1; } else { scan(0); //--- Check value of currently open bar (and repaint if conditions change) } return rates_total; } void scan(int lookback) { for (int i = lookback; i >= 0; i--) { const int prev = i + 1; const double prevLow = Low[prev]; const double prevSMA = SimpleMA(prev); const double currentSMA = SimpleMA(i); const bool isPrevLowAbovePrevSMA = prevLow > prevSMA; const bool isPrevLowBelowCurrentSMA = prevLow < currentSMA; if (isPrevLowAbovePrevSMA && isPrevLowBelowCurrentSMA) { //--- do something } } } double SimpleMA(const int index) { const double value = iMA(NULL, PERIOD_CURRENT, 10, 0, MODE_SMA, PRICE_CLOSE, index); return value; }
When | int i equals | Result |
---|---|---|
rates_total - prev_calculated == rates_total | rates_total - 1 | Your loops checks every bar, including the currently open candle |
rates_total - prev_calculated == 0 | -1 | Your loop checks nothing, since -1 < 0 |
rates_total - prev_calculated == 1 | 0 | Your loop checks 1 bar, the new bar that just opened. It will not check again until another new bar. |
Here's an example of how I would do it. I take it you want a repainting indicator.
Thanks Alex
I just finished compiling it. However, it appears to be functioning in the last LOW[] on the chart which suppose the last low[] in the right side was above the SMA Line.
Thanks Alex
I just finished compiling it. However, it appears to be functioning in the last LOW[] on the chart which suppose the last low[] in the right side was above the SMA Line.
No problem!
The loop itself should be fixed and should fix the issue of "not refreshing."
Don't worry about the following logic as it was only my interpretation of what you're trying to do and I didn't quite understand what you were trying to do, so it's probably not what you want.
const int prev = i + 1; const double prevLow = Low[prev]; const double prevSMA = SimpleMA(prev); const double currentSMA = SimpleMA(i); const bool isPrevLowAbovePrevSMA = prevLow > prevSMA; const bool isPrevLowBelowCurrentSMA = prevLow < currentSMA;Fix the logic according to your needs.
No problem!
The loop itself should be fixed and should fix the issue of "not refreshing."
Don't worry about the following logic as it was only my interpretation of what you're trying to do and I didn't quite understand what you were trying to do, so it's probably not what you want.
Thanks Alex !!
It Works Perfectly .. Your time and support are much appreciated.
See How to do your lookbacks correctly #9 — #14 & #19. (2016)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello All
I'm attempting to get this indicator to update every 15 minutes, but it's not working, and switching the timeframe period manually makes it work. The objective is to print the line on the last previous LOW that is above the SMA, and the indication Print line once this LOW it come below the SMA . same vice versa for last high below SMA and print once this high it come above SMA Do you have any clue what's wrong with the code?
Thanks on advance . .