How can I use CPositionInfo to determine if an opened trade is a Buy or Sell before I close?

 

I have written some code that uses CTrade to open a buy or sell trade (only one trade for now). I want to know the type of trade before I close it and from what I've read it seems I should be using CPositionInfo to determine whether it's a buy or sell. How can I use this CPositionInfo to check the trade type. Please see my code below:


#include <indicators/indicators.mqh>
#include <Trade/Trade.mqh>

CIndicators g_indicators;
CiRSI       *g_rsi;
CiMACD      *g_macd;


input int magicnumber=112223;
input double Lots =0.005;


CTrade trade;
CPositionInfo pos;



int OnInit() {
   g_rsi = new CiRSI();
   g_indicators.Add(g_rsi);
   g_macd = new CiMACD();
   g_indicators.Add(g_macd);
   bool is_init = g_rsi.Create(_Symbol, PERIOD_M5, 2, PRICE_CLOSE);
   is_init &= g_macd.Create(_Symbol, PERIOD_M5, 12, 26, 9, PRICE_CLOSE);
   return is_init ? INIT_SUCCEEDED : INIT_FAILED;
}

void OnTick() {
   g_indicators.Refresh();
   
  
      if (g_macd.Main(0) > g_macd.Signal(0) && g_rsi.Main(0) > 84) { // Here is your OPEN BUY rule
      
         if (PositionsTotal() == 0) {
         double Ask = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits); // Get the Ask Price
         trade.Buy(Lots, NULL, Ask, 0, 0, NULL);
           }
         }
      
  
   if (g_macd.Main(0) < g_macd.Signal(0) && g_rsi.Main(0) < 16) { // Here is your OPEN SELL rule      

         if (PositionsTotal() == 0) {
         double Bid = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits); // Get the Bid Price
         trade.Sell(Lots, NULL, Bid, 0, 0, NULL);
          }
            
         }
         
     if (g_macd.Main(0) > g_macd.Signal(0) && g_rsi.Main(0) > 84) { // Here is your CLOSE SELL rule
     
            
            if (PositionsTotal() == 1) {
               //CHECK POSITION IF SELL THEN CLOSE
            }
         
         }
      
  
   if (g_macd.Main(0) < g_macd.Signal(0) && g_rsi.Main(0) < 16) { // Here is your CLOSE BUY rule      
            if (PositionsTotal() == 1) {
               //CHECK POSITION IF BUY THEN CLOSE
            }
         
            
         }
          
   }
   
   
 
Jackery:

I have written some code that uses CTrade to open a buy or sell trade (only one trade for now). I want to know the type of trade before I close it and from what I've read it seems I should be using CPositionInfo to determine whether it's a buy or sell. How can I use this CPositionInfo to check the trade type. Please see my code below:


Good to see you're putting my code to use :) Remember the D.R.Y. principle, you don't need to repeat the algo ecpressions twice. Since this is an always in type strategy the you can use net position as a factor for opening/reversing trades. If you have a sell position of 1 lot then your net position is (-1). In order to reverse that you would want to place a buy trade for 2 lots, -1 + 2 = 1. Your new position is +1. In this case it would be easier to calculate the net position size and make use of netting. 

#include <indicators/indicators.mqh>
#include <Trade/Trade.mqh>

input ulong inp_magic = 112223;
input double inp_lots = 1;
input int   inp_rsi_delta_zone = 16;

CIndicators    g_indicators;
CiRSI          *g_rsi;
CiMACD         *g_macd;
CTrade         g_trade;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit() {
   g_trade.SetExpertMagicNumber(inp_magic); //////////////////
   g_rsi = new CiRSI();
   g_indicators.Add(g_rsi);
   g_macd = new CiMACD();
   g_indicators.Add(g_macd);
   bool is_init = g_rsi.Create(_Symbol, PERIOD_M5, 2, PRICE_CLOSE);
   is_init &= g_macd.Create(_Symbol, PERIOD_M5, 12, 26, 9, PRICE_CLOSE);
   return is_init ? INIT_SUCCEEDED : INIT_FAILED;
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick() {
   g_indicators.Refresh();
   double net_position = netPosition();///////////////////////////
   double lots = inp_lots;
   if(g_macd.Main(0) > g_macd.Signal(0)
         && g_rsi.Main(0) > (100 - inp_rsi_delta_zone)
         && net_position <= 0.0
     ) {  // Here is your OPEN BUY rule
      if (net_position < 0.0) {
         lots -= net_position;
      }
      g_trade.Buy(lots);
   }
   if(g_macd.Main(0) < g_macd.Signal(0)
         && g_rsi.Main(0) < inp_rsi_delta_zone
         && net_position >= 0.0
     ) {  // Here is your OPEN SELL rule
      if (net_position > 0.0) {
         lots += net_position;
      }
      g_trade.Sell(lots);
   }
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double netPosition() {
   CPositionInfo  pos_info;
   double volume = 0.0;
   if (pos_info.SelectByMagic(_Symbol, inp_magic)) {
      volume = pos_info.Volume();
      if (volume > 0.0) {
         volume = pos_info.PositionType() == POSITION_TYPE_BUY ? volume : -volume;
      }
   }
   return volume;
}

//+------------------------------------------------------------------+