Closing a single position in a hedging account if it has profit

To add comments, please log in or register
micke81
15
micke81  

im am really struggling with this code I have got it to place buy orders and it opens the correctnumber of trades. Then I want to close a single ticket if that ticket number has a profit of 1.20.Please helpVery much appreciated

#include <Math\Stat\Normal.mqh>
#include <Trade\PositionInfo.mqh>
#include <Trade\Trade.mqh>
#include <Expert\Expert.mqh>
CPositionInfo  m_position;                   // trade position object
CTrade         m_trade;                      // trading object
CSymbolInfo    m_symbol;
input ENUM_ORDER_TYPE_FILLING Trade_Type_Filling  =ORDER_FILLING_IOC; //Trade Type Filling (FOK,IOC, Return)
#define EXPERT_MAGIC 358100   // MagicNumber of the expert
#define TAKEPROFIT 10       // Take profit value



void OnTick()
  {
    // get the ask price
   double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
   
   // get the account balance 
   double Balance=AccountInfoDouble(ACCOUNT_BALANCE);
  
   // get the equity
   double Equity=AccountInfoDouble(ACCOUNT_EQUITY);
   
   double PositionProfit=PositionGetDouble(POSITION_PROFIT);   //position profit variable
   double PositionSwap=PositionGetDouble(POSITION_SWAP);       //Position swap
   // Print ("### Swap ",PositionSwap);
   // Print ("### Profit ",PositionProfit);
   double ProfitThisCurrencyPair =(PositionProfit+PositionSwap);  //add profit and swap
   // Print ("### ProfitPair ",ProfitThisCurrencyPair);
   
   
   // open some buy positions to have something to close
   if (PositionsTotal()<7)
//+------------------------------------------------------------------+
//| Opening Buy position                                             |
//+------------------------------------------------------------------+
  {
//--- declare and initialize the trade request and result of trade request
   MqlTradeRequest request={0};
   MqlTradeResult  result={0};
//--- parameters of request
   request.action   =TRADE_ACTION_DEAL;                     // type of trade operation
   request.symbol   =Symbol();                              // symbol
   request.volume   =0.1;                                   // volume of 0.1 lot
   request.type     =ORDER_TYPE_BUY;   
   request.type_filling =Trade_Type_Filling;                     // order type
   request.price    =SymbolInfoDouble(Symbol(),SYMBOL_ASK); // price for opening
   request.deviation=5;                                     // allowed deviation from the price
   request.magic    =EXPERT_MAGIC;                          // MagicNumber of the order
//--- send the request
   if(!OrderSend(request,result))
      PrintFormat("OrderSend error %d",GetLastError());     // if unable to send the request, output the error code
//--- information about the operation
   PrintFormat("retcode=%u  deal=%I64u  order=%I64u",result.retcode,result.deal,result.order);
  }
  
//+------------------------------------------------------------------+
//| Opening Sell position                                            |
//+------------------------------------------------------------------+
 // open some buy positions to have something to close

   if (PositionsTotal()<1)
  {
//--- declare and initialize the trade request and result of trade request
   MqlTradeRequest request={0};
   MqlTradeResult  result={0};
//--- parameters of request
   request.action   =TRADE_ACTION_DEAL;                     // type of trade operation
   request.symbol   =Symbol();                              // symbol
   request.volume   =0.2;                                   // volume of 0.2 lot
   request.type     =ORDER_TYPE_SELL;  
   request.type_filling =Trade_Type_Filling;                     // order type
   request.price    =SymbolInfoDouble(Symbol(),SYMBOL_BID); // price for opening
   request.deviation=5;                                     // allowed deviation from the price
   request.magic    =EXPERT_MAGIC;                          // MagicNumber of the order
//--- send the request
   if(!OrderSend(request,result))
      PrintFormat("OrderSend error %d",GetLastError());     // if unable to send the request, output the error code
//--- information about the operation
   PrintFormat("retcode=%u  deal=%I64u  order=%I64u",result.retcode,result.deal,result.order);
  }
//+------------------------------------------------------------------+


//+------------------------------------------------------------------+
//| Close all positions by opposite positions                        |
//+------------------------------------------------------------------+
  
   if(ProfitThisCurrencyPair > TAKEPROFIT)
   
  {
//--- declare and initialize the trade request and result of trade request
   MqlTradeRequest request;
   MqlTradeResult  result;
   int total=PositionsTotal(); // number of open positions   
//--- iterate over all open positions
   for(int i=total-1; i>=0; i--)
     {
      //--- parameters of the order
      ulong  position_ticket=PositionGetTicket(i);                                      // ticket of the position
      string position_symbol=PositionGetString(POSITION_SYMBOL);                        // symbol 
      int    digits=(int)SymbolInfoInteger(position_symbol,SYMBOL_DIGITS);              // number of decimal places
      ulong  magic=PositionGetInteger(POSITION_MAGIC);                                  // MagicNumber of the position
      double volume=PositionGetDouble(POSITION_VOLUME);                                 // volume of the position
      ENUM_POSITION_TYPE type=(ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);    // type of the position
      //--- output information about the position
      PrintFormat("#%I64u %s  %s  %.2f  %s [%I64d]",
                  position_ticket,
                  position_symbol,
                  EnumToString(type),
                  volume,
                  DoubleToString(PositionGetDouble(POSITION_PRICE_OPEN),digits),
                  magic);
      //--- if the MagicNumber matches
      if(magic==EXPERT_MAGIC)
        {
         //--- zeroing the request and result values
         ZeroMemory(request);
         ZeroMemory(result);
         //--- setting the operation parameters
         request.action   =TRADE_ACTION_DEAL;        // type of trade operation
         request.position =position_ticket;          // ticket of the position
         request.symbol   =position_symbol;          // symbol 
         request.volume   =volume;                   // volume of the position
         request.deviation=5;                        // allowed deviation from the price
         request.magic    =EXPERT_MAGIC;             // MagicNumber of the position
         //--- set the price and order type depending on the position type
         if(type==POSITION_TYPE_BUY)
           {
            request.price=SymbolInfoDouble(position_symbol,SYMBOL_BID);
            request.type =ORDER_TYPE_SELL;
           }
         else
           {
            request.price=SymbolInfoDouble(position_symbol,SYMBOL_ASK);
            request.type =ORDER_TYPE_BUY;
           }
         //--- output information about the closure
         PrintFormat("Close #%I64d %s %s",position_ticket,position_symbol,EnumToString(type));
         //--- send the request
         if(!OrderSend(request,result))
            PrintFormat("OrderSend error %d",GetLastError());  // if unable to send the request, output the error code
         //--- information about the operation   
         PrintFormat("retcode=%u  deal=%I64u  order=%I64u",result.retcode,result.deal,result.order);
         //---
        }
     }
  }
}
Seng Joo Thio
556
Seng Joo Thio  
micke81:

im am really struggling with this code I have got it to place buy orders and it opens the correctnumber of trades. Then I want to close a single ticket if that ticket number has a profit of 1.20.Please helpVery much appreciated

In this section:

//+------------------------------------------------------------------+
//| Close all positions by opposite positions                        |
//+------------------------------------------------------------------+

You're aware of the need to iterate through PositionsTotal() and then use PositionGetTicket(i) to select individual position for further processing.

However, you didn't do the same at the start, when u have these lines:

   double PositionProfit=PositionGetDouble(POSITION_PROFIT);   //position profit variable
   double PositionSwap=PositionGetDouble(POSITION_SWAP);       //Position swap
So your ProfitThisCurrencyPair will always be zero, and no position will meet the closing criteria.


To add comments, please log in or register