Results 1 to 10 of 10
 0 Attachment(s)    

Thread: Pipstep change as more orders are placed

  1. #1

    Default Pipstep change as more orders are placed

    Hi Funyoo....I'd like to modify this EA to be able to control Pipstep...SH (and TP) better.
    I'd like to either be able to set SHag for each order...ie.
    SH 1 = 24 TP 1 = 28
    SH 2 = 24 TP 2 = 28
    SH 3 = 36 TP 3 = 38
    SH 4 = 36 TP 4 = 38
    SH 5 = 72 TP 5 = 76
    SH 6 = 72 TP 6 = 76
    SH 7 = 120 TP 7 = 100
    etc etc...
    Or possibly have one SH and TP for the first three orders, then another different for the next three and then another for the last three etc....
    This is Martingale style...My strategy is a small pipstep (SH) works well in a sideways market, but goes bad when market moves....and a large pipstep is safe in a sideways market but makes no money until market moves.... The EA would be much more configurable if individual orders had programmable SH and TP . Can you help??? 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]*10) > tp;
    //----
    if(M_os[ks_max][2] > 0.0)
    scs = M_os[ks_max][7] / (M_os[ks_max][2]*10) > 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 = 2.0*max_lot_b;
    //----
    while(tic == -1 && kk < 3)
    {
    tic = OrderSend(Symbol(), OP_BUY, NormalizeDouble(lotsi,2.0), 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 = 2.0*max_lot_s;
    //----
    while(tic == -1 && kk < 3)
    {
    tic = OrderSend(Symbol(), OP_SELL, NormalizeDouble(lotsi,2.0), 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
    Administrator funyoo's Avatar
    Join Date
    Sep 2008
    Posts
    7,003

    Default

    Hi BillR,

    You can set global variables (for SH and TP) and say that the SH and the TP will be incremented if the total orderprofit of the three last trades is negative.

    I suggest you to study this : Close all open trades after every x profit (GlobalVariableSet/GlobalVariableGet). And the martingale code inside my last template, for example here.

  3. #3

    Default

    Hi Funyoo....a few problems. 1st. The Global change should not be linear ie. the first step may be 10 pips but the next change should be 25 pips (or something other than 10 pips or 10 plus other previous increases). They'd like to be individually setable or setable in groups of three) 2nd. "Close all open trades" also is not linear...ie. We should profit more from the last..... a 6.40 lot size order, than the .1 first order. (Although a profit all, close all, command should work) 3rd. The syntax required for this code is currently beyond my capabilility....(trying to improve everyday, but still realistic). Would it be possible for you to modify the included EA to.... Version 1 : Trade only orders 1 through number 3 orders....Version 2 : Trade orders 4 throuh 7 and then Version 3 : Orders 8 throuh 11 etc....?? That way we can run 3 EA's at the same time with different parameters....covering all conditions, with unique SH and TP.. Thank you my friend.. BillR
    Last edited by BillR; 04-11-2009 at 01:03.

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

    Default

    Quote Originally Posted by BillR View Post
    Hi Funyoo....a few problems. 1st. The Global change should not be linear ie. the first step may be 10 pips but the next change should be 25 pips (or something other than 10 pips or 10 plus other previous increases). They'd like to be individually setable or setable in groups of three) 2nd. "Close all open trades" also is not linear...ie. We should profit more from the last..... a 6.40 lot size order, than the .1 first order. (Although a profit all, close all, command should work) 3rd. The syntax required for this code is currently beyond my capabilility....(trying to improve everyday, but still realistic). Would it be possible for you to modify the included EA to.... Version 1 : Trade only orders 1 through number 3 orders....Version 2 : Trade orders 4 throuh 7 and then Version 3 : Orders 8 throuh 11 etc....?? That way we can run 3 EA's at the same time with different parameters....covering all conditions, with unique SH and TP.. Thank you my friend.. BillR
    It would take me too many time to understand your code. It is hard to read it. And I don't have other ideas.

  5. #5

    Default

    Thanks Funyoo...Anyone else have any ideas??... Limiting an EA to only trade orders 3 to 7, or 8 through 11, should not be too hard....or possibly just checking for an already open order before executing?? Please take a minute to try. What is the correct syntax for..... if order is not order 2 through 12 then execute??

    Or possibly an

    extern int tp1 = 24;
    extern int sh1 = 24;
    extern int tp2 = 43;
    extern int sh2 = 49;
    extern int tp3 = 76;
    extern int sh3 = 84; etc ...


    if OrderTotal() Function<2;

    if(max_lot_b == 0.0)
    lotsi = 0.1;
    else
    lotsi = 2.0*max_lot_b;
    //----
    while(tic == -1 && kk < 3)
    {
    tic = OrderSend(Symbol(), OP_BUY, NormalizeDouble(lotsi,2.0), Ask, slip, Ask - (sl)*Point, Ask + (tp1 + 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;



    And then just duplicate these modules with
    if OrderTotal() Function>1<3;
    if OrderTotal() Function>2<4;
    if OrderTotal() Function>3<5;
    etc etc...
    BillR
    Last edited by BillR; 04-11-2009 at 14:03.

  6. #6
    Member
    Join Date
    Jan 2009
    Location
    Austria
    Posts
    325

    Default

    Quote Originally Posted by BillR View Post
    Hi Funyoo....I'd like to modify this EA to be able to control Pipstep...SH (and TP) better.
    I'd like to either be able to set SHag for each order...ie.
    SH 1 = 24 TP 1 = 28
    SH 2 = 24 TP 2 = 28
    SH 3 = 36 TP 3 = 38
    SH 4 = 36 TP 4 = 38
    SH 5 = 72 TP 5 = 76
    SH 6 = 72 TP 6 = 76
    SH 7 = 120 TP 7 = 100
    etc etc...
    Or possibly have one SH and TP for the first three orders, then another different for the next three and then another for the last three etc....
    This is Martingale style...My strategy is a small pipstep (SH) works well in a sideways market, but goes bad when market moves....and a large pipstep is safe in a sideways market but makes no money until market moves.... The EA would be much more configurable if individual orders had programmable SH and TP . Can you help??? Thanks Bill


    *snip*
    }
    Bill,

    personal i think there are three possible solutions available:

    1) Define the pipstep values 1-7 per currency, (magic #!) and precalc all further orders in advance and load to memory - its like a script, but not placed as pending orders! You can unload, with removal the EA from the given currency - the bad: if the market is ranging you will never hit enough orders to get high bucks - the pros: if the market is trending, you will hit maybe the last order and earn big bucks

    2) calculate the daily average per currency and multiple the pipstep#1 by 1, pipstep#2 by 1.5, pipstep#3 by 2 = you multiple the pipstep like the lot size in martingale

    if current orders = 1, next order (order #2) * pipstep#1 (*1)
    if current orders = 2, next order (order #3) * pipstep#1
    if current orders = 3, next order (order #4) * pipstep#2 (*1.5)
    and so on

    3) calculate the daily average per currency and dividide flat by 5, example:
    GBP/CHF daily average is 500 pips, pipstep is always 100 to open an new order = possible orders are 5 or dividide by max 7 orders (pipstep 70) like your trading style - this is the safiest way....you know at every time what the ea is doing

    I trade this by script and manual martingale and pending orders at alpari:
    OrderSend(Symbol(),OP_BUY,0.01,Ask,5,Ask-1500*Point,Ask+3000*Point,"",255,0,CLR_NONE);
    Sleep(2);
    OrderSend(Symbol(),OP_BUYLIMIT,0.01,Ask-1500*Point,5,Ask-3000*Point,Ask+3000*Point,"",255,0,CLR_NONE);
    Sleep(2);
    OrderSend(Symbol(),OP_BUYLIMIT,0.02,Ask-3000*Point,5,Ask-4500*Point,Ask+3000*Point,"",255,0,CLR_NONE);
    Sleep(2);
    OrderSend(Symbol(),OP_BUYLIMIT,0.04,Ask-4500*Point,5,Ask-6000*Point,Ask+4500*Point,"",255,0,CLR_NONE);
    Sleep(2);
    OrderSend(Symbol(),OP_BUYLIMIT,0.08,Ask-7500*Point,5,0,0,"",255,0,CLR_NONE);

    Daily average:
    R1 = (iHigh(NULL,PERIOD_D1,1)-iLow(NULL,PERIOD_D1,1))/Point;
    for(i=1;i<=5;i++) //1 Day
    R5 = R5 + (iHigh(NULL,PERIOD_D1,i)-iLow(NULL,PERIOD_D1,i))/Point;
    for(i=1;i<=10;i++) //5 Days
    R10 = R10 + (iHigh(NULL,PERIOD_D1,i)-iLow(NULL,PERIOD_D1,i))/Point;
    for(i=1;i<=20;i++) //10 Days
    R20 = R20 + (iHigh(NULL,PERIOD_D1,i)-iLow(NULL,PERIOD_D1,i))/Point;
    for(i=1;i<=40;i++)
    R40 = R40 + (iHigh(NULL,PERIOD_D1,i)-iLow(NULL,PERIOD_D1,i))/Point;
    for(i=1;i<=60;i++)
    R60 = R60 + (iHigh(NULL,PERIOD_D1,i)-iLow(NULL,PERIOD_D1,i))/Point;
    for(i=1;i<=80;i++)
    R80 = R80 + (iHigh(NULL,PERIOD_D1,i)-iLow(NULL,PERIOD_D1,i))/Point;
    for(i=1;i<=100;i++)
    R100 = R100 + (iHigh(NULL,PERIOD_D1,i)-iLow(NULL,PERIOD_D1,i))/Point;
    for(i=1;i<=120;i++)
    R120 = R120 + (iHigh(NULL,PERIOD_D1,i)-iLow(NULL,PERIOD_D1,i))/Point;
    for(i=1;i<=240;i++)
    R240 = R240 + (iHigh(NULL,PERIOD_D1,i)-iLow(NULL,PERIOD_D1,i))/Point;

    R5 = R5/5;
    R10 = R10/10;
    R20 = R20/20;
    R40 = R40/40;
    R60 = R60/60;
    R80 = R80/80;
    R100 = R100/100;
    R120 = R120/120;
    R240 = R240/240;

    RAvg = (R1+R5+R10+R20+R40+R60+R80+R100+R120+R240)/10;


    Now you have the daily average from 1 day to 240 days back

    IX

  7. #7

    Default

    Hi IX, I like #1.This option gives complete control. The strategy is that when the market first opens, or when it does not trend, the currency value goes up and down many times but only 20 or 30 pips........The fixed pip step does not make any trades as the usual 2nd martingale trade would be at 40 to 50 pips...so we're not making any profit. So for this a pip step of 20 would make profit. But you wouldn't want to have your extern int for all pip step to be 20 as account blow out would happen very quickly when the market did start to move. So then the "intermediate" pip step of 40 to 50 would apply for the next 3 or four progressive orders.....The original small pipstep orders would not be too large of a burden on margin because they are very small lot sizes..... This "intermediate" would be the normal Martingale trading we use for profit....This is regular operation. Next...the biggest level of pip step should be the final or very long legged one of say 100 to 200 pips so at this point the market is really going too far and you don't want trades every 40 or 50 pips because account blow out would happen fast. In the past I've seen strategies that modify the lot exponent multiplier as orders accumulated but none that modified pip step... When trading maually I find pip step size to be valueable to preserve the account and make good profit. My attempts to code it have not been successful as all I get are errors or non working EA when/after compiling.... I find simple to be much better. I also very much like the pending orders on #3 idea. This way would be hard coded but must also have the EA close the unfullfilled limit orders when closing the open orders at the profit. I wonder if Funyoos Martingale could be more easily modified...BillR
    Last edited by BillR; 04-13-2009 at 21:21.

  8. #8
    Member
    Join Date
    Jan 2009
    Location
    Austria
    Posts
    325

    Default

    Quote Originally Posted by BillR View Post
    Hi IX, I like #1.This option gives complete control. The strategy is that when the market first opens, or when it does not trend, the currency value goes up and down many times but only 20 or 30 pips........The fixed pip step does not make any trades as the usual 2nd martingale trade would be at 40 to 50 pips...so we're not making any profit. So for this a pip step of 20 would make profit. But you wouldn't want to have your extern int for all pip step to be 20 as account blow out would happen very quickly when the market did start to move. So then the "intermediate" pip step of 40 to 50 would apply for the next 3 or four progressive orders.....The original small pipstep orders would not be too large of a burden on margin because they are very small lot sizes..... This "intermediate" would be the normal Martingale trading we use for profit....This is regular operation. Next...the biggest level of pip step should be the final or very long legged one of say 100 to 200 pips so at this point the market is really going too far and you don't want trades every 40 or 50 pips because account blow out would happen fast. In the past I've seen strategies that modify the lot exponent multiplier as orders accumulated but none that modified pip step... When trading maually I find pip step size to be valueable to preserve the account and make good profit. My attempts to code it have not been successful as all I get are errors or non working EA when/after compiling.... I find simple to be much better. I also very much like the pending orders on #3 idea. This way would be hard coded but must also have the EA close the unfullfilled limit orders when closing the open orders at the profit. I wonder if Funyoos Martingale could be more easily modified...BillR
    Bill, personal i like #3 too, its my favorit, why..

    if you have opened orders 1+2 (2 was pending) and you are not sure whats going on, simply delete the pending order #3 and go further with pending order #4 (doubled pipstep) - this the ultimate control in martingale. an ea is always doing, the one and only human intervention is to remove the ea from the currency pair and reload the terminal to clear the memory!

    you can cascade your scripts too, if the first script is a winner, the first code line of the second script is to delete all existing pendings and sets new one, you know, scripts are executed always on the active currency chart

    further i think, you must stay always at the desk to observe the ea what he is doing, so you can trade manual too. thats the reason why i trade solution 3. and yes i have a sucessful automatic operating martingale system ready, which you can leave alone 24x5, but only with an account equity from 25.000 bucks upwards. this is currently no solution for me, because i trade options and futures too, with 25k bucks in options, i can earn more money as with my forex ea system in one month. so the risk/reward ratio is 3:1 at forex and 1:3 at options currently, if i can optimize my ea to use/work with a max starting account balance of 10k, yes i will trade the ea, the ratio forex/options is now 1:1

    - you see, you must always calc your risk/reward ratio at forex to other markets too -

    IX

    PS: currently i trade crude oil options :-)

  9. #9

    Default

    Thanks IX...Good Luck on Oil Ops... speculators here in the States are being blamed for the recent High energy prices and subsequent economic crisis...Government is watching....
    On your Script/and Limit orders...I added them to my martingale EA and it creates orders fine (did get some error 4110)...But closing ALL open Buys or Sells at Profit did not happen....When one would close at a profit duplicate limit orders are created. I tried using the EA's existing close orders code, but it did not recognize the limit orders should be closed also. I also tried to modify the code from this thread, but no success.
    http://www.tradingsystemforex.com/mq...equence-2.html
    Could a simple Orderdelete line be added???
    if (OrdersTotal is not X then OrderDelete)
    I don't know the proper syntax to code this line...I've been trying to learn on MQL4.com but can not get it to work or work on only Buys or Sells... any ideas??
    #define m 20050611
    //----
    extern int sl=550;
    extern int tp = 47;
    extern int sh = 63;

    //----
    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]*10) > tp;
    //----
    if(M_os[ks_max][2] > 0.0)
    scs = M_os[ks_max][7] / (M_os[ks_max][2]*10) > 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)
    {

    //----
    while(tic == -1 && kk < 3)
    {
    tic = OrderSend(Symbol(),OP_BUY,0.1, Ask,5,Ask-150*Point,Ask+300*Point,"",255,0,CLR_NONE);
    Sleep(2);
    OrderSend(Symbol(),OP_BUYLIMIT,0.1, Ask-150*Point,5,Ask-300*Point,Ask+300*Point,"",255,0,CLR_NONE);
    Sleep(2);
    OrderSend(Symbol(),OP_BUYLIMIT,0.2, Ask-300*Point,5,Ask-450*Point,Ask+300*Point,"",255,0,CLR_NONE);
    Sleep(2);
    OrderSend(Symbol(),OP_BUYLIMIT,0.4, Ask-450*Point,5,Ask-600*Point,Ask+450*Point,"",255,0,CLR_NONE);
    Sleep(2);
    OrderSend(Symbol(),OP_BUYLIMIT,0.8, Ask-750*Point,5,0,0,"",255,0,CLR_NONE);
    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)
    {

    //----
    while(tic == -1 && kk < 3)
    {

    tic = OrderSend(Symbol(),OP_SELL,0.1, Bid,5,Bid+150*Point,Bid-300*Point,"",255,0,CLR_NONE);
    Sleep(2);
    OrderSend(Symbol(),OP_SELLLIMIT,0.1, Bid+150*Point,5,Bid+300*Point,Bid-300*Point,"",255,0,CLR_NONE);
    Sleep(2);
    OrderSend(Symbol(),OP_SELLLIMIT,0.2, Bid+300*Point,5,Bid+450*Point,Bid-300*Point,"",255,0,CLR_NONE);
    Sleep(2);
    OrderSend(Symbol(),OP_SELLLIMIT,0.4, Bid+450*Point,5,Bid-600*Point,Bid-450*Point,"",255,0,CLR_NONE);
    Sleep(2);
    OrderSend(Symbol(),OP_SELLLIMIT,0.8, Bid-750*Point,5,0,0,"",255,0,CLR_NONE);
    Print("tic_sell=", tic);
    //----
    if(tic == -1)
    {
    gle = GetLastError();
    kk++;
    Print(" ", gle, " sell ", kk);
    Sleep(6000);
    RefreshRates();
    }
    }
    lastt = CurTime();
    return;
    }
    }
    Last edited by BillR; 04-14-2009 at 03:40.

  10. #10
    Member
    Join Date
    Jan 2009
    Location
    Austria
    Posts
    325

    Default

    Quote Originally Posted by BillR View Post
    Thanks IX...Good Luck on Oil Ops... speculators here in the States are being blamed for the recent High energy prices and subsequent economic crisis...Government is watching....
    On your Script/and Limit orders...I added them to my martingale EA and it creates orders fine (did get some error 4110)...But closing ALL open Buys or Sells at Profit did not happen....When one would close at a profit duplicate limit orders are created. I tried using the EA's existing close orders code, but it did not recognize the limit orders should be closed also. I also tried to modify the code from this thread, but no success.
    http://www.tradingsystemforex.com/mq...equence-2.html
    Could a simple Orderdelete line be added???
    if (OrdersTotal is not X then OrderDelete)
    I don't know the proper syntax to code this line...I've been trying to learn on MQL4.com but can not get it to work or work on only Buys or Sells... any ideas??
    #define m 20050611
    //----
    extern int sl=550;
    extern int tp = 47;
    extern int sh = 63;

    //----
    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]*10) > tp;
    //----
    if(M_os[ks_max][2] > 0.0)
    scs = M_os[ks_max][7] / (M_os[ks_max][2]*10) > 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)
    {

    //----
    while(tic == -1 && kk < 3)
    {
    tic = OrderSend(Symbol(),OP_BUY,0.1, Ask,5,Ask-150*Point,Ask+300*Point,"",255,0,CLR_NONE);
    Sleep(2);
    OrderSend(Symbol(),OP_BUYLIMIT,0.1, Ask-150*Point,5,Ask-300*Point,Ask+300*Point,"",255,0,CLR_NONE);
    Sleep(2);
    OrderSend(Symbol(),OP_BUYLIMIT,0.2, Ask-300*Point,5,Ask-450*Point,Ask+300*Point,"",255,0,CLR_NONE);
    Sleep(2);
    OrderSend(Symbol(),OP_BUYLIMIT,0.4, Ask-450*Point,5,Ask-600*Point,Ask+450*Point,"",255,0,CLR_NONE);
    Sleep(2);
    OrderSend(Symbol(),OP_BUYLIMIT,0.8, Ask-750*Point,5,0,0,"",255,0,CLR_NONE);
    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)
    {

    //----
    while(tic == -1 && kk < 3)
    {

    tic = OrderSend(Symbol(),OP_SELL,0.1, Bid,5,Bid+150*Point,Bid-300*Point,"",255,0,CLR_NONE);
    Sleep(2);
    OrderSend(Symbol(),OP_SELLLIMIT,0.1, Bid+150*Point,5,Bid+300*Point,Bid-300*Point,"",255,0,CLR_NONE);
    Sleep(2);
    OrderSend(Symbol(),OP_SELLLIMIT,0.2, Bid+300*Point,5,Bid+450*Point,Bid-300*Point,"",255,0,CLR_NONE);
    Sleep(2);
    OrderSend(Symbol(),OP_SELLLIMIT,0.4, Bid+450*Point,5,Bid-600*Point,Bid-450*Point,"",255,0,CLR_NONE);
    Sleep(2);
    OrderSend(Symbol(),OP_SELLLIMIT,0.8, Bid-750*Point,5,0,0,"",255,0,CLR_NONE);
    Print("tic_sell=", tic);
    //----
    if(tic == -1)
    {
    gle = GetLastError();
    kk++;
    Print(" ", gle, " sell ", kk);
    Sleep(6000);
    RefreshRates();
    }
    }
    lastt = CurTime();
    return;
    }
    }
    Bill, thx for wishes, well oil prices are to low for the upcoming holiday season on july and august, i think we'll reach usd 60-65 per barrel at the end of june. so i have 2.5k call options for 48usd each (6month)

    to your code, is your statement the complete code?, if not i can give you only some examples, you can send me private if necessary.

    if you trade only one currency pair you can use this example - combined with a tp reached, if you trade more pairs, you have to select by Symbol() first:

    int start()
    { //or call as a separate function like CloseAllOrdersIfTPReached
    int total = OrdersTotal();
    for(int i=total-1;i>=0;i--)
    {
    OrderSelect(i, SELECT_BY_POS);
    int type = OrderType();

    bool result = false;

    switch(type)
    {
    //Close pending orders
    case OP_BUYLIMIT :
    case OP_BUYSTOP :
    case OP_SELLLIMIT :
    case OP_SELLSTOP : result = OrderDelete( OrderTicket() );
    }

    if(result == false)
    {
    Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
    Sleep(3000);
    }
    }

    return(0);
    }

    IX

Similar Threads

  1. New tool to open orders with 1 click and change order TP/SL dragging lines.
    By landofcash in forum Commercial manual systems
    Replies: 1
    Last Post: 10-24-2013, 10:08
  2. Multiple position ea--pipstep
    By fxrock in forum MQL programming
    Replies: 0
    Last Post: 10-03-2012, 15:39
  3. Change direction EA
    By daniel22 in forum Ideas for expert advisors
    Replies: 0
    Last Post: 09-14-2011, 09:44
  4. Problem with Pipstep in Countertrend
    By Daldrar in forum MQL programming
    Replies: 0
    Last Post: 05-26-2010, 22:12
  5. How to add pipstep or pip distance between orders
    By tamaraofx in forum MQL programming
    Replies: 3
    Last Post: 05-22-2010, 17:04

Tags for this Thread

100, alpari, automatic, average, change, closing, color, forex, forex ea, funyoo, high, learn, magic, manual, martingale, money, mql, pips, profit, programming, safe, scripts, sells, simple, strategies, strategy, study, system, thank you, time, trading, trailingstep, trailingstop, trend, usd

Posting Permissions

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