Add max order

 

Hi

I'm new to programming and have been trying to code some ea but struggling with getting max order. would be glad if someone can help.

Thanks

// External variables
extern int MagicNumber = 23310;
extern double StopLoss = 0;
extern double TakeProfit = 0;
extern double TrailingStopLimit = 0;
extern double TrailingStopStop = 0;
extern int    CCIperiod = 20;
extern int    Buy_CCICrossLevel=-100;
extern int    Sell_CCICrossLevel=100;
extern double spread_limit=10;
extern bool MM = TRUE;
extern double Risk = 10;
extern double Lots = 0.1;
extern double LotDigits =2;
extern double ratio = 0.1;
extern double delta = 100;
extern int    max_order =5


// Global variables
int LongTicket;
int ShortTicket;
double RealPoint;
double Closed;



// Init function
int init()
        {
      RealPoint = RealPipPoint(Symbol());
     
        }


// Start function
int start()
        {
       
      
        //Variables
      double CCI1 = iCCI( NULL, 0, CCIperiod, PRICE_TYPICAL, 1 );
      double CCI2 = iCCI( NULL, 0, CCIperiod, PRICE_TYPICAL, 2 );
      double spread = MarketInfo(Symbol(), MODE_SPREAD);  
                
                // Long
                
                OrderSelect(LongTicket,SELECT_BY_TICKET);
                if(OrderCloseTime() != 0 || LongTicket == 0)
                {
                
                    
bool buy_condition_1 = ((CCI2 < Buy_CCICrossLevel && CCI1 > Buy_CCICrossLevel+3 &&   spread < spread_limit)||(CCI1 > Sell_CCICrossLevel+3 && spread < spread_limit))  ;


                                 
                if( buy_condition_1   )
                        {
                                                                

                                
                                
                                
                                
                                LongTicket = OrderSend(Symbol(),OP_BUY,GetLots(),Ask,0,0,0,"Buy Order",MagicNumber,0,Blue);
                                
                                
                                OrderSelect(LongTicket,SELECT_BY_TICKET); 
                                double OpenPrice = OrderOpenPrice();
                                
            
             
            if(StopLoss > 0) double LongStopLoss = OpenPrice - (StopLoss * RealPoint);
            if(TakeProfit > 0) double LongTakeProfit = OpenPrice + (TakeProfit * RealPoint);
            
            
                                if(LongStopLoss > 0 || LongTakeProfit > 0) 
                                {
               bool LongMod = OrderModify(LongTicket,OpenPrice,LongStopLoss, LongTakeProfit,0);
                                }
                                
                                
                        }
                
                }
                                
                //Close long
      if (OrdersTotal() > 0)
                {              
                
                    
                
                    
                    
bool close_buy_condition_1 = (CCI2 > Sell_CCICrossLevel && CCI1 < Sell_CCICrossLevel-3) ;

                    
                
                 if( close_buy_condition_1   )
                        {
                                OrderSelect(LongTicket,SELECT_BY_TICKET);
                
                                if(OrderCloseTime() == 0 && LongTicket > 0)
                                        {

                                                Closed = OrderClose(LongTicket,OrderLots(),Bid,0,Pink);
                                                
                                                
                                        }               
                    }
                }
                               
                
                // Short
                
                OrderSelect(ShortTicket,SELECT_BY_TICKET);
                if(OrderCloseTime() != 0 || ShortTicket == 0)
                {
                 
                
                                    
bool sell_condition_1 = ((CCI2 > Sell_CCICrossLevel && CCI1 < Sell_CCICrossLevel-3 && spread < spread_limit)||(CCI1 < Buy_CCICrossLevel-3 && spread < spread_limit)) ;
  
                                    
                if( sell_condition_1   )
                        {
                                        
                                
                                
                                
                                
                                
                                ShortTicket = OrderSend(Symbol(),OP_SELL,GetLots(),Bid,0,0,0,"Sell Order",MagicNumber,0,Red);
                                
                                OrderSelect(ShortTicket,SELECT_BY_TICKET); 
                                OpenPrice = OrderOpenPrice();
                                
            
            
            if(StopLoss > 0) double ShortStopLoss = OpenPrice + (StopLoss * RealPoint);
            if(TakeProfit > 0) double ShortTakeProfit = OpenPrice - (TakeProfit * RealPoint);
            
                                if(ShortStopLoss > 0 || ShortTakeProfit > 0) 
                                {
               bool ShortMod = OrderModify(ShortTicket,OpenPrice,ShortStopLoss, ShortTakeProfit,0);
                                }
                                
                                
                        }
                
                
                }
                             

                
                 //Close Short
                 if (OrdersTotal() > 0)
                {
                 
                 
                                     
bool close_sell_condition_1 = (CCI2 < Buy_CCICrossLevel && CCI1 > Buy_CCICrossLevel+3);

                 
                 if( close_sell_condition_1   )
                        {
                                OrderSelect(ShortTicket,SELECT_BY_TICKET);
                
                                if(OrderCloseTime() == 0 && ShortTicket > 0)
                                        {

                                                Closed = OrderClose(ShortTicket,OrderLots(),Ask,0,Yellow);
                                                
                                                
                                        }               
                        }
                }
 
                return(0);
        }


// Pip Point Function
double RealPipPoint(string Currency)
        {
                int CalcDigits = MarketInfo(Currency,MODE_DIGITS);
                if(CalcDigits == 2 || CalcDigits == 3) double CalcPoint = 0.01;
                else if(CalcDigits == 4 || CalcDigits == 5) CalcPoint = 0.0001;
                return(CalcPoint);
        }
 double GetLots()

{
double minlot = MarketInfo(Symbol(), MODE_MINLOT);
double maxlot = MarketInfo(Symbol(), MODE_MAXLOT);
double leverage = AccountLeverage();
double lotsize = MarketInfo(Symbol(), MODE_LOTSIZE);
double stoplevel = MarketInfo(Symbol(), MODE_STOPLEVEL);

double MinLots = 0.1; double MaximalLots = 10000.0;

if(MM)
{
double lots = NormalizeDouble(AccountFreeMargin()*Risk/100 / 1000.0, LotDigits);
if(lots < minlot) lots = minlot;
if (lots > MaximalLots) lots = MaximalLots;
if (AccountFreeMargin() < Ask * lots * lotsize / leverage) {

Print("We have no money. Lots = ", lots, " , Free Margin = ", AccountFreeMargin());
Comment("We have no money. Lots = ", lots, " , Free Margin = ", AccountFreeMargin());
}}
else lots=NormalizeDouble(Lots,Digits);
return(lots);
}

       


rder deals. Please can somebody help me to add maximum order to the  attached ea,

Thank yo

 
// External variables
extern int MagicNumber = 23310;
extern double StopLoss = 0;
extern double TakeProfit = 0;
extern double TrailingStopLimit = 0;
extern double TrailingStopStop = 0;
extern int    CCIperiod = 20;
extern int    Buy_CCICrossLevel=-100;
extern int    Sell_CCICrossLevel=100;
extern double spread_limit=10;
extern bool MM = TRUE;
extern double Risk = 10;
extern double Lots = 0.1;
extern double LotDigits =2;
extern double ratio = 0.1;
extern double delta = 100;
extern int    max_order =5;


// Global variables
int LongTicket;
int ShortTicket;
double RealPoint;
double Closed;



// Init function
int init()
        {
      RealPoint = RealPipPoint(Symbol());
     
        }


// Start function
int start()
        {
       
      
        //Variables
      double CCI1 = iCCI( NULL, 0, CCIperiod, PRICE_TYPICAL, 1 );
      double CCI2 = iCCI( NULL, 0, CCIperiod, PRICE_TYPICAL, 2 );
      double spread = MarketInfo(Symbol(), MODE_SPREAD);  
                
                // Long
                
                OrderSelect(LongTicket,SELECT_BY_TICKET);
                if(OrderCloseTime() != 0 || LongTicket == 0)
                {
                
                    
bool buy_condition_1 = ((CCI2 < Buy_CCICrossLevel && CCI1 > Buy_CCICrossLevel+3 &&   spread < spread_limit)||(CCI1 > Sell_CCICrossLevel+3 && spread < spread_limit))  ;


                                 
                if( buy_condition_1   )
                        {
                                                                

                                
                                
                                if(OrdersTotal()<max_order)
                                 {
                                  LongTicket = OrderSend(Symbol(),OP_BUY,GetLots(),Ask,0,0,0,"Buy Order",MagicNumber,0,Blue);
                                 }
                                
                                OrderSelect(LongTicket,SELECT_BY_TICKET); 
                                double OpenPrice = OrderOpenPrice();
                                
            
             
            if(StopLoss > 0) double LongStopLoss = OpenPrice - (StopLoss * RealPoint);
            if(TakeProfit > 0) double LongTakeProfit = OpenPrice + (TakeProfit * RealPoint);
            
            
                                if(LongStopLoss > 0 || LongTakeProfit > 0) 
                                {
               bool LongMod = OrderModify(LongTicket,OpenPrice,LongStopLoss, LongTakeProfit,0);
                                }
                                
                                
                        }
                
                }
                                
                //Close long
      if (OrdersTotal() > 0)
                {              
                
                    
                
                    
                    
bool close_buy_condition_1 = (CCI2 > Sell_CCICrossLevel && CCI1 < Sell_CCICrossLevel-3) ;

                    
                
                 if( close_buy_condition_1   )
                        {
                                OrderSelect(LongTicket,SELECT_BY_TICKET);
                
                                if(OrderCloseTime() == 0 && LongTicket > 0)
                                        {

                                                Closed = OrderClose(LongTicket,OrderLots(),Bid,0,Pink);
                                                
                                                
                                        }               
                    }
                }
                               
                
                // Short
                
                OrderSelect(ShortTicket,SELECT_BY_TICKET);
                if(OrderCloseTime() != 0 || ShortTicket == 0)
                {
                 
                
                                    
bool sell_condition_1 = ((CCI2 > Sell_CCICrossLevel && CCI1 < Sell_CCICrossLevel-3 && spread < spread_limit)||(CCI1 < Buy_CCICrossLevel-3 && spread < spread_limit)) ;
  
                                    
                if( sell_condition_1   )
                        {
                                        
                                
                                
                                
                                if(OrdersTotal()<max_order)
                                  {
                                   ShortTicket = OrderSend(Symbol(),OP_SELL,GetLots(),Bid,0,0,0,"Sell Order",MagicNumber,0,Red);
                                  }

                                OrderSelect(ShortTicket,SELECT_BY_TICKET); 
                                OpenPrice = OrderOpenPrice();
                                
            
            
            if(StopLoss > 0) double ShortStopLoss = OpenPrice + (StopLoss * RealPoint);
            if(TakeProfit > 0) double ShortTakeProfit = OpenPrice - (TakeProfit * RealPoint);
            
                                if(ShortStopLoss > 0 || ShortTakeProfit > 0) 
                                {
               bool ShortMod = OrderModify(ShortTicket,OpenPrice,ShortStopLoss, ShortTakeProfit,0);
                                }
                                
                                
                        }
                
                
                }
                             

                
                 //Close Short
                 if (OrdersTotal() > 0)
                {
                 
                 
                                     
bool close_sell_condition_1 = (CCI2 < Buy_CCICrossLevel && CCI1 > Buy_CCICrossLevel+3);

                 
                 if( close_sell_condition_1   )
                        {
                                OrderSelect(ShortTicket,SELECT_BY_TICKET);
                
                                if(OrderCloseTime() == 0 && ShortTicket > 0)
                                        {

                                                Closed = OrderClose(ShortTicket,OrderLots(),Ask,0,Yellow);
                                                
                                                
                                        }               
                        }
                }
 
                return(0);
        }


// Pip Point Function
double RealPipPoint(string Currency)
        {
                int CalcDigits = MarketInfo(Currency,MODE_DIGITS);
                if(CalcDigits == 2 || CalcDigits == 3) double CalcPoint = 0.01;
                else if(CalcDigits == 4 || CalcDigits == 5) CalcPoint = 0.0001;
                return(CalcPoint);
        }
 double GetLots()

{
double minlot = MarketInfo(Symbol(), MODE_MINLOT);
double maxlot = MarketInfo(Symbol(), MODE_MAXLOT);
double leverage = AccountLeverage();
double lotsize = MarketInfo(Symbol(), MODE_LOTSIZE);
double stoplevel = MarketInfo(Symbol(), MODE_STOPLEVEL);

double MinLots = 0.1; double MaximalLots = 10000.0;

if(MM)
{
double lots = NormalizeDouble(AccountFreeMargin()*Risk/100 / 1000.0, LotDigits);
if(lots < minlot) lots = minlot;
if (lots > MaximalLots) lots = MaximalLots;
if (AccountFreeMargin() < Ask * lots * lotsize / leverage) {

Print("We have no money. Lots = ", lots, " , Free Margin = ", AccountFreeMargin());
Comment("We have no money. Lots = ", lots, " , Free Margin = ", AccountFreeMargin());
}}
else lots=NormalizeDouble(Lots,Digits);
return(lots);
}
 
Marco vd Heijden:

Thank you very much. Really helps. but how do I make it to open new  buy or sell if there is an existing buy or sell trade at new signal?

Thanks

 

I don't understand.

You wanted 5 orders max so i made it five orders max.

Whenever a position closes, there will be room for a new one since then orderstotal() drops with it.

elsa:

how do I make it to open new  buy or sell if there is an existing buy or sell trade at new signal?

And now you want to ? break the rule again ??

Please explain.

I have no idea what

elsa:

how do I make it to open new  buy or sell if there is an existing buy or sell trade at new signal?

This means.
 
Marco vd Heijden:

I don't understand.

You wanted 5 orders max so i made it five orders max.

Whenever a position closes, there will be room for a new one since then orderstotal() drops with it.

And now you want to ? break the rule again ??

Please explain.

I have no idea what

This means.

what I meant is that the ea opens only 2 trades. that is it opens one buy and one sell at a time. it does not permit a third trade to open unless the original trade is closed. but I want to make a rule where by it can continue to buy or sell on new signals even if  buy and sell trades are already opened  until max orders are achieved.

for instance if sell is opened.....in case of a new signal to sell another sell order should be placed till  reached max orders. all sell trades a closed by  signal to close trade. same apply to buy orders. see attached chart for an example

Thank you

Files:
EA_LOGIC.png  28 kb
 

Okay then you first need to count buy and sell orders in the orderselect loop

int buy_orders=0;
int sell_orders=0;


// in orderselect loop add:

if(OrderType()==OP_BUY)
 {
  buy_orders++;
 }
if(OrderType()==OP_SELL)
 {
  sell_orders++;
 }

// then you are left with n buy_orders and n sell_orders 
// so then you replace 
OrdersTotal() // in previous example with buy_orders and sell_orders respectively.
 
Marco vd Heijden:

Okay then you first need to count buy and sell orders in the orderselect loop


Thank you!

but it seem not to have any effect. dont know if Im missing something in the code

Reason: