Object Oriented Approach to MQL

 

I was trying implement an object oriented approach to my EA's. I know basic OOP programming in Java, but the only C++ I know is from writing basic EA's in MQL4. I wrote a short test script just to understand the basics first, I just want it to open a trade based on a condition. The condition I've put should always be true so it should just open a trade. When I try to open the script nothing happens, nothing appears in the trade journal or anything. I'm sure it's something very basic, I just need to get started. 


Thanks in advance.


void OnStart()
  {
//---

   Condition condition = new Condition("Ask > 0.000001");
   Trade newTrade = new Trade(NULL,buy,.1,10,10,condition);
   newTrade.makeTrade();
   
  }
//+------------------------------------------------------------------+
  
// Define Class
class Trade{
   public:
      // Declare variables
      string pair;
      string buySell;
      int lots;
      string askBid;
      string price;
      double stopLoss;
      double takeProfit;
      double priceLess;
      double pricePlus;
      Condition condition; 
      string orderType;
      double priceMinus(int, string);
      double pricePlus(int, string);
      void makeTrade();   
}


// Class constructor
Trade::Trade(string currency, string theBuySell, int amount, int stop, int profit, Condition theCondition){
   // Set member variables
   pair = currency;
   buySell = theBuySell;
   lots = amount;
   stopLoss = stop;
   takeProfit = profit;
   condition = theCondition;
   
   // Define order type and price based on buy or sell order
   if(buySell.compare("buy") == 0){
      orderType = OP_BUY;
      price = "Ask";
   }
   else if(buySell.compare("sell") == 0){
      orderType = OP_SELL;
      price = "Bid";
   }
   
   
   // Set stop loss and take profit based on pips from price
   if(buySell.compare("buy") == 0)
      stopLoss = priceMinus(stop, "Ask");
   else if(buySell.compare("sell") == 0)
      stopLoss = pricePlus(stop, "Bid");
      
   if(buySell.compare("buy") == 0)
      takeProfit = pricePlus(profit, "Ask");
   else if(buySell.compare("sell") == 0)
      takeProfit = priceMinus(profit, "Bid");
}


// Define makeTrade method
void Trade::makeTrade(){
   if(condition)
      OrderSend(pair,orderType,lots,price,50,stopLoss,takeProfit,NULL,0,0,clrNONE);
}


// Define Condition class
class Condition{
   public:
      String condition;
}


// Condition class constructor
Condition::Condition(string theCondition){
   condition = theCondition;
}


// Define priceMinus method
double Trade::priceMinus(int minusAmount, string theAskBid){
   // pips below the price
   priceLess = theAskBid - Point*minusAmount*10;
   return priceLess
}


// Define pricPlus method
double Trade::pricePlus(int plusAmount, string theAskBid){
   // pips above the price
   pricePlus = theAskBid + Point*plusAmount*10;
   return pricePlus
}

 
Drew Clayman:

I was trying implement an object oriented approach to my EA's. I know basic OOP programming in Java, but the only C++ I know is from writing basic EA's in MQL4. I wrote a short test script just to understand the basics first, I just want it to open a trade based on a condition. The condition I've put should always be true so it should just open a trade. When I try to open the script nothing happens, nothing appears in the trade journal or anything. I'm sure it's something very basic, I just need to get started. 


Thanks in advance.


Try to call same code in Expert Advisor file type not script, usually scripts are executed only once and some conflict can happen.
 
By the way, you need structure to open trade in mql5
 
You need to define a structure and then OrderSend( request,result) or call from Ctrade class the object property buy function.
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;                        // 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
 

When you test whether your condition is true or not, you cannot say 'if condition'. Because this variable holds the pointer to your class instance. So 'if condition' effectively queries if the variable is empty, which is of course not, and so it is always true and would always open an order.

This is just one of numerous flaws that would need to be corrected. Another one is that you call OrderSend with orderType being a string(!) instead of an enum.

You said that you coded in Java, hey does Java allow a string to be converted into an enum on the fly? Surely not. Get back to the drawing board. :D

 

Hey, thanks, yeah, with the condition, that's true, I just wasn't thinking, though I did say I gave a condition that's always true. And, alright, with the enumeration, didn't realize the string wouldn't work. I said I coded in Java, but I didn't say I code well in Java :) . So just to make it simpler, should this code create a trade?

void OnStart()
  {
//---
   Trade newTrade = new Trade(true);
   newTrade.makeTrade();
   
  }
//+------------------------------------------------------------------+


class Trade(){
   public:
      bool boolean;
      void makeTrade();
}

Trade::Trade(bool theBoolean){
   boolean = theBoolean;
}

void Trade::makeTrade(){
   if(boolean){
      MqlTradeRequest request={0};
      MqlTradeResult  result={0};
      request.action   =TRADE_ACTION_DEAL;                     
      request.symbol   =Symbol();                             
      request.volume   =0.1;                                   
      request.type     =ORDER_TYPE_BUY;                        
      request.price    =SymbolInfoDouble(Symbol(),SYMBOL_ASK); 
      request.deviation=5;                                     
      request.magic    =EXPERT_MAGIC;                          
      if(!OrderSend(request,result))
         PrintFormat("OrderSend error %d",GetLastError());     
   }
}


Theoretically should this basic code make a script that opens a trade


 
Drew Clayman:

Hey, thanks, yeah, with the condition, that's true, I just wasn't thinking, though I did say I gave a condition that's always true. And, alright, with the enumeration, didn't realize the string wouldn't work. I said I coded in Java, but I didn't say I code well in Java :) . So just to make it simpler, should this code create a trade?


Theoretically should this basic code make a script that opens a trade


Are you coding MQL 5 or 4? Because you started the topic with 4. It looks ok, but I don't know if a script will trigger the order. Didn't do scripts so far. 'boolean' is quite a bit cheesy sort of a name for a member variable, don't you think? And things like if eval("Ask > 0.000001") makeTrade() wouldn't work since there is no string eval in MQL.
Reason: