Help with break even stop loss code for each tick

Heb852  

Hi I'm in the early leaning stages of coding, can anyone help me modify the break even code.

The EA opens trades on a new bar open, but I would like the break even stop loss to move when the tick reaches the correct point not when the new bar opens.

Any help would be great.

Thanks

if (BreakEvenAt>0)
      
      {
         if (OrderType()==OP_BUY)
         {
            if (MarketInfo(OrderSymbol(),MODE_BID)-OrderOpenPrice()>=0.0001*BreakEvenPoint)
            {
               if (OrderStopLoss()<OrderOpenPrice() + BreakEvenMove*0.0001)
                  OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice() - BreakEvenMove*0.0001,OrderTakeProfit(),0,Green); 
            }
         }
         else if (OrderType()==OP_SELL)
         {
            if (OrderOpenPrice()-MarketInfo(OrderSymbol(),MODE_ASK)>=0.0001*BreakEvenPoint)
            {
               if (OrderStopLoss()>OrderOpenPrice() - BreakEvenMove*0.0001)
                  OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice() + BreakEvenMove*0.0001,OrderTakeProfit(),0,Red); 
            }
         }
      }
Keith Watford  
Heb852:

The EA opens trades on a new bar open, but I would like the break even stop loss to move when the tick reaches the correct point not when the new bar opens.

Check it on every tick and not only when a new bar opens.

Where do you select the order?

OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice() - BreakEvenMove*0.0001,OrderTakeProfit(),0,Green); 

If you are modifying to BE, it should be

OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,Green); 

For a sell

 if (OrderStopLoss()>OrderOpenPrice() - BreakEvenMove*0.0001)

should probably be

 if (OrderStopLoss()>OrderOpenPrice() || OrderStopLoss()==0)
//Instead of 
MarketInfo(OrderSymbol(),MODE_BID)

MarketInfo(OrderSymbol(),MODE_ASK}

//When an order has been selected and you need the current price you can use
OrderClosePrice()
Heb852  
Keith Watford:

Check it on every tick and not only when a new bar opens.

Where do you select the order?

If you are modifying to BE, it should be

For a sell

should probably be

Hi Keith

Thanks for the reply, when you say check it on every tick, how do i do that? I've tried searching for the info. Sorry if this is a basic question but i'm trying to teach myself through a lot of trial and error and looking at code and reading what i can find. Thanks again

Lorentzos Roussos  
Keith Watford:


//Instead of
MarketInfo(OrderSymbol(),MODE_BID)

MarketInfo(OrderSymbol(),MODE_ASK)    
    
    
//When an order has been selected and you need the current price you can use
OrderClosePrice()

Thats so handy ,i did not know that .

Keith Watford  
Heb852:

Hi Keith

Thanks for the reply, when you say check it on every tick, how do i do that? I've tried searching for the info. Sorry if this is a basic question but i'm trying to teach myself through a lot of trial and error and looking at code and reading what i can find. Thanks again

In OnTick() as the function name implies.

Heb852  
Keith Watford:

In OnTick() as the function name implies.

Thanks again for you time Keith, I think this is where my understanding is coming unstuck. Because I want my open trade to operate on the start of a new bar not new tick can you you have void Ontick(); handler just for the break even part of the code? as I keep getting error" 'Ontick' - function declarations are allowed on global, namespace or class scope only"

Do you have any recommended reading for the structures of code etc.

Thanks again

Keith Watford  
Heb852:

Thanks again for you time Keith, I think this is where my understanding is coming unstuck. Because I want my open trade to operate on the start of a new bar not new tick can you you have void Ontick(); handler just for the break even part of the code? as I keep getting error" 'Ontick' - function declarations are allowed on global, namespace or class scope only"

Do you have any recommended reading for the structures of code etc.

Thanks again

You already have the OnTick() function.
If you don't then you should have! 

This will use a variable newBar which you may use many times in your code. (there may be times that you execute code every tick that may also want to check if it is a new bar

void OnTick()
{
   static datetime barTime=0;
   bool newBar=false;
   datetime nowTime=iTime(_Symbol,PERIOD_CURRENT,0);
   if(barTime!=nowTime)
     {
      barTime=nowTime;
      newBar=true;
     }
   if(newBar)
     {
      //Here do stuff that you only want executed on a new bar open.
     }
//Here do stuff that you want executed every tick
}

Alternative without using the variable newBar

void OnTick()
{
   static datetime barTime=0;
   datetime nowTime=iTime(_Symbol,PERIOD_CURRENT,0);
   if(barTime!=nowTime)
     {
      barTime=nowTime;
      //Here do stuff that you only want executed on a new bar open.
     }
//Here do stuff that you want executed every tick
}
Heb852  
Keith Watford:

You already have the OnTick() function.
If you don't then you should have! 

This will use a variable newBar which you may use many times in your code. (there may be times that you execute code every tick that may also want to check if it is a new bar

Alternative without using the variable newBar

Thanks a lot! I appreciate the help.


Cheers