//+------------------------------------------------------------------+
//| BuySell2.mq4 |
//| Copyright 2022, AM2 |
//| https://www.forexsystems.biz |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, AM2"
#property link "https://www.forexsystems.biz"
#property version "1.00"
#property strict
//--- Inputs
extern double Lots = 0.1; // лот
extern double KLot = 2; // умножение лота
extern double MaxLot = 5; // максимальный лот
extern int StopLoss = 200; // лось
extern int TakeProfit = 250; // язь
extern int TrailingStop = 0; // трал
extern int Slip = 30; // реквот
extern int BuySell = 0; // 0-открытие по свечам 1-бай 2-селл
extern int Magic = 123; // магик
//+------------------------------------------------------------------+
//| 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 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);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
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);
}
//+------------------------------------------------------------------+
//| Результат последней сделки |
//+------------------------------------------------------------------+
int LastDealResult()
{
int res=0;
if(OrderSelect(OrdersHistoryTotal()-1,SELECT_BY_POS,MODE_HISTORY))
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{
if(OrderProfit()>0)
{
res=1;//tp
}
if(OrderProfit()<0)
{
res=2;//sl
}
}
}
return(res);
}
//+------------------------------------------------------------------+
//| Тип последней сделки |
//+------------------------------------------------------------------+
int LastDealType()
{
int type=8;
if(OrderSelect(OrdersHistoryTotal()-1,SELECT_BY_POS,MODE_HISTORY))
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{
if(OrderType()==0)
{
type=1;//buy
}
if(OrderType()==1)
{
type=2;
}
}
}
return(type);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void TrailingAll()
{
bool mod;
double all=0,count=0,sl=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)
{
all+=OrderOpenPrice()*OrderLots();
count+=OrderLots();
}
}
}
}
if(count>0)
all=NormalizeDouble(all/count,_Digits);
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-all>TrailingStop*_Point)
{
if(OrderStopLoss()<Bid-TrailingStop*_Point)
{
sl=NormalizeDouble(Bid-TrailingStop*_Point,_Digits);
if(OrderStopLoss()!=sl)
mod=OrderModify(OrderTicket(),OrderOpenPrice(),sl,OrderTakeProfit(),0,Yellow);
}
}
}
if(OrderType()==OP_SELL)
{
if(all-Ask>TrailingStop*_Point)
{
if((OrderStopLoss()>(Ask+TrailingStop*_Point)) || (OrderStopLoss()==0))
{
sl=NormalizeDouble(Bid+TrailingStop*_Point,_Digits);
if(OrderStopLoss()!=sl)
mod=OrderModify(OrderTicket(),OrderOpenPrice(),sl,OrderTakeProfit(),0,Yellow);
}
}
}
}
}
}
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
if(TrailingStop>0)
TrailingAll();
if(CountTrades()<1)
{
if(LastDealResult()==2 && LastDealType()==2)
{
PutOrder(0,Ask);
}
if(LastDealResult()==2 && LastDealType()==1)
{
PutOrder(1,Bid);
}
if((Close[0]>Open[0] && BuySell==0)||(BuySell==1))
{
PutOrder(0,Ask);
}
if((Close[0]<Open[0] && BuySell==0)||(BuySell==2))
{
PutOrder(1,Bid);
}
}
Comment("\n Lot: ",Lot());
}
//+------------------------------------------------------------------+
зачем он нужен поясните
AM2