Multiple trades
This is the code that I used in mt3 but I just cannot covert it :(
trd=0;
for cnt=1 to TotalTrades
{
If (Ord(cnt,VAL_SYMBOL)= Symbol) then trd += 1;
}
If trd <1
//+------------------------------------------------------------------+
//| Multipe Trades with SAR |
//| Copyright © 2005, FrankC |
//+------------------------------------------------------------------+
extern double TakeProfit = 50;
extern double Lots = 1;
extern double TrailingStop = 20;
extern double StopLoss = 40;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start()
{
int cnt, ticket, total,ADX, CurrentTrades;
int maxTradesPerPair=1;
double O,C,H,L,O1,C1,H1,L1,O2,C2,H2,L2,sl,SAR;
datetime prevtime=0;
// initial data checks
if(Bars<100)
{
Print("bars less than 100");
return(0);
}
if(TakeProfit<10)
{
Print("TakeProfit less than 10");
return(0); // check TakeProfit
}
//Define the beginning of a new bar
if(prevtime == Time[0]) return(0);
prevtime = Time[0];
SAR = iSAR(NULL,0,0.05,0.5,1);
ADX = iADX(NULL,0,14,PRICE_CLOSE,MODE_MAIN,1);
O=iOpen(NULL,0,0);
C=iClose(NULL,0,0);
H=iHigh(NULL,0,0);
L=iLow(NULL,0,0);
O1=iOpen(NULL,0,1);
C1=iClose(NULL,0,1);
H1=iHigh(NULL,0,1);
L1=iLow(NULL,0,1);
O2=iOpen(NULL,0,2);
C2=iClose(NULL,0,2);
H2=iHigh(NULL,0,2);
L2=iLow(NULL,0,2);
//============ ORDER MANAGEMENT ================================
total=OrdersTotal();
CurrentTrades = 0;
for(cnt=1;cnt==total;cnt++)
{
if (OrderSymbol()==Symbol())
{
CurrentTrades = CurrentTrades + 1;
}
}
for(cnt=1;cnt==total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL && // check for opened position
CurrentTrades != 0 &&
OrderSymbol()==Symbol()) // check for symbol
{
if(OrderType()==OP_BUY) // long position is opened
{
// should it be closed?
if(C < (SAR-10*Point))
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
return(0); // exit
}
// check for trailing stop
if(TrailingStop>0)
{
if(Bid-OrderOpenPrice()>Point*TrailingStop)
{
if(OrderStopLoss()<Bid-Point*TrailingStop)
{
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);
return(0);
}
}
}
}
else // go to short position
{
// should it be closed?
if(C > (SAR+10*Point))
{
OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position
return(0); // exit
}
// check for trailing stop
if(TrailingStop>0)
{
if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
{
if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
{
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);
return(0);
}
}
}
}
}
return(0);
}
//============ ORDER ENTRY =====================================
if(CurrentTrades < maxTradesPerPair)
{
if(AccountFreeMargin()<(1000*Lots))
{
Print("We have no money. Free Margin = ", AccountFreeMargin());
return(0);
}
// check for long position (BUY) possibility
if((MathAbs(L1-SAR)<10*Point) && (ADX > 15))
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,StopLoss*Point,Ask+TakeProfit*Point,"SAR",19073,0,Green);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
}
else Print("Error opening BUY order : ",GetLastError());
return(0);
} // end of if for BUY position
// check for short position (SELL) possibility
if((MathAbs(H1-SAR)<10*Point) && (ADX > 15))
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,StopLoss*Point,Bid-TakeProfit*Point,"SAR",19073,0,Red);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
}
else Print("Error opening SELL order : ",GetLastError());
return(0);
} // end of if for SHORT position
return(0);
} // end of if(CurrentTrades < maxTradesPerPair)
} // end of int start()
int CurrentTrades=0,total,cnt;
total=OrdersTotal();
for(cnt=1;cnt==total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol()==Symbol()) CurrentTrades++;
}
if(CurrentTrades==0)
{
// BUY/SELL order entry routine
...
...
But still getting multiple trades on the same currency pair...I am getting a feeling this is a bug in MT4.
total=OrdersTotal();
tt=0;
for(cnt=1;cnt==total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol()==Symbol()) tt++;
}
if((total < 6) && tt==0)
{
...
....
Are you sure your for loop is well coded?
... [b]for(cnt=1;cnt==total;cnt++)[/b] ...
In that case the loop will only execute if OrdersTotal() == 1.
If you are trying to go through the entire orders pool, you should write something like this.
... [b]for(cnt=0;cnt<total;cnt++)[/b] ...
The Orders pool is zero based, so you should start from zero.
By the way, another way to acomplish your objective "to check 6 currency pairs, and initiate a trade if there is a buy/sell, but only on one pair at a time" is to increment a global variable each time you open a position (short/long) and avoiding entering if the global variable is >= 6.
Hope this help
Matias Romeo
Custom Metatrader Systems
matiasdotromeoatgmail.com
defines: maxTradesPerPair(1);
vars CurrentTrades(0);
....
....
// =======================================================
// OPEN ORDER CHECK -
// Each instance of a script is attached to one currency pair.
// When this check executes, it sets CurrentTrades to 1 so that
// only one trade for this symbol will be open, which is enforced
// by "if CurrentTrades = 0".
// =========================================================
CurrentTrades = 0;
for cnt = 1 to TotalTrades
{
if OrderValue(cnt,VAL_SYMBOL) = Symbol then
{
CurrentTrades = CurrentTrades + 1;
};
};
// =========================================================
// ORDER CLOSURE and TRAILING STOP UPDATE
// ==========================================================
for cnt = 1 to TotalTrades
{
if CurrentTrades <> 0 and
OrderValue(cnt,VAL_SYMBOL) = Symbol then
{
if OrderValue(cnt,VAL_TYPE) = OP_BUY then
{
....
.....
CloseOrder(OrderValue(cnt,VAL_TICKET), OrderValue(cnt,VAL_LOTS),Bid,3,Violet);
Exit;
};
if OrderValue(cnt,VAL_TYPE) = OP_SELL then
{
.....
......
CloseOrder(OrderValue(cnt,VAL_TICKET),OrderValue(cnt,VAL_LOTS),Ask,3,Violet);
Exit;
};
}; // end for cnt=1 to TotalTrades
// =======================================================
// TRADE ENTRY
// =======================================================
if FreeMargin < Margincutoff then Exit;
if CurrentTrades < maxTradesPerPair and
hour > OpenTime and hour < CloseTime then
{
//LONG TRADES ENTRY CRITERIA
if iSAR(0.05,0.5,0) < Ask and // If SAR less than Ask, and
tHour != TimeHour(time[0]) then // then request order.
{
tHour = TimeMinute(time[0]);
...
...
SetOrder(OP_BUY,
lotMM,
Bid,
Slippage,
sl,
tp,
LIME);
Exit;
};
//SHORT TRADES ENTRY CRITERIA
if iSAR(0.05,0.5,0) > Bid and // If SAR greater than Bid, and
tHour != TimeHour(time[0]) then // then request order.
{
tHour = TimeMinute(time[0]);
....
....
SetOrder(OP_SELL,
lotMM,
Ask,
Slippage,
sl,
tp,
RED);
Exit;
};
}; // end of if CurrentTrades < maxTradesPerPair
Question: Converting the above code to MT4 is not a big job, however I wonder do I need to convert the above codes line-by-line to achieve my task, or is there any easier way in MT4 to do the coding? Thanks in advance.