Ордера закрываться должны по тралу профита-убытка
//+------------------------------------------------------------------+
//| Simple.mq4 |
//| Copyright 2023, AM2 |
//| https://www.forexsystems.biz |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, AM2"
#property link "https://www.forexsystems.biz"
#property version "1.00"
#property strict
//--- Inputs
extern double Lots = 0.1; // лот
extern int StopLoss = 0; // лось
extern int TakeProfit = 0; // язь
extern int Step = 333; // шаг
extern int Slip = 30; // реквот
extern int Magic1 = 111; // магик 1
extern int Magic2 = 222; // магик 1
extern string Comm = "Simple";
datetime t=0;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
Comment("");
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
Comment("");
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void PutOrder(int type,double price,int mn)
{
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,Comm,mn,0,clr);
return;
}
//+------------------------------------------------------------------+
//| Подсчет позиций |
//+------------------------------------------------------------------+
int CountTrades(int ot=-1)
{
int count=0;
for(int i=OrdersTotal()-1; i>=0; i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol() && (OrderMagicNumber()==Magic1 || OrderMagicNumber()==Magic2))
{
if(OrderType()==0 && (ot==0 || ot==-1))
count++;
if(OrderType()==1 && (ot==1 || ot==-1))
count++;
}
}
}
return(count);
}
//+------------------------------------------------------------------+
//| Подсчет ордеров по типу |
//+------------------------------------------------------------------+
int CountOrders(int mn=0)
{
int count=0;
for(int i=OrdersTotal()-1; i>=0; i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==mn)
{
if(OrderType()>1)
count++;
}
}
}
return(count);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double Lot()
{
double lot=Lots;
return(lot);
}
//+------------------------------------------------------------------+
//| Удаление отложенных ордеров |
//+------------------------------------------------------------------+
void DelOrder(int type=-1,int mn=0)
{
bool del=1;
for(int i=OrdersTotal()-1; i>=0; i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==mn)
{
if(OrderType()==type || type==-1)
del=OrderDelete(OrderTicket());
}
}
}
}
//+------------------------------------------------------------------+
//| Профит всех ордеров по типу ордера |
//+------------------------------------------------------------------+
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()==Magic1 || OrderMagicNumber()==Magic2))
{
if(OrderType()==0 && (ot==0 || ot==-1))
{
pr+=OrderProfit()+OrderCommission()+OrderSwap();
}
if(OrderType()==1 && (ot==1 || ot==-1))
{
pr+=OrderProfit()+OrderCommission()+OrderSwap();
}
}
}
}
return(pr);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
if(CountTrades()<1)
{
PutOrder(1,Bid,Magic1);
PutOrder(0,Ask,Magic2);
}
if(CountOrders(Magic1)<1)
{
PutOrder(3,Bid+Step*_Point,Magic1);
PutOrder(5,Bid-Step*_Point,Magic1);
}
if(CountOrders(Magic2)<1)
{
PutOrder(2,Bid-Step*_Point,Magic2);
PutOrder(4,Bid+Step*_Point,Magic2);
}
if(CountOrders(Magic1)<2)
DelOrder(-1,Magic1);
if(CountOrders(Magic2)<2)
DelOrder(-1,Magic2);
Comment("\n All Profit: ",AllProfit());
}
//+------------------------------------------------------------------+
AM2