
Originally Posted by
ivowetters
Hi Funyoo,
Is it possible to add the code for a hidden TP and SL?
I have an ea that send TP and SL with first order but my broker won't except that. Your lucky ea wiht hidden TP and SL does well. So I want to put this also in the other ea (firebird).
Thank you in advance.
Ivo
Hi ivowetters and welcome,
At the end of the code, add :
Code:
//|---------close buy orders
int CloseBuyOrders(int Magic)
{
int result,total=OrdersTotal();
for (int cnt=total-1;cnt>=0;cnt--)
{
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
if(OrderMagicNumber()==Magic&&OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUY)
{
OrderClose(OrderTicket(),OrderLots(),Bid,3);
switch(OrderType())
{
case OP_BUYLIMIT:
case OP_BUYSTOP:
result=OrderDelete(OrderTicket());
}
}
}
}
return(0);
}
//|---------close sell orders
int CloseSellOrders(int Magic)
{
int result,total=OrdersTotal();
for(int cnt=total-1;cnt>=0;cnt--)
{
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
if(OrderMagicNumber()==Magic&&OrderSymbol()==Symbol())
{
if(OrderType()==OP_SELL)
{
OrderClose(OrderTicket(),OrderLots(),Ask,3);
switch(OrderType())
{
case OP_SELLLIMIT:
case OP_SELLSTOP:
result=OrderDelete(OrderTicket());
}
}
}
}
return(0);
}
After start(){, add :
Code:
double LastBuyOpenPrice=0;
double LastSellOpenPrice=0;
int BuyOpenPosition=0;
int SellOpenPosition=0;
int TotalOpenPosition=0;
int cnt=0;
for(cnt=0;cnt<OrdersTotal();cnt++)
{
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
if(OrderSymbol()==Symbol()&&OrderMagicNumber()==Magic&&OrderCloseTime()==0)
{
TotalOpenPosition++;
if(OrderType()==OP_BUY)
{
BuyOpenPosition++;
LastBuyOpenPrice=OrderOpenPrice();
}
if(OrderType()==OP_SELL)
{
SellOpenPosition++;
LastSellOpenPrice=OrderOpenPrice();
}
}
}
if((HideSL&&StopLoss>0&&Bid<(LastBuyOpenPrice-StopLoss*Point))||(HideTP&&TakeProfit>0&&Bid>(LastBuyOpenPrice+TakeProfit*Point)))
{
CloseBuyOrders(Magic);
}
if((HideSL&&StopLoss>0&&Ask>(LastSellOpenPrice+StopLoss*Point))||(HideTP&&TakeProfit>0&&Ask<(LastSellOpenPrice-TakeProfit*Point)))
{
CloseSellOrders(Magic);
}
At the beginning, after extern parameters add :
Code:
extern int StopLoss=0;//|------------------------stop loss
extern int TakeProfit=0;//|----------------------take profit
extern bool HideSL=false;//|---------------------hide stop loss
extern bool HideTP=false;//|---------------------hide take profit
extern int Magic=2009;//|------------------------magic number
If the StopLoss, TakeProfit or Magic are already defined, then there is no need to add them in extern parameters.