Trading the 15m TF - Checking open trades and closing them if it is negative after a 1HR bar close

 

I wish to check my open trades (from the 15m TF) and close trades that are negative (after that trade was executed) after the 1 HR bar closes.

Any help is very much appreciated thank you.

Here's what I have so far:

void checkCloseOnTime()

{

double HourBarClose=iClose(mySymbol, PERIOD_H1, 1);

for(int pos = OrdersTotal() - 1; pos >= 0; pos--)

{

if(OrderSelect(pos, SELECT_BY_POS)

&& OrderMagicNumber() == MagicNumber

&& OrderSymbol() == Symbol()

&& TimeCurrent() - OrderOpenTime()>3600

&& HourBarClose)

{

CloseAll();

}

}

return;

}

 


Please use this to post code . . . it makes it easier to read.

 
TraderJoe:

I wish to check my open trades (from the 15m TF) and close trades that are negative (after that trade was executed) after the 1 HR bar closes.

Any help is very much appreciated thank you.

Here's what I have so far:

void checkCloseOnTime()

{

double HourBarClose=iClose(mySymbol, PERIOD_H1, 1);

for(int pos = OrdersTotal() - 1; pos >= 0; pos--)

{

if(OrderSelect(pos, SELECT_BY_POS)

&& OrderMagicNumber() == MagicNumber

&& OrderSymbol() == Symbol()

&& TimeCurrent() - OrderOpenTime()>3600

&& HourBarClose)

{

CloseAll();

}

}

return;

}


You have to learn a lot

Beginning with this

double HourBarClose=iClose(mySymbol, PERIOD_H1, 1);
What do you think it is doing ???
 
What do you think this test does
double HourBarClose=iClose(mySymbol, PERIOD_H1, 1);

for(int pos = OrdersTotal() - 1; pos >= 0; pos--){
if(...
&& HourBarClose)
 

You click the SRC button and paste your code into the box that appears, then click insert

int pos = OrdersTotal () - 1 ; // initialize pos to equal all order entered -1 (not sure I completely get the -1 part since these are orders and not bars)

pos >= 0 ; // as long as pos is greater than or equal to 0

pos--) // increment through all orders beginning with the last to the earliest order entered (we are scanning backwards from normal) .

and then click Add your comment

 

ahhh... it was the insert part I missed

Hello and thank you for your instruction. In the HourBarClose below I am trying to simply use the 1HR bar close condition but from a default 15m chart.

double HourBarClose=iClose(mySymbol, PERIOD_H1, 1);

As far as your question "what do you think this test does?"

double HourBarClose=iClose(mySymbol, PERIOD_H1, 1);

for(int pos = OrdersTotal() - 1; pos >= 0; pos--){
if(...
&& HourBarClose)

I was hoping to find the order based on it's magic number and on the condition that the 1HR bar closed, close the trade.

I almost but not quite understand how the for loop works. For instance:
Please correct me where I am incorrect.

1. the initialization expression:

int pos = OrdersTotal() - 1;

initializes pos to equal all current open orders. I believe the -1 sets the index to the last counted order but not sure.

2. the test expression

pos >= 0;

this basically asks the question, if there are more than 0 orders

3. the incrememt

pos--)

count back from the last order entered to the first

if(...
&& HourBarClose)

if this order is found and the Current H1 bar just closed.

{ do something }

Thank you for helping me understand the for loop.

 
void checkCloseOnTime()

{

//double HourBarClose=iClose(mySymbol, PERIOD_H1, 1);
/*makes no sense to use for selecting open trades
  it is calcultating the closeprice of the last closed one hour bar of mySymbol*/

for(int pos = OrdersTotal() - 1; pos >= 0; pos--)/* This loop is passing all your open trades one by one */

{

if(OrderSelect(pos, SELECT_BY_POS)/*it select a trade*/

&& OrderMagicNumber() == MagicNumber  /*and then you do more selecting to have only the trade of your EA*/

&& OrderSymbol() == Symbol()          /*if MagicNumber and Symbol()are selected then you don't mess normally with other trades*/

&& TimeCurrent() - OrderOpenTime()>3600   /*This is not the way for checking condition 1HR bar close you look here if the trade is already running more then an hour */ 

&& HourBarClose)   /*useless condition*/

{

CloseAll();         /*close every trade with the conditions you selected*/

}

}

return;

} 
 
Your coding is not checking the orderprofit of the trades
The way to check it when 1H bar close is wrong
I think that if you find one trade negative you all trades of this EA are going to close with your other function CloseAll()
but it is also possible that it not only trades of this EA close but also the other trades not from this EA
you have to close only the one with the right conditions for closing























Reason: