trading code

 

Hi, I got a short question, hopfully not too simple for the experts here.

I'm wanting to create a very simple EA for checking things.

A stop an reverse system, so it is always either long or short (i.e. it has always exactly 1 open position).

My code for the trading operations:

int TicketBuy,TicketSell;

if (......condition ti buy .... = true) i.e. open Long position...and close short if it exists

{TicketBuy=OrderSend(Symbol(),OP_BUY,0.1,Ask,20, Bid-Stoploss*Point,Bid+Takeprofit*Point);OrderClose(TicketSell,0.1,Ask,20,Yellow)}

or vice versa

if (......condition sell .... = true)

{TicketSell=OrderSend(Symbol(),OP_SELL,0.1,Bid,20,Ask+Stoploss*Point,Ask-Takeprofit*Point);OrderClose(TicketBuy,0.1,Bid,20,Yellow)}

Now I realised that sometimes an open position is not closed (so 2 positions short and long remain open) or for exmaple a short position is not triggered/opened

I guess the mistake in the code is a simple one?

thanks, if anybody has an idea

 
fxnew:

Hi, I got a short question, hopfully not too simple for the experts here.

I'm wanting to create a very simple EA for checking things.

Read this article: https://book.mql4.com/samples/expert
 
fxnew:

Now I realised that sometimes an open position is not closed (so 2 positions short and long remain open) or for exmaple a short position is not triggered/opened

I guess the mistake in the code is a simple one?

Always code your EA to handle restarts (The computer reboots, the terminal crashes, the user switches charts or selects refresh)

Find out if you have a open order (orderCount/orderSelect) before opening a new one.