Preventing repeatition of trade in LiveAutoTrading


Dear Sir,


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
Subhash Nayak


For the answer see User KB:

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.