How to account for weekend time?

 

I need some help to account for weekend time. I am writing a script that keeps track of time trades are open. However, no matter what I can't figure out the code to make it subtract the 'weekend time' from the total time that trades are open.

Help! Any ideas?

Here is the code I am working with:

for(j=OpenBar-1;j>=CloseBar;j--)
            {
            if(TimeDayOfWeek(j+5)==5 && TimeDayOfWeek(j)==1)                      // If friday = 5 and Monday = 1, then account for weekened time
               {
               DuringTradeWeekendTime=DuringTradeWeekendTime+(24+24+2)*3600;      // Weekend occurred when trade was open
               }
            }

ActiveTradeTimeArray[TotalOrdersHistory-1]=1.*(OrderCloseTime()-OrderOpenTime()-DuringTradeWeekendTime);///60.;    // Time in minutes for which the trade was active
 
Use Bar[], or iBars() instead and then you wouldn't have to worry about weekend.
 
outofdebt:

I need some help to account for weekend time. I am writing a script that keeps track of time trades are open. However, no matter what I can't figure out the code to make it subtract the 'weekend time' from the total time that trades are open.

Help! Any ideas?

Here is the code I am working with:

Try it code.

extern int     TimeStartModay = 0;       //Time to start trade moday
extern int     TimeStopFriday = 23;      //Time to stop trade friday
bool RunIndicators;
int start()
{
RunIndicators=true;
if(((DayOfWeek()==1)&&(TimeHour(TimeCurrent())>=TimeStartModay))||((DayOfWeek()==5)&&(TimeHour(TimeCurrent())<=TimeStopFriday))) RunIndicators=false;

.......


return(0);
}

 
outofdebt:
I need some help to account for weekend time. I am writing a script that keeps track of time trades are open.
    for(int iPos = OrdersTotal()-1; iPos >= 0 ; iPos--) if (
        OrderSelect(iPos, SELECT_BY_POS)                    // Only my orders w/
    &&  OrderMagicNumber()  == Magic.Number                 // my magic number
    &&  OrderSymbol()       == chart.symbol                 // and my pair.
    ){
        datetime OOT    = OrderOpenTime();
        int duration    = TimeCurrent() - OOT,
            hours       = duration / 3600,
            mins        = duration % 3600 / 60;
        Print("Order ", OrderTicket(), "open for ", hours, ":", mins, "total");

        int iBar        = iBarShift(NULL,0, OOT),
            secPer      = Period() * 60;
        duration        = (TimeCurrent() - Time[0]) // Time current bar.
                        - (OOT - Time[iBar])        // Before open, open bar.
                        + iBar * secPer             // Completed bars.
        hours           = duration / 3600,
        mins            = duration % 3600 / 60;
        Print("Order ", OrderTicket(), "open for ", hours, ":", mins,
                                            "excluding weekends and holidays");
        break;
    }
 
WHRoeder:

Thanks, I am using this script on orders generated in the strategy tester. How would I go about modifying your code to work in that situation?
 

ok, i figured it out. The variables I was using were referencing the the 1 min timeframe because they are used in another calculation. I created separate vars for the

open and close bar that use the timeframe of the chart that is being backtested, and it picks up the correct weekend time and subtracts from total time as needed.

Reason: