What you need to do is first locate the last crossover of the price with the SMA(50). To do this, you can compare Close[i] with the value of iMA(...) and scroll back through the candles until you find where it changes from up to down (or vice versa).
Once you have that index, simply start adding High[] and Low[] from the candle following that crossover to the most recent closed candle.
Something like that
// Bullish: price was below MA and crossed up if(close[i-1] <= sma_values[i-1] && close[i] > sma_values[i]) // Bearish: price was aboxe MA and crossed down if(close[i-1] >= sma_values[i-1] && close[i] < sma_values[i]) return i;
Improperly formatted code edited by moderator. Please always use the CODE button (Alt-S) when inserting code.
Something like that
Improperly formatted code edited by moderator. Please always use the CODE button (Alt-S) when inserting code.
this isn't a good approach because
sma_values[i]
is the current repainting value
i want to calculate sum high and lows just after latest(most recent) price crossing sma 50 but i don't know how to get that index of candle that price cross MA in most recent cross
thanks
Hello,
To calculate the sum of Highs and Lows after the most recent price crossing of the SMA50, follow these steps:
-
Identify the latest crossover: Look for the last candle where price crossed above or below the SMA50 (previous candle on one side, current candle on the other).
-
Determine the starting index: Record the index of this last crossover; this will be the starting point for summing.
-
Sum Highs and Lows: From that candle to the most recent one, add up the High and Low values of each candle.
This approach ensures you analyze price behavior immediately after the most recent SMA50 crossover, providing accurate insights for trading or statistical purposes.
Best regards,
Hamid Marwi
Hi i Made that with custom Function for get index of candle that cross over happen but i have a problem in for loop for start and end in loop to show fractals from cross over and also future code for calculating high and lows
#property indicator_chart_window #property indicator_buffers 2 #property indicator_plots 2 //--- plot Swing High #property indicator_label1 "Swing High" #property indicator_type1 DRAW_ARROW #property indicator_color1 clrRed #property indicator_width1 2 //--- plot Swing Low #property indicator_label2 "Swing Low" #property indicator_type2 DRAW_ARROW #property indicator_color2 clrBlue #property indicator_width2 2 //--- input parameters input int InpRangeBars = 2; // Number of bars to check on each side //--- indicator buffers double SwingHighBuffer[]; double SwingLowBuffer[]; datetime time_alert; //used when sending alert //--- variables to store swing points double H1 = 0; double L1 = 0; datetime H1Time = 0; datetime L1Time = 0; double Open[]; int MA_handle; double MA[]; int crossoverIndex=-1; double Low[]; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- indicator buffers mapping SetIndexBuffer(0, SwingHighBuffer, INDICATOR_DATA); SetIndexBuffer(1, SwingLowBuffer, INDICATOR_DATA); //--- setting a code from the Wingdings charset as the property of PLOT_ARROW PlotIndexSetInteger(0, PLOT_ARROW, 234); PlotIndexSetInteger(1, PLOT_ARROW, 233); //--- setting indicator digits IndicatorSetInteger(INDICATOR_DIGITS, _Digits); //--- name for DataWindow and indicator subwindow label string short_name = "Improved Swing High/Low Identifier"; IndicatorSetString(INDICATOR_SHORTNAME, short_name); MA_handle = iMA(NULL, PERIOD_CURRENT, 50, 0, MODE_SMA, PRICE_CLOSE); if(MA_handle < 0) { Print("The creation of iMA has failed: MA_handle=", INVALID_HANDLE); Print("Runtime error = ", GetLastError()); return(INIT_FAILED); } return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ 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[]) { datetime Time[]; if(CopyOpen(Symbol(), PERIOD_CURRENT, 0, rates_total, Open) <= 0) return(rates_total); ArraySetAsSeries(Open, true); if(BarsCalculated(MA_handle) <= 0) return(0); if(CopyBuffer(MA_handle, 0, 0, rates_total, MA) <= 0) return(rates_total); ArraySetAsSeries(MA, true); if(CopyLow(Symbol(), PERIOD_CURRENT, 0, rates_total, Low) <= 0) return(rates_total); ArraySetAsSeries(Low, true); if(CopyTime(Symbol(), Period(), 0, rates_total, Time) <= 0) return(rates_total); ArraySetAsSeries(Time, true); FindMostRecentCross(); int start; if(prev_calculated == 0) start = InpRangeBars + crossoverIndex ; else start =rates_total ; Print( "cross index = ",crossoverIndex); for(int i = start; i <= InpRangeBars ; i--) { SwingHighBuffer[i] = EMPTY_VALUE; SwingLowBuffer[i] = EMPTY_VALUE; bool isSwingHigh = true; bool isSwingLow = true; // Check for Swing High for(int j = 1; j <= InpRangeBars; j++) { if(high[i] <= high[i+j] || high[i] <= high[i-j]) { isSwingHigh = false; break; } } // Check for Swing Low for(int j = 1; j <= InpRangeBars; j++) { if(low[i] >= low[i+j] || low[i] >= low[i-j]) { isSwingLow = false; break; } } if(isSwingHigh) { SwingHighBuffer[i] = high[i]; CreateLabel("SwingHigh_" + IntegerToString(i), time[i], high[i], "H", clrRed); if(high[i] > H1 || H1 == 0) { H1 = high[i]; H1Time = time[i]; } } if(isSwingLow) { SwingLowBuffer[i] = low[i]; CreateLabel("SwingLow_" + IntegerToString(i), time[i], low[i], "L", clrBlue); if(low[i] < L1 || L1 == 0) { L1 = low[i]; L1Time = time[i]; } } } return(rates_total); } //+------------------------------------------------------------------+ //| Create a text label on the chart | //+------------------------------------------------------------------+ void CreateLabel(const string name, const datetime time, const double price, const string text, const color clr) { ObjectCreate(0, name, OBJ_TEXT, 0, time, price); ObjectSetString(0, name, OBJPROP_TEXT, text); ObjectSetInteger(0, name, OBJPROP_COLOR, clr); ObjectSetInteger(0, name, OBJPROP_FONTSIZE, 8); ObjectSetInteger(0, name, OBJPROP_ANCHOR, ANCHOR_LEFT_LOWER); } //+------------------------------------------------------------------+ //| Custom indicator deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { ObjectsDeleteAll(0, "SwingHigh_"); ObjectsDeleteAll(0, "SwingLow_"); } int FindMostRecentCross(){ for(int i = 0; i >= 0; i++) { double closePrice = iClose(NULL, 0, i+1); double prevClosePrice = iClose(NULL, 0, i+2); double prevclosePricecck = iClose(NULL, 0, i+3); double sma50 = MA[i+1]; double prevSma50 = MA[i+2]; //int crossoverIndex=-1; //Check for crossover (price crosses above SMA 50) if(prevclosePricecck < prevSma50 && prevClosePrice > sma50 && closePrice > sma50) { crossoverIndex = i; //Print( "cross index = ",crossoverIndex); break; // Exit loop once the most recent crossover is found } if(prevclosePricecck > prevSma50 && prevClosePrice < sma50 && closePrice < sma50) { crossoverIndex = i; //Print( "cross index = ",crossoverIndex); break; // Exit loop once the most recent crossover is found } } return crossoverIndex; // Returns the index of the most recent crossover candle }
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
i want to calculate sum high and lows just after latest(most recent) price crossing sma 50 but i don't know how to get that index of candle that price cross MA in most recent cross
thanks