
//+------------------------------------------------------------------+
//| ProboyDay.mq5 |
//| Copyright 2020, AM2 |
//| http://www.forexsystems.biz |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, AM2"
#property link "http://www.forexsystems.biz"
#property version "1.00"
#include <Trade\Trade.mqh> // Подключаем торговый класс CTrade
input ulong Magic = 1824; // магик
input double Lots = 0.01; // лот
input double KLot = 2; // увеличение лота
input double MaxLot = 10; // максимальный лот
input int Stop = 2000; // стоплосс
input int Take = 2000; // тейкпрофит
input int StartHour = 8; // час начала торговли
input int StartMin = 30; // минута начала торговли
input int EndHour = 19; // час окончания торговли
input int EndMin = 30; // минута окончания торговли
input bool BuyStop = 1; // BuyStop
input bool SellStop = 1; // SellStop
CTrade trade; // Используем торговый класс CTrade
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
trade.SetExpertMagicNumber(Magic);
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool TimeSession(int aStartHour,int aStartMinute,int aStopHour,int aStopMinute,datetime aTimeCur)
{
//--- время начала сессии
int StartTime=3600*aStartHour+60*aStartMinute;
//--- время окончания сессии
int StopTime=3600*aStopHour+60*aStopMinute;
//--- текущее время в секундах от начала дня
aTimeCur=aTimeCur%86400;
if(StopTime<StartTime)
{
//--- переход через полночь
if(aTimeCur>=StartTime || aTimeCur<StopTime)
{
return(true);
}
}
else
{
//--- внутри одного дня
if(aTimeCur>=StartTime && aTimeCur<StopTime)
{
return(true);
}
}
return(false);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double Lot()
{
double lot=Lots;
HistorySelect(0,TimeCurrent());
ulong ticket=0;
for(int i=HistoryDealsTotal()-1; i>=0; i--)
{
if((ticket=HistoryDealGetTicket(i))>0)
{
if(HistoryDealGetString(ticket,DEAL_SYMBOL)==_Symbol)
{
if(HistoryDealGetInteger(ticket,DEAL_MAGIC)==Magic)
{
double profit=HistoryDealGetDouble(ticket,DEAL_PROFIT);
double LastLot=HistoryDealGetDouble(ticket,DEAL_VOLUME);
if(profit>0)
lot=Lots;
if(profit<0)
lot=LastLot*KLot;
break;
}
}
}
}
if(lot>MaxLot)
lot=Lots;
return(lot);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void DelOrder()
{
for(int i=OrdersTotal()-1; i>=0; i--)
{
if(OrderGetInteger(ORDER_MAGIC)==Magic)
{
ulong ticket=OrderGetTicket(i);
trade.OrderDelete(ticket);
}
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int CountOrders(ENUM_ORDER_TYPE type=-1)
{
int count=0;
for(int i=OrdersTotal()-1; i>=0; i--)
{
if(OrderSelect(OrderGetTicket(i)))
{
if(OrderGetInteger(ORDER_MAGIC)==Magic)
{
if(OrderGetInteger(ORDER_TYPE)==type || type==-1)
count++;
}
}
}
return(count);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
double sl=0,tp=0;
double lo=iLow(NULL,PERIOD_D1,1);
double hi=iHigh(NULL,PERIOD_D1,1);
double Bid=SymbolInfoDouble(_Symbol,SYMBOL_BID);
if(TimeSession(StartHour,StartMin,EndHour,EndMin,TimeCurrent()) && CountOrders()<1)
{
if(Bid<hi)
{
if(BuyStop && CountOrders(4)<1)
{
sl=NormalizeDouble(hi-Stop*_Point,_Digits);
tp=NormalizeDouble(hi+Take*_Point,_Digits);
trade.BuyStop(Lot(),hi,NULL,sl,tp,0,0 ,"");
}
}
if(Bid>lo)
{
if(SellStop && CountOrders(5)<1)
{
sl=NormalizeDouble(lo+Stop*_Point,_Digits);
tp=NormalizeDouble(lo-Take*_Point,_Digits);
trade.SellStop(Lot(),lo,NULL,sl,tp,0,0,"");
}
}
}
if(!TimeSession(StartHour,StartMin,EndHour,EndMin,TimeCurrent()))
DelOrder();
Comment("\n Hi: ",hi,
"\n Lo: ",lo);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
int CountDayDeal()
{
int count=0;
for(int i=0; i<OrdersHistoryTotal(); i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
{
if(TimeDay(OrderCloseTime())==Day() && OrderType()<2)
count++;
}
}
}
return(count);
}
//+------------------------------------------------------------------+
vypchela