| New signals service! | |
|
|||||||
| Register in Forex TSD! | |
|
Trading Systems Leaders in this forum (automated trading systems) are winning more than 3000 pips in a month (30000$ investing one lot every time). Click here to register and get more information |
|
![]() |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
Need help in Expert
I am the beginer in MQL programing and I need som help.
I have two separate experts, one for long and another for short positions. When I put that experts(code) together my results are different. This is the code for opening the trades. Max number of open positions is 2 – one Buy – one Sell, so we can't open two Buy or two Sell positions. //************************************************** ********************************** if( Condition for Long position is meet ) { if (OrdersTotal()==1) { OrderSelect(1, SELECT_BY_POS , MODE_TRADES); if(OrderType()== OP_SELL) //If alredy open position is Sell then open Buy position { ticket=OrderSend(Symbol(),OP_BUY,0.1,Ask,0,Ask-xx*Point,Ask+yy*Point,"",16384,0,Green); { if(ticket<0) { Print("OrderSend failed with error #",GetLastError()); return(0); } } } } if (OrdersTotal()==0) // If there is no open position then open the Buy position. { ticket=OrderSend(Symbol(),OP_BUY,0.1,Ask,0,Ask-xx*Point,Ask+yy*Point,"",16384,0,Green); { if(ticket<0) { Print("OrderSend failed with error #",GetLastError()); return(0); } } } } //*********************************************** else { if( Condition for Short position is meet) { if (OrdersTotal==1) { OrderSelect(1, SELECT_BY_POS , MODE_TRADES); if(OrderType()== OP_BUY) // If open position is Buy then open Sell position { ticket=OrderSend(Symbol(),OP_SELL,0.1,Bid,0,Bid+xx *Point,Bid-yy*Point,"",16384,0,Green); { if(ticket<0) { Print("OrderSend failed with error #",GetLastError()); return(0); } } } } if (OrdersTotal==0) // If there is no open positions then open Sell position { ticket=OrderSend(Symbol(),OP_SELL,0.1,Bid,0,Bid+xx *Point,Bid-yy*Point,"",16384,0,Green); { if(ticket<0) { Print("OrderSend failed with error #",GetLastError()); return(0); } } } } //************************************************** ******************************************** ....... What is wrong with this code? Can someone provide different code for this chase. Thanks! |
|
||||
|
Buy and sell at same time?
I'm not sure if it's intentional or a basic trading rule or something, but the overall code structure rules out the possibility of making short trade whenever the conditions for long are met, regardless of whether there is along trade or not. In particular, even when a long position is open, the EA won't consider making a short trade if the conditions for long are met.
If the EA is formed by using independent logics for long and short trading, maybe this should be combined independently? A good software design for that would be to preserve all the code of two trading directions, but add a prefix like "long_" and "short_" to their identifiers. The combined EA would then have an outline: //-----------+ int init() { long_init(); short_init(); return(0); } int deinit() { long_deinit(); short_deinit(); return(0); } int start() { long_start(); short_start(); return(0); } //-----------------+ This approach of course relies on that the two sub systems don't hog the thread in Sleep calls. There is also a preference for long trades that may have an impact when the equity is low. ... just a thought |
|
|||
|
I modified the expert but problem persist.
Expert opens total 925 positions - 698 Short and 227 Long. When I set Long condition to unattainable then expert, of course, opens only Short positions and number of Short trades trades is the same - 698. But when I set Short condition to unattainable number of Long positions is 358(this is correct number of trades) . So total number of trades must be 1056, not 925. What is the problem in above code? Btw, opening conditions depends(among others) on time. So, short positions are opened in the forenoon (GMT) and Long positions in the afternoon. I assume Long positions cannot be open when Short position is open. Why? Last edited by n7drazen; 01-10-2007 at 08:38 AM. |
|
|||
|
Quote:
In some cases Expert opens Short positions even if one Short position is already open - but code says - open Short position if(OrderType()== OP_BUY) or if OrdersTotal()==0. Now I am confused. Last edited by n7drazen; 01-10-2007 at 09:06 AM. |
|
||||
|
missing parentheses?
Well, the code you submitted as well as Nicholishen's corrected code has the conditions like
: if ( OrdersTotal == 1 ) ... and : if ( OrdersTotal == 0 ) ... in the short trade logic. That is, it lacks the parentheses "OrdersTotal()". It seems peculiar that it compiles, but perhaps "OrdersTotal" without parentheses gets treated as a variable with value 0 ? |
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Expert Question and Expert | mj10 | Expert Advisors - Metatrader 4 | 24 | 03-27-2006 01:31 PM |