Checking Candle Open - page 4

 
GumRai:

Small change as it was checking to close an order when there wasn't one.

According to the code there is no reason for a sell to be opened immediately a buy closes.

The condition to exit a buy is not the same as the conditions to open a sell .

Remember that as you are only checking on the candle open, Close[0] will be the bid value of the first tick received for the candle.

 

Hey GumRai,

I was wondering if you'd be able/willing to help me a bit more with my code? If you could just tell me what codes I should look into, I'll try coding it myself first and then just come back with any questions .

What I want is that once a trade is placed, it puts a Stop loss at, say, 50 pips (which is what it does now). However, once price moves 60 pips in the direction of the trade, it moves SL to breakeven, and further on becomes a trailing stop 70 pips away from the trade.

How would I go about doing this? As always, any help would be greatly appreciated. 

 

For the break-even, I always move the SL to break-even + 1 point at least. That avoids any possible problems with comparing doubles at later checks

It's simply a case of checking 2 conditions (for a buy )

OrderOpenPrice()>OrderStopLoss()

 and

OrderClosePrice()-OrderOpenPrice>=BreakEvenPoints*Point

 then modify the order so that the SL is the open price +1 point or whatever amount you may decide to lock in.

 

For a sell it is the opposite conditions, but you should also code to account for if SL is 0 initially 

OrderOpenPrice()>OrderStopLoss() || OrderStopLoss()==0 
 
GumRai:

For the break-even, I always move the SL to break-even + 1 point at least. That avoids any possible problems with comparing doubles at later checks

It's simply a case of checking 2 conditions (for a buy )

 and

 then modify the order so that the SL is the open price +1 point or whatever amount you may decide to lock in.

 

For a sell it is the opposite conditions, but you should also code to account for if SL is 0 initially 

 

Thanks a lot. In fact, what I'll do is add enough points so that it just (roughly) covers the cost of brokerage for the trade.

For the second line..why would I use Orderclose price-OrderOpenPrice? I didn't quite understand that code. Shouldn't I use Bid-OrderOpenPrice, since that would track how much the price has moved?

Am I even close with what I'm doing here below?:

 if(BuyTicket==-1)
        {
         if((iOpen(NULL,0,1)<PreviousSlow && PreviousPriceClose>=PreviousSlow && Bid>=(PreviousSlow+PipsBeforeEntry*Pips)) )
           {
            BuyTicket=OrderSend(Symbol(),OP_BUY,LotSize,Ask,Slippage,Ask-(StopLoss*Pips),0,"Main Entry EA",MagicNumber,0,clrLimeGreen);
           }
           if(OrderOpenPrice()>OrderStopLoss())
               if(Bid-OrderOpenPrice>=(BuyTicket+(Commission*LotSize)))
                  OrderModify(BuyTicket,NULL,Bid-(50*Point),NULL,NULL,clrNONE);
        }

 I've declared commission above as an extern. And I used Bid instead. 

 

Edit:

So I guess (based on what I found below) that the code I have posted above is incorrect? I found the following in the MQL4 book/help...It seems to be just a trailing stop, whereas what I need is a Breakeven stop first, followed by trailing stop. How would I modify this?

 int TrailingStop = 50;
               //--- modifies Stop Loss price for buy order 
            if(TrailingStop>0)
               {
               OrderSelect(BuyTicket,SELECT_BY_TICKET);
                if(Bid-OrderOpenPrice()>Point*TrailingStop)
                {
                if(OrderStopLoss()<Bid-Point*TrailingStop)
                  {
                  bool res =OrderModify(BuyTicket,OrderOpenPrice(),NormalizeDouble(Bid-Point*TrailingStop,Digits),OrderTakeProfit(),0,clrNONE);
                   if(!res)
                        Print("Error in OrderModify. Error code=",GetLastError());
                     else
                        Print("Order modified successfully.");
                    }
                 }
                 }
 
 if(BuyTicket==-1)
        {
         if((iOpen(NULL,0,1)<PreviousSlow && PreviousPriceClose>=PreviousSlow && Bid>=(PreviousSlow+PipsBeforeEntry*Pips)) )
           {
            BuyTicket=OrderSend(Symbol(),OP_BUY,LotSize,Ask,Slippage,Ask-(StopLoss*Pips),0,"Main Entry EA",MagicNumber,0,clrLimeGreen);
           }
           if(OrderOpenPrice()>OrderStopLoss())
               if(Bid-OrderOpenPrice>=(BuyTicket+(Commission*LotSize)))
                  OrderModify(BuyTicket,NULL,Bid-(50*Point),NULL,NULL,clrNONE);
        }

Your whole block of code is conditional on BuyTicket==-1

If an order is opened, then BuyTicket will not = -1, so no checks will be done for moving to BE while the trade is open.

if(Bid-OrderOpenPrice>=(BuyTicket+(Commission*LotSize)))

 What does the ticket number have to do with the price?

You must select an order before using OrderOpenPrice() 

OrderClosePrice() is the Bid at the time that the order is selected for a buy, ask for a sell.(while the order is still open) 

 
SharkWaters:

So I guess (based on what I found below) that the code I have posted above is incorrect? I found the following in the MQL4 book/help...It seems to be just a trailing stop, whereas what I need is a Breakeven stop first, followed by trailing stop. How would I modify this?

  int BreakEvenPoints=50;
  double BE_Decimal=BreakEvenPoints*Point;
//--- modifies Stop Loss price for buy order 
  if(BreakEvenPoints>0)
  {
   OrderSelect(BuyTicket,SELECT_BY_TICKET);
   if(Bid-OrderOpenPrice()>BE_Decimal)
     {
      if(OrderStopLoss()<OrderOpenPrice())
        {
         bool res=OrderModify(BuyTicket,OrderOpenPrice(),NormalizeDouble(OrderOpenPrice()+Point,Digits),
                              OrderTakeProfit(),0,clrNONE);
         if(!res)
            Print("Error in OrderModify to BE. Error code=",GetLastError());
         else
            Print("Order modified to BE successfully.");
        }
     }
  }
Try this
 
GumRai:
Try this

Thanks for the points! I've made the necessary changes. When I compile, it shows an alert that "return value of 'OrderSelect' should be checked. This, strangely, translates to only one buy being triggered and then for the rest it's a 4108 error code again. Sell works okay (I haven't made any changes to that).

 Here's the entire section of the buy code, in case I'm doing something wrong--but that part shouldn't be, since all I've added is this code in the middle. The rest was as before which worked perfectly well. What am I getting wrong here?

int start()
  {
   static datetime bar_time=0;
   if(bar_time!=Time[0])
     {
      bar_time=Time[0];
      double PreviousSlow=iMA(NULL,0,SlowMa,SlowMaShift,SlowMaMethod,SlowMaAppliedTo,1);
      double PreviousSlow2=iMA(NULL,0,SlowMa,SlowMaShift,SlowMaMethod,SlowMaAppliedTo,2);
      double PreviousPriceClose=iClose(NULL,0,1);   //You can just use Close[1]
      double PreviousPriceClose2=iClose(NULL,0,2);
      if(BuyTicket==-1)
        {
         if((iOpen(NULL,0,1)<PreviousSlow && PreviousPriceClose>=PreviousSlow && Bid>=(PreviousSlow+PipsBeforeEntry*Pips)) )
           {
            BuyTicket=OrderSend(Symbol(),OP_BUY,LotSize,Ask,Slippage,Ask-(StopLoss*Pips),0,"Main Entry EA",MagicNumber,0,clrLimeGreen);
               }
           }
   //Stop Order Modify for buy starts here
         int BreakEvenPoints=50;
         double BE_Decimal=BreakEvenPoints*Point;
         
         if(BreakEvenPoints>0)
              {
               OrderSelect(BuyTicket,SELECT_BY_TICKET);
               if(Bid-OrderOpenPrice()>BE_Decimal)
                 {
                  if(OrderStopLoss()<OrderOpenPrice())
                    {
                     bool res=OrderModify(BuyTicket,OrderOpenPrice(),NormalizeDouble(OrderOpenPrice()+Point,Digits),OrderTakeProfit(),0,clrNONE);
                     if(!res)
                        Print("Error in OrderModify to BE. Error code=",GetLastError());
                     else
                        Print("Order modified to BE successfully.");
                    }
                 }
              }
      else
      if(OrderSelect(BuyTicket,SELECT_BY_TICKET))
        {
         if(OrderCloseTime()==0)
           {
            if(Close[0]<PreviousSlow)
              {
               bool CloseTicket=OrderClose(BuyTicket,LotSize,Bid,Slippage,clrPink);
               if(CloseTicket)
                  BuyTicket=-1;
              }
           }
         else
            BuyTicket=-1; //Order has closed so reset variable
        }
        

 

 

 Update:

Googled some more, and changed it to the following:

 

int BreakEvenPoints=50;
         double BE_Decimal=BreakEvenPoints*Point;
         
         if(BreakEvenPoints>0)
              {
               if (OrderSelect(BuyTicket,SELECT_BY_TICKET))
               {
               if(Bid-OrderOpenPrice()>BE_Decimal)
                 {
                  if(OrderStopLoss()<OrderOpenPrice())
                    {
                     bool res=OrderModify(BuyTicket,OrderOpenPrice(),NormalizeDouble(OrderOpenPrice()+10*Point,Digits),OrderTakeProfit(),0,clrNONE);
                     if(!res)
                        Print("Error in OrderModify to BE. Error code=",GetLastError());
                     else
                        Print("Order modified to BE successfully.");
                    }
                  }  
                 }
              }


 This removes the error generated when I click compile. But it still creates OrderModify error 4108 in the Journal. 

 

All your code is contained in the if condition

 if(bar_time!=Time[0])  

 so it is only executed once per new bar. 

 

When BuyTicket == -1 you are trying to select the order with a ticket number of -1 and of course it doesn't exist

Check that BuyTicket>0 before trying to select an order 

 
GumRai:

All your code is contained in the if condition

 so it is only executed once per new bar. 


Thanks for pointing this out. 

So, for this, I created an extra bracket on the top, right after Start like: 

int start() 
{
{

and then closed one bracket (which ended the whole if(bar_time!=Time[0]) section and then placed the code at the end followed by a return (0); and end bracket to close the start().

No errors, either in the code, or the backtest Journal but it only took one buy trade and that's it--that too which it closed right at open although price didn't budge any higher from the open--let alone move 50 pips.

 

Think this might have to do with the BuyTicket being == -1 that you mentioned. But I honestly have no idea how to change that. I'd put that -1 thing in based on your recommendation for the initial code--which worked fantastically. But how would I check that BuyTicket>0 before trying to select an order? 

 
SharkWaters:

But how would I check that BuyTicket>0 before trying to select an order? 

I am sorry, but if you have to ask this, you are trying to write code way too complicated for your knowledge level.

I can't write your code for you a bit at a time. 

Reason: