Welcome to Forex-TSD!, one of the largest Forex forums worldwide, where you will be able to find the most complete and reliable Forex information imaginable.
From the list below, select the forum that you want to visit and register to post, as many times you want. It’s absolutely free. Click here for registering on Forex-TSD.
Exclusive Forum
The Exclusive Forum is the only paid section. Once you subscribe, you will get free access to real cutting-edge Trading Systems (automated and not), Indicators, Signals, Articles, etc., that will help and guide you, in ways that you could only imagine, with your Forex trading.
Elite Section
Get access to private discussions, specialized support, indicators and trading systems reported every week.
Advanced Elite Section
For professional traders, trading system developers and any other member who may need to use and/or convert, the most cutting-edge exclusive indicators and trading systems for MT4 and MT5.
Hi, i'm new to MT4.. Could someone please let me know how i can place a restriction in an EA that requires a minimum of "x" bars to either open a new position or close a previous one?
I just started with my new EA and opening positions is OK, I setup tp and sl. EA open and close 1 by 1 position and never there are more than 1 order. But I would like to close position if some of indicators alert me before sl or tp limit.
If I try something like this:
Code:
for (int i=1; i<=OrdersTotal(); i++) {
if (OrderSelect(i, SELECT_BY_POS, 2)) {
if (OrderSymbol()==Symbol()) {
Ans=OrderClose(OrderTicket(),alLots,Bid,2);// Order closing
}
}
}
Nothing happened. Doesn't find any opened positions and I know there is 1.
If I try something like this:
Code:
for (int i=1; i<=OrdersTotal(); i++) {
CloseOrder(OrderValue(i,VAL_TICKET),OrderValue(i,VAL_LOTS),Bid,3);
}
Compiler report error message that VAL_TICKER and VAL_LOTS are not defined and in examples I found they are nowhere declared.
If I try direcly by number to close:
Code:
Ans=OrderClose(0,alLots,Bid,2);// Order closing
I get error message 4107 or 4105 that ticker is invalid
Can somebody check this and my ea in attachment and help please?
int iLowest( string symbol, int timeframe, int type, int count=WHOLE_ARRAY, int start=0)
Returns the shift of the least value over a specific number of periods depending on type.
Parameters:
symbol - Symbol the data of which should be used to calculate indicator. NULL means the current symbol.
timeframe - Timeframe. It can be any of Timeframe enumeration values. 0 means the current chart timeframe.
type - Series array identifier. It can be any of Series array identifier enumeration values.
count - Number of periods (in direction from the start bar to the back one) on which the calculation is carried out.
start - Shift showing the bar, relative to the current bar, that the data should be taken from.
Sample:
// calculating the lowest value on the 10 consequtive bars in the range
// from the 10th to the 19th index inclusive on the current chart
double val=Low[iLowest(NULL,0,MODE_LOW,10,10)];
for (int i=OrdersTotal()-1;i>=0 i--) {
if (OrderSelect(i, SELECT_BY_POS)) {
if (OrderSymbol()==Symbol()&&OrderType()==0) {
Ans=OrderClose(OrderTicket(),alLots,Bid,2);// Order closing
}
if (OrderSymbol()==Symbol()&&OrderType()==1) {
Ans=OrderClose(OrderTicket(),alLots,Ask,2);// Order closing
}
}
Like that I found alTicker (after opening position) which I close later on.
But your code I will use to optimize my Closing function. Sorry for maybe "beginner's code", I am into mq4 just 2 days ... will improve - I promise
Thanks again
Quote:
Originally Posted by Roger09
Try this way
Code:
for (int i=OrdersTotal()-1;i>=0 i--) {
if (OrderSelect(i, SELECT_BY_POS)) {
if (OrderSymbol()==Symbol()&&OrderType()==0) {
Ans=OrderClose(OrderTicket(),alLots,Bid,2);// Order closing
}
if (OrderSymbol()==Symbol()&&OrderType()==1) {
Ans=OrderClose(OrderTicket(),alLots,Ask,2);// Order closing
}
}