Price Data Components
Indicators Used
Miscellaneous
1
Views
0
Downloads
0
Favorites
ichimokusuppresmtf_ad
//+------------------------------------------------------------------+
//| ichimokusuppresmtf_ad.mq4 |
//+------------------------------------------------------------------+
#property copyright "Scriptong"
#property link "http://advancetools.net"
#property description "English: Support and resistance levels based on Ichimoku cloud. Multitimeframe version.\nRussian: Óðîâíè ïîääåðæêè è ñîïðîòèâëåíèÿ íà îñíîâå îáëàêà Ichimoku. Ìóëüòèïåðèîäíàÿ âåðñèÿ."
#property version "2.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 0
#define PREFIX "ICSUREMTF_"
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
enum ENUM_YESNO
{
NO, // No / Íåò
YES // Yes / Äà
};
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
enum ENUM_CUSTOM_TF
{
TF_NOT_USED = 0, // Not used / Íå èñïîëüçîâàòü
TF_M1 = PERIOD_M1, // 1 Minute / 1 Ìèíóòà
TF_M5 = PERIOD_M5, // 5 Minutes / 5 Ìèíóò
TF_M15 = PERIOD_M15, // 15 Minutes / 15 Ìèíóò
TF_M30 = PERIOD_M30, // 30 Minutes / 30 Ìèíóò
TF_H1 = PERIOD_H1, // 1 Hour / 1 ×àñ
TF_H4 = PERIOD_H4, // 4 Hours / 4 ×àñà
TF_D1 = PERIOD_D1, // 1 Day / 1 Äåíü
TF_W1 = PERIOD_W1, // 1 Week / 1 Íåäåëÿ
TF_MN1 = PERIOD_MN1, // 1 Month / 1 Ìåñÿö
};
input uint i_tenkanSenPeriod = 9; // Tenkan-sen period / Ïåðèîä ðàñ÷åòà Tenkan-sen
input uint i_kijunSenPeriod = 26; // Kijun-sen period / Ïåðèîä ðàñ÷åòà Kijun-sen
input uint i_senkouSpanPeriod = 52; // Senkou Span B period / Ïåðèîä ðàñ÷åòà Senkou Span B
input uint i_fractalRank = 13; // Fractal rank / Ðàíã ôðàêòàëà
input string i_string0End = ""; //
input ENUM_CUSTOM_TF i_tf1 = TF_H4; // Time frame 1 / Òàéìôðåéì 1
input color i_support1Color = clrDodgerBlue; // Color of support lines / Öâåò ëèíèé ïîääåðæêè
input ENUM_LINE_STYLE i_support1Style = STYLE_DOT; // Style of support lines / Òèï ëèíèé ïîääåðæêè
input uint i_support1Width = 0; // Thickness of support lines / Òîëùèíà ëèíèé ïîääåðæêè
input color i_resistance1Color = clrMaroon; // Color of resistance lines / Öâåò ëèíèé ñîïðîòèâëåíèÿ
input ENUM_LINE_STYLE i_resistance1Style = STYLE_DOT; // Style of resistance lines / Òèï ëèíèé ñîïðîòèâëåíèÿ
input uint i_resistance1Width = 0; // Thickness of resistance lines / Òîëùèíà ëèíèé ñîïðîòèâëåíèÿ
input string i_string1End = ""; //
input ENUM_CUSTOM_TF i_tf2 = TF_D1; // Time frame 2 / Òàéìôðåéì 2
input color i_support2Color = clrTurquoise; // Color of support lines / Öâåò ëèíèé ïîääåðæêè
input ENUM_LINE_STYLE i_support2Style = STYLE_SOLID; // Style of support lines / Òèï ëèíèé ïîääåðæêè
input uint i_support2Width = 1; // Thickness of support lines / Òîëùèíà ëèíèé ïîääåðæêè
input color i_resistance2Color = clrBrown; // Color of resistance lines / Öâåò ëèíèé ñîïðîòèâëåíèÿ
input ENUM_LINE_STYLE i_resistance2Style = STYLE_SOLID; // Style of resistance lines / Òèï ëèíèé ñîïðîòèâëåíèÿ
input uint i_resistance2Width = 1; // Thickness of resistance lines / Òîëùèíà ëèíèé ñîïðîòèâëåíèÿ
input string i_string2End = ""; //
input ENUM_CUSTOM_TF i_tf3 = TF_W1; // Time frame 3 / Òàéìôðåéì 3
input color i_support3Color = clrBlue; // Color of support lines / Öâåò ëèíèé ïîääåðæêè
input ENUM_LINE_STYLE i_support3Style = STYLE_SOLID; // Style of support lines / Òèï ëèíèé ïîääåðæêè
input uint i_support3Width = 2; // Thickness of support lines / Òîëùèíà ëèíèé ïîääåðæêè
input color i_resistance3Color = clrRed; // Color of resistance lines / Öâåò ëèíèé ñîïðîòèâëåíèÿ
input ENUM_LINE_STYLE i_resistance3Style = STYLE_SOLID; // Style of resistance lines / Òèï ëèíèé ñîïðîòèâëåíèÿ
input uint i_resistance3Width = 2; // Thickness of resistance lines / Òîëùèíà ëèíèé ñîïðîòèâëåíèÿ
input string i_string3End = ""; //
input int i_indBarsCount=10000; // Number of bars to display / Êîë-âî áàðîâ îòîáðàæåíèÿ
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
enum ENUM_MESSAGE_CODE
{
MESSAGE_CODE_INVALID_FRACTAL_RANK1,
MESSAGE_CODE_INVALID_FRACTAL_RANK2,
MESSAGE_CODE_MA_SLOW_LESS_THAN_ZERO,
MESSAGE_CODE_TERMINAL_FATAL_ERROR1,
MESSAGE_CODE_NOT_ENOUGHT_MEMORY
};
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
struct TFParam
{
ENUM_TIMEFRAMES tf;
color supportColor;
ENUM_LINE_STYLE supportStyle;
uint supportWidth;
color resistanceColor;
ENUM_LINE_STYLE resistanceStyle;
uint resistanceWidth;
};
// Global variables
bool g_activate;
int g_tfsCnt,
g_centerBar,
g_sidesBars,
g_maxPeriod;
TFParam g_tfParam[];
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
//| Custom indicator initialization function |
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
int OnInit()
{
g_activate=false;
if(!TuningParameters())
return INIT_FAILED;
g_tfsCnt=0;
if(!IsTFCopy(i_tf1,i_support1Color,i_support1Style,i_support1Width,i_resistance1Color,i_resistance1Style,i_resistance1Width) ||
!IsTFCopy(i_tf2,i_support2Color,i_support2Style,i_support2Width,i_resistance2Color,i_resistance2Style,i_resistance2Width) ||
!IsTFCopy(i_tf3,i_support3Color,i_support3Style,i_support3Width,i_resistance3Color,i_resistance3Style,i_resistance3Width))
return INIT_FAILED;
g_activate=true;
return INIT_SUCCEEDED;
}
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
//| Checking the correctness of values of tuning parameters |
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
bool TuningParameters()
{
string name=WindowExpertName();
if(i_fractalRank<3)
{
Alert(name,GetStringByMessageCode(MESSAGE_CODE_INVALID_FRACTAL_RANK1));
return false;
}
if(i_fractalRank%2==0)
{
Alert(name,GetStringByMessageCode(MESSAGE_CODE_INVALID_FRACTAL_RANK2));
return false;
}
g_centerBar = (int)i_fractalRank / 2 + 1;
g_sidesBars = (int)i_fractalRank / 2;
g_maxPeriod=(int)MathMax(i_tenkanSenPeriod,MathMax(i_kijunSenPeriod,i_senkouSpanPeriod));
return true;
}
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
//| Copying the timeframe parameters to array |
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
bool IsTFCopy(ENUM_CUSTOM_TF tf,color supportColor,ENUM_LINE_STYLE supportStyle,uint supportWidth,color resistanceColor,ENUM_LINE_STYLE resistanceStyle,uint resistanceWidth)
{
if(tf==TF_NOT_USED || (int)tf<Period())
return true;
if(ArrayResize(g_tfParam,g_tfsCnt+1,3)!=g_tfsCnt+1)
{
Alert(WindowExpertName(),GetStringByMessageCode(MESSAGE_CODE_NOT_ENOUGHT_MEMORY));
return false;
}
g_tfParam[g_tfsCnt].tf=(ENUM_TIMEFRAMES)tf;
g_tfParam[g_tfsCnt].supportColor = supportColor;
g_tfParam[g_tfsCnt].supportStyle = supportStyle;
g_tfParam[g_tfsCnt].supportWidth = supportWidth;
g_tfParam[g_tfsCnt].resistanceColor = resistanceColor;
g_tfParam[g_tfsCnt].resistanceStyle = resistanceStyle;
g_tfParam[g_tfsCnt].resistanceWidth = resistanceWidth;
g_tfsCnt++;
return true;
}
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
void OnDeinit(const int reason)
{
ObjectsDeleteAll(0,PREFIX);
}
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
//| Determination of bar index which needed to recalculate |
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
int GetRecalcIndex(int &total,const int ratesTotal,const int prevCalculated)
{
total=ratesTotal-g_maxPeriod-2;
if(i_indBarsCount>0 && i_indBarsCount<total)
total=MathMin(i_indBarsCount,total);
if(prevCalculated<ratesTotal-1)
{
ObjectsDeleteAll(0,PREFIX);
return total;
}
return (MathMin(ratesTotal - prevCalculated, total));
}
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
//| Checking of chart data availability |
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
bool IsSymbolDataReady(ENUM_TIMEFRAMES tf)
{
iTime(NULL,tf,0);
return GetLastError() == ERR_NO_ERROR;
}
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
//| The cast of timeframe to string representation |
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
string TFToString(ENUM_TIMEFRAMES tf)
{
switch(tf)
{
case PERIOD_M1: return "M1";
case PERIOD_M5: return "M5";
case PERIOD_M15: return "M15";
case PERIOD_M30: return "M30";
case PERIOD_H1: return "H1";
case PERIOD_H4: return "H4";
case PERIOD_D1: return "D1";
case PERIOD_W1: return "W1";
case PERIOD_MN1: return "MN1";
}
return "M" + IntegerToString(tf / 60);
}
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
//| Finding the up fractal |
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
bool IsUpFractal(ENUM_TIMEFRAMES tf,int barIndex)
{
int totalBars=iBars(NULL,tf);
if(barIndex-g_sidesBars<=0 || barIndex+g_sidesBars>=totalBars)
return false;
double curHigh=iHigh(NULL,tf,barIndex);
// On right of bar
for(int i=barIndex-g_sidesBars; i<barIndex; i++)
if(iHigh(NULL,tf,i)>=curHigh)
return false;
// On left of bar
int leftBar=barIndex+g_sidesBars;
for(int i=barIndex+1; i<=leftBar; i++)
{
double high=iHigh(NULL,tf,i);
if(high>curHigh)
return false;
if(high==curHigh)
{
leftBar++;
if(leftBar>=totalBars)
return false;
}
}
return true;
}
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
//| Finding the down fractal |
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
bool IsDownFractal(ENUM_TIMEFRAMES tf,int barIndex)
{
int totalBars=iBars(NULL,tf);
if(barIndex-g_sidesBars<=0 || barIndex+g_sidesBars>=totalBars)
return false;
double curLow=iLow(NULL,tf,barIndex);
// On right of bar
for(int i=barIndex-g_sidesBars; i<barIndex; i++)
if(iLow(NULL,tf,i)<=curLow)
return false;
// On left of bar
int leftBar=barIndex+g_sidesBars;
for(int i=barIndex+1; i<=leftBar; i++)
{
double low=iLow(NULL,tf,i);
if(low<curLow)
return false;
if(low==curLow)
{
leftBar++;
if(leftBar>=totalBars)
return false;
}
}
return true;
}
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
//| Add bars value to time and cast the result to time |
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
datetime AddBarsToTime(ENUM_TIMEFRAMES tf,datetime startTime,int addBars)
{
int startBar = iBarShift(NULL, tf, startTime);
int barsSumm = startBar - addBars;
int barsCount= iBars(NULL,tf);
datetime curTimeBar=iTime(NULL,tf,0);
if(barsSumm>=0 && barsSumm<barsCount && startTime<=curTimeBar)
return iTime(NULL, tf, barsSumm);
int periodSeconds=(int)tf*60;
if(startTime>curTimeBar)
return startTime + addBars * periodSeconds;
if(barsSumm<0)
return curTimeBar + (-barsSumm) * periodSeconds;
return iTime(NULL, tf, barsCount - 1);
}
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
//| Finding the bar at current TF with specified extremum type and returning it opening time |
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
datetime GetExtremumTimeAtCurrentTF(datetime startTime,datetime endTime,int mode)
{
int startBarIndex=iBarShift(NULL,PERIOD_CURRENT,startTime);
int endBarIndex=-1;
if(endTime<=iTime(NULL,PERIOD_CURRENT,0))
endBarIndex=iBarShift(NULL,PERIOD_CURRENT,endTime);
double extPrice = 0.0;
int extBarIndex = -1;
for(int i=startBarIndex; i>endBarIndex; i--)
{
if(mode==MODE_HIGH && iHigh(NULL,PERIOD_CURRENT,i)>extPrice)
{
extBarIndex=i;
extPrice=iHigh(NULL,PERIOD_CURRENT,i);
}
if(mode==MODE_LOW && (iLow(NULL,PERIOD_CURRENT,i)<extPrice || extBarIndex<0))
{
extBarIndex=i;
extPrice=iLow(NULL,PERIOD_CURRENT,i);
}
}
if(extBarIndex<0)
return startTime;
return iTime(NULL, PERIOD_CURRENT, extBarIndex);
}
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
//| Show the trend line |
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
void ShowTrendLine(string name,datetime time1,double price1,datetime time2,double price2,string toolTip,color clr,int width,ENUM_LINE_STYLE style)
{
if(ObjectFind(0,name)<0)
{
ObjectCreate(0,name,OBJ_TREND,0,time1,price1,time2,price2);
ObjectSetInteger(0,name,OBJPROP_COLOR,clr);
ObjectSetInteger(0,name,OBJPROP_BACK,false);
ObjectSetInteger(0,name,OBJPROP_WIDTH,width);
ObjectSetInteger(0,name,OBJPROP_STYLE,style);
ObjectSetInteger(0,name,OBJPROP_RAY,false);
ObjectSetInteger(0,name,OBJPROP_HIDDEN,true);
ObjectSetInteger(0,name,OBJPROP_SELECTABLE,false);
ObjectSetString(0,name,OBJPROP_TOOLTIP,toolTip);
return;
}
ObjectMove(0,name,0,time1,price1);
ObjectMove(0,name,1,time2,price2);
ObjectSetInteger(0,name,OBJPROP_COLOR,clr);
ObjectSetString(0,name,OBJPROP_TOOLTIP,toolTip);
}
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
//| Show support and resistance lines |
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
void ShowSupportAndResistanceLines(int barIndex,const TFParam &tfParam)
{
datetime time=iTime(NULL,PERIOD_CURRENT,barIndex);
int tfBarIndex=iBarShift(NULL,tfParam.tf,time);
// Finding the fractals
int fractalIndex=tfBarIndex+g_centerBar;
bool isUpFractal = IsUpFractal(tfParam.tf, fractalIndex);
bool isDnFractal = IsDownFractal(tfParam.tf, fractalIndex);
if(!isUpFractal && !isDnFractal)
return;
// Calculation parameters of line
datetime startTime=iTime(NULL,tfParam.tf,fractalIndex);
datetime endTime=AddBarsToTime(tfParam.tf,startTime,i_kijunSenPeriod);
double senkouA = iIchimoku(NULL, tfParam.tf, i_tenkanSenPeriod, i_kijunSenPeriod, i_senkouSpanPeriod, MODE_SENKOUSPANA, fractalIndex - i_kijunSenPeriod);
double senkouB = iIchimoku(NULL, tfParam.tf, i_tenkanSenPeriod, i_kijunSenPeriod, i_senkouSpanPeriod, MODE_SENKOUSPANB, fractalIndex - i_kijunSenPeriod);
// Support line
if(isDnFractal)
{
datetime fractalCurTime=(tfParam.tf==Period())? startTime : GetExtremumTimeAtCurrentTF(startTime,iTime(NULL,tfParam.tf,fractalIndex-1),MODE_LOW);
ShowTrendLine(PREFIX+"SUPPORT_"+IntegerToString(int(tfParam.tf))+"_"+IntegerToString(startTime),fractalCurTime,iLow(NULL,tfParam.tf,fractalIndex),endTime,MathMin(senkouA,senkouB),
"Support of "+TFToString(tfParam.tf),tfParam.supportColor,tfParam.supportWidth,tfParam.supportStyle);
}
// Resistance line
if(isUpFractal)
{
datetime fractalCurTime=(tfParam.tf==Period())? startTime :GetExtremumTimeAtCurrentTF(startTime,iTime(NULL,tfParam.tf,fractalIndex-1),MODE_HIGH);
ShowTrendLine(PREFIX+"RESISTANCE_"+IntegerToString(int(tfParam.tf))+"_"+IntegerToString(startTime),fractalCurTime,iHigh(NULL,tfParam.tf,fractalIndex),endTime,MathMax(senkouA,senkouB),
"Resistance of "+TFToString(tfParam.tf),tfParam.resistanceColor,tfParam.resistanceWidth,tfParam.resistanceStyle);
}
}
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
//| Calculation of indicators values |
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
void CalcIndicatorData(int limit,int total)
{
for(int barIndex=limit; barIndex>=0; barIndex--)
for(int tfIndex=0; tfIndex<g_tfsCnt; tfIndex++)
ShowSupportAndResistanceLines(barIndex,g_tfParam[tfIndex]);
}
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
//| 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[])
{
static bool isComment=false;
if(!g_activate)
return rates_total;
for(int i=0; i<g_tfsCnt; i++)
if(!IsSymbolDataReady((ENUM_TIMEFRAMES)g_tfParam[i].tf))
{
Comment("Waiting data of timeframe ",TFToString((ENUM_TIMEFRAMES)g_tfParam[i].tf),". If no new ticks press refresh chart.");
isComment=true;
return prev_calculated;
}
if(isComment)
{
Comment("");
isComment=false;
}
int total;
int limit=GetRecalcIndex(total,rates_total,prev_calculated);
CalcIndicatorData(limit,total);
return rates_total;
}
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
//| Getting string by code of message and terminal language |
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
string GetStringByMessageCode(ENUM_MESSAGE_CODE messageCode)
{
string language=TerminalInfoString(TERMINAL_LANGUAGE);
if(language=="Russian")
return GetRussianMessage(messageCode);
return GetEnglishMessage(messageCode);
}
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
//| Getting string by code of message for russian language |
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
string GetRussianMessage(ENUM_MESSAGE_CODE messageCode)
{
switch(messageCode)
{
case MESSAGE_CODE_INVALID_FRACTAL_RANK1: return ": ðàíã ôðàêòàëà äîëæåí áûòü 3 è áîëåå. Èíäèêàòîð îòêëþ÷åí.";
case MESSAGE_CODE_INVALID_FRACTAL_RANK2: return ": ðàíã ôðàêòàëà äîëæåí áûòü íå÷åòíûì ÷èñëîì. Èíäèêàòîð îòêëþ÷åí.";
case MESSAGE_CODE_TERMINAL_FATAL_ERROR1: return ": ôàòàëüíàÿ îøèáêà òåðìèíàëà - ïóíêò ðàâåí íóëþ. Èíäèêàòîð îòêëþ÷åí.";
case MESSAGE_CODE_NOT_ENOUGHT_MEMORY: return ": íå õâàòàåò ïàìÿòè. Èíäèêàòîð îòêëþ÷åí.";
}
return "";
}
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
//| Getting string by code of message for english language |
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
string GetEnglishMessage(ENUM_MESSAGE_CODE messageCode)
{
switch(messageCode)
{
case MESSAGE_CODE_INVALID_FRACTAL_RANK1: return ": fractal rank must me 3 or more. The indicator is turned off.";
case MESSAGE_CODE_INVALID_FRACTAL_RANK2: return ": fractal rank must be odd number. The indicator is turned off.";
case MESSAGE_CODE_TERMINAL_FATAL_ERROR1: return ": terminal fatal error - point equals to zero. The indicator is turned off.";
case MESSAGE_CODE_NOT_ENOUGHT_MEMORY: return ": not enought memory. The indicator is turned off.";
}
return "";
}
//+------------------------------------------------------------------+
Comments
Markdown Formatting Guide
# H1
## H2
### H3
**bold text**
*italicized text*
[title](https://www.example.com)

`code`
```
code block
```
> blockquote
- Item 1
- Item 2
1. First item
2. Second item
---