This forum is in read-only mode now. You may discuss your questions on forums of MQL4.community and MQL5.community
Multiple tradesBack to topics list |
|
FrankC
2005.05.20 23:29
Wonder any of you MT4 experts can help me with this. I've been looking at a very interesting code written for MT3 platform which I fetched from Moneytec forum, and is called AlwaysInPlay. The reason I found the code interesting is because it can open many trades for different currency pairs. But only one trade per currency pair. Here is the bulk of the code:
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. |
|
mpfx
2005.05.24 00:03
I am having exact same problem,
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 |
|
FrankC
2005.05.24 08:18
Here is my MT4 conversion for multiple trades...strangely, I keep getting "Trade Context: Ping failed", "19073 cannot login!" in my journal. The connection seems to be ok, and no problem with the other MT3 platform. Anyway, I've decided to share this code to see if it makes any sense. The trading part is not my concern at the moment, only to see if it can place multiple trades:
//+------------------------------------------------------------------+ //| 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() |
|
FrankC
2005.05.25 07:52
Well, I wrote the code in MT4 for multiple trades, only need to see the trade results later. Meanwhile, may I ask Metaquotes if there is any way of exectuing multiple trades in MQL4 without a complex logic?
|
|
FrankC
2005.05.26 22:59
Here is the code in MT4:
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. |
|
FrankC
2005.05.27 22:11
Slawa, I know you are a very busy man, but could you please help me to figure out what I am doing wrong in this code. All I need to do is to check 6 currency pairs, and initiate a trade if there is a buy/sell, but only on one pair at a time. It is not working! I am getting duplicated trades on the same currency pair, but the for loop should have avoided this situation. Please don't ignore me. Anyone please! TIA.
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) { ... .... |
|
Matias
2005.05.29 10:01
FrankC,
Are you sure your for loop is well coded? ... [b]for(cnt=1;cnt==total;cnt++)[/b] ...
... [b]for(cnt=0;cnt<total;cnt++)[/b] ...
|
|
FrankC
2005.05.29 18:19
Thanks Matias...corrected my errors. Will wait for the demo to kick-in this Sunday to test my EA. Cheers.
|
Download MetaTrader 5 (450 Kb, web installer) — a new terminal for financial markets
