//+------------------------------------------------------------------+
//| TimeOpen.mq4 |
//| Copyright 2015, AM2 |
//| http://www.forexsystems.biz |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, AM2"
#property link "http://www.forexsystems.biz"
#property version "1.00"
#property description "Time Open expert advisor"
#include <stdlib.mqh>
//--- Inputs
extern int BuySell = 2; // 0-бай. 1-селл. 2-бай/селл. 3-селл/бай
extern int LossCount = 1; // количество лосей после которых увеличение лота
extern int Count = 1; // количество открываемых ордеров
extern int MAGIC = 123; // магик
extern double Lots = 1; // лот
extern double KLot = 2; // увеличение лота
extern double MaxLot = 50; // максимальный лот
extern string t = "Время";
extern int t1 = 3; // время 1
extern int t2 = 3; // время 2
extern int t3 = 3; // время 3
extern int t4 = 3; // время 4
extern int t5 = 3; // время 5
extern int t6 = 3; // время 6
extern int t7 = 3; // время 7
extern int t8 = 3; // время 8
extern int t9 = 3; // время 9
int tf[9],k=0;
datetime StartTime;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
StartTime=TimeCurrent();
tf[0]=t1;
tf[1]=t2;
tf[2]=t3;
tf[3]=t4;
tf[4]=t5;
tf[5]=t6;
tf[6]=t7;
tf[7]=t8;
tf[8]=t9;
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
Comment("");
}
//+------------------------------------------------------------------+
int CountTrades()
{
int count=0;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGIC)
{
if(OrderType()==OP_BUY || OrderType()==OP_SELL) count++;
}
}
}
return(count);
}
//+------------------------------------------------------------------+
void OpenOrd(int time)
{
int res,err;
double ask=MarketInfo(OrderSymbol(),MODE_ASK);
double bid=MarketInfo(OrderSymbol(),MODE_BID);
//--- sell conditions
if(BuySell==1)
{
res=OrderSend(Symbol(),OP_SELL,Lot(),NormalizeDouble(bid,Digits),0,0,0,IntegerToString(time),MAGIC,0,Red);
if(res<0)
{
err=GetLastError();
Print("ОШИБКА ВЫСТАВЛЕНИЯ ОРДЕРА SELL: ",err,"(",ErrorDescription(err),")");
} else {
RefreshRates();
}
return;
}
Sleep(1000);
//--- buy conditions
if(BuySell==0)
{
res=OrderSend(Symbol(),OP_BUY,Lot(),NormalizeDouble(ask,Digits),0,0,0,IntegerToString(time),MAGIC,0,Blue);
if(res<0)
{
err=GetLastError();
Print("ОШИБКА ВЫСТАВЛЕНИЯ ОРДЕРА BUY: ",err,"(",ErrorDescription(err),")");
} else {
RefreshRates();
}
return;
}
Sleep(1000);
//--- buy sell conditions
if(BuySell==2)
{
//--- buy conditions
if(LastDealType()==2 || LastDealType()==0)
{
res=OrderSend(Symbol(),OP_BUY,Lot(),NormalizeDouble(ask,Digits),0,0,0,IntegerToString(time),MAGIC,0,Blue);
if(res<0)
{
err=GetLastError();
Print("ОШИБКА ВЫСТАВЛЕНИЯ ОРДЕРА BUY: ",err,"(",ErrorDescription(err),")");
} else {
RefreshRates();
}
return;
}
Sleep(1000);
//--- sell conditions
if(LastDealType()==1 || LastDealType()==0)
{
res=OrderSend(Symbol(),OP_SELL,Lot(),NormalizeDouble(bid,Digits),0,0,0,IntegerToString(time),MAGIC,0,Red);
if(res<0)
{
err=GetLastError();
Print("ОШИБКА ВЫСТАВЛЕНИЯ ОРДЕРА SELL: ",err,"(",ErrorDescription(err),")");
} else {
RefreshRates();
}
return;
}
Sleep(1000);
}
//--- sell buy conditions
if(BuySell==3)
{
//--- sell conditions
if(LastDealType()==1 || LastDealType()==0)
{
res=OrderSend(Symbol(),OP_SELL,Lot(),NormalizeDouble(bid,Digits),0,0,0,IntegerToString(time),MAGIC,0,Red);
if(res<0)
{
err=GetLastError();
Print("ОШИБКА ВЫСТАВЛЕНИЯ ОРДЕРА SELL: ",err,"(",ErrorDescription(err),")");
} else {
RefreshRates();
}
return;
}
Sleep(1000);
//--- buy conditions
if(LastDealType()==2 || LastDealType()==0)
{
res=OrderSend(Symbol(),OP_BUY,Lot(),NormalizeDouble(ask,Digits),0,0,0,IntegerToString(time),MAGIC,0,Blue);
if(res<0)
{
err=GetLastError();
Print("ОШИБКА ВЫСТАВЛЕНИЯ ОРДЕРА BUY: ",err,"(",ErrorDescription(err),")");
} else {
RefreshRates();
}
return;
}
Sleep(1000);
}
//---
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int LastDealType()
{
int type=0;
if(OrderSelect(3,SELECT_BY_POS,MODE_HISTORY))
{
if(OrderType()==OP_BUY)
{
type=1;//buy
}
if(OrderType()==OP_SELL)
{
type=2;//sell
}
}
return(type);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int Losses()
{
int loss=0;
for(int i=OrdersHistoryTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
{
if(OrderProfit()>0) break;
if(OrderProfit()<0) loss++;
}
}
return(loss);
}
//+------------------------------------------------------------------+
//| Last lot size |
//+------------------------------------------------------------------+
double LastLot()
{
double lot=0;
if(OrderSelect(1,SELECT_BY_POS,MODE_HISTORY))
{
if(OrderProfit()<0) lot=OrderLots();
}
if(lot==0) lot=Lots;
return(lot);
}
//+------------------------------------------------------------------+
//| Calculate optimal lot size |
//+------------------------------------------------------------------+
double Lot()
{
double lot;
int i=OrdersHistoryTotal();
if(OrderSelect(1,SELECT_BY_POS,MODE_HISTORY))
{
//if(OrderProfit()>0) break;
if(OrderProfit()<0 && Losses()>=LossCount)
{
lot=LastLot()*KLot;
}
}
if(lot>MaxLot || lot==0) lot=Lots;
return(lot);
}
//+------------------------------------------------------------------+
//| OnTick function |
//+------------------------------------------------------------------+
void OnTick()
{
if(k>9) k=0;
if(CountTrades()<Count)
{
OpenOrd(tf[k]);
k++;
}
Comment("\n Current Time: ",TimeToString(TimeCurrent(),TIME_SECONDS),
"\n Time Frame: ",tf[k],
"\n K: ",k,
"\n Lot: ",Lot(),
"\n Last Deal Type: ",LastDealType(),
"\n Losses: ",Losses(),
"\n Last Lot: ",LastLot());
}
//+------------------------------------------------------------------+
Лучше всего если советник переделает Андрей, но я уже исчерпал свой лимит заказов в этом месяце.
AM2