TakeProfit & Stop Trading

 

How would you code a routine that would say the following;

If TakeProfit reaches 50 pips

Close the order

Stop trading

 

Hi Chewbaca, maybe you could try something like this :

extern int

TP = 50,

magic = 1234,

Sleeping = 1800000; // 30 x 60 x 1000

bool

AllowTrade = true;

int init()

{

return (0);

}

int deinit()

{

return (0);

}

int start()

{

int cnt;

if(!AllowTrade)

{

// after reach TP, it will wait for 30 minutes before the EA start to active again

if(Sleeping>0)

{

Sleep(Sleeping);

AllowTrade=true;

return(0);

}

// if Sleeping set to 0, it will stop the EA for doing other command after reach TP

return(0);

}

for(cnt=0;cnt<OrdersTotal();cnt++)

{

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

if(OrderType()<=OP_SELL &&

OrderSymbol()==Symbol() &&

OrderMagicNumber()==magic)

{

if(OrderType()==OP_BUY) // buy position is opened

{

if(Bid-OrderOpenPrice()>=Point*TP)

{

OrderClose(OrderTicket(),OrderLots(),Bid,5,Violet);

AllowTrade=false;

return(0);

}

}

if(OrderType()==OP_SELL) // sell position is opened

{

if(OrderOpenPrice()-Ask>=Point*TP)

{

OrderClose(OrderTicket(),OrderLots(),Bid,5,Violet);

AllowTrade=false;

return(0);

}

}

}

}

// put your entry etc. here

//

//

//-------------

return (0);

}

Don't set TP for your entry and let this code close the trade for you if the TP reach. Hope this help

 

Ye, this is the main rule

Bid-OrderOpenPrice()>=Point*TP

OrderOpenPrice()-Ask>=Point*TP

Good code

 
firedave:
Hi Chewbaca, maybe you could try something like this :
extern int

TP = 50,

magic = 1234,

Sleeping = 1800000; // 30 x 60 x 1000

bool

AllowTrade = true;

int init()

{

return (0);

}

int deinit()

{

return (0);

}

int start()

{

int cnt;

if(!AllowTrade)

{

// after reach TP, it will wait for 30 minutes before the EA start to active again

if(Sleeping>0)

{

Sleep(Sleeping);

AllowTrade=true;

return(0);

}

// if Sleeping set to 0, it will stop the EA for doing other command after reach TP

return(0);

}

for(cnt=0;cnt<OrdersTotal();cnt++)

{

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

if(OrderType()<=OP_SELL &&

OrderSymbol()==Symbol() &&

OrderMagicNumber()==magic)

{

if(OrderType()==OP_BUY) // buy position is opened

{

if(Bid-OrderOpenPrice()>=Point*TP)

{

OrderClose(OrderTicket(),OrderLots(),Bid,5,Violet);

AllowTrade=false;

return(0);

}

}

if(OrderType()==OP_SELL) // sell position is opened

{

if(OrderOpenPrice()-Ask>=Point*TP)

{

OrderClose(OrderTicket(),OrderLots(),Bid,5,Violet);

AllowTrade=false;

return(0);

}

}

}

}

// put your entry etc. here

//

//

//-------------

return (0);

}
Don't set TP for your entry and let this code close the trade for you if the TP reach. Hope this help

i've been searching for this code.....thanks firedave.

i have a small request....how to make the ea to stop trading when take profit is reached and start trading again next day.

thanks

Reason: