<code>extern double Start = 1.1; // цена старт ордера</code>
<code>extern int Type = 0; // 0-Buy,1-Sell extern int Try = 5; // количество попыток выставить ордер</code>
что тут так сложно с точки зрения кодинга
алгоритм весьма простой
//+------------------------------------------------------------------+
//| Start.mq4 |
//| Copyright 2017, AM2 |
//| http://www.forexsystems.biz |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, AM2"
#property link "http://www.forexsystems.biz"
#property version "1.00"
#property strict
//--- Inputs
extern double Start = 1.1; // старт
extern double Lots = 0.1; // лот
extern double LockLoss = 3000; // убыток когда ставим лок
extern double Loss = 3000; // лось в рублях
extern double Profit = 1000; // язь в рублях
extern int StopLoss = 2222; // лось
extern int TakeProfit = 150; // язь
extern int StartHour = 0; // час начала торговли
extern int StartMin = 30; // минута начала торговли
extern int EndHour = 23; // час окончания торговли
extern int EndMin = 30; // минута окончания торговли
extern int Slip = 30; // реквот
extern int Magic = 123; // магик
datetime t=0;
bool trade=true;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void PutOrder(int type,double price)
{
int r=0;
color clr=Green;
double sl=0,tp=0;
if(type==1 || type==3 || type==5)
{
clr=Red;
if(StopLoss>0) sl=NormalizeDouble(price+StopLoss*Point,Digits);
if(TakeProfit>0) tp=NormalizeDouble(price-TakeProfit*Point,Digits);
}
if(type==0 || type==2 || type==4)
{
clr=Blue;
if(StopLoss>0) sl=NormalizeDouble(price-StopLoss*Point,Digits);
if(TakeProfit>0) tp=NormalizeDouble(price+TakeProfit*Point,Digits);
}
r=OrderSend(NULL,type,Lots,NormalizeDouble(price,Digits),Slip,sl,tp,"",Magic,0,clr);
return;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
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()<2) count++;
}
}
}
return(count);
}
//+------------------------------------------------------------------+
//| Подсчет ордеров по типу |
//+------------------------------------------------------------------+
int CountOrders(int type)
{
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()==type) count++;
}
}
}
return(count);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double CountLots(int type)
{
double lots=0;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{
if(OrderType()==type) lots+=OrderLots();
}
}
}
return(lots);
}
//+------------------------------------------------------------------+
//| Закрытие позиции по типу ордера |
//+------------------------------------------------------------------+
void CloseAll(int ot=-1)
{
bool cl;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{
if(OrderType()==0 && (ot==0 || ot==-1))
{
RefreshRates();
cl=OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid,Digits),Slip,White);
}
if(OrderType()==1 && (ot==1 || ot==-1))
{
RefreshRates();
cl=OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Ask,Digits),Slip,White);
}
}
}
}
}
//+------------------------------------------------------------------+
//| Профит всех ордеров по типу ордера |
//+------------------------------------------------------------------+
double AllProfit(int ot=-1)
{
double pr=0;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{
if(OrderType()==0 && (ot==0 || ot==-1))
{
pr+=OrderProfit()+OrderCommission()+OrderSwap();
}
if(OrderType()==1 && (ot==1 || ot==-1))
{
pr+=OrderProfit()+OrderCommission()+OrderSwap();
}
}
}
}
return(pr);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void OpenPos()
{
if(Bid<Start)
{
PutOrder(1,Bid);
}
if(Ask>Start)
{
PutOrder(0,Ask);
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool isTradeTimeInt(int hb=0,int mb=0,int he=0,int me=0)
{
datetime db, de; // Время начала и окончания работы
int hc; // Часы текущего времени торгового сервера
db=StrToTime(TimeToStr(TimeCurrent(), TIME_DATE)+" "+(string)hb+":"+(string)mb);
de=StrToTime(TimeToStr(TimeCurrent(), TIME_DATE)+" "+(string)he+":"+(string)me);
hc=TimeHour(TimeCurrent());
if(db>=de)
{
if(hc>=he) de+=24*60*60; else db-=24*60*60;
}
if(TimeCurrent()>=db && TimeCurrent()<=de) return(True);
else return(False);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void PutLockOrder(int type,double price)
{
int r=0;
color clr=Green;
double lot=MathAbs(CountLots(0)-CountLots(1));
if(type==1 || type==3 || type==5)
{
clr=Red;
}
if(type==0 || type==2 || type==4)
{
clr=Blue;
}
r=OrderSend(NULL,type,lot,NormalizeDouble(price,Digits),Slip,0,0,"",Magic,0,clr);
return;
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
if(t!=Time[0] && isTradeTimeInt(StartHour,StartMin,EndHour,EndMin) && trade)
{
OpenPos();
if((AllProfit()>=Profit && Profit>0) || (AllProfit()<-Loss && Loss>0)) CloseAll();
t=Time[0];
}
if(!isTradeTimeInt(StartHour,StartMin,EndHour,EndMin) && trade)
{
if(AllProfit()<-LockLoss && LockLoss>0 && CountOrders(0)<CountOrders(1)) {PutLockOrder(0,Ask); trade=false;}
if(AllProfit()<-LockLoss && LockLoss>0 && CountOrders(1)<CountOrders(0)) {PutLockOrder(1,Bid); trade=false;}
}
Comment("\n All Profit: ",AllProfit(),
"\n Count Trades: ",CountTrades(),
"\n Count Buy: ",CountOrders(0),
"\n Count Sell: ",CountOrders(1),
"\n Count Buy Lots: ",CountLots(0),
"\n Count Sell Lots: ",CountLots(1));
}
//+------------------------------------------------------------------+
<code>extern int EndHour = 23; // час окончания торговли extern int EndMin = 30; // минута окончания торговли</code>
Kashtan