Execute Function when OrdersTotal() = N

Post Reply
desmondc
Posts: 7
Joined: Wed Mar 05, 2014 2:53 am

Execute Function when OrdersTotal() = N

Post by desmondc »

I know what I am want to do and I know that I am almost there, but I cant see what I am missing. Please help.. been at this for hours. It must be something simple .. must be!! Argh~!!

Objective:
1) Have the function only execute at ever new bar not every tick to reduce CPU logic
2) Close all position when all active open order is at n count (e.g. OrdersTotal()=3)

Problem:
When adding if ( varOrderCount = 3), i notice that my function funcCloseAll() keeps getting executed at every bar. What i expect is that it would execute every 3rd bar.


Code without Condition

Code: Select all

   
if(iVolume(NULL,0,0)>1) OpenBar = false;      //To Verify that every hour an action is taken.
      if (OpenBar) {  
         int varOrderCount = OrdersTotal();
         if ( varOrderCount = 3)
         {
            //funcCloseAll();                                      //Close all position but do not reset balance and target value.
         }                 
         funcInitiateInitiate();                                  //Initiate Trade
      }

FailedCodeSnip

Code: Select all

   if(iVolume(NULL,0,0)>1) OpenBar = false;      //To Verify that every hour an action is taken.
      if (OpenBar) {  
         int varOrderCount = OrdersTotal();
         if ( varOrderCount = 3)
         {
            funcCloseAll();                                      //Close all position
         }                 
         funcInitiateInitiate();                                  //Initiate NewTrade Entry
      }
You do not have the required permissions to view the files attached to this post.
User avatar
SteveHopwood
Owner
Posts: 9904
Joined: Tue Nov 15, 2011 8:43 am
Location: Misterton - an insignificant village in England. Very pleasant to live in.

Execute Function when OrdersTotal() = N

Post by SteveHopwood »

The chances of trading volume being <= 1 are remote even at the open of a new candle.

Thinking about it, and playing from memory here, it takes a ridiculous number of trades to be placed at the same time to generate a tick, so the chances are actually zero.

Instead, use a construct like this:

Code: Select all

static datetime OldCcaReadTime = 0;

if (OldCcaReadTime != iTime(Symbol(), 0, 0) )
{
      OldCcaReadTime = iTime(Symbol(), 0, 0);
      Do stuff
}//if (OldCcaReadTime != iTime(Symbol(), 0, 0) )
   
:xm:
Read the effing manual, ok?

Afterprime is the official SHF broker. Read about them at https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?p=175790#p175790.

I still suffer from OCCD. Good thing, really.

Anyone here feeling generous? My paypal account is always in the market for a tiny donation. pianodoodler@hotmail.com is the account.

To see The Weekly Roundup of stuff you guys might have missed Click here

My special thanks to Thomas (tomele) for all the incredible work he does here.
desmondc
Posts: 7
Joined: Wed Mar 05, 2014 2:53 am

Execute Function when OrdersTotal() = N

Post by desmondc »

Hey Steve..

I'll guess you are right. i notice that there will be times a new bar has no entry; if what i understand from your post.. this does mean that there was a certain vol for that bar at that time which is why no entry was executed.

Will look at the time based entry later..

~~~

To update this thread, i did find a solution to my problem. In summary since I can't get seem to get the if condition to work direclty with OrderTotal() what I did was get a loop to pull out that figure for my IF condition. Its stupid but hell as long as we get the result we want..

Code: Select all

for (int index = 0; index < OrderTotal(); index++)
{

         if ( index == 3)
         {
            //funcCloseAll();                                      //Close all position but do not reset balance and target value.
         }                
         funcInitiateInitiate();   
}
**We can close this thread if there is nothing more..
User avatar
renexxxx
Trader
Posts: 860
Joined: Sat Dec 31, 2011 3:48 am

Execute Function when OrdersTotal() = N

Post by renexxxx »

desmondc » Tue Mar 22, 2016 4:00 am wrote:To update this thread, i did find a solution to my problem. In summary since I can't get seem to get the if condition to work direclty with OrderTotal() what I did was get a loop to pull out that figure for my IF condition. Its stupid but hell as long as we get the result we want..
Your code is both faulty, inefficient and ineffective. I get goosebumps even looking at it:

Faulty because there is no function OrderTotal(). You probably mean OrdersTotal().
Inefficient as OrdersTotal() is re-evaluated for every iteration in the loop. If you want to iterate over the open-orders cursor in chronological order use:

Code: Select all

int ordersTotal = OrdersTotal();
for (int index=0; index < ordersTotal; index++) {
   if ( OrderSelect(index, SELECT_BY_POS, MODE_TRADES ) ) {
      ...
   }
}
and if you wish to iterate over the open-orders cursor in reverse chronological order use:

Code: Select all

for (int index = OrdersTotal()-1; index >= 0; index--) {
   if ( OrderSelect(index, SELECT_BY_POS, MODE_TRADES ) ) {
      ...
   }
}
If you want to start closing orders, whilst iterating over the open-orders cursor, you have start at the end, i.e. you have to iterate in reverse chronological order, as it totally messes up the cursor if you close orders at the start or in the middle. Still, even if you start at the end, and you close orders depending on a certain condition, you have to first collect their ticket numbers, and then close each ticket separately, like so:

Code: Select all

   int ticketsToBeClosed[];
   ArrayResize( ticketsToBeClosed, 0 );
   for (int index = OrdersTotal()-1; index >= 0; index--) {
      if ( OrderSelect(index, SELECT_BY_POS, MODE_TRADES ) ) {
         if ( index % 2 == 0 ) {    // For arguments sake, just close the 'even' orders
            addToIntArray( ticketsToBeClosed, OrderTicket() );
         }
      }
   }

   for( int index = 0; index < ArraySize(ticketsToBeClosed); index++ ) {
      if ( OrderSelect( ticketsToBeClosed[index], SELECT_BY_TICKET ) ) {
         if ( ( OrderCloseTime() == 0 ) && ( ( OrderType() == OP_BUY ) || ( OrderType() == OP_SELL ) ) )  {
            OrderClose( ticketsToBeClosed[index], OrderLots(), ( OrderType() == OP_BUY ) ? MarketInfo( OrderSymbol(), MODE_BID ) : MarketInfo( OrderSymbol(), MODE_ASK ), SLIPPAGE, clrNONE );
         }
      }
   } 

void addToIntArray( int &array[], int num ) {
   int currentSize = ArraySize( array );
   ArrayResize( array, currentSize+1 );
   array[currentSize] = num;
}

Ineffective As I don't think you achieve what you have set out to do, and the above code is good to know but has nothing to do with what you want. The requirement is to execute a function when OrdersTotal() has reached a set number (N). So, presumably, there is another thread (may be the same thread) or process that is opening trades. You just need to add somewhere in your OnTick() or OnTimer() thread the following code:

Code: Select all

   if ( OrdersTotal() >= N ) {
      // ExecuteRequiredFunction()
   }
Why '>=' and not '=='? Because, imagine that you missed the moment that OrdersTotal() == N. The code could happily add orders ad infinitum, without your required function being called ever.
Note, that OrdersTotal() returns all active orders on the account, including pending orders and orders with different magic numbers (manual orders have MagicNumber == 0 ). So, if you want to execute the required function only when the total of your orders equal or exceed N, you'd need to do something like:

Code: Select all

   if ( numOrders() >= N ) {
      // ExecuteRequiredFunction()
   }

int numOrders() {
   int result = 0;
   for(int index=OrdersTotal()-1; index >= 0; index--) {
      if ( OrderSelect( index, SELECT_BY_POS ) ) {
         if ( OrderMagicNumber() != MagicNumber ) continue;
         if ( (OrderType() != OP_BUY) && (OrderType() != OP_SELL) ) continue;
         result++;
      }
   }
   return(result);
}
Hope this has clarified a few points.
desmondc
Posts: 7
Joined: Wed Mar 05, 2014 2:53 am

Execute Function when OrdersTotal() = N

Post by desmondc »

Hey Renexx..

Many thanks. Sorry about the code, I had to look at another computer screen and type it in to another. Hence I only put in what is enough to show my work around.. I'll give a try on your example and see how much I can tweak it to what i need..

Again.. Many thanks.
Post Reply

Return to “Coders Hangout”