+ Reply to Thread
Page 1 of 3 1 2 3 LastLast
Results 1 to 10 of 26
 1 Attachment(s)    

Thread: Add Hidden TP and SL into code

  1. #1
    Junior Member
    Join Date
    Jan 2009
    Posts
    5

    Default Add Hidden TP and SL into code

    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

  2. #2
    Administrator funyoo's Avatar
    Join Date
    Sep 2008
    Posts
    6,833

    Default

    Quote Originally Posted by ivowetters View Post
    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.

  3. #3
    Junior Member
    Join Date
    Jan 2009
    Posts
    5

    Default

    Hi Funyoo,

    Thank you for hidden TP and SL. Took me some time to change the open order settings also, but I did it. Bit by bit I learn something more about programming ea's.

    Thank you very much.

    Ivo

  4. #4
    Junior Member
    Join Date
    Jan 2009
    Posts
    5

    Default

    Hi Funyoo,

    Do you also have a basic code to send an order without SL and TP, then 1 second later send a ordermodify with SL and TP?

    Because of high volatility completly hidding cost my to much pips.

    Thank you in advance.

    Ivo

  5. #5
    Administrator funyoo's Avatar
    Join Date
    Sep 2008
    Posts
    6,833

    Default

    Quote Originally Posted by ivowetters View Post
    Hi Funyoo,

    Do you also have a basic code to send an order without SL and TP, then 1 second later send a ordermodify with SL and TP?

    Because of high volatility completly hidding cost my to much pips.

    Thank you in advance.

    Ivo
    No, I have not.

    You can put 0 in the OrderSend function, instead of the SL and the TP.

    Then you add a sleep function :

    Sleep(1000);

    And finally a trailing stop function with only this condition :

    if(OrderStopLoss()==0)OrderModify(etc...

  6. #6
    Member
    Join Date
    Feb 2009
    Posts
    326

    Default

    Hi Funyoo,
    I tried to add your code but doesn't seem to work...I thought it might be because of the magic number...but when I remove that code it still doesn't act right....Can you help? I'd like add Stop loss...Thanks Bill



    #define m 20050611
    //----
    extern int tp = 65;
    extern int sh = 41;
    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
    //----
    datetime lastt;
    //+------------------------------------------------------------------+
    //| |
    //+------------------------------------------------------------------+
    int kol_buy()
    {
    int kol_ob = 0;
    //----
    for(int i = 0; i < OrdersTotal(); i++)
    {
    if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == false)
    break;
    //----
    if(OrderType() == OP_BUY)
    kol_ob++;
    }
    return(kol_ob);
    }
    //+------------------------------------------------------------------+
    //| |
    //+------------------------------------------------------------------+
    int kol_sell()
    {
    int kol_os = 0;
    //----
    for(int i = 0; i < OrdersTotal(); i++)
    {
    if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == false)
    break;
    //----
    if(OrderType() == OP_SELL)
    kol_os++;
    }
    return(kol_os);
    }
    //+------------------------------------------------------------------+
    //| |
    //+------------------------------------------------------------------+
    int start()
    {
    if((HideSL&&StopLoss>0&&OrderType()==OP_BUY&&(Orde rOpenPrice()-Ask)/Point>=StopLoss)||(HideTP&&TakeProfit>0&&OrderType ()==OP_BUY&&(Bid-OrderOpenPrice())/Point>=TakeProfit))
    {
    CloseBuyOrders(Magic);
    }
    if((HideSL&&StopLoss>0&&OrderType()==OP_SELL&&(Bid-OrderOpenPrice())/Point>=StopLoss)||(HideTP&&TakeProfit>0&&OrderType ()==OP_SELL&&(OrderOpenPrice()-Ask)/Point>=TakeProfit))
    {
    CloseSellOrders(Magic);
    }
    int slip, i, ii, tic, total, kk, gle;
    double lotsi = 0.0;
    bool sob = false, sos = false, scb = false, scs = false;
    int kb, kb_max = 0;
    kb = kol_buy() + 1;
    double M_ob[11][8];
    ArrayResize(M_ob,kb);
    int ks = 0, ks_max = 0;
    ks = kol_sell() + 1;
    double M_os[11][8];
    ArrayResize(M_os,ks);
    ArrayInitialize(M_ob, 0.0);
    int kbi = 0;
    //----
    for(i = 0; i < OrdersTotal(); i++)
    {
    if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == false)
    break;
    //----
    if(OrderSymbol() == Symbol() && OrderType() == OP_BUY)
    {
    kbi++;
    M_ob[kbi][0] = OrderTicket();
    M_ob[kbi][1] = OrderOpenPrice();
    M_ob[kbi][2] = OrderLots();
    M_ob[kbi][3] = OrderType();
    M_ob[kbi][4] = OrderMagicNumber();
    M_ob[kbi][5] = OrderStopLoss();
    M_ob[kbi][6] = OrderTakeProfit();
    M_ob[kbi][7] = OrderProfit();
    }
    }
    M_ob[0][0] = kb;
    double max_lot_b = 0.0;
    //----
    for(i = 1; i < kb; i++)
    if(M_ob[i][2] > max_lot_b)
    {
    max_lot_b = M_ob[i][2];
    kb_max = i;
    }
    double buy_lev_min = M_ob[kb_max][1];
    ArrayInitialize(M_os,0.0);
    int ksi = 0;
    //----
    for(i = 0; i < OrdersTotal(); i++)
    {
    if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false )
    break;
    //----
    if(OrderSymbol()==Symbol() && OrderType()==OP_SELL)
    {
    ksi++;
    M_os[ksi][0] = OrderTicket();
    M_os[ksi][1] = OrderOpenPrice();
    M_os[ksi][2] = OrderLots();
    M_os[ksi][3] = OrderType();
    M_os[ksi][4] = OrderMagicNumber();
    M_os[ksi][5] = OrderStopLoss();
    M_os[ksi][6] = OrderTakeProfit();
    M_os[ksi][7] = OrderProfit();
    }
    }
    M_os[0][0] = ks;
    double max_lot_s = 0.0;
    //----
    for(i = 1;i < ks; i++)
    if(M_os[i][2] > max_lot_s)
    {
    max_lot_s = M_os[i][2];
    ks_max = i;
    }
    double sell_lev_max = M_os[ks_max][1];
    //----
    if(Bars < 100 || IsTradeAllowed() == false)
    return(0);
    sob = (kol_buy() < 1 || buy_lev_min - sh*Point > Ask) &&
    AccountFreeMargin() > AccountBalance()*0.5;
    sos = (kol_sell() < 1 || sell_lev_max + sh*Point < Bid) &&
    AccountFreeMargin() > AccountBalance()*0.5;
    //----
    if(M_ob[kb_max][2] > 0.0)
    scb = M_ob[kb_max][7] / (M_ob[kb_max][2]*9) > tp;
    //----
    if(M_os[ks_max][2] > 0.0)
    scs = M_os[ks_max][7] / (M_os[ks_max][2]*9) > tp;
    kk = 0;
    ii = 0;
    //----
    if(scb)
    {
    while(kol_buy() > 0 && kk < 3)
    {
    for(i = 1; i <= kb; i++)
    {
    ii = M_ob[i][0];
    //----
    if(!OrderClose(ii,M_ob[i][2],Bid,slip,White))
    {
    gle = GetLastError();
    kk++;
    Print(" ", gle, " close buy ", kk);
    Sleep(6000);
    RefreshRates();
    }
    }
    kk++;
    }
    }
    kk = 0;
    ii = 0;
    //----
    if(scs)
    {
    while(kol_sell() > 0 && kk < 3)
    {
    for(i = 1; i <= ks; i++)
    {
    ii = M_os[i][0];
    //----
    if(!OrderClose(ii,M_os[i][2], Ask, slip, White))
    {
    gle = GetLastError();
    kk++;
    Print(" ", gle, " close sell ", kk);
    Sleep(6000);
    RefreshRates();
    }
    }
    kk++;
    }
    }
    kk = 0;
    tic = -1;
    //----
    if(sob)
    {
    if(max_lot_b == 0.0)
    lotsi = 0.1;
    else
    lotsi = 1.5*max_lot_b;
    //----
    while(tic == -1 && kk < 3)
    {
    tic = OrderSend(Symbol(), OP_BUY, lotsi, Ask, slip, 0, Ask + (tp + 25)*Point,
    " ", m, 0, Yellow);
    Print("tic_buy=", tic);
    //----
    if(tic==-1)
    {
    gle = GetLastError();
    kk++;
    Print(" ", gle, " buy ", kk);
    Sleep(6000);
    RefreshRates();
    }
    }
    lastt = CurTime();
    return;
    }
    tic = -1;
    kk = 0;
    //----
    if(sos)
    {
    if(max_lot_s == 0.0)
    lotsi = 0.1;
    else
    lotsi = 1.5*max_lot_s;
    //----
    while(tic == -1 && kk < 3)
    {
    tic = OrderSend(Symbol(), OP_SELL, lotsi, Bid, slip, 0, Bid - (tp + 25)*Point,
    " ", m, 0, Red);
    Print("tic_sell=", tic);
    //----
    if(tic == -1)
    {
    gle = GetLastError();
    kk++;
    Print(" ", gle, " sell ", kk);
    Sleep(6000);
    RefreshRates();
    }
    }
    lastt = CurTime();
    return;
    }
    }
    //+------------------------------------------------------------------+
    //|---------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()==Symbo l())
    {
    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()==Symbo l())
    {
    if(OrderType()==OP_SELL)
    {
    OrderClose(OrderTicket(),OrderLots(),Ask,3);
    switch(OrderType())
    {
    case OP_SELLLIMIT:
    case OP_SELLSTOP:
    result=OrderDelete(OrderTicket());
    }
    }
    }
    }
    return(0);
    }

  7. #7
    Administrator funyoo's Avatar
    Join Date
    Sep 2008
    Posts
    6,833

    Default

    Hi BillR,

    I have updated the guide.

  8. #8
    Member
    Join Date
    Feb 2009
    Posts
    326

    Default

    Hi Funyoo,
    I wasn't able to add your code and get it to work. Here I've added StopLoss which is what I want ....and it works, but can you make it a "Hidden" Stoploss? Also could it be made so a Spike or a Broker Stop Hunting NOT trigger it??
    Thanks Bill


    //+------------------------------------------------------------------+
    //| _.mq4 |
    //| Copyright 2006, |
    //| popcorn@aaanet.ru |
    //+------------------------------------------------------------------+
    #define m 20050611
    //----
    extern int tp = 65;
    extern int sh = 41;
    extern int sl=100;
    //----
    datetime lastt;
    //+------------------------------------------------------------------+
    //| |
    //+------------------------------------------------------------------+
    int kol_buy()
    {
    int kol_ob = 0;
    //----
    for(int i = 0; i < OrdersTotal(); i++)
    {
    if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == false)
    break;
    //----
    if(OrderType() == OP_BUY)
    kol_ob++;
    }
    return(kol_ob);
    }
    //+------------------------------------------------------------------+
    //| |
    //+------------------------------------------------------------------+
    int kol_sell()
    {
    int kol_os = 0;
    //----
    for(int i = 0; i < OrdersTotal(); i++)
    {
    if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == false)
    break;
    //----
    if(OrderType() == OP_SELL)
    kol_os++;
    }
    return(kol_os);
    }
    //+------------------------------------------------------------------+
    //| |
    //+------------------------------------------------------------------+
    int start()
    {
    int slip, i, ii, tic, total, kk, gle;
    double lotsi = 0.0;
    bool sob = false, sos = false, scb = false, scs = false;
    int kb, kb_max = 0;
    kb = kol_buy() + 1;
    double M_ob[11][8];
    ArrayResize(M_ob,kb);
    int ks = 0, ks_max = 0;
    ks = kol_sell() + 1;
    double M_os[11][8];
    ArrayResize(M_os,ks);
    ArrayInitialize(M_ob, 0.0);
    int kbi = 0;
    //----
    for(i = 0; i < OrdersTotal(); i++)
    {
    if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == false)
    break;
    //----
    if(OrderSymbol() == Symbol() && OrderType() == OP_BUY)
    {
    kbi++;
    M_ob[kbi][0] = OrderTicket();
    M_ob[kbi][1] = OrderOpenPrice();
    M_ob[kbi][2] = OrderLots();
    M_ob[kbi][3] = OrderType();
    M_ob[kbi][4] = OrderMagicNumber();
    M_ob[kbi][5] = OrderStopLoss();
    M_ob[kbi][6] = OrderTakeProfit();
    M_ob[kbi][7] = OrderProfit();
    }
    }
    M_ob[0][0] = kb;
    double max_lot_b = 0.0;
    //----
    for(i = 1; i < kb; i++)
    if(M_ob[i][2] > max_lot_b)
    {
    max_lot_b = M_ob[i][2];
    kb_max = i;
    }
    double buy_lev_min = M_ob[kb_max][1];
    ArrayInitialize(M_os,0.0);
    int ksi = 0;
    //----
    for(i = 0; i < OrdersTotal(); i++)
    {
    if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false )
    break;
    //----
    if(OrderSymbol()==Symbol() && OrderType()==OP_SELL)
    {
    ksi++;
    M_os[ksi][0] = OrderTicket();
    M_os[ksi][1] = OrderOpenPrice();
    M_os[ksi][2] = OrderLots();
    M_os[ksi][3] = OrderType();
    M_os[ksi][4] = OrderMagicNumber();
    M_os[ksi][5] = OrderStopLoss();
    M_os[ksi][6] = OrderTakeProfit();
    M_os[ksi][7] = OrderProfit();
    }
    }
    M_os[0][0] = ks;
    double max_lot_s = 0.0;
    //----
    for(i = 1;i < ks; i++)
    if(M_os[i][2] > max_lot_s)
    {
    max_lot_s = M_os[i][2];
    ks_max = i;
    }
    double sell_lev_max = M_os[ks_max][1];
    //----
    if(Bars < 100 || IsTradeAllowed() == false)
    return(0);
    sob = (kol_buy() < 1 || buy_lev_min - sh*Point > Ask) &&
    AccountFreeMargin() > AccountBalance()*0.5;
    sos = (kol_sell() < 1 || sell_lev_max + sh*Point < Bid) &&
    AccountFreeMargin() > AccountBalance()*0.5;
    //----
    if(M_ob[kb_max][2] > 0.0)
    scb = M_ob[kb_max][7] / (M_ob[kb_max][2]*9) > tp;
    //----
    if(M_os[ks_max][2] > 0.0)
    scs = M_os[ks_max][7] / (M_os[ks_max][2]*9) > tp;
    kk = 0;
    ii = 0;
    //----
    if(scb)
    {
    while(kol_buy() > 0 && kk < 3)
    {
    for(i = 1; i <= kb; i++)
    {
    ii = M_ob[i][0];
    //----
    if(!OrderClose(ii,M_ob[i][2],Bid,slip,White))
    {
    gle = GetLastError();
    kk++;
    Print(" ", gle, " close buy ", kk);
    Sleep(6000);
    RefreshRates();
    }
    }
    kk++;
    }
    }
    kk = 0;
    ii = 0;
    //----
    if(scs)
    {
    while(kol_sell() > 0 && kk < 3)
    {
    for(i = 1; i <= ks; i++)
    {
    ii = M_os[i][0];
    //----
    if(!OrderClose(ii,M_os[i][2], Ask, slip, White))
    {
    gle = GetLastError();
    kk++;
    Print(" ", gle, " close sell ", kk);
    Sleep(6000);
    RefreshRates();
    }
    }
    kk++;
    }
    }
    kk = 0;
    tic = -1;
    //----
    if(sob)
    {
    if(max_lot_b == 0.0)
    lotsi = 0.1;
    else
    lotsi = 1.5*max_lot_b;
    //----
    while(tic == -1 && kk < 3)
    {
    tic = OrderSend(Symbol(), OP_BUY, lotsi, Ask, slip, Ask - (sl)*Point, Ask + (tp + 25)*Point,
    " ", m, 0, Yellow);
    Print("tic_buy=", tic);
    //----
    if(tic==-1)
    {
    gle = GetLastError();
    kk++;
    Print(" ", gle, " buy ", kk);
    Sleep(6000);
    RefreshRates();
    }
    }
    lastt = CurTime();
    return;
    }
    tic = -1;
    kk = 0;
    //----
    if(sos)
    {
    if(max_lot_s == 0.0)
    lotsi = 0.1;
    else
    lotsi = 1.5*max_lot_s;
    //----
    while(tic == -1 && kk < 3)
    {
    tic = OrderSend(Symbol(), OP_SELL, lotsi, Bid, slip, Bid + (sl)*Point, Bid - (tp + 25)*Point,
    " ", m, 0, Red);
    Print("tic_sell=", tic);
    //----
    if(tic == -1)
    {
    gle = GetLastError();
    kk++;
    Print(" ", gle, " sell ", kk);
    Sleep(6000);
    RefreshRates();
    }
    }
    lastt = CurTime();
    return;
    }
    }
    //+------------------------------------------------------------------+

  9. #9
    Administrator funyoo's Avatar
    Join Date
    Sep 2008
    Posts
    6,833

    Default

    Ok I have added the hidden part.
    Attached Files

  10. #10
    Member
    Join Date
    Feb 2009
    Posts
    326

    Default

    Hi Funyoo,
    The S/L don't seem to be hidden....In order for the S/L to be hidden shouldn't the code be removed from the OrderSend that I added??

    tic = OrderSend(Symbol(), OP_BUY, lotsi, Ask, slip, Ask - (sl)*Point, Ask + (tp + 25)*Point,
    " ", m, 0, Yellow);

    tic = OrderSend(Symbol(), OP_SELL, lotsi, Bid, slip, Bid + (sl)*Point, Bid - (tp + 25)*Point,
    " ", m, 0, Red);

    Actually just the

    Ask - (sl)*point

    and

    Bid + (sl)*Point

    Thanks again!! Bill R
    Last edited by BillR; 02-14-2009 at 22:08. Reason: Removed extra words...

+ Reply to Thread
Page 1 of 3 1 2 3 LastLast

Similar Threads

  1. Hidden SL & TP
    By jidonk in forum MQL programming
    Replies: 2
    Last Post: 04-20-2009, 17:35
  2. MA cross code
    By comdoc in forum MQL programming
    Replies: 0
    Last Post: 01-26-2009, 03:48
  3. How to code MA on RSI in an EA ?
    By funyoo in forum MQL programming
    Replies: 0
    Last Post: 11-17-2008, 20:03
  4. Lucky code
    By funyoo in forum Expert advisors live statements
    Replies: 0
    Last Post: 09-23-2008, 11:13

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts