Results 1 to 5 of 5
 0 Attachment(s)    

Thread: Profit of the oldest open long- and short-position

  1. #1
    Junior Member
    Join Date
    May 2011
    Location
    Switzerland
    Posts
    16

    Default Profit of the oldest open long- and short-position

    Hello
    I would like to calculate the Profit of the oldest Long-Position and the Profit of the oldest short-Position (yes i do not know how).


    Code:
    //+------------------------------------------------------------------+
    double countprofitoldestshort(int mode,int order_type)
      {
       double profit=0;
       if(OrdersTotal()>0)
         {
          for(i=OrdersTotal()-1;i>=0;i--)
            {
             OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
             if(OrderSymbol()==Symbol() &&  && OrderType()==OP_SELL && OrderMagicNumber()==magic)
               {
                if(mode==0)profit=profit+(OrderClosePrice()-OrderOpenPrice())/pt;
                if(mode==1)profit=profit+OrderProfit()+OrderCommission()+OrderSwap();
               }
            }
         }
       return(profit);
      }
     
    
    
    //+------------------------------------------------------------------+
    double countprofitoldestlong(int mode,int order_type)
      {
       double profit=0;
       if(OrdersTotal()>0)
         {
          for(i=OrdersTotal()-1;i>=0;i--)
            {
             OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
             if(OrderSymbol()==Symbol() &&  && OrderType()==OP_BUY && OrderMagicNumber()==magic)
               {
                if(mode==0)profit=profit+(OrderClosePrice()-OrderOpenPrice())/pt;
                if(mode==1)profit=profit+OrderProfit()+OrderCommission()+OrderSwap();
               }
            }
         }
       return(profit);
      }
    best regards
    Chris

  2. #2
    Junior Member
    Join Date
    Feb 2009
    Posts
    5

    Default

    Try this code.

    PHP Code:

    double ProfitBuy
    ;
    double ProfitSell;

    void countprofitoldestshort()
      {
       
    ProfitBuy=0;
       
    ProfitSell=0;
       if(
    OrdersTotal()>0)
         {
          for(
    i=OrdersTotal()-1;i>=0;i--)
            {
             
    OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
             if(
    OrderSymbol()==Symbol() &&  (OrderType()==OP_SELL || OrderType()==OP_BUY ) && OrderMagicNumber()==magic)
               {
                if(
    OrderType()==OP_BUYProfitBuy=OrderProfit()+OrderCommission()+OrderSwap();
                if(
    OrderType()==OP_SELLProfitSell=OrderProfit()+OrderCommission()+OrderSwap();
               }
            }
         }
         } 

  3. #3
    Junior Member
    Join Date
    May 2011
    Location
    Switzerland
    Posts
    16

    Default

    Thank you.
    after Long time trying i got it:

    PHP Code:
    //_Type OP_BUY = BUY, _Type OP_SELL = SELL
    double GetOldestPositionPL(int _Modeint _Type)
    {
       
    double TotalProfit=0;
       
    datetime OpenTime=0;
       for(
    int i=0;i<OrdersTotal();i++)
       {
          if(
    OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
          {
             if(
    OrderMagicNumber()!=magic || OrderSymbol()!=Symbol())        continue;
             
             if(
    OrderOpenTime()<OpenTime || OpenTime==0)
             {
                if(
    _Type==OP_BUY && OrderType()==OP_BUY)
                {
                   if(
    _Mode==0)
                   {
                      
    TotalProfit = (OrderClosePrice()-OrderOpenPrice())*MathPow(10,_Digits);
                   }else if(
    _Mode==1)
                   {
                      
    TotalProfit OrderProfit()+OrderCommission()+OrderSwap();
                   }
                   
    OpenTime OrderOpenTime();
                }else if(
    _Type==OP_SELL && OrderType()==OP_SELL)
                {
                   if(
    _Mode==0)
                   {
                      
    TotalProfit = (OrderOpenPrice()-OrderClosePrice())*MathPow(10,_Digits);
                   }else if(
    _Mode==1)
                   {
                      
    TotalProfit OrderProfit()+OrderCommission()+OrderSwap();
                   }
                   
    OpenTime OrderOpenTime();
                }
             }
             
          }
       }
       
       return 
    NormalizeDouble(TotalProfit,2);

    Last edited by christion; 08-08-2018 at 12:24.
    best regards
    Chris

  4. #4
    Junior Member
    Join Date
    May 2011
    Location
    Switzerland
    Posts
    16

    Default

    Next would be to Close the Oldest-Position
    like Close-Oldest-Open-Buy-Position and Close-Oldest-Open-Sell-Position....but no idea now how to do it...
    best regards
    Chris

  5. #5
    Junior Member
    Join Date
    Feb 2009
    Posts
    5

    Default

    To get orders' results

    PHP Code:
    double ProfitBuy;
    double ProfitSell;
    int OlderTicketBuy;
    int OlderTicketSell;

    void countprofitoldestshort() 
      { 
       
    ProfitBuy=0
       
    ProfitSell=0;
       
    OlderTicketBuy=-1;
       
    OlderTicketBuy=-1;
       if(
    OrdersTotal()>0
         { 
          for(
    i=OrdersTotal()-1;i>=0;i--) 
            { 
             
    OrderSelect(i,SELECT_BY_POS,MODE_TRADES); 
             if(
    OrderSymbol()==Symbol() &&  (OrderType()==OP_SELL || OrderType()==OP_BUY ) && OrderMagicNumber()==magic
               { 
                if(
    OrderType()==OP_BUY) {ProfitBuy=OrderProfit()+OrderCommission()+OrderSwap(); OlderTicketBuy=OrderTicket();}
                if(
    OrderType()==OP_SELL) {ProfitSell=OrderProfit()+OrderCommission()+OrderSwap();  OlderTicketSell=OrderTicket();}
               } 
            } 
         } 
        }


    //code to close orders

    void CloseOrders(int CloseType)
      {
      
    bool CloseTicket;
      if(
    OrdersTotal()>0
         { 
          for(
    i=OrdersTotal()-1;i>=0;i--) 
            {
             
    CloseTicket=false;
             
    OrderSelect(i,SELECT_BY_POS,MODE_TRADES); 
             if(
    OrderSymbol()==Symbol() &&  (OrderType()==OP_SELL || OrderType()==OP_BUY ) && OrderMagicNumber()==magic
               { 
                if((
    CloseType==OP_BUY)&&(OrderType()==OP_BUY)&&(OrderTicket()==OlderTicketBuy)) CloseTicket=OrderClose(OrderTicket(),OrderLots(),Bid,3,clrMediumAquamarine);
                if((
    CloseType==OP_SELL)&&(OrderType()==OP_SELL )&&(OrderTicket()==OlderTicketSell)) CloseTicket=OrderClose(OrderTicket(),OrderLots(),Ask,3,clrMediumAquamarine);
               } 
            } 
         } 
      } 

Similar Threads

  1. Open Reverse Position after SL
    By jidonk in forum MQL programming
    Replies: 4
    Last Post: 08-07-2010, 10:11
  2. Open Long and Short Order simultaneously
    By fairylord in forum Scripts and other tools
    Replies: 9
    Last Post: 03-09-2010, 13:25
  3. Open Position at Close Candle
    By Xandra in forum Ideas for expert advisors
    Replies: 1
    Last Post: 12-30-2009, 06:50
  4. Add Close previous order when open new position
    By darkkiller in forum MQL programming
    Replies: 1
    Last Post: 03-21-2009, 05:55

Tags for this Thread

buy, calculate, code, double, iforderstotal0, ifordersymbol==symbol, int, mode, order_type, position, profit, return, sell, short, trades, type

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •