Open orders on current price plus x pips

 
 
luisneves:

Hi,

Have a problem with opening orders where I'm using this code;

Global variables

 <SNIP>

Using this code only buy orders are opened, could anyone tell me where I get wrong  ?

Thank you in advance for any help toy could provide


Please edit your post . . .   or have your code deleted


Please use this to post code . . . it makes it easier to read.

 
luisneves:

Hi,

Have a problem with opening orders where I'm using this code;

Using this code only buy orders are opened, could anyone tell me where I get wrong  ?

What error do you get ?  you don't get an error . . .  the Sell is never attempted . . . if the Buy works . . . 

if(BuyTicket>0)

  Limit=true;

 Limit is true,  then . . . 

if(tick_Sell< tick_previous -Distance*UsePoint &&     ! Limit     && SellTicket==0)

   SellTicket = OrderSend(Symbol(),OP_SELL,LotSize,Bid,UseSlippage,0,0,"Sell Order",MagicNumber,0,Red);

  . . .  meaning only place the Sell if Limit is false . . . 

 

What is the Buy fails ?  don't you want to know why ?  

 What are Function return values ? How do I use them ?

 

Hi RaptorUk,

Thank you for your prompt reply to my topic and advise for insert code in a right manner, but after insert it and Add comment I receive a message saying that text is to long (is that the text or code is included?) That's why I put the code divided in the next two "add comment"

 While I do believe that you are doing your best to teach people like me I am very disappointed with myself  for not full understand your explanation.

So, the way I understand the code is;

if I want to open orders based in Ask or Bid I start to  declare tick_previous in Global because putting it on start it never stay in 0 and will be impossible to compare with Ask or Bid at present time.

But if I just use this once I put the EA running it keeps to open orders with no limitation because the condition of Ask still be true all the way (the same should be for Bid).

So, once an order is opened and get confirmation from the server ticket should be >0 and here telling Limit to be true no other orders should happen. 

With the code If I run it on tester once I start an buy order comes immediately  and here another problem comes to my mind if I put a distance from the price don’t should this happen, meaning an order just could be opened after the price goes up or down that distance ?

Once again, my thanks for a little more help... 

Luis

 

Hi Raptor,

here is part of the code

extern bool   DynamicLotSize = true;
extern double EquityPercent  = 2;
extern double FixedLotSize   = 0.01;
extern double StopLoss       = 50;
extern double TakeProfit     = 5;
extern int    Slippage       = 3;
extern int    Distance       = 2;
extern int    MinLimit       = 2;
extern int    MagicNumber    = 12345;

// Global variables
int BuyTicket;
int SellTicket;
double tick_previous=0.0;
double UsePoint;
int UseSlippage;
int ErrorCode;
bool Limit=false;

// Init function
int init()
        {
                UsePoint = PipPoint(Symbol());
                UseSlippage = GetSlippage(Symbol(),Slippage);   
   }

// Start function
int start()
        {
//--------------------------------------------------------------------------------------------------------+ 
    
 double tick_Sell=Bid;
 double tick_Buy =Ask;        
        
//+-------------------------------------------------------------------------------------------------------+             
// Lot size calculation

      if(DynamicLotSize == true)
        {
           double RiskAmount = AccountEquity() * (EquityPercent / 100);
           double TickValue = MarketInfo(Symbol(),MODE_TICKVALUE);
           if(Point == 0.001 || Point == 0.00001) TickValue *= 10;
           double CalcLots = (RiskAmount / StopLoss) / TickValue;
           double LotSize = CalcLots;
        }
      else LotSize = FixedLotSize;
     
// Lot size verification

      if(LotSize < MarketInfo(Symbol(),MODE_MINLOT))
        {
           LotSize = MarketInfo(Symbol(),MODE_MINLOT);
        }
      else if(LotSize > MarketInfo(Symbol(),MODE_MAXLOT))
        {
           LotSize = MarketInfo(Symbol(),MODE_MAXLOT);
        }
                
      if(MarketInfo(Symbol(),MODE_LOTSTEP) == 0.1)
        {
           LotSize = NormalizeDouble(LotSize,1);
        }
      else LotSize = NormalizeDouble(LotSize,2);
        
//+-------------------------------------------------------------------------------------------------------+             
// Buy Order Condition to Open


        if(tick_Buy > tick_previous + Distance*UsePoint && BuyTicket==0)

         {
            OrderSelect(SellTicket,SELECT_BY_TICKET);
            
// Close Order  
              
                      if(OrderCloseTime() == 0 && SellTicket > 0)
                              {
                                      double CloseLots = OrderLots();
                                      
                                      while(IsTradeContextBusy()) Sleep(10);

                  RefreshRates();       
                  double ClosePrice = Ask;

                  bool Closed = OrderClose(SellTicket,CloseLots,ClosePrice,UseSlippage,Red);
                  
                  // Error handling
                  if(Closed == false)
                     {
                        ErrorCode = GetLastError();
                        string ErrDesc = ErrorDescription(ErrorCode);

                        string ErrAlert = StringConcatenate("Close Sell Order - Error ",ErrorCode,": ",ErrDesc);
                        Alert(ErrAlert);

                        string ErrLog = StringConcatenate("Ask: ",Ask," Lots: ",LotSize," Ticket: ",SellTicket);
                        Print(ErrLog);
                     }
                                      
                              }
                                
// Open buy order

                                while(IsTradeContextBusy()) Sleep(10);
                                RefreshRates(); 
                        
            BuyTicket = OrderSend(Symbol(),OP_BUY,LotSize,Ask,UseSlippage,0,0,"Buy Order",MagicNumber,0,Green);
            if(BuyTicket>0)
            Limit=true;
//+----------------------------------------------------------------------------------------------------------------+

//Enter Buy Order  Out Freeze Zone Limits          
               {
                  OrderSelect(BuyTicket,SELECT_BY_TICKET);
                  double OpenPrice = OrderOpenPrice();
                  
// Calculate stop level
                                
                                      double StopLevel = MarketInfo(Symbol(),MODE_STOPLEVEL) * Point;

                                      RefreshRates();   
                                      double UpperStopLevel = Ask + StopLevel;
                                      double LowerStopLevel = Bid - StopLevel;
                                      double MinStop    = MinLimit * UsePoint;
                                      
// Calculate stop loss and take profit

                                      if(StopLoss > 0)   double BuyStopLoss   = OpenPrice - (StopLoss * UsePoint);
                  if(TakeProfit > 0) double BuyTakeProfit = OpenPrice + (TakeProfit * UsePoint);
                  
// Verify  minimum stop loss and take profit limits

                                      if(BuyStopLoss > 0 && BuyStopLoss > LowerStopLevel) 
                                              {                                 
                                                      BuyStopLoss = LowerStopLevel - MinStop;
                                              }
                                      
                                      if(BuyTakeProfit > 0 && BuyTakeProfit < UpperStopLevel) 
                                              {
                                                      BuyTakeProfit = UpperStopLevel + MinStop;
                                              }
// Modify order
                                      if(IsTradeContextBusy()) Sleep(10);       

                  if(BuyStopLoss > 0 || BuyTakeProfit > 0) 
                     {          
                        bool TicketMod = OrderModify(BuyTicket,OpenPrice,BuyStopLoss,BuyTakeProfit,0);
                       
                      // Error handling
                        if(TicketMod == false)
                           {
                              ErrorCode = GetLastError();
                              ErrDesc = ErrorDescription(ErrorCode);

                              ErrAlert = StringConcatenate("Modify Buy Order - Error ",ErrorCode,": ",ErrDesc);
                              Alert(ErrAlert);

                              ErrLog = StringConcatenate("Ask: ",Ask," Bid: ",Bid," Ticket: ",BuyTicket," Stop: ",BuyStopLoss," Profit: ",BuyTakeProfit);
                              Print(ErrLog);
                           }
                     }
               }

                      SellTicket = 0;
             }

 

Hi RaptorUK,

Here is the last part

//+----------------------------------------------------------------------------------------------------------------+            
// Sell Order Condition to Open

      
         if(tick_Sell < tick_previous -Distance*UsePoint && SellTicket==0)              
                        {
                                OrderSelect(BuyTicket,SELECT_BY_TICKET);
                
                      if(OrderCloseTime() == 0 && BuyTicket > 0)
               {
                  CloseLots = OrderLots();

                  while(IsTradeContextBusy()) Sleep(10);

                  RefreshRates();
                  ClosePrice = Bid;

                  Closed = OrderClose(BuyTicket,CloseLots,ClosePrice,UseSlippage,Red);
                  
// Error handling
                  if(Closed == false)
                     {
                        ErrorCode = GetLastError();
                        ErrDesc = ErrorDescription(ErrorCode);

                        ErrAlert = StringConcatenate("Close Buy Order - Error ",ErrorCode,": ",ErrDesc);
                        Alert(ErrAlert);

                        ErrLog = StringConcatenate("Bid: ",Bid," Lots: ",LotSize," Ticket: ",BuyTicket);
                        Print(ErrLog);
                     }
               }
               
// Open buy order       
                        
                           while(IsTradeContextBusy()) Sleep(10);
                                RefreshRates(); 
                                                        
                                SellTicket = OrderSend(Symbol(),OP_SELL,LotSize,Bid,UseSlippage,0,0,"Sell Order",MagicNumber,0,Red);
                                if(SellTicket>0)
                                Limit=true;
//+---------------------------------------------------------------------------------------------------------------+
//Modify Sell Order Out Freeze Zone Limits                      
               {
                  OrderSelect(SellTicket,SELECT_BY_TICKET);
                  OpenPrice = OrderOpenPrice();

                                      StopLevel = MarketInfo(Symbol(),MODE_STOPLEVEL) * Point;

                                      RefreshRates();   
                                      UpperStopLevel = Ask + StopLevel;
                                      LowerStopLevel = Bid - StopLevel;
                                      MinStop    = MinLimit * UsePoint;
                                      
// Calculate stop loss and take profit
                              
                                      if(StopLoss > 0) double SellStopLoss = OpenPrice + (StopLoss * UsePoint);
                                      if(TakeProfit > 0) double SellTakeProfit = OpenPrice - (TakeProfit *      UsePoint);
                                      
// Verify  minimum stop loss and take profit limits

                                      if(SellStopLoss > 0 && SellStopLoss < UpperStopLevel) 
                                              {                                 
                                                      SellStopLoss = UpperStopLevel + MinStop;
                                              }
                                      if(SellTakeProfit  > 0 && SellTakeProfit > LowerStopLevel) 
                                              {
                                                      SellTakeProfit = LowerStopLevel - MinStop;
                                              }
// Modify Order
                                      if(IsTradeContextBusy()) Sleep(10);       

                  if(SellStopLoss > 0 || SellTakeProfit > 0) 
                    {           
                       TicketMod = OrderModify(SellTicket,OpenPrice,SellStopLoss,SellTakeProfit,0);
                       
// Error handling
                       if(TicketMod == false)
                          {
                             ErrorCode = GetLastError();
                             ErrDesc = ErrorDescription(ErrorCode);

                             ErrAlert = StringConcatenate("Modify Sell Order - Error ",ErrorCode,": ",ErrDesc);
                             Alert(ErrAlert);

                             ErrLog = StringConcatenate("Ask: ",Ask," Bid: ",Bid," Ticket: ",SellTicket," Stop: ",SellStopLoss," Profit: ",SellTakeProfit);
                             Print(ErrLog);
                          }
                    }
               }
            
            BuyTicket = 0;
                        }
                        
                return(0);
        }

// Pip Point Function

double PipPoint(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);
        }

// Get Slippage Function

int GetSlippage(string Currency, int SlippagePips)
        {
                int CalcDigits = MarketInfo(Currency,MODE_DIGITS);
                if(CalcDigits == 2 || CalcDigits == 4) double CalcSlippage = SlippagePips;
                else if(CalcDigits == 3 || CalcDigits == 5) CalcSlippage = SlippagePips * 10;
                return(CalcSlippage);
        }
 
luisneves:

Hi RaptorUk,

Thank you for your prompt reply to my topic and advise for insert code in a right manner, but after insert it and Add comment I receive a message saying that text is to long (is that the text or code is included?) That's why I put the code divided in the next two "add comment" 

You could have just added part of the code . . .  or if you want to add the whole code then add it as a File attachment using this . . .

 

Add file 

 
luisneves:

Hi RaptorUk,

Thank you for your prompt reply to my topic and advise for insert code in a right manner, but after insert it and Add comment I receive a message saying that text is to long (is that the text or code is included?) That's why I put the code divided in the next two "add comment"

 While I do believe that you are doing your best to teach people like me I am very disappointed with myself  for not full understand your explanation.

 

Limit is declared and set to false, after your first trade Limit = true . . .  where and when do you set it to false so you can place your second, third, fourth, etc  trades ?
 
RaptorUK:
Limit is declared and set to false, after your first trade Limit = true . . .  where and when do you set it to false so you can place your second, third, fourth, etc  trades ?


Hi Raptor;

as you already know I'm an ignorant in mql, nevertheless here is where I've set Limit to be false;

 

if ((tick_Buy > tick_previous + Distance*UsePoint && BuyTicket==0)&& Limit==false)

 and as you imagine...still doesn't work

Before you start to put me off from here I would like to tell you how I understand this code;

If I don't have Limit to receive any modification on the price then my open condition is valid and order will be open, once  confirmation is received through the ticket then I change Limit to true and no more variations will be accepted till my order close. 

Thanks

Luis 

Reason: