Comment("\n Lot: ",Lot(),
"\n Trades: ",CountTrades(),
"\n Profit: ",AllProfit(),
"\n Profit2: ",AllProfit2(),
"\n Second: ",CountTrades()-Count);
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CloseAll2()
{
bool cl;
int second=CountTrades()-1-Second;
for(int i=CountTrades()-1;i>second;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{
if(OrderType()==0)
{
RefreshRates();
cl=OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid,Digits),Slip,White);
}
if(OrderType()==1)
{
RefreshRates();
cl=OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Ask,Digits),Slip,White);
}
}
}
}
}
//+------------------------------------------------------------------+
//| Профит всех ордеров по типу ордера |
//+------------------------------------------------------------------+
double AllProfit2()
{
double pr=0;
int second=CountTrades()-1-Second;
for(int i=CountTrades()-1;i>second;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{
if(OrderType()==0)
{
pr+=OrderProfit()+OrderCommission()+OrderSwap();
}
if(OrderType()==1)
{
pr+=OrderProfit()+OrderCommission()+OrderSwap();
}
}
}
}
return(pr);
}
double AllProfit2()
{
double pr=0;
int second=OrdersTotal()-1-Second;
for(int i=OrdersTotal()-1;i>second;i--)
<code> //+------------------------------------------------------------------+ //| Smurfik.mq4 | //| Copyright 2018, AM2 | //| http://www.forexsystems.biz | //+------------------------------------------------------------------+ #property copyright "Copyright 2018, AM2" #property link "http://www.forexsystems.biz" #property version "1.00" #property strict //--- Inputs extern double Lots = 0.1; // лот extern double KLot = 1; // умножение лота extern double MaxLot = 5; // максимальный лот extern int StopLoss = 2000; // лось extern int TakeProfit = 3000; // язь extern int StartHour = 0; // час начала торговли extern int StartMin = 30; // минута начала торговли extern int EndHour = 23; // час окончания торговли extern int EndMin = 30; // минута окончания торговли extern int Slip = 30; // реквот extern int Shift = 1; // на каком баре сигнал индикатора extern int Magic = 123; // магик extern string IndName = "CCI_smoothed"; input int CCI_Period = 14; input ENUM_APPLIED_PRICE CCI_Price = 0; input int Period_Smoothed = 3; input ENUM_MA_METHOD MA_Method = 0; extern double cci_buy_level = -100; extern double cci_sell_level = 100; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- Comment(""); //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { Comment(""); } 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 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,Lot(),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); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void OpenPos() { double blu = iCustom(NULL,0,IndName,CCI_Period,CCI_Price,Period_Smoothed,MA_Method,cci_buy_level,cci_sell_level,0,Shift); double red = iCustom(NULL,0,IndName,CCI_Period,CCI_Price,Period_Smoothed,MA_Method,cci_buy_level,cci_sell_level,1,Shift); if(blu>0) { PutOrder(0,Ask); } if(red>0) { PutOrder(1,Bid); } } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void ClosePos() { double blu = iCustom(NULL,0,IndName,CCI_Period,CCI_Price,Period_Smoothed,MA_Method,cci_buy_level,cci_sell_level,0,Shift); double red = iCustom(NULL,0,IndName,CCI_Period,CCI_Price,Period_Smoothed,MA_Method,cci_buy_level,cci_sell_level,1,Shift); if(red>0) { CloseAll(0); } if(blu>0) { CloseAll(1); } } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ double Lot() { double lot=Lots; for(int i=OrdersHistoryTotal()-1;i>=0;i--) { if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) { if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic) { if(OrderProfit()>0) break; if(OrderProfit()<0) { lot=OrderLots()*KLot; break; } } } } if(lot>MaxLot)lot=Lots; return(lot); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void Trailing() { bool mod; 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) { if(Bid-OrderOpenPrice()>TrailingStop*Point) { if(OrderStopLoss()<Bid-TrailingStop*Point) { mod=OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailingStop*Point,OrderTakeProfit(),0,Yellow); return; } } } if(OrderType()==OP_SELL) { if((OrderOpenPrice()-Ask)>TrailingStop*Point) { if((OrderStopLoss()>(Ask+TrailingStop*Point)) || (OrderStopLoss()==0)) { mod=OrderModify(OrderTicket(),OrderOpenPrice(),Ask+TrailingStop*Point,OrderTakeProfit(),0,Yellow); return; } } } } } } } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ 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); } } } } } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { double blu = iCustom(NULL,0,IndName,CCI_Period,CCI_Price,Period_Smoothed,MA_Method,cci_buy_level,cci_sell_level,0,Shift); double red = iCustom(NULL,0,IndName,CCI_Period,CCI_Price,Period_Smoothed,MA_Method,cci_buy_level,cci_sell_level,1,Shift); if(CountTrades()<1 && isTradeTimeInt(StartHour,StartMin,EndHour,EndMin)) OpenPos(); else ClosePos(); if(BULevel>0) BU(); if(TrailingStop>0) Trailing(); Comment("\n blu: ",blu, "\n red: ",red); } //+------------------------------------------------------------------+ </code>
GAWroch