Download MetaTrader 5

Place pending order

To add comments, please log in or register
Gerardo Bustos
746
Gerardo Bustos  

Hello Forum, good day.

 

I'm staring with MQL5 and tried to place a pending order, but when I ran it in the strategy tester I couldn't make any buy/sell operations. What I'm trying to do is to place a buy stop order if +DI > -DI and a sell stop order if +DI < -DI. I also tried to buy if close price < open price, and sell if close price > open price but couldn't make it.

Any help will be really appreciated, here is the code: 

// Input variables

input double Lots    = 0.1;

input int StopLoss   = 1000;

input int TakeProfit = 1000;

input int MAPeriod   = 10;

input int ADXPeriod  = 14;

// Global variables

bool glBuyPlaced, glSellPlaced;

// OnInit() event handler

int OnInit()

  {

//---

   

//---

   return(0);

  }

// OnTick() event handler

void OnTick()

{

// Trade structures

MqlTradeRequest request;

MqlTradeResult result;

ZeroMemory(request);

// Moving average

double ma[];

ArraySetAsSeries(ma,true);

int maHandle=iMA(_Symbol,0,MAPeriod,MODE_SMA,0,PRICE_CLOSE);

CopyBuffer(maHandle,0,0,1,ma);

// ADX Indicator values

double ADX_Val[], Plus_DI[], Minus_DI[];

ArraySetAsSeries(ADX_Val,true);

ArraySetAsSeries(Plus_DI,true);

ArraySetAsSeries(Minus_DI,true);

int ADX_Handle = iADX(NULL, 0, ADXPeriod);

CopyBuffer(ADX_Handle, 0, 0, 3, ADX_Val);

CopyBuffer(ADX_Handle, 1, 0, 3, Plus_DI);

CopyBuffer(ADX_Handle, 2, 0, 3, Minus_DI);

// Open price

double open[];

ArraySetAsSeries(open,true);

CopyOpen(_Symbol,0,0,1,open);

// High price

double high[];

ArraySetAsSeries(high,true);

CopyHigh(_Symbol,0,0,1,high);

// Low price

double low[];

ArraySetAsSeries(low,true);

CopyLow(_Symbol,0,0,1,low);

// Close price

double close[];

ArraySetAsSeries(close,true);

CopyClose(_Symbol,0,0,1,close);

// Current position information

bool openPosition = PositionSelect(_Symbol);

long positionType = PositionGetInteger(POSITION_TYPE);

double currentVolume = 0;

if ( openPosition == true ) currentVolume = PositionGetDouble(POSITION_VOLUME);

// Open buy market order

if ( Plus_DI[0] > Minus_DI[0] && glBuyPlaced == false && (positionType != POSITION_TYPE_BUY || openPosition == false) )

{

request.action = TRADE_ACTION_PENDING;

request.type = ORDER_TYPE_BUY_STOP;

request.symbol = _Symbol;

request.volume = Lots + currentVolume;

request.type_filling = ORDER_FILLING_FOK;

request.price = SymbolInfoDouble(_Symbol,SYMBOL_ASK);

request.sl = 0;

request.tp = 0;

request.type_time = ORDER_TIME_SPECIFIED;

request.deviation = 50;

request.stoplimit = 0;

OrderSend(request,result);

// Modify SL/TP

if ( result.retcode == TRADE_RETCODE_PLACED || result.retcode == TRADE_RETCODE_DONE )

{

request.action = TRADE_ACTION_SLTP;

do Sleep(100); while(PositionSelect(_Symbol) == false);

double positionOpenPrice = PositionGetDouble(POSITION_PRICE_OPEN);

if ( StopLoss > 0 ) request.sl = positionOpenPrice - (StopLoss * _Point);

if ( TakeProfit > 0 ) request.tp = positionOpenPrice + (TakeProfit * _Point);

if ( request.sl > 0 && request.tp > 0 ) OrderSend(request,result);

glBuyPlaced = true;

glSellPlaced = false;

}

}

// Open sell market order

else if ( Minus_DI[0] < Plus_DI[0] && glSellPlaced == false && positionType != POSITION_TYPE_SELL )

{

request.action = TRADE_ACTION_PENDING;

request.type = ORDER_TYPE_SELL_STOP;

request.symbol = _Symbol;

request.volume = Lots + currentVolume;

request.type_filling = ORDER_FILLING_FOK;

request.price = SymbolInfoDouble(_Symbol,SYMBOL_BID);

request.sl = 0;

request.tp = 0;

request.type_time = ORDER_TIME_SPECIFIED;

request.deviation = 50;

request.stoplimit = 0;

OrderSend(request,result);

// Modify SL/TP

if ( (result.retcode == TRADE_RETCODE_PLACED || result.retcode == TRADE_RETCODE_DONE) && (StopLoss > 0 || TakeProfit > 0) )

{

request.action = TRADE_ACTION_SLTP;

do Sleep(100); while(PositionSelect(_Symbol) == false);

double positionOpenPrice = PositionGetDouble(POSITION_PRICE_OPEN);

if ( StopLoss > 0 ) request.sl = positionOpenPrice + (StopLoss * _Point);

if ( TakeProfit > 0 ) request.tp = positionOpenPrice - (TakeProfit * _Point);

if ( request.sl > 0 && request.tp > 0 ) OrderSend(request,result);

glBuyPlaced = false;

glSellPlaced = true;

}

}

} 

Regards and thank you in advance,

codeMolecules 

Alain Verleyen
Moderator
31189
Alain Verleyen  

Forum on trading, automated trading systems and testing trading strategies


Hello,

Please use the SRC button when you post code. Thank you.


This time, I edited it for you.


Alain Verleyen
Moderator
31189
Alain Verleyen  
codeMolecules:

Hello Forum, good day.

 

I'm staring with MQL5 and tried to place a pending order, but when I ran it in the strategy tester I couldn't make any buy/sell operations. What I'm trying to do is to place a buy stop order if +DI > -DI and a sell stop order if +DI < -DI. I also tried to buy if close price < open price, and sell if close price > open price but couldn't make it.

Any help will be really appreciated, here is the code: 

Regards and thank you in advance,

codeMolecules 

Why do you want to place a pending order at market price ?
Gerardo Bustos
746
Gerardo Bustos  
angevoyageur:
Why do you want to place a pending order at market price ?

Hello angevoyageur,

Sorry, I forgot to edit/format the code I posted, thanks for editing.

 

I started this code as a TRADE_ACTION_DEAL and then started to modify it to make it a TRADE_ACTION_PENDING order and tried to use NormalizeDouble(open[0]_Digits); to get the latest open price but didn't work, so I did some ctrl+z to return just after I started modifying it to a pending order and when I pasted the code on the forum I had returned to request.price = SymbolInfoDouble(_Symbol,SYMBOL_ASK); sorry, my bad.

I'm currently searching in the forum and found this: OrderGetDouble(ORDER_PRICE_OPEN). Is this the correct way to do it? Or should I try it with: MqlRates rates[]; and then request.price = NormalizeDouble(rates[0].open_Digits);?


Regards and thank you in advance,

codeMolecules 

Alain Verleyen
Moderator
31189
Alain Verleyen  
codeMolecules:

Hello angevoyageur,

Sorry, I forgot to edit/format the code I posted, thanks for editing.

 

I started this code as a TRADE_ACTION_DEAL and then started to modify it to make it a TRADE_ACTION_PENDING order and tried to use NormalizeDouble(open[0]_Digits); to get the latest open price but didn't work, so I did some ctrl+z to return just after I started modifying it to a pending order and when I pasted the code on the forum I had returned to request.price = SymbolInfoDouble(_Symbol,SYMBOL_ASK); sorry, my bad.

I'm currently searching in the forum and found this: OrderGetDouble(ORDER_PRICE_OPEN). Is this the correct way to do it? Or should I try it with: MqlRates rates[]; and then request.price = NormalizeDouble(rates[0].open_Digits);?


Regards and thank you in advance,

codeMolecules 

With MqlRates, but you have to use CopyRates, see documentation for an example.
Gerardo Bustos
746
Gerardo Bustos  
angevoyageur:
With MqlRates, but you have to use CopyRates, see documentation for an example.

Thanks for your response angevoyageur. I'll try it with CopyRates.

 

Regards and thank you,

codeMolecules 

To add comments, please log in or register