0
Завтра будем открывать ордера.
А пока, всем доброй ночи.
avatar

kvashnin007

  • 6 июня 2026, 22:35
0
Ладно.
Остряков много, как я посмотрю, а займемся таки делом.

Для начала создал скилет, на который и будем всех собак вешать.
<code>

//+------------------------------------------------------------------+
//|                                                  ТрахТибиДох.mq4 |
//|                                              Copyright 2026, KAE |
//|                                            kvashnin007@gmail.com |
//+------------------------------------------------------------------+
#property version   "1.00"
#property strict

//--- Inputs       Чем их меньше, тем быстрее и проще оптимизировать
extern int    CountOrders    = 20;       // Число позиций в серии
extern bool   Reverse        = false;    // Обратный сигнал
extern double Lots           = 0.02;     // Стартовый лот
extern double Risk           = 1.0;      // Риск в % от маржи
extern int    TrailingStop   = 255;      // Дистанция трала БУ
//---
extern int    Slippage       = 30;       // Проскальзывание
extern int    MagicNumber    = 1961;     // Магик

string   Comm = "ТрахТибиДох";  // Тут и коню понятно.
datetime CurrentTime = 0;       // Для того, чтобы на свече было не более одного ордера.
double   BuySL = 0, SellSL = 0; // Значения тралов БУ. Всем нужна эта инфа.
// По ходу творчества внешние переменные будем добавлять.
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
   Comment("");
}
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{                                                         // Получение сигнала на покупку-продажу 
         bool Buy, Sell, B=0, S=0;
         
         B = Close[2]<Open[2];                            // Свеча закрылась вверх - сигнал Buy
         S = Close[2]>Open[2];                            // Свеча закрылась вниз - сигнал Sell
         //---
         Buy  = B; Sell = S;
         if(Reverse)  {Buy  = S; Sell = B;}               // Если Reverse - наоборот
//--------------------- Открываем ордера - один на свечу ----------------------------------
         if(CurrentTime!=Time[0])
            {
            
            CurrentTime=Time[0];
            }
}
//+------------------------------------------------------------------+

                                           // Напишем несколько функций, которые на первое время понадобятся.
                                           
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void PutOrder(int type)
{
            int r=0;
            color clr=clrNONE;
            double sl=0,tp=0,price=0;
         
            if(type==OP_SELL)
               {
               clr=Red;
               price=Bid;
               }
         
            if(type==OP_BUY)
               {
               clr=Blue;
               price=Ask;
               }
       r=OrderSend(NULL,type,Lot(),NormalizeDouble(price,_Digits),Slippage,0,0,Comm,MagicNumber,0,clr);
}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double Lot()
{
         double lot=Lots;
         
         if(Risk>0 && MarketInfo(Symbol(), MODE_MARGINREQUIRED)!=0)
            lot=AccountFreeMargin()*Risk/100/MarketInfo(Symbol(), MODE_MARGINREQUIRED);
   return(NormalizeDouble(lot,2));
}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int CountTrades(int ot=-1)
{
         int countB=0, countS=0;
      
         for(int i=OrdersTotal()-1; i>=0; i--)
            if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
               if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
                 {
                  if(OrderType()==OP_BUY)
                     countB++;
                  if(OrderType()==OP_SELL)
                     countS++;
                 }
         if (ot == OP_BUY)
            return (countB);
         if (ot == OP_SELL)
            return (countS);
         if (ot == 11)             // CountTrades(11) - количество всех ордеров
            return (countB+countS);
   return(0);
}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void TrailingAll()
{
         bool rez=false;
      
         double all=0, allB=0, allS=0, countB=0, countS=0; BuySL=0; SellSL=0;
      
         for(int i=OrdersTotal()-1; i>=0; i--)
            if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
               if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
                  {
                  if(OrderType()==OP_BUY)
                     {
                     allB+=OrderOpenPrice()*OrderLots();
                     countB+=OrderLots();
                     }         
                  if(OrderType()==OP_SELL)
                     {
                     allS+=OrderOpenPrice()*OrderLots();
                     countS+=OrderLots();
                     }         
                  }
         if(countB>0)
            allB=NormalizeDouble(allB/countB,_Digits);
         if(countS>0)
            allS=NormalizeDouble(allS/countS,_Digits);
         if((countB+countS)>0)
            all=NormalizeDouble((allB+allB),_Digits);//NormalizeDouble((allB+allB)/(countB+countS),_Digits);//
      
         for(int i=OrdersTotal()-1; i>=0; i--)
            if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
               if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
                  {
                  if(OrderType()==OP_BUY)
                      if(Bid-allB>TrailingStop*_Point)
                         if(OrderStopLoss()<Bid-TrailingStop*_Point)
                           {
                           BuySL=NormalizeDouble(Bid-TrailingStop*_Point,_Digits);
                           if(OrderStopLoss()!=BuySL)
                              rez=OrderModify(OrderTicket(),OrderOpenPrice(),BuySL,OrderTakeProfit(),0,Yellow);
                           }
                  if(OrderType()==OP_SELL)
                      if(BuySL-Ask>TrailingStop*_Point)
                         if((OrderStopLoss()>(Ask+TrailingStop*_Point)) || (OrderStopLoss()==0))
                           {
                           SellSL=NormalizeDouble(Bid+TrailingStop*_Point,_Digits);
                           if(OrderStopLoss()!=SellSL)
                              rez=OrderModify(OrderTicket(),OrderOpenPrice(),SellSL,OrderTakeProfit(),0,Yellow);
                           }
                  }
}
//+------------------------------------------------------------------+
//|   Удаление самого старого крайнего ордера.                       |
//+------------------------------------------------------------------+
void CloseOld(int type)
{
            double PriceBuyMin=0,PriceSellMax=0,LotBuyMin=0,LotSellMax=0;
            int    TicketBuyMin=0,TicketSellMax=0;
            bool   rez=false;
            
            for(int i=OrdersTotal()-1; i>=0; i--)
               if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
                  if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
                     {
                     double lt  = OrderLots(),
                            op  = OrderOpenPrice();
                     int    tic = OrderTicket();
                     
                     if (type==OP_BUY && OrderType()==OP_BUY)
                        if (PriceBuyMin==0 || op<PriceBuyMin)
                           {
                           PriceBuyMin  = op;
                           LotBuyMin    = lt;
                           TicketBuyMin = tic;
                           }
                     if (OrderType()==OP_SELL)
                        if (PriceSellMax==0 || op>PriceSellMax)
                           {
                           PriceSellMax  = op;
                           LotSellMax    = lt;
                           TicketSellMax = tic;
                           }
                     //---
                     if (type == OP_BUY)
                        rez = OrderClose(TicketBuyMin,LotBuyMin,Bid,Slippage);
                        
                     if (type == OP_SELL)
                        rez = OrderClose(TicketSellMax,LotSellMax,Ask,Slippage);
                     }
}

<code>
avatar

kvashnin007

  • 6 июня 2026, 22:23