How can I access the close price from bo.EnterTrade?

Hi, I need to access the close price from a trade object. Manual says:

GetPrice method is available for OPEN POSITIONS only, when called on closed trade returns Null value

But it doesn't say how to access close price in a closed trade or if that is possible.

The following code tries to reiniciate trades previously closed by Money Management reasons. Static var is true in Closed symbols that need to be reopened. Restart day is TRUE on the first bar allowed to reiniciate the trades.

How can I access the close price (see the question marks in the code) from bo.EnterTrade?

I am not very familiar with CBT so forgive me if my question is very basic or if it is based on incorrect assumptions.

Thank you

//CLOSED POSITIONS LOOP TO REINICIATE TRADES
		for( trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade() )
		{
              if( RestartDay AND StaticVarGet(trade.Symbol + "-Closed") == 1 )
			  {
			   bo.EnterTrade(i, trade.symbol, True, ??????, AvailEQ); 
			   StaticVarSet(trade.Symbol + "-Closed", 0);  //reopened 
			   _TRACE("***** REOPENED " + trade.Symbol +  " *******");			  
			  }	
		}		

In this forum I found the solution using FOREIGN().

I show the code just in case it is of any help to others:

Foreign is slow and should be avoided.
You should be using much faster technique described here:
http://www.amibroker.com/kb/2014/11/20/how-to-show-indicator-values-in-backtest-trade-list/

1 Like

Thank you!
See the difference (Intel i7):
c09

And the code:

//"NameC" stores the close price to be used with CBT (i.e "AAPLC")
StaticVarSet( Name() + "C", Close );

SetCustomBacktestProc(""); 
 
if (Status("action") == actionPortfolio)  
{ 
    bo = GetBacktesterObject();		//Get backtester object 
    
		//CLOSED TRADES LOOP TO REINICIATE TRADES PREVIOUSLY CLOSED
		for( trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade() )
		{
		  PosSize = bo.Equity/NumPos;	
		  SymbolPrice = StaticVarGet(trade.Symbol + "C"); 
		  if( RestartDay AND StaticVarGet(trade.Symbol + "Closed") == 1 )
		  {		  
		   bo.EnterTrade(i, trade.symbol, True, SymbolPrice[i], PosSize);			   		   
		   StaticVarSet(trade.Symbol + "Closed", 0);  //this is done
		   _TRACE("***** RESTARTED " + trade.Symbol +  " *******");		   			  
		  } 
		}	   
    } 
    bo.PostProcess();	//  Do post-processing (always required) 
} 

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.