295 lines
24 KiB
Plaintext
295 lines
24 KiB
Plaintext
//+------------------------------------------------------------------+
|
|
//| SilverTrend.mq5 |
|
|
//| Ramdass - Conversion only |
|
|
//+------------------------------------------------------------------+
|
|
#property copyright "SilverTrend rewritten by CrazyChart"
|
|
#property link "http://viac.ru/"
|
|
//---- номер версии индикатора
|
|
#property version "1.01"
|
|
//---- отрисовка индикатора в главном окне
|
|
#property indicator_chart_window
|
|
//---- для расчёта и отрисовки индикатора использовано пять буферов
|
|
#property indicator_buffers 5
|
|
//---- использовано всего одно графическое построение
|
|
#property indicator_plots 1
|
|
//---- в качестве индикатора использованы цветные свечи
|
|
#property indicator_type1 DRAW_COLOR_CANDLES
|
|
#property indicator_color1 clrLime,clrTeal,clrGray,clrPurple,clrRed
|
|
//---- отображение метки индикатора
|
|
#property indicator_label1 "SilverTrendOpen;SilverTrendHigh;SilverTrendLow;SilverTrendClose"
|
|
|
|
//+----------------------------------------------+
|
|
//| Входные параметры индикатора |
|
|
//+----------------------------------------------+
|
|
input int SSP=9;
|
|
input int RISK=3; //степень риска
|
|
input uint NumberofBar=1;//Номер бара для подачи сигнала
|
|
input bool SoundON=true; //Разрешение алерта
|
|
input uint NumberofAlerts=2;//Количество алертов
|
|
input bool EMailON=false; //Разрешение почтовой отправки сигнала
|
|
input bool PushON=false; //Разрешение отправки сигнала на мобильный
|
|
//+----------------------------------------------+
|
|
//---- объявление динамических массивов, которые будут в дальнейшем использованы в качестве индикаторных буферов
|
|
double ExtOpenBuffer[];
|
|
double ExtHighBuffer[];
|
|
double ExtLowBuffer[];
|
|
double ExtCloseBuffer[];
|
|
double ExtColorsBuffer[];
|
|
//----
|
|
int K;
|
|
//---- объявление целых переменных начала отсчета данных
|
|
int min_rates_total;
|
|
//+------------------------------------------------------------------+
|
|
//| Custom indicator initialization function |
|
|
//+------------------------------------------------------------------+
|
|
void OnInit()
|
|
{
|
|
//---- инициализация переменных начала отсчета данных
|
|
min_rates_total=SSP+1;
|
|
//---- превращение динамических массивов в индикаторные буферы
|
|
SetIndexBuffer(0,ExtOpenBuffer,INDICATOR_DATA);
|
|
SetIndexBuffer(1,ExtHighBuffer,INDICATOR_DATA);
|
|
SetIndexBuffer(2,ExtLowBuffer,INDICATOR_DATA);
|
|
SetIndexBuffer(3,ExtCloseBuffer,INDICATOR_DATA);
|
|
//---- превращение динамического массива в цветовой, индексный буфер
|
|
SetIndexBuffer(4,ExtColorsBuffer,INDICATOR_COLOR_INDEX);
|
|
//---- осуществление сдвига начала отсчёта отрисовки индикатора 1
|
|
PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,min_rates_total);
|
|
//---- индексация элементов в буферах как в таймсериях
|
|
ArraySetAsSeries(ExtOpenBuffer,true);
|
|
ArraySetAsSeries(ExtHighBuffer,true);
|
|
ArraySetAsSeries(ExtLowBuffer,true);
|
|
ArraySetAsSeries(ExtCloseBuffer,true);
|
|
ArraySetAsSeries(ExtColorsBuffer,true);
|
|
|
|
//---- Установка формата точности отображения индикатора
|
|
IndicatorSetInteger(INDICATOR_DIGITS,_Digits);
|
|
//---- имя для окон данных и метка для подокон
|
|
string short_name="SilverTrend";
|
|
IndicatorSetString(INDICATOR_SHORTNAME,short_name);
|
|
//----
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| 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[])
|
|
{
|
|
//---- проверка количества баров на достаточность для расчета
|
|
if(rates_total<min_rates_total) return(0);
|
|
|
|
//---- объявления локальных переменных
|
|
int limit,trend;
|
|
double Range,AvgRange,smin,smax,SsMax,SsMin,price;
|
|
static int trend_prev;
|
|
|
|
//---- расчеты необходимого количества копируемых данных
|
|
//---- и стартового номера limit для цикла пересчета баров
|
|
if(prev_calculated>rates_total || prev_calculated<=0)// проверка на первый старт расчета индикатора
|
|
{
|
|
K=33-RISK;
|
|
limit=rates_total-min_rates_total; // стартовый номер для расчета всех баров
|
|
trend_prev=0;
|
|
}
|
|
else
|
|
{
|
|
limit=rates_total-prev_calculated; // стартовый номер для расчета новых баров
|
|
}
|
|
|
|
//---- индексация элементов в массивах как в таймсериях
|
|
ArraySetAsSeries(open,true);
|
|
ArraySetAsSeries(high,true);
|
|
ArraySetAsSeries(low,true);
|
|
ArraySetAsSeries(close,true);
|
|
ArraySetAsSeries(spread,true);
|
|
|
|
//---- основной цикл расчета индикатора
|
|
for(int bar=limit; bar>=0 && !IsStopped(); bar--)
|
|
{
|
|
ExtOpenBuffer[bar]=open[bar];
|
|
ExtHighBuffer[bar]=high[bar];
|
|
ExtLowBuffer[bar]=low[bar];
|
|
ExtCloseBuffer[bar]=close[bar];
|
|
ExtColorsBuffer[bar]=2;
|
|
//----
|
|
trend=trend_prev;
|
|
Range=0;
|
|
AvgRange=0;
|
|
for(int iii=bar; iii<=bar+SSP; iii++) AvgRange+=MathAbs(high[iii]-low[iii]);
|
|
Range=AvgRange/(SSP+1);
|
|
//----
|
|
SsMax=low[bar];
|
|
SsMin=close[bar];
|
|
//----
|
|
for(int kkk=bar; kkk<=bar+SSP-1; kkk++)
|
|
{
|
|
price=high[kkk];
|
|
if(SsMax<price) SsMax=price;
|
|
price=low[kkk];
|
|
if(SsMin>=price) SsMin=price;
|
|
}
|
|
//----
|
|
smin=SsMin+(SsMax-SsMin)*K/100;
|
|
smax=SsMax-(SsMax-SsMin)*K/100;
|
|
|
|
if(close[bar]<smin) trend=-1;
|
|
if(close[bar]>smax) trend=+1;
|
|
//----
|
|
if(trend>0)
|
|
{
|
|
if(open[bar]<=close[bar]) ExtColorsBuffer[bar]=0;
|
|
else ExtColorsBuffer[bar]=1;
|
|
}
|
|
//----
|
|
if(trend<0)
|
|
{
|
|
if(open[bar]>=close[bar]) ExtColorsBuffer[bar]=4;
|
|
else ExtColorsBuffer[bar]=3;
|
|
}
|
|
//----
|
|
if(bar>0) trend_prev=trend;
|
|
}
|
|
//---
|
|
BuySignal("SilverTrend",ExtColorsBuffer,rates_total,prev_calculated,close,spread);
|
|
SellSignal("SilverTrend",ExtColorsBuffer,rates_total,prev_calculated,close,spread);
|
|
//---
|
|
return(rates_total);
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Получение таймфрейма в виде строки |
|
|
//+------------------------------------------------------------------+
|
|
string GetStringTimeframe(ENUM_TIMEFRAMES timeframe)
|
|
{return(StringSubstr(EnumToString(timeframe),7,-1));}
|
|
//+------------------------------------------------------------------+
|
|
//| TimeFramesCheck() |
|
|
//+------------------------------------------------------------------+
|
|
bool TimeFramesCheck(string IndName,
|
|
ENUM_TIMEFRAMES TFrame) //Период графика индикатора (таймфрейм)
|
|
{
|
|
//--- проверка периодов графиков на корректность
|
|
if(TFrame<Period() && TFrame!=PERIOD_CURRENT)
|
|
{
|
|
Print("Период графика для индикатора "+IndName+" не может быть меньше периода текущего графика!");
|
|
Print("Следует изменить входные параметры индикатора!");
|
|
return(false);
|
|
}
|
|
//---
|
|
return(true);
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Buy signal function |
|
|
//+------------------------------------------------------------------+
|
|
void BuySignal(string SignalSirname,// текст имени индикатора для почтовых и пуш-сигналов
|
|
double &ColorBuff[],// индикаторный буфер с сигналами для покупки
|
|
const int Rates_total, // текущее количество баров
|
|
const int Prev_calculated, // количество баров на предыдущем тике
|
|
const double &Close[], // цена закрытия
|
|
const int &Spread[]) // спред
|
|
{
|
|
//---
|
|
static uint counter=0;
|
|
if(Rates_total!=Prev_calculated) counter=0;
|
|
|
|
bool BuySignal=false;
|
|
bool SeriesTest=ArrayGetAsSeries(ColorBuff);
|
|
int index,index1;
|
|
if(SeriesTest)
|
|
{
|
|
index=int(NumberofBar);
|
|
index1=index+1;
|
|
}
|
|
else
|
|
{
|
|
index=Rates_total-int(NumberofBar)-1;
|
|
index1=index-1;
|
|
}
|
|
|
|
if(ColorBuff[index1]>1 && ColorBuff[index]<2) BuySignal=true;
|
|
if(BuySignal && counter<=NumberofAlerts)
|
|
{
|
|
counter++;
|
|
MqlDateTime tm;
|
|
TimeToStruct(TimeCurrent(),tm);
|
|
string text=TimeToString(TimeCurrent(),TIME_DATE)+" "+string(tm.hour)+":"+string(tm.min);
|
|
SeriesTest=ArrayGetAsSeries(Close);
|
|
if(SeriesTest) index=int(NumberofBar);
|
|
else index=Rates_total-int(NumberofBar)-1;
|
|
double Ask=Close[index];
|
|
double Bid=Close[index];
|
|
SeriesTest=ArrayGetAsSeries(Spread);
|
|
if(SeriesTest) index=int(NumberofBar);
|
|
else index=Rates_total-int(NumberofBar)-1;
|
|
Bid+=Spread[index]*_Point;
|
|
string sAsk=DoubleToString(Ask,_Digits);
|
|
string sBid=DoubleToString(Bid,_Digits);
|
|
string sPeriod=GetStringTimeframe(ChartPeriod());
|
|
if(SoundON) Alert("BUY signal \n Ask=",Ask,"\n Bid=",Bid,"\n currtime=",text,"\n Symbol=",Symbol()," Period=",sPeriod);
|
|
if(EMailON) SendMail(SignalSirname+": BUY signal alert","BUY signal at Ask="+sAsk+", Bid="+sBid+", Date="+text+" Symbol="+Symbol()+" Period="+sPeriod);
|
|
if(PushON) SendNotification(SignalSirname+": BUY signal at Ask="+sAsk+", Bid="+sBid+", Date="+text+" Symbol="+Symbol()+" Period="+sPeriod);
|
|
}
|
|
|
|
//---
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Sell signal function |
|
|
//+------------------------------------------------------------------+
|
|
void SellSignal(string SignalSirname, // текст имени индикатора для почтовых и пуш-сигналов
|
|
double &ColorBuff[], // индикаторный буфер с сигналами для покупки
|
|
const int Rates_total, // текущее количество баров
|
|
const int Prev_calculated, // количество баров на предыдущем тике
|
|
const double &Close[], // цена закрытия
|
|
const int &Spread[]) // спред
|
|
{
|
|
//---
|
|
static uint counter=0;
|
|
if(Rates_total!=Prev_calculated) counter=0;
|
|
|
|
bool SellSignal=false;
|
|
bool SeriesTest=ArrayGetAsSeries(ColorBuff);
|
|
int index,index1;
|
|
if(SeriesTest)
|
|
{
|
|
index=int(NumberofBar);
|
|
index1=index+1;
|
|
}
|
|
else
|
|
{
|
|
index=Rates_total-int(NumberofBar)-1;
|
|
index1=index-1;
|
|
}
|
|
|
|
if(ColorBuff[index1]<3 && ColorBuff[index]>2) SellSignal=true;
|
|
if(SellSignal && counter<=NumberofAlerts)
|
|
{
|
|
counter++;
|
|
MqlDateTime tm;
|
|
TimeToStruct(TimeCurrent(),tm);
|
|
string text=TimeToString(TimeCurrent(),TIME_DATE)+" "+string(tm.hour)+":"+string(tm.min);
|
|
SeriesTest=ArrayGetAsSeries(Close);
|
|
if(SeriesTest) index=int(NumberofBar);
|
|
else index=Rates_total-int(NumberofBar)-1;
|
|
double Ask=Close[index];
|
|
double Bid=Close[index];
|
|
SeriesTest=ArrayGetAsSeries(Spread);
|
|
if(SeriesTest) index=int(NumberofBar);
|
|
else index=Rates_total-int(NumberofBar)-1;
|
|
Bid+=Spread[index]*_Point;
|
|
string sAsk=DoubleToString(Ask,_Digits);
|
|
string sBid=DoubleToString(Bid,_Digits);
|
|
string sPeriod=GetStringTimeframe(ChartPeriod());
|
|
if(SoundON) Alert("SELL signal \n Ask=",Ask,"\n Bid=",Bid,"\n currtime=",text,"\n Symbol=",Symbol()," Period=",sPeriod);
|
|
if(EMailON) SendMail(SignalSirname+": SELL signal alert","SELL signal at Ask="+sAsk+", Bid="+sBid+", Date="+text+" Symbol="+Symbol()+" Period="+sPeriod);
|
|
if(PushON) SendNotification(SignalSirname+": SELL signal at Ask="+sAsk+", Bid="+sBid+", Date="+text+" Symbol="+Symbol()+" Period="+sPeriod);
|
|
}
|
|
//---
|
|
}
|
|
//+------------------------------------------------------------------+
|