Results 1 to 3 of 3
 0 Attachment(s)    

Thread: Tweaking Martingale....

  1. #1

    Default Tweaking Martingale....

    Hi Funyoo....This Martingale EA Hedges.....(trading both directions at the same time....) If the currency is rising....the sells keep opening new positions at the PIP step (sh) and at the higher Lot exponent multiplier.....The Buys keep opening and closing at a profit again at every rising pip step too...BUT every new buy position goes back to the default lotsi..... .1 or .01 depending on my coding..... I'd like to have the default lotsi on these newly generated positions have a lot size equal to the largest size on the sell side.....effectively having a real hedged position which will now be very profitable when the currency moves either up or down in value.......(The code should treat the direction opposite to my description properly too.....) How'd this be coded?? Thanks Bill


    #define m 20050611
    //----

    extern int sl=370;
    extern int tp = 43;
    extern int sh = 49;
    extern int TrailingStop=15;
    extern int TrailingStep=2;
    //----
    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;
    if(TrailingStop>0)MoveTrailingStop();
    //----
    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, NormalizeDouble(lotsi,1.5), 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, NormalizeDouble(lotsi,1.5), 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;
    }
    }

    //+------------------------------------------------------------------+
    void MoveTrailingStop()
    {
    int cnt,total=OrdersTotal();
    for(cnt=0;cnt<total;cnt++)
    {
    OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
    if(OrderType()<=OP_SELL&&OrderSymbol()==Symbol())
    {
    if(OrderType()==OP_BUY)
    {
    if(TrailingStop>0&&Bid>(OrderOpenPrice()+(Trailing Stop+TrailingStep)*Point))
    {
    if((NormalizeDouble(OrderStopLoss(),Digits)<Normal izeDouble(Bid-Point*(TrailingStop+TrailingStep),Digits))||(Order StopLoss()==0))
    {
    OrderModify(OrderTicket(),OrderOpenPrice(),Normali zeDouble(Bid-Point*TrailingStop,Digits),OrderTakeProfit(),0,Gre en);
    return(0);
    }
    }
    }
    else
    {
    if(TrailingStop>0&&Ask<(OrderOpenPrice()-(TrailingStop+TrailingStep)*Point))
    {
    if((NormalizeDouble(OrderStopLoss(),Digits)>(Norma lizeDouble(Ask+Point*(TrailingStop+TrailingStep),D igits)))||(OrderStopLoss()==0))
    {
    OrderModify(OrderTicket(),OrderOpenPrice(),Normali zeDouble(Ask+Point*TrailingStop,Digits),OrderTakeP rofit(),0,Red);
    return(0);
    }
    }
    }
    }
    }
    }

  2. #2

    Default

    Hi Funyoo...
    Forgot to mention that..."Trailing Stop and/or Take Profit" need to close all effected orders appropriately too .....I have these conditions set *9 here as the Lot Exponent in the previous text is set at NormalizeDouble(lotsi,1.5)


    TTYL Bill R


    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;

  3. #3
    Administrator funyoo's Avatar
    Join Date
    Sep 2008
    Posts
    7,003

    Default

    Hi BillR,

    You need to elaborate a loop in order to dertemine the largest sell size, something like that :

    Code:
    int cnt=0;
    double Count=0,LargetSellLots=0;
       for(cnt=OrdersTotal();cnt>=0;cnt--)
         {
          OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
          if(OrderSymbol()==Symbol()&&OrderMagicNumber()==m)
             if(OrderType()==OP_SELL)
               {
                LargetSellLots=OrderLots();
                if(LargetSellLots>Count)Count=LargetSellLots;
               }
         }

Similar Threads

  1. MA martingale ea
    By surtoday in forum Ideas for expert advisors
    Replies: 2
    Last Post: 07-30-2012, 08:20
  2. RSI + martingale
    By noiahei in forum Ideas for expert advisors
    Replies: 1
    Last Post: 06-21-2010, 17:01
  3. (REQ) Martingale
    By 626mx in forum Ideas for expert advisors
    Replies: 2
    Last Post: 05-05-2010, 01:40
  4. ATR MA Martingale EA
    By funyoo in forum Expert advisors live statements
    Replies: 0
    Last Post: 02-19-2009, 22:40

Tags for this Thread

100, closing, coding, funyoo, martingale, profit, profitable, real, sells, stop, time, trading, trailing, trailingstep, trailingstop

Posting Permissions

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