Preventing repetition of trade in LiveAutoTrading

Dear Sir,

Greetings!

I tested my afl in live market, but find unexpected results, very different from when I tested it off-line. When the Buy signal is generated, the system(afl) keeps on buying, repeatedly, until the Sell signal is generated. And, when the Sell signal is generated, the system keeps on selling, repeatedly, until the Buy signal is generated.
I have tried using program control methods like Boolean flags, state control, etc, but still the problem persists.
Here is the code

_ib  = GetTradingInterface( "IB" );

if( LastValue( TimeNum() ) < 152930 AND _ib.IsConnected() ){
    _mfi      = mfi( 2 );
    _mfi_prev = Ref( mfi( 2 ), -1 );
    
    if( LastValue( _mfi ) > LastValue( _mfi_prev ) ){
		_position = _ib.GetPositionSize( Name() );
        if( _position == 0 ){
            _order_id = _ib.PlaceOrder( Name(), "BUY", _lot_size, "MKT", 0, 0, "DAY", True);
            _position = _ib.GetPositionSize(Name());
        }
    }
    
    if(_position > 0){
		_order_id = _ib.PlaceOrder( Name(), "SELL", _position, "MKT", 0, 0, "DAY", True);
		_position = _ib.GetPositionSize(Name());
	}
}

How do I prevent such repeated Buy or Sell in the afl?
Could you kindly advice?
Thank you
Sincerely
Subhash Nayak

1 Like

For the answer see User KB:
http://www.amibroker.org/userkb/2007/07/14/preventing-repeat-orders-and-whipsaws/

BTW: Please read How to use this site site as your post was incorrectly formatted and yet again I had to fix formatting of your post. Really I have better and more important things to do than reformatting each and every new post just because new user fails to read the rules.

Hi,
@fxshrat, @Tomasz omasz
The link http://www.amibroker.org/userkb/2007/07/14/preventing-repeat-orders-and-whipsaws/ is explaining with IB context.I am using different brokarage,where we can not get the order status.We can just place the orders. So can you please explain the algorithm .So i can implement with my own brokerage context.

The same can be applied to any brokerage, the thing is that you use Static variables when you place the order and it avoids repetitions. Static variable holds the 'state' (the fact that you already placed the order).

// SIMPLIFIED pseudo-code showing the idea
if( LastValue( Buy ) And Nz( StaticVarGet("OrderAlreadyPlaced") ) == False )
{
   PlaceOrder();
   StaticVarSet( "OrderAlreadyPlaced", True );
} 
2 Likes
// SIMPLIFIED pseudo-code showing the idea
if( LastValue( Buy ) And Nz( StaticVarGet("OrderAlreadyPlaced") ) == False )
{
   PlaceOrder();
   StaticVarSet( "OrderAlreadyPlaced", True );
} 

Is this work with ModifyOrder as well ? the reason I asked because sometimes when the order is modify and in the same time the order fill the order resend again,is this prevent cases like this ?
Thank You

Logic works regardless if you call Place or Modify. The link to article explaining things in great detail was already given: http://www.amibroker.org/userkb/2007/07/14/preventing-repeat-orders-and-whipsaws/ did you read that?