• Get access

My first EA, Need help with limiting orders to 16 send orders after signal.

To add comments, please log in or register
Sirru82
71
Sirru82  

Dear community,  

Please help. I  am trying for past week to limit orders to 16 (bars or pending orders) after receiving signal for buy/sell. closest i get was by using argument  :

if(FastMA>SlowMA && BuyTicket <=16 && BuyMarketCount(Symbol(),MagicNumber) == 0)

 But thats wrong way. 

 

 

I tried to make a counter , didint work.

What will be the best to use ? Any ideas?

Here is the code without any SL, tp, etc to limit the problems at the moment (i know how to add them later by OrderModify()):

 

#include <FX001a.mqh>

extern bool DynamicLotSize = true;
extern double EquityPercent = 2;
extern double FixedLotSize = 0.1;

extern double StopLoss = 30;


extern int TrailingStop = 30;
extern int MinimumProfit = 30;
extern int BreakEvenProfit = 30;

extern int Slippage = 5;
extern int MagicNumber = 123;

extern int FastMAPeriod = 21;
extern int SlowMAPeriod = 89;

extern bool CheckOncePerBar = true;

//Signals
   double Signal()
      {
       double FastMA = iMA(NULL,0,FastMAPeriod,0,1,0,0);
       double SlowMA = iMA(NULL,0,SlowMAPeriod,0,0,0,0);
       if(FastMA>SlowMA)return(10);
       if(FastMA<SlowMA)return(10);
       return(30);
      }

//global variabbles

int ErrorCode;
int Counter = 1;
int BuyTicket;
int SellTicket;
double UsePoint;
int UseSlippage;

datetime CurrentTimeStamp;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
   UsePoint = PipPoint(Symbol());
   UseSlippage = GetSlippage(Symbol(),Slippage);
   
   CurrentTimeStamp = Time[0];
  }



//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
   //execute on bar open
   if(CheckOncePerBar == true)
      {
       int BarShift = 1;
       if(CurrentTimeStamp != Time[0])
          { 
           CurrentTimeStamp = Time[0];
           bool NewBar = true;
          }
       else NewBar = false;
      }
   else 
      {
       NewBar = true;
       BarShift = 0;
      }
   //MovingAverages
   double FastMA = iMA(NULL,0,FastMAPeriod,0,1,0,0);
   double SlowMA = iMA(NULL,0,SlowMAPeriod,0,0,0,0);
   //Signals
   
   //calclotsize
   double LotSize = CalcLotSize(DynamicLotSize,EquityPercent,StopLoss,FixedLotSize);
   LotSize = VerifyLotSize(LotSize);
   //BEGINNING OF TRADE BLOCK
   if(NewBar == true)
      { 
       
       //BUY SIGNAL & ORDERS
       
       if(FastMA>SlowMA && BuyTicket <=16 && BuyMarketCount(Symbol(),MagicNumber) == 0)
          {
           if(SellMarketCount(Symbol(),MagicNumber) > 0)
              {
               CloseAllSellLimitOrders(Symbol(),MagicNumber);
              }
           SellTicket = 0;
           RefreshRates();
           double PendingPrice = NormalizeDouble(SlowMA,5);
           PendingPrice = AdjustAboveStopLevel(Symbol(),PendingPrice,5);
           BuyTicket = OpenBuyLimitOrder(Symbol(),LotSize,PendingPrice,Slippage, MagicNumber,"Buy Limit Order");
          }
       
       //sellorder
       if(FastMA<SlowMA && SellTicket == 0 && SellMarketCount(Symbol(),MagicNumber) == 0)
          {
           if(BuyMarketCount(Symbol(),MagicNumber) > 0)
              {
               CloseAllBuyLimitOrders(Symbol(),MagicNumber);
              }
           BuyTicket = 0;
           RefreshRates();
           PendingPrice = NormalizeDouble(SlowMA,5);
           PendingPrice = AdjustBelowStopLevel(Symbol(),PendingPrice,5);
           
           SellTicket = OpenSellLimitOrder(Symbol(), LotSize, PendingPrice,Slippage, MagicNumber,"Sell Limit Order");
           
           
          }
      
      }
     
    return(0);
   }
//+------------------------------------------------------------------+

Cheers! 

Files:
fx001a.mqh 19 kb
sammcdj
32
sammcdj  
Have you tried using OrderHistoryTotal() function instead?
sammcdj
32
sammcdj  
cdjindia:
Have you tried using OrderHistoryTotal() function instead?


Sorry, I meant OrdersTotal

 Here are online refs 

 https://docs.mql4.com/trading/ordershistorytotal 

 https://docs.mql4.com/trading/OrdersTotal 

Sirru82
71
Sirru82  

Hi,

I might be wrong but, I tried using OrdersTotal but that function is for getting all active orders. My orders expire after 1 hr each untill one is filled. Therefore OrdersTotal wont work for limiting orders to 16 per signal. OrderHistory too, as its store all orders for EA. Maybe if i could reset it somehow on each opposite signal.

I think that function i already have should do the job but its not :

int BuyMarketCount(string argSymbol, int argMagicNumber)
   {
    int Counter = 1;
    int OrderCount;
    for(Counter = 0; Counter <= OrdersTotal()-1; Counter++)
       {
        OrderSelect(Counter, SELECT_BY_POS);
        if(OrderMagicNumber() == argMagicNumber && OrderSymbol() == argSymbol && OrderType() == OP_BUYLIMIT)
           {
            OrderCount++;
           }
        
       }
    return(OrderCount);
   }

 similar for selllimit's.

And the problem i have is that when i remove expiration it will work. But I must have either delete or expire on every  bar close. And on new bar open - open new pending on Moving Average. To avoid multiple orders filling at once , on volatile time for example.

Heres how it works some better using this :

if(FastMA>SlowMA  && BuyMarketCount(Symbol(),MagicNumber) <= 16 )

 result :

 

 I think i must change something in counting function , because if i add expiration or deleting function this stop working as it resets each time.

Any ideas what to change, add?

Simon Gniadkowski
17226
Simon Gniadkowski  

Why can't you just look through the Order pool and the History pool and count the number of orders opened since the signal was generated ?

By the way there are problems with the code in your include file:  when closing or deleting Orders from a loop you must count down not up,  read this  Loops and Closing or Deleting Orders

Sirru82
71
Sirru82  

Thanks, I will do that, I needed just some idea as I had brain glitch after many days of thinking....

sammcdj
32
sammcdj  

Three recommendations

One. Initialize OrderCount to zero before you start incrementing it. 

Two ... Place an if statement around OrderSelect and only proceed down.

Three. Place parenthesis in your critical if statement. Precedence rules are not being wrongly used but () makes it lot clearer. 

 

:-)

Sirru82
71
Sirru82  
cdjindia:

Three recommendations

One. Initialize OrderCount to zero before you start incrementing it. 

Two ... Place an if statement around OrderSelect and only proceed down.

Three. Place parenthesis in your critical if statement. Precedence rules are not being wrongly used but () makes it lot clearer. 

 

:-)

 


Thanks cdjindia for intrest. Do u refer to function above?

int BuyMarketCount(string argSymbol, int argMagicNumber)

 

If so , I have changed it to this shape now, is that what u recommend?

int BuyMarketCount(string argSymbol, int argMagicNumber)
   {
    int Counter = OrdersTotal();   //at first this is 0 
    int OrderCount;
    for(Counter = OrdersTotal()-1; Counter >= 0; Counter--)// counting is made down as RaptorUK explained. tho I understand countin down only refers to closing/deleting functions ??? or all counting in general???
       {
        if(OrderSelect(Counter, SELECT_BY_POS)==true); //,,if,, is added and ==true as this is boolean, not needed tho.
           {
            (OrderMagicNumber() == argMagicNumber && OrderSymbol() == argSymbol && OrderType() == OP_BUYLIMIT)
               {
                OrderCount++;
               }
           }
       }
    return(OrderCount);
   }

 I dont really understand your Third recommendation...please explain more detailed if u find have time.

Kind regards,

P.S. I did some major changes to code now, after we finish with current subject i will post result, screenshots and problems occuring now for me ....thx 

William Roeder
20410
William Roeder  
Sirru82: I  am trying for past week to limit orders to 16 (bars or pending orders) after receiving signal for buy/sell. closest i get was by using argument  :
if(FastMA>SlowMA && BuyTicket <=16 && BuyMarketCount(Symbol(),MagicNumber) == 0)

 But thats wrong way. 

Wait for the signal to exit, wait for the signal to resume, count your openings.
static bool signalCurr; bool signalPrev = signalCurr; signalCurr = FastMA>SlowMA;
if (signalCurr){ static int openCount;
    bool newSignal = signalCurr && !signalPrev;
    if (newSignal) openCount = 0;
    if (openCount >= 16) return; // No more than 16
    int ticket = OrderSend(...);
    if (ticket < 0) Alert(...);
    else openCount++;
}
Sirru82
71
Sirru82  
WHRoeder:
Wait for the signal to exit, wait for the signal to resume, count your openings.


Thank you very much. To be honest i have this implemented in new code(to wait untill signals), taken from your previous posts :) but applied wrongly anyway as i see it now.

I will now reapply everything and show results shortly. Cheers!

Sirru82
71
Sirru82  

OK it seems it work great now.

//+------------------------------------------------------------------+
//|                                                        FX001.mq4 |
//|                        Copyright 2012, Pipsomaniak              
//|                                        
//+------------------------------------------------------------------+
#property copyright "Pipsomaniak"


#include <FX001a.mqh>

extern bool DynamicLotSize = true;
extern double EquityPercent = 2;
extern double FixedLotSize = 0.1;

extern double TakeProfit = 90;
extern double StopLoss = 30;
extern int maxorders = 16;
extern int TrailingStop = 30;
extern int MinimumProfit = 30;
extern int BreakEvenProfit = 30;

extern int Slippage = 5;
extern int MagicNumber = 123;

extern int FastMAPeriod = 21;
extern int SlowMAPeriod = 89;
extern bool CheckOncePerBar = true;
//global variabbles

int ErrorCode;

int BuyTicket;
int SellTicket;
double UsePoint;
int UseSlippage;

datetime CurrentTimeStamp;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
   UsePoint = PipPoint(Symbol());
   UseSlippage = GetSlippage(Symbol(),Slippage);
   
   CurrentTimeStamp = Time[0];
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
   //MovingAverages
   double FastMA = iMA(NULL,0,FastMAPeriod,0,1,0,0);
   double SlowMA = iMA(NULL,0,SlowMAPeriod,0,0,0,0);
   
   //Check once per bar
   if(CheckOncePerBar == true)
      {
       int BarShift = 1;
       if(CurrentTimeStamp != Time[0])
          { 
           CurrentTimeStamp = Time[0];
           bool NewBar = true;
          }
       else NewBar = false;
       }
   else 
       {
        NewBar = true;
        BarShift = 0;
       }
   static bool signalCurrBuy; bool signalPrevBuy = signalCurrBuy; signalCurrBuy = FastMA>SlowMA;
   if(signalCurrBuy && NewBar == true)
      {
       static int BuyOpenCount;
       bool newSignalBuy = signalCurrBuy && !signalPrevBuy;
       if (newSignalBuy) BuyOpenCount = 0;
       if (BuyOpenCount >= maxorders) return; // No more than 16
   //-------------BUY ORDER-----------------------------------------------------------------
       //calclotsize
       double LotSize = CalcLotSize(DynamicLotSize,EquityPercent,StopLoss,FixedLotSize);
       LotSize = VerifyLotSize(LotSize);
       //What price is PendingPrice && adjust stoplvl-----------------------
       RefreshRates();
       SlowMA = iMA(NULL,0,SlowMAPeriod,0,0,0,0);
       double PendingPrice = NormalizeDouble(SlowMA,5);
       PendingPrice = AdjustAboveStopLevel(Symbol(),PendingPrice,5);
       //Order
       int BuyTicket = OrderSend(Symbol(),OP_BUYLIMIT,LotSize,PendingPrice,Slippage,0,0,"Buy limit Order",MagicNumber,0,Green);
       if(BuyTicket < 0)
          {
           int ErrorCode = GetLastError();
           string ErrAlert = StringConcatenate("Open Buy Limit Order - Error",ErrorCode);
           Alert(ErrAlert);
           string ErrLog = StringConcatenate("Ask: ",MarketInfo(Symbol(),MODE_ASK),"Lots: ",LotSize, "Price: ",PendingPrice); 
           Print(ErrLog);
          }
       else BuyOpenCount++;
       //Add SL and TP now...for ECN broker
       if(BuyTicket>0 && (StopLoss >0 || TakeProfit >0))
                 {
                  OrderSelect(BuyTicket,SELECT_BY_TICKET);
                  double OpenPrice = OrderOpenPrice();
                  double BuyStopLoss = CalcBuyStopLoss(Symbol(),StopLoss,OpenPrice);
                  if(BuyStopLoss > 0)
                     {
                      BuyStopLoss =  AdjustAboveStopLevel(Symbol(),BuyStopLoss,5);
                     }
                  double BuyTakeProfit = CalcBuyTakeProfit(Symbol(),TakeProfit,OpenPrice);
                  if(BuyTakeProfit > 0)
                     {
                      BuyTakeProfit =  AdjustBelowStopLevel(Symbol(),BuyTakeProfit,5);
                     }
                  AddStopProfit(BuyTicket,BuyStopLoss,BuyTakeProfit);//NOTE THAT THIS MODIFICATION ADDING 1 hr EXPIRATION 
                 }
      }
   static bool signalCurrSell; bool signalPrevSell = signalCurrSell; signalCurrSell = FastMA<SlowMA;
   if(signalCurrSell && NewBar == true)
      {
       static int SellOpenCount;
       bool newSignalSell = signalCurrSell && !signalPrevSell;
       if (newSignalSell) SellOpenCount = 0;
       if (SellOpenCount >= maxorders) return; // No more than 16
   //-------------SELL ORDER-----------------------------------------------------------------
       //calclotsize
       LotSize = CalcLotSize(DynamicLotSize,EquityPercent,StopLoss,FixedLotSize);
       LotSize = VerifyLotSize(LotSize);
       //What price is PendingPrice && adjust stoplvl-----------------------
       RefreshRates();
       SlowMA = iMA(NULL,0,SlowMAPeriod,0,0,0,0);
       PendingPrice = NormalizeDouble(SlowMA,5);
       PendingPrice = AdjustBelowStopLevel(Symbol(),PendingPrice,5);
       //Order
       int SellTicket = OrderSend(Symbol(),OP_SELLLIMIT,LotSize,PendingPrice,Slippage,0,0,"Sell limit Order",MagicNumber,0,Green);
       if(SellTicket < 0)
          {
           ErrorCode = GetLastError();
           ErrAlert = StringConcatenate("Open Sell Limit Order - Error",ErrorCode);
           Alert(ErrAlert);
           ErrLog = StringConcatenate("Bid: ",MarketInfo(Symbol(),MODE_BID),"Lots: ",LotSize, "Price: ",PendingPrice); 
           Print(ErrLog);
          }
       else SellOpenCount++;
       //Add SL and TP now...for ECN broker
       if(SellTicket>0 && (StopLoss >0 || TakeProfit >0))
              {
               OrderSelect(SellTicket,SELECT_BY_TICKET);
               OpenPrice = OrderOpenPrice();
               double SellStopLoss = CalcSellStopLoss(Symbol(),StopLoss,OpenPrice);
               if(SellStopLoss > 0)
                  {
                   SellStopLoss = AdjustBelowStopLevel(Symbol(),SellStopLoss,5);
                  }
               double SellTakeProfit = CalcSellTakeProfit(Symbol(),TakeProfit,OpenPrice);
               if(SellTakeProfit > 0)
                  {
                   SellTakeProfit = AdjustAboveStopLevel(Symbol(),SellTakeProfit,5);
                  }
               AddStopProfit(SellTicket,SellStopLoss,SellTakeProfit);//NOTE THAT THIS MODIFICATION ADDING 1 hr EXPIRATION 
              }

      }
    return(0);
   }
//+------------------------------------------------------------------+

 

Now i will focus on 1. Start counting signals from signal 1....dont start buying/selling from random place..when test started.

                            2. Make array pool which will store and count signals....later used for optimalization....ability to ignore some signals ,,if,,

                            3. Limit only to 1 trade open per signal. After first order is filled == dont trade this signal anymore.

                            4. Adding BE and Trailing stop. ( have this already in my library for later) 

Thanks all for help! I want to learn untill i die :)  

Files:
fx001a_1.mqh 19 kb
To add comments, please log in or register