Previous Next

GTrade.Open()

Opens a trade. Only a stop loss is required. Everything else is determined automatically.

int  GTrade.Open(
   double   stopLoss = 0,      // Position of the StopLoss. This value must be specified
   double   takeProfit = 0,    // Optional TakeProfit
   double   pendingPrice = 0,  // Optional Pending Price
   string   comment = ""       // Comment to the order. Useful for further GTrade functions
   );

Parameters

stopLoss

[in]  StopLoss price. This value is required for automation.

takeProfit

[in]  Optional TakeProfit price. 0 to not specify a value.

pendingPrice

[in]  Optional pending price. Whether Buy-Limit, Sell-Stop, ... is calculated automatically based on the positions of the StopLoss - Pending - Current Bid. 0 to specify no value.

comment

[in]  Comment on the order. Comments can facilitate access via GTrade to certain positions.

Return value

If an error occurs during the calculation, -1 is returned. If the request can be forwarded to the server, the RetCode of the broker is returned.

Note

As which type (Buy, Sell, Buy-Stop, ...) an order is opened is automatically calculated by the positions of the different prices (StopLoss, Pending, ...) to each other and to the current market price.


Example:

//--- Include Glib classes
#include <Glib.mqh>

//--- Creation of a pointer to an empty class object
GTrade *trade;
GChartPatterns *chartPatterns;


int OnInit ()
{
   //--- Assign the class to the empty class object
   trade = gTrade();
   chartPatterns = gChartPatterns();

   //--- Initialize the class data
   trade.Init();
   chartPatterns.Init();

   return(INIT_SUCCEEDED);
}

void OnDeinit (const int reason)
{
   //--- Release memory on exit
   delete trade;
   delete chartPatterns;
}


void OnTick ()
{
   if ( chartPatterns.Ready() ) //--- Will only be executed when all data is ready
   {
      //--- Create structure array
      GStructChartPatterns patternInfo;

      //--- Waits for a breakout signal
      if (chartPatterns.Signal(patternInfo))
      {
         //--- If the direction of the formation is Long, then open a trade with StopLoss at the lowest point of the formation
         if (patternInfo.targetDirection == DIRECTION_LONG)
         {
            trade.Open(patternInfo.lowest);
         }
      }
   }
}


See also

GTrade.Init, GTrade.CloseAllOnSymbol, GTrade.CloseWithComment