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.
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
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
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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.