Simple code to open and close a position

 
  • Usually people who cannot code do not receive free help on this forum.
  • If you show your attempts and describe your problem clearly, you will most probably receive an answer from the community. Use the CODE button (Alt-S) when inserting code.
  • To learn MQL programming, you can research the many available Articles on the subject, or examples in the Codebase, as well as reference the online Documentation.
  • If you do not want to learn to code, that is not a problem. You can either look at the Codebase if something free already exists, or in the Market for paid products (also sometimes free). However, recommendations or suggestions for Market products are not allowed on the forum, so you will have to do your own research.
  • Finally, you also have the option to hire a programmer in the Freelance section.
 


I know how to code but in MQL4:

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+

int stoplossAchat= 0;
int takeprofitAchat= 14000;
int stoplossVente= 14000;
int takeprofitVente= 0;
double quantite= 1;
int slippage= 3;
int ticketAchat;
int ticketVente;
double cours;
double position;
double rsi;
double rsiPasse;

void OnTick()
{

if(1==1)
{
rsi=iRSI(NULL,0,14,PRICE_CLOSE,0);
rsiPasse=iRSI(NULL,0,14,PRICE_CLOSE,1);

if(rsi>30 && rsiPasse<30 && position==0)
{
ticketAchat= OrderSend(NULL,OP_BUY,quantite,Ask,slippage,stoplossAchat,takeprofitAchat,"commentaire",16384,0,Green);
position=1;
}


if(rsi>50 && position==1)
{
OrderClose(ticketAchat, quantite, Bid, slippage, Green);
position=0;
}


if(rsi<70 && rsiPasse>70 && position==0)
{
ticketVente= OrderSend(NULL,OP_SELL,quantite,Bid,slippage,stoplossVente,takeprofitVente,"commentaire",16384,0,Red);
position=-1;
}

if(rsi<50 && position==-1)
{
OrderClose(ticketVente, quantite, Ask, slippage, Red);
position=0;
}


}

//return(INIT_SUCCEEDED);
}




//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
Can someone help me to translate the opening and closing positions in MQL5?
 
yoannh: Can someone put here a very easy code in MQL5 to open and close a position. I tried this: PositionOpen, but it doesn't work.

You must use the CTrade Class if you want to use the PositionOpen function.

for example:

#include <Trade\Trade.mqh>
CTrade myTrade;
 
Thank you two both. I will try that.