Closing order programming question

 

Hello,

I have question regarding code needed to close my order in certain conditions.

I want my order to get closed if its:

a) RSI (7) value gets lower than 50

OR

b) RSI (7) value gets higher than 70 and I want to close my order when it will start to reverse (go down)

like when I had on previous bar value of 86 and on actual bar there is 80.

Please help me up with the code, because I can't figure it out how to program it.

Huge Thanks,

Maine

 

...

Check this thread : https://www.mql5.com/en/forum/177180

I think that it might help you with what you want to do

Maine:
Hello,

I have question regarding code needed to close my order in certain conditions.

I want my order to get closed if its:

a) RSI (7) value gets lower than 50

OR

b) RSI (7) value gets higher than 70 and I want to close my order when it will start to reverse (go down)

like when I had on previous bar value of 86 and on actual bar there is 80.

Please help me up with the code, because I can't figure it out how to program it.

Huge Thanks,

Maine
 
Maine:
Hello,

I have question regarding code needed to close my order in certain conditions.

I want my order to get closed if its:

a) RSI (7) value gets lower than 50

OR

b) RSI (7) value gets higher than 70 and I want to close my order when it will start to reverse (go down)

like when I had on previous bar value of 86 and on actual bar there is 80.

Please help me up with the code, because I can't figure it out how to program it.

Huge Thanks,

Maine

Hi Maine,

This is an example function using a stochastic, would imagine you would just need to change stoch out with Rsi.

void CheckForClose()

{

RefreshRates();

double stonow = iCustom(s_symbol,0,"Double smoothed stochastic 2",Close.Stochastic.Period,Close.Ema.Smoothing.Period,0,Close.bar);

double stopre = iCustom(s_symbol,0,"Double smoothed stochastic 2",Close.Stochastic.Period,Close.Ema.Smoothing.Period,0,Close.bar+1);

//

//

//

//

//

for (int i = 0;i < OrdersTotal(); i++)

{

if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES) == false) break;

if (OrderMagicNumber()!= MAGIC) continue;

if (OrderSymbol() != Symbol()) continue;

//

//

//

//

//

if (OrderType() == OP_BUY)

{

if (stonow < stopre && stonow Up.CloseLevel)

{

bool Closed = OrderClose(OrderTicket(),OrderLots(),Bid,Slippage*Point*pipMultiplier,clCloseBuy);

if (Closed == false)

{

int ErrorCode = GetLastError();

string ErrDesc = ErrorDescription(ErrorCode);

string ErrLog = StringConcatenate("Bid: ",Bid," Lots: ",OrderLots(), " Ticket: ",OrderTicket());

Print(ErrLog);

}

}

break;

}

//

//

//

//

//

if (OrderType() == OP_SELL)

{

if (stonow > stopre && stonow >= Dn.CloseLevel && stopre < Dn.CloseLevel)

{

Closed = OrderClose(OrderTicket(),OrderLots(),Ask,Slippage*Point*pipMultiplier,clCloseSell);

if (Closed == false)

{

ErrorCode = GetLastError();

ErrDesc = ErrorDescription(ErrorCode);

ErrLog = StringConcatenate("Ask: ",Ask," Lots: ",OrderLots(), " Ticket: ",OrderTicket());

Print(ErrLog);

}

}

break;

}

}

}

then somewhere's after Start() put a line something like this to call your function

if(UseStochClose) CheckForClose();

The Up.Close.Level and Dn.Close.Level and stoch close settings made them external parameters to be able to change if needed.

Reason: