461 lines
30 KiB
MQL5
461 lines
30 KiB
MQL5
//+------------------------------------------------------------------+
|
|
//| check_and_add.mq5 |
|
|
//| Copyright 2019, MetaQuotes Software Corp. |
|
|
//| https://www.mql5.com |
|
|
//+------------------------------------------------------------------+
|
|
#property copyright "Copyright 2019, MetaQuotes Software Corp."
|
|
#property link "https://www.mql5.com"
|
|
#property version "1.00"
|
|
//--- input parameters
|
|
//input int frame=5;
|
|
|
|
|
|
#include<Trade\SymbolInfo.mqh>
|
|
#include<Trade\AccountInfo.mqh>
|
|
#include<Trade\Trade.mqh>
|
|
|
|
#include<Trade\PositionInfo.mqh>
|
|
#include<Expert\Money\MoneyFixedRisk.mqh>
|
|
|
|
CTrade trade;
|
|
CSymbolInfo m_symbol;
|
|
CMoneyFixedRisk money;
|
|
CPositionInfo position;
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
enum Creation
|
|
{
|
|
Call_iAO, // использовать iAO
|
|
Call_IndicatorCreate // использовать IndicatorCreate
|
|
};
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
enum sell_or_buy1
|
|
{
|
|
sell,// продать
|
|
buy , // купить
|
|
all // оба
|
|
};
|
|
|
|
input ENUM_TIMEFRAMES frame=PERIOD_M5;
|
|
input ENUM_TIMEFRAMES frame_boom=PERIOD_M1;
|
|
input ENUM_TIMEFRAMES frame_sl=PERIOD_M15;
|
|
input ENUM_TIMEFRAMES frame_tp=PERIOD_H1;
|
|
input Creation type=Call_iAO;
|
|
input sell_or_buy1 sell_or_buy=all;
|
|
input int Kperiod=5; // K-период (количество баров для расчетов)
|
|
input int Dperiod=3; // D-период (период первичного сглаживания)
|
|
input int slowing=3; // период для окончательного сглаживания
|
|
input ENUM_MA_METHOD ma_method=MODE_SMA; // тип сглаживания
|
|
input ENUM_STO_PRICE price_field=STO_LOWHIGH;
|
|
input bool tr_stop=false;
|
|
input int max_orders=1;
|
|
input double Money_FixRisk_Percent=0.5;
|
|
bool was_send=false;
|
|
double AO[],ST[],BAND[],BAND_h[],BAND_tp[],BAND_h_tp[],iBand_bang_top[],iBand_bang_bottom[]; // массив для индикатора iMA
|
|
//---- handles for indicators
|
|
int AO_handle,ST_handle,iBand_handle,iBand_handle_tp,iBand_handle_bang;
|
|
int order_buy_c=0,order_sell_c=0;
|
|
string pos_com="cool";
|
|
|
|
bool enter_boom = false; //вошли ли мы в провал ?
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| Expert initialization function |
|
|
//+------------------------------------------------------------------+
|
|
int OnInit()
|
|
{
|
|
EventSetTimer(1);
|
|
//---
|
|
AO_handle=iAO(NULL,frame);
|
|
//--- если произошла ошибка при создании объекта, то выводим сообщение
|
|
if(AO_handle<0)
|
|
{
|
|
Print("Объект iMA не создан: MA_handle= ",INVALID_HANDLE);
|
|
Print("Ошибка исполнения = ",GetLastError());
|
|
//--- принудительное завершение программы
|
|
return(-1);
|
|
}
|
|
ST_handle=iStochastic(NULL,frame,Kperiod,Dperiod,slowing,ma_method,price_field);
|
|
//--- если произошла ошибка при создании объекта, то выводим сообщение
|
|
if(ST_handle<0)
|
|
{
|
|
Print("Объект iStochastic не создан: iStochastic= ",INVALID_HANDLE);
|
|
Print("Ошибка исполнения = ",GetLastError());
|
|
//--- принудительное завершение программы
|
|
return(-1);
|
|
}
|
|
|
|
|
|
iBand_handle=iBands(NULL,frame_sl,20,0,2.0,PRICE_CLOSE);
|
|
//--- если произошла ошибка при создании объекта, то выводим сообщение
|
|
if(iBand_handle<0)
|
|
{
|
|
Print("Объект iBands не создан: iStochastic= ",INVALID_HANDLE);
|
|
Print("Ошибка исполнения = ",GetLastError());
|
|
//--- принудительное завершение программы
|
|
return(-1);
|
|
}
|
|
iBand_handle_bang=iBands(NULL,frame_boom,20,0,2.0,PRICE_CLOSE);
|
|
//--- если произошла ошибка при создании объекта, то выводим сообщение
|
|
if(iBand_handle_bang<0)
|
|
{
|
|
Print("Объект iBands не создан: iStochastic= ",INVALID_HANDLE);
|
|
Print("Ошибка исполнения = ",GetLastError());
|
|
//--- принудительное завершение программы
|
|
return(-1);
|
|
}
|
|
iBand_handle_tp=iBands(NULL,frame_tp,20,0,2.0,PRICE_CLOSE);
|
|
//--- если произошла ошибка при создании объекта, то выводим сообщение
|
|
if(iBand_handle_tp<0)
|
|
{
|
|
Print("Объект iBands не создан: iStochastic= ",INVALID_HANDLE);
|
|
Print("Ошибка исполнения = ",GetLastError());
|
|
//--- принудительное завершение программы
|
|
return(-1);
|
|
}
|
|
|
|
m_symbol.Name(Symbol());
|
|
m_symbol.Refresh();
|
|
int digits_adjust=1;
|
|
money.Init(GetPointer(m_symbol),Period(),m_symbol.Point()*digits_adjust);
|
|
// return(INIT_FAILED);
|
|
string pr=ObjectGetString(0,"my",OBJPROP_TEXT);
|
|
double Money_FixRisk_Percent2=Money_FixRisk_Percent;
|
|
if(StringToDouble(pr)>0)Money_FixRisk_Percent2=StringToDouble(pr);
|
|
|
|
money.Percent(Money_FixRisk_Percent2);
|
|
|
|
//---
|
|
return(INIT_SUCCEEDED);
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Expert deinitialization function |
|
|
//+------------------------------------------------------------------+
|
|
void OnDeinit(const int reason)
|
|
{
|
|
//---
|
|
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Expert tick function |
|
|
//+------------------------------------------------------------------+
|
|
void OnTimer()
|
|
{
|
|
|
|
//---
|
|
if(CopyBuffer(AO_handle,0,0,3,AO)<=0) return;
|
|
//--- задаём порядок индексации массива MA[] как в таймсерии
|
|
ArraySetAsSeries(AO,true);
|
|
|
|
// сигнальная
|
|
if(CopyBuffer(ST_handle,1,0,3,ST)<=0) return;
|
|
//--- задаём порядок индексации массива MA[] как в таймсерии
|
|
ArraySetAsSeries(ST,true);
|
|
|
|
string ee=_Symbol+" "+ST[0];
|
|
|
|
long buy_id,sell_id;
|
|
ulong ticket;
|
|
//string type;
|
|
uint total=PositionsTotal();
|
|
order_buy_c=0;
|
|
order_sell_c=0;
|
|
|
|
//Alert(total);
|
|
//--- пройдем в цикле по всем ордерам
|
|
for(uint i=0;i<total;i++)
|
|
{
|
|
//--- получим тикет ордера по его позиции в списке
|
|
if((ticket=PositionGetTicket(i))>0)
|
|
{
|
|
//--- получим свойства ордера
|
|
|
|
// type =EnumToString(ENUM_ORDER_TYPE(OrderGetInteger(ORDER_TYPE)));
|
|
//--- подготовим и выведем информацию об ордере
|
|
// Alert(PositionGetInteger(POSITION_TYPE));
|
|
if(PositionGetString(POSITION_SYMBOL)==_Symbol)
|
|
{
|
|
if(PositionGetInteger(POSITION_TYPE)==1)
|
|
{
|
|
order_sell_c++;
|
|
sell_id=PositionGetInteger(POSITION_IDENTIFIER);
|
|
if(PositionGetDouble(POSITION_SL)>PositionGetDouble(POSITION_PRICE_OPEN) || tr_stop==true)
|
|
{
|
|
double stop=make_stop("sell");
|
|
|
|
if(stop<PositionGetDouble(POSITION_PRICE_OPEN) && stop<PositionGetDouble(POSITION_SL))trade.PositionModify(sell_id,stop,PositionGetDouble(POSITION_TP));
|
|
}
|
|
}
|
|
else
|
|
{
|
|
order_buy_c++;
|
|
buy_id=PositionGetInteger(POSITION_IDENTIFIER);
|
|
if(PositionGetDouble(POSITION_SL)<PositionGetDouble(POSITION_PRICE_OPEN) || tr_stop==true)
|
|
{
|
|
double stop=make_stop("buy");
|
|
if(stop>PositionGetDouble(POSITION_PRICE_OPEN) && stop>PositionGetDouble(POSITION_SL))trade.PositionModify(buy_id,stop,PositionGetDouble(POSITION_TP));
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
double lot2=0.01;
|
|
// open_order(check_ao(AO[0]));
|
|
string s_o_b=EnumToString(sell_or_buy);
|
|
|
|
if (check_enter_boom() != "null"){
|
|
Alert("boom");
|
|
|
|
open_order(check_enter_boom());
|
|
pos_com="boom";
|
|
}
|
|
|
|
|
|
if (check_enter_bang() != "null") {
|
|
Alert("bang");
|
|
pos_com="bang";
|
|
open_order(check_enter_bang());
|
|
}
|
|
|
|
|
|
|
|
if(check_ao(AO[0])==check_st(ST[0]) && check_ao(AO[0])!="null" && (s_o_b==check_ao(AO[0]) || s_o_b=="all"))
|
|
{
|
|
Alert("3xz");
|
|
string push=ee+" "+check_ao(AO[0]);
|
|
open_order(check_ao(AO[0]));
|
|
|
|
SendNotification(push);
|
|
|
|
}
|
|
|
|
// Alert(ee);
|
|
string com="";
|
|
com+=":"+order_buy_c+"-"+order_sell_c+"-"+buy_id;
|
|
com+=check_ao(AO[0])+ "st-"+check_st(ST[0]);
|
|
double line=ObjectGetDouble(0,"my",OBJPROP_PRICE);
|
|
double line_sl = ObjectGetDouble(0,"sl",OBJPROP_PRICE);
|
|
|
|
com+="-"+line;
|
|
double Ask=SymbolInfoDouble(_Symbol,SYMBOL_ASK);
|
|
|
|
if(Ask>line && s_o_b=="sell" && was_send==false){
|
|
was_send=true;
|
|
string send=_Symbol+" больше "+line;
|
|
SendNotification(send);
|
|
Alert(send);
|
|
}
|
|
|
|
|
|
if(Ask < line_sl && s_o_b=="buy" && was_send==false){
|
|
enter_boom = true;
|
|
was_send=true;
|
|
string send=_Symbol+" ASK < SL && BUY "+line;
|
|
SendNotification(send);
|
|
Alert(send);
|
|
}
|
|
|
|
|
|
|
|
if(Ask > line_sl && s_o_b=="sell" && was_send==false){
|
|
enter_boom = true;
|
|
was_send=true;
|
|
string send=_Symbol+" больше "+line;
|
|
SendNotification(send);
|
|
Alert(send);
|
|
}
|
|
|
|
|
|
Comment(com);
|
|
}
|
|
double make_stop(string in="null"){
|
|
CopyBuffer(iBand_handle,2,0,10,BAND);
|
|
CopyBuffer(iBand_handle,1,0,10,BAND_h);
|
|
ArraySetAsSeries(BAND,true);
|
|
ArraySetAsSeries(BAND_h,true);
|
|
double stop;
|
|
if(in=="sell") stop=BAND_h[0]+m_symbol.SpreadFloat()*Point()+Point()*20;
|
|
if(in=="buy") stop=BAND[0]-Point()*10;
|
|
return stop;
|
|
}
|
|
string check_enter_bang()
|
|
{
|
|
CopyBuffer(iBand_handle_bang,2,0,3,iBand_bang_bottom);
|
|
CopyBuffer(iBand_handle_bang,1,0,3,iBand_bang_top);
|
|
ArraySetAsSeries(iBand_bang_top,true);
|
|
ArraySetAsSeries(iBand_bang_bottom,true);
|
|
double diff_point_last = check_point(iBand_bang_bottom[1], iBand_bang_top[1]);
|
|
double diff_point_now = check_point(iBand_bang_bottom[0], iBand_bang_top[0]);
|
|
double Ask=SymbolInfoDouble(_Symbol,SYMBOL_ASK);
|
|
|
|
//Alert(diff_point_now + " - " + diff_point_last * 2);
|
|
|
|
if (diff_point_now > (diff_point_last * 2)) {
|
|
|
|
if (Ask > iBand_bang_top[1]) {
|
|
return "buy";
|
|
}
|
|
|
|
if (Ask < iBand_bang_bottom[1]) {
|
|
return "sell";
|
|
}
|
|
|
|
Alert("NOW > LAST *2 " );
|
|
return "buy";
|
|
}
|
|
|
|
return "null";
|
|
}
|
|
/**
|
|
Проверяем пробивку уровня стоп лосса и возвращаем необходимое действие (покупка или продажа)
|
|
*/
|
|
string check_enter_boom()
|
|
{
|
|
string result = "null";
|
|
|
|
if (enter_boom == true) {
|
|
double line_sl = ObjectGetDouble(0,"sl",OBJPROP_PRICE);
|
|
double Ask=SymbolInfoDouble(_Symbol,SYMBOL_ASK);
|
|
string s_o_b=EnumToString(sell_or_buy);
|
|
|
|
|
|
if (s_o_b == "buy") {
|
|
if (Ask > line_sl) {
|
|
//enter_boom = false;
|
|
result = "buy";
|
|
Alert("Hello");
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (s_o_b == "sell") {
|
|
if (Ask < line_sl) {
|
|
//enter_boom = false;
|
|
result = "sell";
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
bool open_order(string in="null")
|
|
{
|
|
double lot2=0.01;
|
|
|
|
// int high=iHighest(NULL,frame,MODE_HIGH,200,0);
|
|
// int low=iLowest(NULL,frame,MODE_LOW,200,0);
|
|
// iLowest(
|
|
//iHighest(
|
|
// lastday_top=iHigh(NULL,PERIOD_H4,high);
|
|
// lastday_bot=iLow(NULL,PERIOD_H4,low);
|
|
|
|
// double lastday_top=iHigh(NULL,frame,high);
|
|
// double lastday_bot=iLow(NULL,frame,low);
|
|
CopyBuffer(iBand_handle,2,0,50,BAND);
|
|
CopyBuffer(iBand_handle,1,0,50,BAND_h);
|
|
ArraySetAsSeries(BAND,true);
|
|
ArraySetAsSeries(BAND_h,true);
|
|
double lastday_top=BAND_h[ArrayMaximum(BAND_h)];
|
|
double lastday_bot=BAND[ArrayMinimum(BAND)];
|
|
double Bid=SymbolInfoDouble(_Symbol,SYMBOL_BID);
|
|
CopyBuffer(iBand_handle_tp,2,0,50,BAND_tp);
|
|
CopyBuffer(iBand_handle_tp,1,0,50,BAND_h_tp);
|
|
ArraySetAsSeries(BAND_tp,true);
|
|
ArraySetAsSeries(BAND_h_tp,true);
|
|
double lastday_top_tp=BAND_h_tp[ArrayMaximum(BAND_h_tp)];
|
|
double lastday_bot_tp=BAND_tp[ArrayMinimum(BAND_tp)];
|
|
|
|
double tp_line=ObjectGetDouble(0,"tp2",OBJPROP_PRICE);
|
|
|
|
|
|
Alert("open");
|
|
|
|
if(tp_line>0){lastday_top_tp=tp_line;lastday_bot_tp=tp_line;}
|
|
if(in=="sell" && order_sell_c<max_orders)
|
|
{
|
|
|
|
if(check_point(Bid,lastday_top)<check_point(Bid,lastday_bot_tp))
|
|
{
|
|
lastday_top=lastday_top+m_symbol.SpreadFloat()*Point()+Point()*20;
|
|
lot2=money.CheckOpenShort(SymbolInfoDouble(NULL,SYMBOL_BID),lastday_top);
|
|
if(lot2<0.01)lot2=0.01;
|
|
trade.Sell(lot2,NULL,SymbolInfoDouble(NULL,SYMBOL_BID),lastday_top,lastday_bot_tp,pos_com);
|
|
}
|
|
}
|
|
|
|
if(in=="buy" && order_buy_c<max_orders)
|
|
{
|
|
Alert("buE");
|
|
if(check_point(Bid,lastday_top_tp)>check_point(Bid,lastday_bot))
|
|
{
|
|
// lastday_bot=lastday_bot-Point()*20;
|
|
lot2=money.CheckOpenLong(SymbolInfoDouble(NULL,SYMBOL_ASK),lastday_bot);
|
|
if(lot2<0.01)lot2=0.01;
|
|
|
|
trade.Buy(lot2,NULL,SymbolInfoDouble(NULL,SYMBOL_ASK),lastday_bot,lastday_top_tp,pos_com);
|
|
}
|
|
}
|
|
enter_boom = false;
|
|
return true;
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
int check_point(double from,double to)
|
|
{
|
|
int out;
|
|
out=(from-to)/_Point;
|
|
out=MathAbs(out);
|
|
return out;
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
string check_ao(double ao)
|
|
{
|
|
pos_com="new";
|
|
string out="null";
|
|
if(ao>0)out="buy";
|
|
else out="sell";
|
|
string s_o_b=EnumToString(sell_or_buy);
|
|
if(s_o_b=="buy" && AO[0]>AO[1] && AO[0]<0){pos_com="buy grean";out="buy";}
|
|
if(s_o_b=="sell" && AO[0]<AO[1]&& AO[0]>0){pos_com="sell red";out="sell";}
|
|
|
|
return out;
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
string check_st(double ao)
|
|
{
|
|
string out="null";
|
|
if(ao<20)
|
|
{
|
|
if(ST[1]<ST[0]) out="buy";
|
|
}
|
|
if(ao>80)
|
|
{
|
|
if(ST[1]>ST[0])
|
|
out="sell";
|
|
|
|
}
|
|
// else out="sell";
|
|
|
|
return out;
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|