int BUYduration = iBarShift(0,NULL, OrderOpenTime());
iBarShift returns number of bars for the specified period. If you write NULL, it will use current chart period. If you are using for example H1, you will get number of one hour bars that elapsed since order was opened. If you want to use iBarShift and count minute bars, put PERIOD_M1 as second parameter:
int BUYduration = iBarShift(0,PERIOD_M1, OrderOpenTime());
More things to check:
- you have input variable MagicNumber, but in if conditions you use constant "15". If you change magic number from input parameters form, your code will not work any more
- compiler warns that you are not checking output of OrderClose(). It is a good practice to check if there were errors while executing order handling functions.
iBarShift returns number of bars for the specified period. If you write NULL, it will use current chart period. If you are using for example H1, you will get number of one hour bars that elapsed since order was opened. If you want to use iBarShift and count minute bars, put PERIOD_M1 as second parameter:
More things to check:
- you have input variable MagicNumber, but in if conditions you use constant "15". If you change magic number from input parameters form, your code will not work any more
- compiler warns that you are not checking output of OrderClose(). It is a good practice to check if there were errors while executing order handling functions.
int BUYduration = iBarShift(NULL,PERIOD_M1, OrderOpenTime());
- Check is MagicNumber changed or is it 15 on order?
- 8 bars on H4 is 32 hours. How old are your orders?
- Print iBarShift result.
- Check result of OrderClose()
- Check result of OrderSelect()
- Check logs to see if there is any error information
- Check is MagicNumber changed or is it 15 on order?
- 8 bars on H4 is 32 hours. How old are your orders?
- Print iBarShift result.
- Check result of OrderClose()
- Check result of OrderSelect()
- Check logs to see if there is any error information
Thanks again for taking the time. It is the weekend so no markets to test but I ran it in strategy tester on the IC Markets build and it works fine. Printed out iBarShift and can see it counting up and then closing the orders.... Very odd. So it works in strategy tester but doesn't work on their live market data.
Any thoughts? I guess I can also ask their help desk?
n4btb: Any thoughts? I guess I can also ask their help desk? |
|
This short script will print magic numbers of you open orders:
int start() { for(int i=0; i<OrdersTotal(); i++) { if((OrderSelect(i,SELECT_BY_POS)==true)) { Print( "Order : ", OrderTicket(), ", ", OrderSymbol(), ", MagicNum= ", OrderMagicNumber()); } } return(0); }
Run this and check magic numbers.
- Check is MagicNumber changed or is it 15 on order?
- 8 bars on H4 is 32 hours. How old are your orders?
- Print iBarShift result.
- Check result of OrderClose()
- Check result of OrderSelect()
- Check logs to see if there is any error information
Drazen64..... you were right! Thank you. In the live environment iBraShift needed NULL and PeriodH4. That was a great help. No idea why it worked in test but not on live data but that I guess is why we run things on demo accounts as well as back testing! You have saved me a lot of time and it is a good lesson to learn. I guess it comes down to FTFM as Raptor would probably say.

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
They say the best way to learn is by making mistakes - so I must be learning a lot right now.
I know the coding may not be neat but it is a start for me. I am trying to learn two things.
1) How to only place one order per bar.
2) How to close an order after a set number of bars have passed.
My problem is that no order of any kind get closed.
I am running IC Markets MT version 4 build 646.
(On a back test environment version 4 build 451 the orders do close - which is odd)
Any thoughts on why the orders now won't close?