SoulShatter:
Hi all, I need help with a code.
I need to trigger at every end of bar (currently testing on 15 bars) a code that closes my trades.
So far my code works, but only sometimes. Can´t figure out why.
Can anybody help please?
Thank you
such code can simply be written.
datetime prevtime; bool NewBar() { static datetime lastbar; datetime curbar = Time[0]; if(lastbar!=curbar) { lastbar=curbar; return (true); } else { return(false); } } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ int CloseBarControls() { if(iTime(Symbol(),15,0) == prevtime) return(0); prevtime = iTime(Symbol(),15,0); if(NewBar()) CloseAllTradesThisPair(); return(0); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void OnTick() { CloseBarControls(); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ int CloseAllTradesThisPair() { int total = OrdersTotal(); for(int i=total-1; i>=0; i--) { bool tsd=OrderSelect(i, SELECT_BY_POS); int type = OrderType(); bool result = false; { switch(type) { //Close opened long positions case OP_BUY : result = OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, CLR_NONE); break; //Close opened short positions case OP_SELL : result = OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, CLR_NONE); break; //Close pending orders case OP_BUYLIMIT : case OP_BUYSTOP : case OP_SELLLIMIT : case OP_SELLSTOP : result = OrderDelete(OrderTicket()); } } if(result == false) { if(GetLastError()>0) Alert("Order ", OrderTicket(), " failed to close. Error:", GetLastError()); Sleep(3000); } } return(0); } //+------------------------------------------------------------------+
-
if(time == BarCloseTime){
This will be true only if you get a tick during that exact second. There can be minutes between ticks during the Asian session, think M1 chart.
-
datetime BarCloseTime = Time[0]+(60*15)-1; datetime time = TimeCurrent();
Assuming that you are running on the M15 chart. They will never be equal.
@Mehmet Bastem - Much appreciate it - works like a charm - does not produce the results I was expecting, but works none the less
@William Roeder - Thank you for confirming that was my suspicion
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Hi all, I need help with a code.
I need to trigger at every end of bar (currently testing on 15 bars) a code that closes my trades.
So far my code works, but only sometimes. Can´t figure out why.
Can anybody help please?
Thank you