Forex



Go Back   Forex Trading > Programming > MetaTrader
Forex Forum Register More recent Blogs Calendar Advertising Others Help






Register
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.
See more

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 09-14-2007, 06:43 PM
matrixebiz's Avatar
Senior Member
 
Join Date: Oct 2006
Posts: 1,218
matrixebiz is on a distinguished road
Help to add MaxOpenOrder code please

Hello, just wondering if someone could add for me code to this EA for MaxOpenOrders so I can limit the amount of max trades EA is allowed to open.

Thank you

Code:
//+------------------------------------------------------------------+
//|                                    MA envelope exhausting system |
//+------------------------------------------------------------------+
//----------------------- USER INPUT
extern double MA_length = 10;	 
extern double slip = 0;
extern double Lots = 1;
extern double TakeProfit = 30;
extern double Stoploss = 2000;
extern double PipStep = 30;
   //----------------------- SETUP VARS
double PriceTarget;
double AveragePrice;
double LastPrice;
int flag;
//----------------------- MAIN PROGRAM LOOP
int start()
{
   int cnt=0, total;
   double Stopper=0;  
   total=OrdersTotal();
   OrderSelect(total, SELECT_BY_POS, MODE_TRADES);
   LastPrice=OrderOpenPrice();
   OrderSelect(total, SELECT_BY_POS, MODE_TRADES); 
   flag=0;
   if(iMA(NULL,0,MA_length,0,MODE_SMA,PRICE_OPEN,0)*1.02<Bid && (Bid>=(LastPrice+(PipStep*Point)))||(total<1)) // Go SHORT -> Only sell if >= 30 pips above previous position entry	 
     {
      OrderSend(Symbol(),OP_SELL,Lots,Bid,slip,Bid+(Stoploss*Point),Bid-(TakeProfit*Point),0,0,HotPink);
      if(total>0)flag=1;
     }   
   if(iMA(NULL,0,MA_length,0,MODE_SMA,PRICE_OPEN,0)*0.98>Ask && (Ask<=(LastPrice-(PipStep*Point)))||total<1) // Go LONG -> Only buy if >= 30 pips below previous position entry	 
     {
      OrderSend(Symbol(),OP_BUY,Lots,Ask,slip,Ask-(Stoploss*Point),Ask+(TakeProfit*Point),0,0,Lime);
      if(total>0)flag=1;
     }
//----------------------- CALCULATE AVERAGE OPENING PRICE
   total=OrdersTotal();
   AveragePrice=0;
   for(cnt=0;cnt<total;cnt++)
      {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      AveragePrice=AveragePrice+OrderOpenPrice();
      }
   if (OrdersTotal() > 0)
   AveragePrice=AveragePrice/total;
//----------------------- RECALCULATE STOPLOSS & PROFIT TARGET BASED ON AVERAGE OPENING PRICE
  OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
  if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())  
    {
    flag=1;
    if(OrderType()==OP_BUY) // Calculate profit/stop target for long 
      {
      PriceTarget=AveragePrice+(TakeProfit*Point);
      Stopper=AveragePrice-(Stoploss*Point); 
      }
     else // Calculate profit/stop target for short
      {
      PriceTarget=AveragePrice-(TakeProfit*Point);
      Stopper=AveragePrice+(Stoploss*Point);   
      }
    }
//----------------------- IF NEEDED CHANGE ALL OPEN ORDERS TO NEWLY CALCULATED PROFIT TARGET    
  if(flag==1)// check if average has really changed
    {   
    for(cnt=0;cnt<total;cnt++)
       {
       PriceTarget=total;
       OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);            
       OrderModify(OrderTicket(),0,Stopper,PriceTarget,0,Yellow);// set all positions to averaged levels
       }
    }
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #2 (permalink)  
Old 09-14-2007, 11:33 PM
Senior Member
 
Join Date: Feb 2006
Posts: 587
Michel is on a distinguished road
Quote:
Originally Posted by matrixebiz View Post
Hello, just wondering if someone could add for me code to this EA for MaxOpenOrders so I can limit the amount of max trades EA is allowed to open.

Thank you
There seems to be several mistakes is this EA, here is a cleaner code (not tested)
PHP Code:
//+------------------------------------------------------------------+
//|                                    MA envelope exhausting system |
//+------------------------------------------------------------------+
//----------------------- USER INPUT
extern double MA_length 10;     
extern double slip 0;
extern double Lots 1;
extern double TakeProfit 30;
extern double Stoploss 2000;
extern double PipStep 30
extern int MaxOpenOrders 
5;

//----------------------- MAIN PROGRAM LOOP
int start()
{
   
int total=0cntdir=0ticket=0;
   
double Stopper=0LastPrice=0AveragePrice=0PriceTarget=0;  
   
double SMA=iMA(NULL,0,MA_length,0,MODE_SMA,PRICE_OPEN,0);

   for(
cnt=OrdersTotal()-1;cnt>=0;cnt--)
   {
      
OrderSelect(cntSELECT_BY_POSMODE_TRADES);
      if(
OrderSymbol()!=Symbol()) continue;
      
dir=1-OrderType()*2;
      
total++;
      if(
OrderTicket()>ticket
      {
         
ticket=OrderTicket();
         
LastPrice=OrderOpenPrice();
      }
   }
   if(
total>=MaxOpenOrders) return(0);
   
ticket=0;

   if(
SMA*1.02<Bid && ((dir==-&& Bid>=LastPrice+PipStep*Point)||total<1)) // Go SHORT -> Only sell if >= 30 pips above previous position entry     
      
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,slip,Bid+(Stoploss*Point),Bid-(TakeProfit*Point),0,0,HotPink); 

   if(
SMA*0.98>Ask && ((dir==&& Ask<=LastPrice-PipStep*Point)||total<1)) // Go LONG -> Only buy if >= 30 pips below previous position entry     
      
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,slip,Ask-(Stoploss*Point),Ask+(TakeProfit*Point),0,0,Lime);

   if(
ticket==0) return(0);
   if(
total<1) return(0);


//----------------------- CALCULATE AVERAGE OPENING PRICE
   
total=0;
   for(
cnt=0;cnt<OrdersTotal();cnt++)
   {
      
OrderSelect(cntSELECT_BY_POSMODE_TRADES);
      if(
OrderSymbol()!=Symbol()) continue;
      
total++;
      
AveragePrice+=OrderOpenPrice();
   }
   
AveragePrice/=total;

//----------------------- RECALCULATE STOPLOSS & PROFIT TARGET BASED ON AVERAGE OPENING PRICE
   
PriceTarget=AveragePrice+TakeProfit*Point*dir;
   
Stopper=AveragePrice-Stoploss*Point*dir

//----------------------- IF NEEDED CHANGE ALL OPEN ORDERS TO NEWLY CALCULATED PROFIT TARGET       
   
for(cnt=0;cnt<OrderTotal;cnt++)
   {
      
OrderSelect(cntSELECT_BY_POSMODE_TRADES);
      if(
OrderSymbol()!=Symbol()) continue;            
      
OrderModify(OrderTicket(),0,Stopper,PriceTarget,0,Yellow);// set all positions to averaged levels
    
}

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #3 (permalink)  
Old 09-14-2007, 11:53 PM
matrixebiz's Avatar
Senior Member
 
Join Date: Oct 2006
Posts: 1,218
matrixebiz is on a distinguished road
Quote:
Originally Posted by Michel View Post
There seems to be several mistakes is this EA, here is a cleaner code (not tested)
PHP Code:
//+------------------------------------------------------------------+
//|                                    MA envelope exhausting system |
//+------------------------------------------------------------------+
//----------------------- USER INPUT
extern double MA_length 10;     
extern double slip 0;
extern double Lots 1;
extern double TakeProfit 30;
extern double Stoploss 2000;
extern double PipStep 30
extern int MaxOpenOrders 
5;

//----------------------- MAIN PROGRAM LOOP
int start()
{
   
int total=0cntdir=0ticket=0;
   
double Stopper=0LastPrice=0AveragePrice=0PriceTarget=0;  
   
double SMA=iMA(NULL,0,MA_length,0,MODE_SMA,PRICE_OPEN,0);

   for(
cnt=OrdersTotal()-1;cnt>=0;cnt--)
   {
      
OrderSelect(cntSELECT_BY_POSMODE_TRADES);
      if(
OrderSymbol()!=Symbol()) continue;
      
dir=1-OrderType()*2;
      
total++;
      if(
OrderTicket()>ticket
      {
         
ticket=OrderTicket();
         
LastPrice=OrderOpenPrice();
      }
   }
   if(
total>=MaxOpenOrders) return(0);
   
ticket=0;

   if(
SMA*1.02<Bid && ((dir==-&& Bid>=LastPrice+PipStep*Point)||total<1)) // Go SHORT -> Only sell if >= 30 pips above previous position entry     
      
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,slip,Bid+(Stoploss*Point),Bid-(TakeProfit*Point),0,0,HotPink); 

   if(
SMA*0.98>Ask && ((dir==&& Ask<=LastPrice-PipStep*Point)||total<1)) // Go LONG -> Only buy if >= 30 pips below previous position entry     
      
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,slip,Ask-(Stoploss*Point),Ask+(TakeProfit*Point),0,0,Lime);

   if(
ticket==0) return(0);
   if(
total<1) return(0);


//----------------------- CALCULATE AVERAGE OPENING PRICE
   
total=0;
   for(
cnt=0;cnt<OrdersTotal();cnt++)
   {
      
OrderSelect(cntSELECT_BY_POSMODE_TRADES);
      if(
OrderSymbol()!=Symbol()) continue;
      
total++;
      
AveragePrice+=OrderOpenPrice();
   }
   
AveragePrice/=total;

//----------------------- RECALCULATE STOPLOSS & PROFIT TARGET BASED ON AVERAGE OPENING PRICE
   
PriceTarget=AveragePrice+TakeProfit*Point*dir;
   
Stopper=AveragePrice-Stoploss*Point*dir

//----------------------- IF NEEDED CHANGE ALL OPEN ORDERS TO NEWLY CALCULATED PROFIT TARGET       
   
for(cnt=0;cnt<OrderTotal;cnt++)
   {
      
OrderSelect(cntSELECT_BY_POSMODE_TRADES);
      if(
OrderSymbol()!=Symbol()) continue;            
      
OrderModify(OrderTicket(),0,Stopper,PriceTarget,0,Yellow);// set all positions to averaged levels
    
}

Thank you when I compile I get an error. Had multiple errors before I added the ; on the pipstep line then now the only error I get is;
'OrderTotal' - variable not defined C:\Documents and Settings\!.mq4 (61, 18)

also can I add an 'extern double Percent = 0.3;' line
then change the trade code to look like;
Code:
   if(SMA*(1+Percent/100))>Ask && ((dir==1 && Ask<=LastPrice-PipStep*Point)||total<1)) // Go LONG -> Only buy if >= 30 pips below previous position entry
instead of;
Code:
   if(SMA*0.98>Ask && ((dir==1 && Ask<=LastPrice-PipStep*Point)||total<1)) // Go LONG -> Only buy if >= 30 pips below previous position entry
Thanks and WOW you pretty much changed the whole EA

also, is the StopLoss setting for all open orders added together or for each individual order? If EA is set to close all orders when total SL is hit, can you change it so that SL is for each separate order.

Thanks again

Last edited by matrixebiz; 09-15-2007 at 01:55 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #4 (permalink)  
Old 09-15-2007, 03:56 AM
Senior Member
 
Join Date: Dec 2006
Location: Ukraine
Posts: 513
Shinigami is on a distinguished road
OrderTotal
Actually is
OrdersTotal
__________________
MQL4 programming is easy ^^
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #5 (permalink)  
Old 09-15-2007, 05:23 AM
matrixebiz's Avatar
Senior Member
 
Join Date: Oct 2006
Posts: 1,218
matrixebiz is on a distinguished road
Quote:
Originally Posted by Shinigami View Post
OrderTotal
Actually is
OrdersTotal
That was it plus it was missing the ()

Fixed - OrdersTotal()

Thanks
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #6 (permalink)  
Old 09-16-2007, 10:09 PM
Senior Member
 
Join Date: Feb 2006
Posts: 587
Michel is on a distinguished road
Quote:
Originally Posted by matrixebiz View Post
That was it plus it was missing the ()

Fixed - OrdersTotal()

Thanks
Sorry for the two mistakes, I am in hollyday and cannot check the code from here.
I made two modifications :
1) the EA use now only the positions of the pair on which he is attached.
2) the EA keep the direction of the first position (of that pair). This is needed is some TP / SL / entry configuration where the EA may open buys and sells simultaneously; then the average price, new TP and SL have no meanings anymore. It is possible to write an EA working in both direction at the same time but it's much more complex. The simplest way to do that is to work with two EAs with different MagicNumbers and only long / only short. If you need it, tell me if you want any help.
The other modifs are only the right way to achieve the same goal. For example, one cannot assume that the last order is indexed by OrderTotal(): first it should be OrderTotal()-1, but this may also be wrong because it depends of the sorted collumn of the "trade" tab of the terminal or if you have opened positions on other pairs.
I tryed to keep most of your code, and I keep also most of it's lack, for example, don't play with manual pendings on the same pair: all will be wrong...
It will be possible to improve the security if you find that this basic EA is profitable.

"also, is the StopLoss setting for all open orders added together or for each individual order? If EA is set to close all orders when total SL is hit, can you change it so that SL is for each separate order."
Now the SL is based on the average price (as I understood your code). If you do not want to have a general SL, just don't modify them. Then the "modify" line should only modify the TP :
PHP Code:
OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),PriceTarget,0,Yellow); 
BTW, I am not sure that "0" as OpenPrice() is correct in the curent line.

Last edited by Michel; 09-16-2007 at 10:13 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #7 (permalink)  
Old 09-16-2007, 10:20 PM
matrixebiz's Avatar
Senior Member
 
Join Date: Oct 2006
Posts: 1,218
matrixebiz is on a distinguished road
Quote:
Originally Posted by Michel View Post
Sorry for the two mistakes, I am in hollyday and cannot check the code from here.
I made two modifications :
1) the EA use now only the positions of the pair on which he is attached.
2) the EA keep the direction of the first position (of that pair). This is needed is some TP / SL / entry configuration where the EA may open buys and sells simultaneously; then the average price, new TP and SL have no meanings anymore. It is possible to write an EA working in both direction at the same time but it's much more complex. The simplest way to do that is to work with two EAs with different MagicNumbers and only long / only short. If you need it, tell me if you want any help.
The other modifs are only the right way to achieve the same goal. For example, one cannot assume that the last order is indexed by OrderTotal(): first it should be OrderTotal()-1, but this may also be wrong because it depends of the sorted collumn of the "trade" tab of the terminal or if you have opened positions on other pairs.
I tryed to keep most of your code, and I keep also most of it's lack, for example, don't play with manual pendings on the same pair: all will be wrong...
It will be possible to improve the security if you find that this basic EA is profitable.

"also, is the StopLoss setting for all open orders added together or for each individual order? If EA is set to close all orders when total SL is hit, can you change it so that SL is for each separate order."
Now the SL is based on the average price (as I understood your code). If you do not want to have a general SL, just don't modify them. Then the "modify" line should only modify the TP :
PHP Code:
OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),PriceTarget,0,Yellow); 
BTW, I am not sure that "0" as OpenPrice() is correct in the curent line.
Ok, not sure what you said about the SL ? is it for each order?
so are you also saying that the EA cannot place a buy and sell with the same currency at the same time untill one of them is closed? can that be fixed? also, how do you add a little bullet (graphic) on the chart that shows when the order was placed?
Thanks
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #8 (permalink)  
Old 09-16-2007, 11:12 PM
Senior Member
 
Join Date: Feb 2006
Posts: 587
Michel is on a distinguished road
Quote:
Originally Posted by matrixebiz View Post
Ok, not sure what you said about the SL ? is it for each order?
so are you also saying that the EA cannot place a buy and sell with the same currency at the same time untill one of them is closed? can that be fixed? also, how do you add a little bullet (graphic) on the chart that shows when the order was placed?
Thanks
About the SL, both ideas may work:
if you want a general SL, ie the same level for all positions so they are all closing at the same time, use this line (This was the way your original code was intended to work):
PHP Code:
OrderModify(OrderTicket(),OrderOpenPrice(),Stopper,PriceTarget,0,Yellow); 
But if you want that each order keeps it's SL from it's openning, use this line :
PHP Code:
OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),PriceTarget,0,Yellow); 
In this second case, each order may hit its own SL separetaly; but keep in mind that the EA should then be more complex to work properly: if a position is closed by SL, the average price becomes wrong, so you have to recompute it at each tick and check if the other positions need to be modified.

About to buy and sell at the same time, you must understand that the average price, the SL and TP as they were defined in your code have no meaning if the directions are mixed. Just look at this example:
situation 1: you have a buy at 1.200 and a sell at 1.220;
situation 2: a buy at 1.200 and a buy at 1.220;
Your original code makes no distinction between both situations, but they are really different...
That's why I suggest you to add a MagicNumber to the EA and to work on one chart with the EA to open only the buys and on an other chart with the same EA but with another MagicNumber to open only the sells.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #9 (permalink)  
Old 09-17-2007, 12:18 AM
matrixebiz's Avatar
Senior Member
 
Join Date: Oct 2006
Posts: 1,218
matrixebiz is on a distinguished road
Quote:
Originally Posted by Michel View Post
About the SL, both ideas may work:
if you want a general SL, ie the same level for all positions so they are all closing at the same time, use this line (This was the way your original code was intended to work):
PHP Code:
OrderModify(OrderTicket(),OrderOpenPrice(),Stopper,PriceTarget,0,Yellow); 
But if you want that each order keeps it's SL from it's openning, use this line :
PHP Code:
OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),PriceTarget,0,Yellow); 
In this second case, each order may hit its own SL separetaly; but keep in mind that the EA should then be more complex to work properly: if a position is closed by SL, the average price becomes wrong, so you have to recompute it at each tick and check if the other positions need to be modified.

About to buy and sell at the same time, you must understand that the average price, the SL and TP as they were defined in your code have no meaning if the directions are mixed. Just look at this example:
situation 1: you have a buy at 1.200 and a sell at 1.220;
situation 2: a buy at 1.200 and a buy at 1.220;
Your original code makes no distinction between both situations, but they are really different...
That's why I suggest you to add a MagicNumber to the EA and to work on one chart with the EA to open only the buys and on an other chart with the same EA but with another MagicNumber to open only the sells.
Ok, I don't mind then to only have one order per currency open at one time. So in effect there was no need to add MaxOpenOrders correct? since the EA will only open one order at a time. or will it open two or more orders but only in one direction (sells or buys) until all uni-positions are closed?
If you have time can you fix it so that it will recompute when I change the SL code as you mentioned plus work with multiple direction trades without using a magic number.

Thanks alot

Last edited by matrixebiz; 09-17-2007 at 01:51 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #10 (permalink)  
Old 09-17-2007, 07:46 PM
Senior Member
 
Join Date: Feb 2006
Posts: 587
Michel is on a distinguished road
Quote:
...or will it open two or more orders but only in one direction (sells or buys) until all uni-positions are closed?
Yes, that's true. You can easily check it !
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to code? samahdi MetaTrader 2185 Yesterday 10:29 AM
What does this code mean? matrixebiz Expert Advisors - Metatrader 4 3 09-01-2007 11:38 AM
How to code this? iscuba11 Metatrader 4 mql 4 - Development course 1 08-03-2007 05:22 PM


All times are GMT. The time now is 07:29 AM.



Search Engine Friendly URLs by vBSEO 3.2.0 ©2008, Crawlability, Inc.