I will write an advisor free of charge - page 85

 
Evgenij Litvintsev:

Good afternoon! Maybe someone would be interested in automating my strategy. It's working now, but in semi-automatic mode, I'd like to automate it.


Can I see the trading results?

 

Good evening, could you please advise how to get a ticket order, lot and profit from one function:

double OldTicketSell()// Find the furthest sell order

{
double MaxDist=0,oldprofit,lot;
int ticket=0;
double BID=MarketInfo(Symbol(),MODE_BID);
double ASK=MarketInfo(Symbol(),MODE_ASK);
for(int i=OrdersTotal()-1; i>=0; i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderType()==OP_SELL && MaxDist<MathAbs(OrderOpenPrice()-ASK))
{
MaxDist=MathAbs(OrderOpenPrice()-ASK);
ticket=OrderTicket();
oldprofit = OrderProfit();
lot = OrderLots();
}
}
}
return(ticket);

}

The function is clear, it's not clear how to get the found data out of it, or it's necessary to copy it under lot and profit.


 
Николай:

Good evening, could you please tell me how to get ticket orders, lot and profit from one function.

The function is understandable, it's not clear how to pull the found data from it, or should I copy it under lot and profit.

We could do it like this

struct InfoOrder
{
     int ticket;
     double lot,
            profit,
            swap,
            commission,
            sl, tp;
     datetime timeOpen;
     string comment;
} infoOrder;

void OldSell(void)// Находим самый дальний ордер на продажу
{
   double MaxDist=0;
   double BID=MarketInfo(Symbol(),MODE_BID);
   double ASK=MarketInfo(Symbol(),MODE_ASK);
   for(int i=OrdersTotal()-1; i>=0; i--)
   {
   if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
      {
        if(OrderType()==OP_SELL && MaxDist<MathAbs(OrderOpenPrice()-ASK))
         {
             MaxDist=MathAbs(OrderOpenPrice()-ASK);
             infoOrder.ticket=OrderTicket();
             infoOrder.profit = OrderProfit();
             infoOrder.lot = OrderLots();
         }
      }
   }
   return; 
}
 
Konstantin Nikitin:

You can go like this

It's all good, but I am a dummy, I am trying to write an EA in the extent of my knowledge (I just learned a bit), I understand that the variable is a structure, but how to get data out of this structure is not clear. For example, if we have found the furthest order, then profit should be divided by lot and we get the number of points from the current price to that lot. Then we get the profit of an opposite order (I know how to get it), divide it by the number of points of the opposite order calculated earlier, we get the number of lots and use this number of lots to fully or partially close the far order found.
 
Konstantin Nikitin:

We could do it like this

I would also like to ask if it is possible to find the furthest order from the price by comparing opening prices of orders in the cycle, I think it would be even easier?

 
Николай:

I would also like to ask if it is possible to find the furthest order from the price by comparing opening prices of orders in the cycle, I think it would be even easier?

Of course it is. And whether it is easier or not depends on what you need.
This is an example to your question above.

struct InfoOrder
{
     int ticket;
     double lot,
            profit,
            swap,
            commission,
            sl, tp;
     datetime timeOpen;
     string comment;
} infoOrder;

void OnTick()
{
     OldSell();
     Print("Ticket: ", infoOrder.ticket);
     Print("Profit: ", infoOrder.profit);
     Print("Lot: ", infoOrder.lot);
     Print("-----=====-----");
     
     return;
}

void OldSell(void)// Находим самый дальний ордер на продажу
{
   double MaxDist=0;
   double BID=MarketInfo(Symbol(),MODE_BID);
   double ASK=MarketInfo(Symbol(),MODE_ASK);
   for(int i=OrdersTotal()-1; i>=0; i--)
   {
   if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
      {
        if(OrderType()==OP_SELL && MaxDist<MathAbs(OrderOpenPrice()-ASK))
         {
             MaxDist=MathAbs(OrderOpenPrice()-ASK);
             infoOrder.ticket=OrderTicket();
             infoOrder.profit = OrderProfit();
             infoOrder.lot = OrderLots();
         }
      }
   }
   return; 
}
 
Konstantin Nikitin:

Of course you can. Whether it is easier or not depends on what you need.
And an example to your question above.

Looking at all this, I realize what a tipster I am. So, theresult ofvoid OldSell(void)function is written toInfoOrder structure, and then you can use values of infoOrder.ticket,infoOrder.profit,infoOrder.lot variables.
//-Структура для нахождения самого дальнего ордера на продажу
struct InfoOrder
{
     int ticket;
     double lot,
            profit,
            swap,
            commission,
            sl, tp;
     datetime timeOpen;
     string comment;
} infoOrder;

void OnTick()
{
     OldSell();
     Print("Ticketbuy: ", infoOrder.ticket);
     Print("Profitbuy: ", infoOrder.profit);
     Print("Lotbuy: ", infoOrder.lot);
     Print("Ticketsell: ", infoOrder.ticket);
     Print("Profitsell: ", infoOrder.profit);
     Print("Lotsell: ", infoOrder.lot);
     Print("-----=====-----");
     
     return;
}

void OldSell(void)// Находим самый дальний ордер на продажу
{
   double MaxDist=0;
   double BID=MarketInfo(Symbol(),MODE_BID);
   double ASK=MarketInfo(Symbol(),MODE_ASK);
   for(int i=OrdersTotal()-1; i>=0; i--)
   {
   if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
      {
         if(OrderType()==OP_BUY && MaxDist<MathAbs(OrderOpenPrice()-BID))
          {
             MaxDist=MathAbs(OrderOpenPrice()-BID);
             infoOrder.ticketbuy=OrderTicket();
             infoOrder.profitbuy = OrderProfit();
             infoOrder.lotbuy = OrderLots();
           }  
             
        if(OrderType()==OP_SELL && MaxDist<MathAbs(OrderOpenPrice()-ASK))
          {
             MaxDist=MathAbs(OrderOpenPrice()-ASK);
             infoOrder.ticketsell=OrderTicket();
             infoOrder.profitsell = OrderProfit();
             infoOrder.lotsell = OrderLots();
          }
      }
   }
   return; 
}


 
Konstantin Nikitin:

Of course you can. Whether it's easier or not depends on what you need.
And an example to your question above.

Forgot to change the Print variables
 
Николай:
Looking at all this, I realize what a dummy I am. So, theresult ofvoid OldSell(void)function is written toInfoOrder structure and then we can use infoOrder.ticket,infoOrder.profit,infoOrder.lot.I generalized the function a bit, was it right or not?
//-Структура для нахождения самого дальнего ордера на продажу
struct InfoOrder
{
     int ticket;
     double lot,
            profit,
            swap,
            commission,
            sl, tp,
            MaxDist;
     datetime timeOpen;
     string comment;
} infoOrder;

void OnTick()
{
     if( OldOrder(OP_BUY) )
     {
        Print("Ticketbuy: ", infoOrder.ticket);
        Print("Profitbuy: ", infoOrder.profit);
        Print("Lotbuy: ", infoOrder.lot);
        Print("MaxDistbuy: ", infoOrder.MaxDist);
        Print("-----=====-----");
     }
     if( OldOrder(OP_SELL) )
     {
        Print("Ticketsell: ", infoOrder.ticket);
        Print("Profitsell: ", infoOrder.profit);
        Print("Lotsell: ", infoOrder.lot);
        Print("MaxDistsell: ", infoOrder.MaxDist);
        Print("-----=====-----");
     }
     
     return;
}

bool OldOrder(const int type)// Находим самый дальний ордер на продажу
{
   bool res = false;
   double MaxDist=0;
   double BID=MarketInfo(Symbol(),MODE_BID);
   double ASK=MarketInfo(Symbol(),MODE_ASK);
   for(int i=OrdersTotal()-1; i>=0; i--)
   {
   if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
      {
         switch(type)
           {
            case OP_BUY:
               if(OrderType()!=OP_BUY) break;
               if(MaxDist<=MathAbs(OrderOpenPrice()-BID)) break;
               infoOrder.MaxDist=MathAbs(OrderOpenPrice()-BID);
               infoOrder.ticket=OrderTicket();
               infoOrder.profit = OrderProfit();
               infoOrder.lot = OrderLots();
               res = true;
               break;
             
           case OP_SELL:
               if(OrderType()!=OP_SELL) break;
               if(MaxDist>=MathAbs(OrderOpenPrice()-ASK)) break;
               infoOrder.MaxDist=MathAbs(OrderOpenPrice()-ASK);
               infoOrder.ticket=OrderTicket();
               infoOrder.profit = OrderProfit();
               infoOrder.lot = OrderLots();
               res = true;
               break;
        }
      }
   }
   return res; 
}

Something like this.
 




found this structure in the EA, can you tell me what it defines

struct TMartinData
{
   int    BUY_Positions;
   double BUY_LastPrice;
   double BUY_StopPrice;
   double BUY_Lots;
   datetime BUY_LastTime;
   bool   BUY_AllWD;
   int    SELL_Positions;
   double SELL_LastPrice;
   double SELL_StopPrice;
   double SELL_Lots;
   datetime SELL_LastTime;
   bool   SELL_AllWD;
} MartinData;

void OnTick()

void FillMartinData()
{
   datetime bfirst = TimeCurrent()+1;
   datetime blast = 0;
   datetime sfirst = TimeCurrent()+1;
   datetime slast = 0;
   MartinData.BUY_Positions = 0;
   MartinData.SELL_Positions = 0;
   MartinData.BUY_Lots = 0;
   MartinData.SELL_Lots = 0;
   MartinData.BUY_LastPrice = -1;
   MartinData.SELL_LastPrice = -1;
   MartinData.BUY_StopPrice = -1;
   MartinData.SELL_StopPrice = -1;
   MartinData.BUY_LastTime = 0;
   MartinData.SELL_LastTime = 0;
   MartinData.BUY_AllWD = true;
   MartinData.SELL_AllWD = true;
   for(int i=0;i<OrdersTotal();i++)
   {
     if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
     if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNum)
     {
        if (OrderType()==OP_BUY)
        {
           MartinData.BUY_Positions++;
           if (OrderOpenTime()<bfirst)
           {
              MartinData.BUY_Lots = OrderLots();
              MartinData.BUY_StopPrice = OrderStopLoss();
              bfirst = OrderOpenTime();
           }
           if (OrderOpenTime()>blast)
           {
              MartinData.BUY_LastPrice = OrderOpenPrice();
              blast = OrderOpenTime();
           }
           if (OrderStopLoss()<OrderOpenPrice())
              MartinData.BUY_AllWD = false;
        }
        else if (OrderType()==OP_SELL)
        {
           MartinData.SELL_Positions++;
           if (OrderOpenTime()<sfirst)
           {
              MartinData.SELL_Lots = OrderLots();
              MartinData.SELL_StopPrice = OrderStopLoss();
              sfirst = OrderOpenTime();
           }
           if (OrderOpenTime()>slast)
           {
              MartinData.SELL_LastPrice = OrderOpenPrice();
              slast = OrderOpenTime();
           }
           if ((OrderStopLoss()==0) || (OrderStopLoss()>OrderOpenPrice()))
              MartinData.SELL_AllWD = false;
        }
     }
   }
   MartinData.BUY_LastTime = blast;
   MartinData.SELL_LastTime = slast;
   if (MartinData.BUY_Positions==0) MartinData.BUY_AllWD = false;
   if (MartinData.SELL_Positions==0) MartinData.SELL_AllWD = false;
}