Can someone help me to prevent a trade that has closed from re-opening in the same direction? I have seen code that allows only one trade per bar but I just need it to stop trading in the same direction. This is the code I have but it stops all repeat trades
int ClosedOrdersPerBar() { int orders=0; if(OrdersTotal()>0) <<==Opentrades { for(i=OrdersTotal()-1;i>=0;i--) { OrderSelect(i,SELECT_BY_POS,MODE_HISTORY); // ??? MODE_HISTORY ????
//where is symbol check ??
//where is check ordermagicnumber ??
if(OrderOpenTime()>iTime(NULL,0,0))orders++; //what about the trades opend before last bar
// where is count for ordertype ??
} return(orders);
}
}
prevent reopening same direction that has closed last bar...
if bar opens the trade is still open !!!!
might be buy or sell
if it closed it is not allowed to open a new trade in that direction
set with only first tick of bar
OpenBuy=true;
and OpenSell=true;
then check open trades
if found buy set OpenBuy=false;
if found sell set OpenSell=false;
open new buy only if openbuy is true...
open new sell only if ......
Can someone help me to prevent a trade that has closed from re-opening in the same direction? I have seen code that allows only one trade per bar but I just need it to stop trading in the same direction. This is the code I have but it stops all repeat trades
You could try something like this. . . .
#define BUY_ORDERS 1 #define SELL_ORDERS 2 #define ORDER_INCREMENT 4 // Function definition int ClosedOrdersPerBar() { int orders=0; if(OrdersTotal()>0) { for(int i = OrdersTotal()-1; i>=0; i--) { OrderSelect(i, SELECT_BY_POS, MODE_HISTORY); if(OrderOpenTime() > iTime(NULL,0,0)) orders += ORDER_INCREMENT; if(OrderType() == OP_BUY) orders |= BUY_ORDERS; if(OrderType() == OP_SELL) orders |= SELL_ORDERS; } } return(orders); } // how to use the function int ClosedOrdersCurrentBar = ClosedOrdersPerBar()/ORDER_INCREMENT; if(ClosedOrdersPerBar() & BUY_ORDERS > 0) Print("Order closed during current bar was a Buy"); if(ClosedOrdersPerBar() & SELL_ORDERS > 0) Print("Order closed during current bar was a Sell");
Note: Not compiled or tested . . .
You could try something like this. . . .
Note: Not compiled or tested . . .
MODE_HISTORY - order selected from history pool (closed and canceled order).
You do the same Simon think its wrong because
finding history trades you have to look with a loop in OrdersHistoryTotal ( )
and if you look there then it is not the OrderOpenTime() but OrdercloseTime() you have to compare with openingtime last bar
MODE_HISTORY - order selected from history pool (closed and canceled order).
You do the same Simon think its wrong because
finding history trades you have to look with a loop in OrdersHistoryTotal ( )
and if you look there then it is not the OrderOpenTime() but OrdercloseTime() you have to compare with openingtime last bar
Yep, I just copied the original code and edited it, didn't notice the OrdersTotal() or OOT() . . . so it should be something like this . . .
#define BUY_ORDERS 1 #define SELL_ORDERS 2 #define ORDER_INCREMENT 4 // Function definition int ClosedOrdersPerBar() { int orders=0; if(OrdersHistoryTotal()>0) { for(int i = OrdersHistoryTotal()-1; i>=0; i--) { OrderSelect(i, SELECT_BY_POS, MODE_HISTORY); if(OrderCloseTime() >= iTime(NULL,0,0)) orders += ORDER_INCREMENT; if(OrderType() == OP_BUY) orders |= BUY_ORDERS; if(OrderType() == OP_SELL) orders |= SELL_ORDERS; } } return(orders); } // how to use the function int ClosedOrdersCurrentBar = ClosedOrdersPerBar()/ORDER_INCREMENT; if(ClosedOrdersPerBar() & BUY_ORDERS > 0) Print("Order closed during current bar was a Buy"); if(ClosedOrdersPerBar() & SELL_ORDERS > 0) Print("Order closed during current bar was a Sell");
Yep, I just copied the original code and edited it, didn't notice the OrdersTotal() or OOT() . . . so it should be something like this . . .
Almost still missing a check for Symbol( ) and OrderMagicNumber( )
but my way would be more like
int start() { static datetime Time0; //---- if(Time0 < Time[0]) { OpenBuy=true; OpenSell=true; } //---- if(OrdersTotal()>0) { BUYS=0; SELLS=0; for(int i = OrdersTotal()-1; i >= 0 ; i--) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break; if(OrderMagicNumber()!=MagicNumber || OrderSymbol()!=Symbol()) continue; //---- check order type if(OrderType()==OP_BUY) { BUYS++; OpenBuy=false; } if(OrderType()==OP_SELL) { SELLS++; OpenSell=false; } } } //if(tradecondition sell && OpenSell) Open OP_SELL //or if(tradecondition buy && OpenBuy) Open OP_BUY //---- return(0); }
I think it can be done this way
it can fail if this EA also opens pendingtrades....
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Can someone help me to prevent a trade that has closed from re-opening in the same direction? I have seen code that allows only one trade per bar but I just need it to stop trading in the same direction. This is the code I have but it stops all repeat trades