Need help in Expert

 
 

Is the 4th bracket "{" OK?

 

There are more than a few errors. Please compair code to find them. I hope this helps.

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);

}

}

}

 

ALso, make sure to change the BID, ASK, TP, SL in the OrderSend method to appropriate settings for the order type.

 

Nicholishen, thank you very much for your advice and help.

 

Nicholishen, thank you very much for your advice and help.

 

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?

 
 

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 ?

 
ralph.ronnquist:
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 ?

Thanks ralph.ronnquist, but that is not the problem. I modified that in my code.

I attached simplifyed expert with report. Trades nubmer 27 and 28 are examples for that case.

Files:
Reason: