Correct limit orders handling using mid-level BTC

Hi,
I tried to do accurate ranking of limit order using StaticVarGenarateRanks, but I realized that it still won't correctly enter positions since I can't know how many positions are currently open for accurately limiting the number of positions I can enter.

So I'm trying to turn to mid-level BTC were I can use the GetFirstOpenPos/GetNextOpenPos methods to get that information, then use sig.Price = -1 to drop the redundant signals that shouldn't have entered.

What I want is to better understand how do the GetFirstOpenPos/GetNextOpenPos methods are updated; meaning if I run on bars and close a signal with sig.Price = -1 on the current bar, then does GetFirstOpenPos/GetNextOpenPos methods updates accordingly in the ext bar?

Thanks.

The GetFirstOpenPos/GetNextOpenPos methods are used to iterate through open trades. If you change a particular signal object to prevent a trade entry, then that signal will never result in a new trade being opened, and therefore on the next bar there is nothing new for GetFirstOpenPos/GetNextOpenPos to reflect from that particular signal.

2 Likes

Great explanation, thanks.

Hi,
So I wrote the code and it does what I want, but I see no difference in the results. The code is:

dt = DateTime();
SetCustomBacktestProc("");
     
if( Status( "action" ) == actionPortfolio )      
{          
    bo = GetBacktesterObject();      
    bo.PreProcess();         
	    
    for( i = 1; i < BarCount; i++ )       
    {      
        bo.ProcessTradeSignals( i );      
     
        prev_open_pos_num = 0;   
     
        for( trade = bo.GetFirstOpenPos(); trade; trade = bo.GetNextOpenPos() )       
        {     
			if (trade.BarsInTrade > 1) // position opened before this bar
				prev_open_pos_num++;
        } 
        
        max_entries_num = MaxOpenPositions - prev_open_pos_num;
        
        _TRACE("Date: " + DateTimeToStr(dt[i], 1) + " Bar Number: "  + i + "  , num of open positions: " + prev_open_pos_num + "  , MaxOpenPositions: " + MaxOpenPositions); 
        
        for( sig = bo.GetFirstSignal( i ); sig; sig = bo.GetNextSignal( i ) )
        {
			symbol_rank = StaticVarGet( "rankvalues" +  sig.Symbol ) ;      
        
            if( sig.IsEntry() )
            {
				if (symbol_rank[i] > max_entries_num)
					sig.Price = -1; // tell backtester to ignore signal
					
                _TRACE("Date: " + DateTimeToStr(dt[i], 1) + "  Bar Number: "  + i + "  " + sig.Symbol
                 + "  max_entries_num: " + max_entries_num + "  symbol_rank: " + symbol_rank[i] + "  price: " + sig.Price); 
            }


        }
        
	} 
      
    bo.PostProcess();	//  Do post-processing (always required)
}

I change the sig.Price correctly, but the trades still enter:

btc%20isnt%20working

Can you please tell me what am I doing wrong?

You need to modify the signals for the current bar before you call bo.ProcessTradeSignals(). The way you're doing it will have no effect on the outcome because all the trades have been entered before you disable any signals. Try moving bo.ProcessTradeSignals() after your for loop.

Also, you can just call bo.GetOpenPosQty(); to find the number of open positions without looping.

1 Like

Thanks for the help.