Problems checking for open trade - page 2

 
dazamate:


int start(){
   static datetime Time0;
   if (Time0 == Time[0]) return; Time0 = Time[0];
   // A new bar has started.

WHRoeder, This code looks so simple but I cant work out in my head how it works, won't the if statement always be true if it makes time0 equal to time[0] every time time0 is equal to time[0]? Not saying it doesn't work but I just don't understand how it works structured like that.

1st tick: Time0 does not equal Time[0] so return is not executed, Time0 is set to Time[0] (start time of the current bar) the rest of the start function is executed.

2nd and subsequent ticks: if Time0 still equals the start time of the current bar then we are still on the same bar, return is executed and the start function is exited. If Time0 does not equal the start time of the current bar then we are on a new bar, return is not executed, Time0 is set to Time[0] (start time of the new current bar) the rest of the start function is executed.

 
dazamate:

How would I go about making a counter where if a pending order is place and it is not triggered with in x amount of bars then it is to be canceled ? All I can come up with is put a counter to count every time a new bar forms and if the amount of bars counted == the specified allowed bars before pending orders are canceled. The bar counter is reset everytime a new pending order is opened? How does that sound?

I would create a function that checks for pending orders, it looks to see when each order was opened, checks how long it has been and if longer than the time limit it closes the order.

 
dazamate:

I want to make a function that scans eurusd, usdchf, gbpusd, usdjpy on 1hr tf. It goes back to the last 06:00 gmt candle records the open, goes back another 24 bars to the prev 06:00 gmt candle records that open to and records the 6gmt -6gmt range for each pair. Then compares the 6gmt - 6gmt range from all pairs and returns the one which is the highest. Is it possible for an ea to do that from being attached to 1 time chart?

With all problems like this before you think about code you have to figure out your solution first . . IMO. Create a flow chart or write some pseudo code . . . get a solution that makes sense first . . then look at the code.

To answer you last question, yes it is possible to do that, for example, to get the open value for a particular candle from a different pair to the one the EA is on I would use . . .

iOpen( string symbol, int timeframe, int shift)

once you have your values for the rage of the 4 pairs you can easily determine the largest using something like this:

  MathMax( double value4, (MathMax( double value3, (MathMax( double value1, double value2))))) ;

. . . or you could put the values into an array and use ArraySort . . . there is usually more than one way to do anything, first you have to have a plan of how you are going to solve your problem.

EDIT: bear in mind some of this stuff will not work correctly with the Strategy tester.

 
extern int     iOpenHour                = 6;

int start()
  {
  
int b, scannedhour;
datetime bartime;
double dibsclose, dibsopen, prevdayrange;

for(b=0; b<=24; b++)                                   // scans the last 24 bars 
   {
      bartime = iTime("EURUSD", 60, b);                // checks the open time of each bar
      scannedhour = TimeHour(bartime);                 // extracts the hour of that bar
    
      if ( scannedhour == iOpenHour )                  // Check to see if scanned hour = Dibs hour
         {
            dibsclose    = iOpen("EURUSD", 60, b);     // Get the open value of that bar (Close of 6gmt day) 
            dibsopen     = iOpen("EURUSD", 60, b-24);  // Get the value of the bar 24 bars before the Dibs bar (Open of 6gmt day)
            prevdayrange = (dibsclose-dibsopen);       // Calculate the range of the Dibs day
         }                                             // End of if statement
    }                                                  // End of for statement


Comment("Prev Dibs day range is...",prevdayrange, "dibsclose  =  ", dibsclose, "dibsopen  =  ",dibsopen, "Scannedhour=",scannedhour);      


   return(0);
  }

Alright this is my own attempt to write some code that will scan the eurusd 1hr chart till it finds the last 06:00 bar, records it's open, goes back another 24 bars to record the open of that bar (start of 06:00 day) and gets open-open range from them 2 selected bars.

Surprise surprise it doesn't work . Lol ------> http://myfacewhen.com/307/

Tell me the what I have f*'d up. Or have I just got about this the wrong way? I tried hehe

 

Spotted one issue . . . 24 hours before b is b+24 . . bars count up from current bar ( 0 ) to the left

dibsopen     = iOpen("EURUSD", 60, b-24);  // Get the value of the bar 24 bars before the Dibs bar (Open of 6gmt day)

change to:

dibsopen     = iOpen("EURUSD", 60, b+24);  // Get the value of the bar 24 bars before the Dibs bar (Open of 6gmt day)

Well done for adding the comments, very good practice to get into . . . :-)

 
RaptorUK:

Spotted one issue . . . 24 hours before b is b+24 . . bars count up from current bar ( 0 ) to the left

Well done for adding the comments, very good practice to get into . . . :-)


Wow well spotted that would of taken me a while to figure that out lol. I can't believe I got all that right - that silly mistake. I feel like I am getting somewhere now . Yeah I added the comments to help me keep track of what I am doing and to make is super easy for you guys to see what I am trying to do. Now I am going to get it to compare the 4 pairs and spit out the one with the highest value Ill let you know how I go with it . Thanks RaptorUK
 
Good stuff, well done. You are welcome :-)
 
extern int     iOpenHour                = 6;

int start()
  {
                                                            //  Array Categorys ( 1  EURUSD, 2 GBPUSD, 3 USDCHF, 4 USDJPY )
int b[4], scannedhour[4];
datetime bartime[4];
double dibsclose[4], dibsopen[4], prevdayrange[4];

//----------------------------------------------------------------------------------------------
//        EURUSD PREV DAY RANGE CALC

for(b[1]=0; b[1]<=24; b[1]++)                               // scans the last 24 bars on eurusd
   {
      bartime[1] = iTime("EURUSD", 60, b[1]);               // checks the open time of each bar
      scannedhour[1] = TimeHour(bartime[1]);                // extracts the hour of that bar
    
      if ( scannedhour[1] == iOpenHour )                    // Check to see if scanned hour = Dibs hour
         {
            dibsclose[1]    = iOpen("EURUSD", 60, b[1]);    // Get the open value of that bar (Close of 6gmt day) 
            dibsopen[1]     = iOpen("EURUSD", 60, b[1]+24); // Get the value of the bar 24 bars before the Dibs bar (Open of 6gmt day)
            prevdayrange[1] = (dibsclose[1]-dibsopen[1]);   // Calculate the range of the Dibs day
         }                                                  // End of if statement
    }                                                       // End of for statement
    

//----------------------------------------------------------------------------------------------
//        GBPUSD PREV DAY RANGE CALC

for(b[2]=0; b[2]<=24; b[2]++)                               // scans the last 24 bars on eurusd
   {
      bartime[2] = iTime("GBPUSD", 60, b[2]);               // checks the open time of each bar
      scannedhour[2] = TimeHour(bartime[2]);                // extracts the hour of that bar
    
      if ( scannedhour[2] == iOpenHour )                    // Check to see if scanned hour = Dibs hour
         {
            dibsclose[2]    = iOpen("GBPUSD", 60, b[2]);    // Get the open value of that bar (Close of 6gmt day) 
            dibsopen[2]     = iOpen("GBPUSD", 60, b[2]+24); // Get the value of the bar 24 bars before the Dibs bar (Open of 6gmt day)
            prevdayrange[2] = (dibsclose[2]-dibsopen[2]);   // Calculate the range of the Dibs day
         }                                                  // End of if statement
    }                                                       // End of for statement



//----------------------------------------------------------------------------------------------
//        USDCHF PREV DAY RANGE CALC

for(b[3]=0; b[3]<=24; b[3]++)                               // scans the last 24 bars on eurusd
   {
      bartime[3] = iTime("EURUSD", 60, b[3]);               // checks the open time of each bar
      scannedhour[3] = TimeHour(bartime[3]);                // extracts the hour of that bar
    
      if ( scannedhour[3] == iOpenHour )                    // Check to see if scanned hour = Dibs hour
         {
            dibsclose[3]    = iOpen("USDCHF", 60, b[3]);    // Get the open value of that bar (Close of 6gmt day) 
            dibsopen[3]     = iOpen("USDCHF", 60, b[3]+24); // Get the value of the bar 24 bars before the Dibs bar (Open of 6gmt day)
            prevdayrange[3] = (dibsclose[3]-dibsopen[3]);   // Calculate the range of the Dibs day
         }                                                  // End of if statement
    }                                                       // End of for statement


//----------------------------------------------------------------------------------------------
//        USDJPY PREV DAY RANGE CALC

for(b[4]=0; b[4]<=24; b[4]++)                               // scans the last 24 bars on eurusd
   {
      bartime[4] = iTime("USDJPY", 60, b[4]);               // checks the open time of each bar
      scannedhour[4] = TimeHour(bartime[4]);                // extracts the hour of that bar
    
      if ( scannedhour[4] == iOpenHour )                    // Check to see if scanned hour = Dibs hour
         {
            dibsclose[4]    = iOpen("USDJPY", 60, b[4]);    // Get the open value of that bar (Close of 6gmt day) 
            dibsopen[4]     = iOpen("USDJPY", 60, b[4]+24); // Get the value of the bar 24 bars before the Dibs bar (Open of 6gmt day)
            prevdayrange[4] = (dibsclose[4]-dibsopen[4]);   // Calculate the range of the Dibs day
         }                                                  // End of if statement
    }                                                       // End of for statement




//----------------------------------------------------------------------------------------------



Comment("DIBS RANGE EURUSD:...", prevdayrange[1],        //Display Ranges of each pair
        "DIBS RANGE GBPUSD:...", prevdayrange[2],
        "DIBS RANGE USDCHF:...", prevdayrange[3],
        "DIBS RANGE USDJPY:...", prevdayrange[4]);      


   return(0);
  }
Alright so I turned all the variables into arrays and run the checks for each one. Now it has stopped working. Is not possible to have multiple for loops like this?
 
dazamate:
Alright so I turned all the variables into arrays and run the checks for each one. Now it has stopped working. Is not possible to have multiple for loops like this?
Yes you can, no problem, but don't use an array in the for loop index, you can use b each time if you like. It is reset to 0 for each of the for loops so re-using it is not a problem. what do you actually NEED to use an array for ? isn't it just for the range values ?
 
RaptorUK:
Yes you can, no problem, but don't use an array in the for loop index, you can use b each time if you like. It is reset to 0 for each of the for loops so re-using it is not a problem. what do you actually NEED to use an array for ? isn't it just for the range values ?
Ahhh yes I see what you are saying, Ill fix it up and see how I go. Fast responses man I will pay you back some $$$ if this ea makes me rich lol