Thread: Martingale EA
View Single Post
  #1237 (permalink)  
Old 03-04-2008, 03:53 AM
wolfe's Avatar
wolfe wolfe is offline
Senior Member
 
Join Date: Jan 2006
Posts: 722
wolfe is on a distinguished road
Quote:
Originally Posted by ralph.ronnquist View Post
I've attached a code snippet below, which pairs up buys and sells and close them against each other. The point is that it really doesn't matter which ones you pair up, especially if they are all of the same lot size.

Ideally though, you should first compute the right amount of excess trades to close at current price, because the pairing up and closing takes some time, and the price might be moving while that happens; you would want to close the excess first, and thereafter the paired up ones.

One alternative could be to pair up and close the pairs earlier, at anytime after a positive state is achieved. Though then you will have to account for the balance change induced by the pairwise closing in the remaining cycle life until TS fires; the EA would have to pretend that the pairwise closing hasn't happened yet for the TS calculations and in trade counts etc. That is, if you want to actually close early, but mimic the behaviour as if not having closed.

PHP Code:
int buys[10000];
int sells[10000];

void ClosePairwise(int magic)
{
    
bool both true;
    while ( 
both ) {
        
int sp 0;
        
int bp 0;
        
both false;
        for ( 
int i OrdersTotal() - 1>= 0i-- ) {
            if ( ! 
OrderSelectiSELECT_BY_POS ) )
                continue;
            if ( 
OrderMagicNumber() != magic )
                continue;
            if ( 
OrderType() == OP_BUY ) {
                if ( 
sp ) {
                    
buysbp ] = OrderTicket();
                    
bp += 1;
                    continue;
                }
                
both true;
                
sp -= 1;
                
OrderCloseBysellssp ], OrderTicket(), Yellow );
            } else if ( 
OrderType() == OP_SELL ) {
                if ( 
bp ) {
                    
sellssp ] = OrderTicket();
                    
sp += 1;
                    continue;
                }
                
both true;
                
bp -= 1;
                
OrderCloseBybuysbp ], OrderTicket(), Yellow );
            }
        }
    }

Thank you Ralph! I'll check this out and try to incorporate it. I really appreciate your help and contribution!

Yet another time you have rescued me.
Reply With Quote