Download MetaTrader 5

Simple code for Max Orders

To add comments, please log in or register
Dave
21
Dave  

Hi all, I'm a newbie at mql4, but I'm having a go at putting in a Max Orders function to my EA, can someone help me how to do this?

The code is somthing like

extern int MaxOrders = 12;

Thanks,

Dave.

Dan
220
Dan  
Swatunit4:

Hi all, I'm a newbie at mql4, but I'm having a go at putting in a Max Orders function to my EA, can someone help me how to do this?

The code is somthing like

extern int MaxOrders = 12;

Thanks,

Dave.


Do you have any code so far other then then a variable? If you do post it here using:

What kind of MaxOrders function are you looking for:

1.The entire acct including orders placed by other EA's and manual trades?

2. Just one EA, open orders only?

3. One EA, open and pending orders?

Dave
21
Dave  

I'm trying to make this EA buy and sell at pivot points ONCE only per day, but instead it buys every time the price hits the pivot point.

Also, it doesnt open any short trades. Why is this?

Thanks.

(I had to leave out most of the code, its too long)

#include <WinUser32.mqh>
#include <stderror.mqh>
#include <stdlib.mqh>

extern double LotSize = 1;
extern double TakeProfit = 200;
extern double StopLoss = 750;
extern double DistanceFromSupport = 1;  
extern double DistanceFromResistance = 1;  
extern bool UsePivotPointTakeProfit = true;
extern string BuyComment = "BUY";
extern string SellComment = "SELL";
 
   extern bool Previous          = True;
   extern bool Previous_SR_Levels   = True;
   extern bool Use_Sunday_Data   = True;
   extern bool Daily             = True;
   extern bool Daily_SR_Levels   = True;
   extern bool Weekly            = True;
   extern bool Weekly_SR_Levels  = True;
   extern bool Monthly           = True;
   extern bool Monthly_SR_Levels = True;

   double YesterdayHigh;
   double YesterdayLow;
   double YesterdayClose;
   double Day_Price[][6];
   double Pivot,S1,S2,S3,R1,R2,R3;
   double DM1,DM2,DM3,DM4,DM5,DM6;
    
   double WeekHigh;
   double WeekLow;
   double WeekClose;
   double Weekly_Price[][6];
   double WeekPivot,WS1,WS2,WS3,WR1,WR2,WR3;
   double WM1,WM2,WM3,WM4,WM5,WM6;
   
   double MonthHigh;
   double MonthLow;
   double MonthClose;
   double Month_Price[][6];
   double MonthPivot,MS1,MS2,MS3,MR1,MR2,MR3;
   double MM1,MM2,MM3,MM4,MM5,MM6;

   double PreviousHigh;
   double PreviousLow;
   double PreviousClose;
   double Previous_Price[][6];
   double PreviousPivot,PS1,PS2,PS3,PR1,PR2,PR3;
   double PM1,PM2,PM3,PM4,PM5,PM6;
int init()
  {
   return(0);
  }

ArrayCopyRates(Day_Price,(Symbol()), 1440);

   
  
   YesterdayHigh  = Day_Price[1][3];
   YesterdayLow   = Day_Price[1][2];
   YesterdayClose = Day_Price[1][4];
   
   Pivot = ((YesterdayHigh + YesterdayLow + YesterdayClose)/3);

   R1 = (2*Pivot)-YesterdayLow;
   S1 = (2*Pivot)-YesterdayHigh;

   R2 = Pivot+(R1-S1);
   S2 = Pivot-(R1-S1);
   
   R3 = (YesterdayHigh + (2*(Pivot-YesterdayLow)));
   S3 = (YesterdayLow - (2*(YesterdayHigh-Pivot))); 
   
   DM1 = S3 + ((S2-S3) / 2);
   DM2 = S2 + ((S1-S2) / 2);
   DM3 = S1 + ((Pivot-S1) / 2);
   DM4 = Pivot + ((R1-Pivot) / 2);
   DM5 = R1 + ((R2-R1) / 2);
   DM6 = R2 + ((R3-R2) / 2); 

int ticket_buy, ticket_sell;
double open_price;
datetime ctm_buy, ctm_sell;

   double curr_open = iOpen(NULL, 0, 0);
   double last_high = iHigh(NULL, 0, 1);
   double last_low = iLow(NULL, 0, 1);
   double last_close = iClose(NULL, 0, 1);
   
   
double buy_stop = DM4;
double buy_stoploss = S1;

double buy_takeprofit = NormalizeDouble(buy_stop+TakeProfit*Point,Digits);

double sell_stop = DM3;
double sell_stoploss = R1;

double sell_takeprofit = NormalizeDouble(sell_stop-TakeProfit*Point,Digits);

if (UsePivotPointTakeProfit == true) {
   buy_takeprofit = R1;
   sell_takeprofit = S2;
}


MathSrand(TimeLocal());
int rand_magic = MathRand();
   int check_buy = get_pending_order(OP_BUYLIMIT, BuyComment);
   if (check_buy != 0) {
      ticket_buy = check_buy;
   }
   int check_sell = get_pending_order(OP_SELLLIMIT, SellComment);
   if (check_sell != 0) {
      ticket_sell = check_sell;
   }   
   
   if(OrderSelect(ticket_buy,SELECT_BY_TICKET,MODE_TRADES)) {
      open_price = NormalizeDouble(OrderOpenPrice(),Digits);
      
      if (!CompareDoubles(buy_stop, open_price)) { 
        if (!OrderModify(ticket_buy,buy_stop,buy_stoploss,buy_takeprofit,0,CLR_NONE)) {
            Alert("Last error: ",ErrorDescription(GetLastError()));
        }
      }
      ctm_buy=OrderCloseTime();
      if(ctm_buy>0) { 
        ticket_buy = 0;
      }
   } else {
      ticket_buy=OrderSend(Symbol(),OP_BUYLIMIT,LotSize,buy_stop,3,buy_stoploss,buy_takeprofit,BuyComment,rand_magic,0,CLR_NONE);
      if(ticket_buy<0)
         {
            Comment("OrderSend BUYLIMIT failed with error #",ErrorDescription(GetLastError()));
            return(0);
         }
   } 

   if(OrderSelect(ticket_sell,SELECT_BY_TICKET,MODE_TRADES)) {
      open_price = NormalizeDouble(OrderOpenPrice(),Digits);
      
      if (!CompareDoubles(sell_stop, open_price)) { 
        if (!OrderModify(ticket_sell,sell_stop,sell_stoploss,sell_takeprofit,0,CLR_NONE)) {
           Alert("Last error: ",ErrorDescription(GetLastError()));
        }
      }
      ctm_sell=OrderCloseTime();
      if(ctm_sell>0) { 
        ticket_sell = 0;
      }
   } else {
     ticket_sell=OrderSend(Symbol(),OP_SELLLIMIT,LotSize,sell_stop,3,sell_stoploss,sell_takeprofit,SellComment,rand_magic,0,CLR_NONE);
     if(ticket_sell<0)
      {
       Comment("OrderSend SELLIMIT failed with error #",ErrorDescription(GetLastError()));
       return(0);
      }
   } 

   return(0);
}

int get_pending_order(int order_type, string order_comment) {
   int    cmd,total,order_ticket;

   total=OrdersTotal();

   for(int i=0; i<total; i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         cmd=OrderType();
         
         if(cmd==order_type && OrderComment() == order_comment)
           {
            
            OrderPrint();
                                    
            order_ticket = OrderTicket();
            break;
           }
        } else {
           Comment( "Error when order select ", ErrorDescription(GetLastError())); break;
        }
     }
     
     return(order_ticket);
}
Dave
21
Dave  
danjp:


Do you have any code so far other then then a variable? If you do post it here using:

What kind of MaxOrders function are you looking for:

1.The entire acct including orders placed by other EA's and manual trades?

2. Just one EA, open orders only?

3. One EA, open and pending orders?


I would like the EA to trade only once per times it hits the pivot points
Simon Gniadkowski
Moderator
18037
Simon Gniadkowski  
Swatunit4:

I'm trying to make this EA buy and sell at pivot points ONCE only per day, but instead it buys every time the price hits the pivot point.

Also, it doesnt open any short trades. Why is this?

Add some Print statements at the relevant points so you can see what is going on with your variables and sequence.
whroeder1
16112
whroeder1  
How should we answer ANY of your questions when you post code that won't even compile?
int init()
  {
   return(0);
  }

ArrayCopyRates(... 'Day_Price' - expression on global scope not allowed
Dan
220
Dan  
Swatunit4:

I would like the EA to trade only once per times it hits the pivot points

If you can't fit all your code in a message you can always attach the EA here if you are ok with doing that. I'm willing to help you but I can't make any sense out of what you have posted here.
Dave
21
Dave  
danjp:

If you can't fit all your code in a message you can always attach the EA here if you are ok with doing that. I'm willing to help you but I can't make any sense out of what you have posted here.

Ok thanks Dan, I've attached it as a text file, so if you can complie it for me and run it on 1 micro lot, you will see it places many many long trades, but no short trades. I would like the EA to go long at DM4 pivot and take profit at R1, and short at DM3 pivot and take profit at S1 only ONCE per day. But as you can see it places a trade every time price hits the pivot. This EA looks promising on backtest so far, so if you can help me it would be greatly appreciated :)
Files:
pivoteea.txt 29 kb
Dan
220
Dan  
Swatunit4:

Ok thanks Dan, I've attached it as a text file, so if you can complie it for me and run it on 1 micro lot, you will see it places many many long trades, but no short trades. I would like the EA to go long at DM4 pivot and take profit at R1, and short at DM3 pivot and take profit at S1 only ONCE per day. But as you can see it places a trade every time price hits the pivot. This EA looks promising on backtest so far, so if you can help me it would be greatly appreciated :)


Dave, it's a little more work then a max orders function! Here is where I am at.

1) I have long and shorts working.

2) Right now I allow it to do 1 long and 1 short pending order to be placed per day at D4 and D3, as long as there is not a previous position from the following day. If they are still pending at EOD do you want them deleted?

3) I don't work with PP much but I think you want to place a long pending order only if D4 is > then the ask and only place a short if D3 is > Bid ? You want to hit the long PP(D4) only if the price is below D4 to start, opposite for a short. If not let me know in more detail what you are thinking there.

4) What are you trying to do with OrderModify() in your code, ECN broker, move stop? I'm just ignoring that for now.I have broken up most of your code into functions. Your Objects were being deleted and recreated on every tick. I moved them into two functions DeleteObjects() and CreateObjects() so they are only created called once a day for now. I'll let you play with that when I post the new code.

5) MagicNumber ? You were creating a random MagicNumber on every tick, I put in a extern int MagicNumber as a workaround for now. You don't want a different magic number for every order.

6) I am going to get you something back that will work for backtesting and you can tweak it however you like. It will need much more work, in risk management, error checking, recovery etc.

7) Think of anything else you missed or have misunderstood and post it. I'll try and post it back to you by Wed night.

Dan

Dave
21
Dave  
danjp:


Dave, it's a little more work then a max orders function! Here is where I am at.

1) I have long and shorts working.

2) Right now I allow it to do 1 long and 1 short pending order to be placed per day at D4 and D3, as long as there is not a previous position from the following day. If they are still pending at EOD do you want them deleted?

3) I don't work with PP much but I think you want to place a long pending order only if D4 is > then the ask and only place a short if D3 is > Bid ? You want to hit the long PP(D4) only if the price is below D4 to start, opposite for a short. If not let me know in more detail what you are thinking there.

4) What are you trying to do with OrderModify() in your code, ECN broker, move stop? I'm just ignoring that for now.I have broken up most of your code into functions. Your Objects were being deleted and recreated on every tick. I moved them into two functions DeleteObjects() and CreateObjects() so they are only created called once a day for now. I'll let you play with that when I post the new code.

5) MagicNumber ? You were creating a random MagicNumber on every tick, I put in a extern int MagicNumber as a workaround for now. You don't want a different magic number for every order.

6) I am going to get you something back that will work for backtesting and you can tweak it however you like. It will need much more work, in risk management, error checking, recovery etc.

7) Think of anything else you missed or have misunderstood and post it. I'll try and post it back to you by Wed night.

Dan

Ah thanks Dan,

Yes pending orders deleted at EOD and new ones placed, but open orders remain unchanged.

For the moment I would like the EA to place pending orders regardless of where price is. After a few months testing I might try to tweat it.

We can get rid of that OrderModify() code, ECN etc, that strategy didn't work well.

As for MagicNumber, I would like the EA to attach to all different pairs.

One other thing I'm trying to code is lotsize as a percentage of account size. eg, instead of opening 1 mini lot, open 1% of account.

Thanks for your help.

Dan
220
Dan  
Swatunit4:


Here you go. Good luck
Files:
12
To add comments, please log in or register