A new order allowed not earlier than 10 bars after the last order close?

 

I’m not very fluent in programming. Does anybody know how to write the module allowing for the new order not earlier than 10 bars after the last order close? I tried to find it but all I found it was one trade per bar and it is not I want. I appreciate any help.

Thanks a lot!

 

you need to get the close time of the last order from the history then find out how many bars ago it closed.

look at OrderSelect() using MODE_HISTORY

and look at iBarShift()

 

Ok, I got a little carried away :), Anyways here's an example code below.

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
extern int iAllowBar=10;    //Past Bars Since Last Close
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int     iMagic=7;           //System (Lucky?) Unique Magic#
double  iAns;               //Used as Returned Value in Functions
double  iProfit;            //Value of Selected Order(s) Profit
double  iOrSize;            //Value of Selected Order(s) Lot-Size
int     iTicket;            //Value of Currently Selected Ticket
int     iOpTime;            //Value of Selected Order Open-Time
int     iClTime;            //Value of Selected Order Close-Time
double  iOpPrice;           //Value of Selected Order Open-Price
double  iClPrice;           //Value of Selected Order Close-Price
string  iSymbol_;           //Value of Currently Selected Symbol
int     i;                  //First Counter for Loops
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/*
Note: Some Of the Code Structure are Variants of My Current Format.
The below was generated for testing Bars Passed Before New Orders Allowed.
Its Not Intended for Live Trading and Not Reliable for Platform Shut-Downs.

Code-Logic: 
Uses Global Static Variables to Store Previously Searched Order Info.
Uses Static Bools if Trade Allowed but will Fail during a Shut-Down.
To Make the Static Bools More Reliable, Consider Saving in Terminal Globals.
The Orders are Placed Randomly and Closed When Profit is Greater than 0.
Only One Order Is Allowed, Turns Off Ordering After a Successful Order.
At Successful Closing Of An Order, It Starts Looking if X-Bars Have Passed.
It Only Checks Once-Per New Bar Using the TimeStamp Variable.
If The Time of X-Bars Ago is Greater Than OrderClose Time Then Allow Trading.
*/
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void start(){
    //~~~~~~~~~~
    iSymbol_=Symbol();
    static bool Allow_New_Order=true;
    static bool Last_Order_Closed=false;
    static datetime Time_Stamp;
    //~~~~~~~~~~Check Time Limit
    if(Last_Order_Closed){
        if(iTime(iSymbol_,0,0)!=Time_Stamp){
            if(Order_History(iMagic,"Set_Last_History_Order_wSymbol_wMagic")==true){
                Print("Set_Last_History_Order_wSymbol_wMagic Select___Ok!");
                //~~~~~~~~~~
                if(iTime(iSymbol_,0,iAllowBar)>iClTime){
                    Allow_New_Order=true;
                    Print(iAllowBar+" Bars Have Passed Since Last Close, New Orders Allowed");
                }else{
                    //Allow_New_Order=false;
                    Print(iAllowBar+" Bars Not Passed Since Last Close, Trade Status UnChanged");
                }
            //~~~~~~~~~~
            }else{
                Print("Set_Last_History_Order_wSymbol_wMagic Select___Fail!");
                Print("Last Error="+GetLastError());
            }
            //~~~~~~~~~~
            Time_Stamp=iTime(iSymbol_,0,0);
        }
    }
    //~~~~~~~~~~Open New Order:
    if(Allow_New_Order){
        MathSrand(TimeLocal());     int Com=MathRand()%2+1;
        if(Com==1){
            iAns=OrderSend(iSymbol_,OP_BUY,1,Ask,MarketInfo(iSymbol_,//<--4/5 Digit Preferred Here
            MODE_SPREAD),0,0,DoubleToStr(iMagic,0),iMagic,0,Blue);
            if(iAns>0){Allow_New_Order=false; Last_Order_Closed=false;}else{
                Print("OrderSend for Long Position___Fail!__ErrorGenerated="+GetLastError());
            }
        }
        if(Com==2){
            iAns=OrderSend(iSymbol_,OP_SELL,1,Bid,MarketInfo(iSymbol_,
            MODE_SPREAD),0,0,DoubleToStr(iMagic,0),iMagic,0,Red);
            if(iAns>0){Allow_New_Order=false; Last_Order_Closed=false;}else{
                Print("OrderSend for Short Position___Fail!__ErrorGenerated="+GetLastError());
            }
        }
    }
    //~~~~~~~~~~Close Profit Order:
    if(Order_Manage(iMagic,"Set_Last_Active_Order_wSymbol_wMagic")==true){
        if(iProfit>0){
            if(Order_Manage(iMagic,"Close_Active_Ticket_Order_xSymbol_xMagic")==true){
                Last_Order_Closed=true;
                Print("Last_Order_Closed Successfully Wait "+iAllowBar+" Bars");
            }
        }
    }
    //~~~~~~~~~~
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~Order_Manage-Function
double Order_Manage(int Magic, string Command){
//~~~~~~~~~~
    int Switch, Cnt=0;
    iAns=0; iProfit=0; iOrSize=0;
    //~~~~~~~~~~
    for(i=1;i>0;i--){
        if(Command=="Set_Last_Active_Order_wSymbol_wMagic"){Switch=0;  break;}
        if(Command=="Close_Active_Ticket_Order_xSymbol_xMagic"){Switch=-1;break;}
    }
    //~~~~~~~~~~
    for(i=OrdersTotal()-1;i>=0;i--){
        if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
            //~~~~~~~~~~
            switch(Switch){
                case 0: if(Switch==0){//Set_Last_Active_Order_wSymbol_wMagic
                            if(OrderSymbol()==iSymbol_
                            && OrderMagicNumber()==Magic
                            ){
                                iTicket=OrderTicket();
                                iOpTime=OrderOpenTime();
                                iClTime=OrderCloseTime();
                                iOpPrice=OrderOpenPrice();
                                iClPrice=OrderClosePrice();
                                iProfit=OrderProfit();
                                iOrSize=OrderLots();
                                iAns=true; return(iAns);
                            }
                        }
                case -1:if(Switch==-1){//Close_Active_Ticket_Order_xSymbol_xMagic
                            if(OrderType()<2
                            && OrderTicket()==iTicket
                            ){
                                //<--Check FreezeLevel Here
                                iAns=OrderClose(OrderTicket(),
                                OrderLots(),OrderClosePrice(),
                                MarketInfo(iSymbol_,MODE_SPREAD),White);
                                if(iAns==false){
                                    Print("OrderClose_Fail!_ErrorGenerated="+GetLastError());}
                                else{
                                    return(iAns);
                                }
                            }
                        }
            //~~~~~~~~~~
            }
        }
    }
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~Order_History-Function
double Order_History(int Magic, string Command){
//~~~~~~~~~~
    int Switch, Cnt=0;
    iAns=0; iProfit=0; iOrSize=0;
    //~~~~~~~~~~
    for(i=1;i>0;i--){
        if(Command=="Set_Last_History_Order_wSymbol_wMagic"){Switch=0;  break;}
    }
    //~~~~~~~~~~
    for(i=OrdersHistoryTotal()-1;i>=0;i--){
        if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)){
            //~~~~~~~~~~
            switch(Switch){
                case 0: if(Switch==0){//Set_Last_History_Order_wSymbol_wMagic
                            if(OrderSymbol()==iSymbol_
                            && OrderMagicNumber()==Magic
                            ){
                                iTicket=OrderTicket();
                                iOpTime=OrderOpenTime();
                                iClTime=OrderCloseTime();
                                iOpPrice=OrderOpenPrice();
                                iClPrice=OrderClosePrice();
                                iProfit=OrderProfit();
                                iOrSize=OrderLots();
                                iAns=true; return(iAns);
                            }
                        }
            //~~~~~~~~~~
            }
        }
    }
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In case you're wondering, it only made it from Jan-1-2010 to Jan-13-2010 before it crashed.

 
ubzen:

Ok, I got a little carried away :), Anyways here's an example code below.

In case you're wondering, it only made it from Jan-1-2010 to Jan-13-2010 before it crashed.



thank you very much ;)
 

this code is very helpfull but still its not so easy to rewrite this concept to my Ea ;)

any way i realy appreciate your help ubzen

thank you ;)

 
You're welcome. Yeah there's allot of stuff there. Just take em one command at a time. If you need assistance, just ask within this thread and I'll clarify.
 
bukkiper:

I’m not very fluent in programming. Does anybody know how to write the module allowing for the new order not earlier than 10 bars after the last order close? I tried to find it but all I found it was one trade per bar and it is not I want.

    datetime lastClose;
    for(int iPos=0; iPos < OrdersHistoryTotal(); iPos++) if (
        OrderSelect(iPos, SELECT_BY_POS, MODE_HISTORY)  // Only orders w/
    &&  OrderCloseTime()    > lastClose                 // not yet processed,
    &&  OrderMagicNumber()  == magic.number             // my magic number
    &&  OrderSymbol()       == Symbol()                 // and my pair.
    &&  OrderType()         <= OP_SELL // Avoid cr/bal https://www.mql5.com/en/forum/126192
    ){
        lastClose = OrderCloseTime();
    }
    bool allowedToTrade = lastClose + 10 * Period() < Time[0];
 
SDC:

you need to get the close time of the last order from the history then find out how many bars ago it closed.

look at OrderSelect() using MODE_HISTORY

and look at iBarShift()


sdc thank you
 
WHRoeder:


it look so simply but to be honest .... i dont now in which part of my EA should i paste your module :(

id pasted it above logic part but it dont make any diference ;(

it make me to feel so stupid :(

WHRoeder thank you

 
bukkiper:


it look so simply but to be honest .... i dont now in which part of my EA should i paste your module :(

id pasted it above logic part but it dont make any diference ;(

it make me to feel so stupid :(

WHRoeder thank you

You have to use allowedToTrade to govern if your trade can be placed . . . it's a bool (true or false).

if (allowedToTrade) place trade . .

 
RaptorUK:

You have to use allowedToTrade to govern if your trade can be placed . . . it's a bool (true or false).

if (allowedToTrade) place trade . .

Reason: