The INITRUN was put in place to initialise some global variables for oter parts of the code to use. They never change and are non-asset dependent so I thought it was a good place to put them.

As for the rest of the code, I thought it looked fine too. But, there are trades closing before the end of the simulation or reaching TP. There are also trades (seemingly) opening at the wrong price.

In order to track down the problem I employed many more printf statements than what is in the first post. In fact, I rewrote the entire srcipt and am now utilising a TMF which prints trade attributes at entry, open and close.
To get around not being able get the entry time of a pending trade once it has opened and the fact trades are not assigned a trade number until they are opened, I utilised the TradeVariable[] slots to store a unique id and the entry time of the trade.

Here's the code. It's a bit longer but I think it's closer to how the platform is intended to be used (code wise):
Code:
//defines
#define myID 	TradeVar[0]	//create a slot in TRADE* to keep a trade varaible
#define myDate	TradeVar[1]
#define M 1000					//length of array to fill with random numers
#define N 9999					//random numbers range from 0-N	 

//globals
if(is(INITRUN))
{
	//indicator variables
	var 			splitBy 	= 6;
	var 			sixthsLen 	= 1682;
	//trade variables
	static var 	        printLim        = 0;	//control switch to stop TMF constantly printing when TradeIsPending is true
	static var 	        tradeNum        = 0;	//place holder to keep track of the number of trades opened/closed
	//random number generator variables
	int 			in ;
	int 			im 		= 0;
	char 			is_used[N] 	= { 0 };
	static var 	        vektor[M];
}

//TMF of sorts
int ManageTrades(var tz)
{
	//static var myID;
	static string type;
	
	if(TradeIsPending && printLim == 0)
	{
		//printf("#nTrade opened %02d.%02d.%02d %02d.%02d", day(TradeTime), month(TradeTime), year(TradeTime)-2000, hour(TradeTime), minute(TradeTime));
		//myID = 0;
		myID = vektor[tradeNum];
		var myYear = (year()-2000)*1000000;
		var myMonth =  month()*10000;
		var myDay = day()*100;
		var myHour = hour();
		myDate = myYear+myMonth+myDay+myHour;
								
		if(TradeIsLong) { type = "Long"; /*printf("Test: %s", type);*/}
			else { type = "Short"; }
				
		printf("n%s trade %.0f pending @ %.5f, TP @ %.5f, TZ @ %.5f", type, (var)myID, (var)TradeEntryLimit, (var)TradeProfitLimit, (var)tz);
		printLim = 1;
		printf("nTradeNum = %.0f, ID should be %.0f", (var)tradeNum, vektor[tradeNum]);
                tradeNum += 1;
	}
	
	if(TradeIsOpen && TradeTime <= 0)
	{	
		printf("n%s trade %.0f opened @ %.5f, TP @ %.5f", type, (var)myID, (var)TradeEntryLimit, (var)TradeProfitLimit);	
		printf("#nTrade opened %.0f", /*myDateTime(TradeBarOpen)*/(var)myDate); //TradeTime no good
	}
	
	if(TradeIsClosed)
	{
		printf("n %s trade %.0f closed @ %.5f, TP @ %.5f", type, (var)myID, (var)TradePriceClose, (var)TradeProfitLimit);		
	}
	return 0;
}


function main()
{
	//generate an arary of random numbers to use for trade IDs
	//from: https://stackoverflow.com/questions/1608181/unique-random-numbers-in-an-integer-array-in-the-c-programming-language
	for (in = N - M; in < N && im < M; in++) 
	{
	  int r = rand() % (in + 1); /* generate a random number 'r' */
	
	  if (is_used[r])
	    /* we already have 'r' */
	    r = in; /* use 'in' instead of the generated number */
	  
	  vektor[im++] = r + 1; /* +1 since your range begins from 1 */
	  is_used[r] = 1;
	}
}
function run()
{
	//flags
	set(PLOTNOW);
	//set(TICKS);
	
	//////Variables/////////////////////////////////////////
	//system variables
	StartDate 	= 20100501;
	EndDate 		= 20110201;
	EntryTime 	= 10000000;
	//Verbose 		= 7;
	
	//trade variables
	var 	TP 			= 50*PIP;
	var 	buffer 		= 10*PIP;	
	////////////////////////////////////////////////////////
		
	//set up price/level series for indicator///////////
	vars sHH			= series(HH(sixthsLen));						//highest high series
	vars sLL			= series(LL(sixthsLen));						//lowest low series
	vars sRange		= series(sHH[1]-sLL[1]);						//market range series
	vars sUpperTZ 	= series(sHH[1] - ((sHH[1]-sLL[1])/6));	//uper trade zone series
	vars sLowerTZ 	= series(sLL[1] + ((sHH[1]-sLL[1])/6));	//lower trade zone series
	vars Price 		= series(priceClose()); 						//close series
	
	//////Trade Criteria////////////////////////////////////
	//short entry
	if(crossOver(Price, sUpperTZ))
	{
		Entry = sUpperTZ[1] - buffer;
		TakeProfit = (sUpperTZ[1] - buffer) - TP;
		printLim = 0;		
		enterShort(ManageTrades, sUpperTZ[1]); 
		//printf("nprice = %.5f, sixthsHigh = %.5f, pending price = %.5f", price(), sUpperTZ[1], sUpperTZ[1] - buffer);	
		//printf("nFresh upperTZ calc to %.5f and pending price is %.5f", HH(sixthsLen) - ((HH(sixthsLen)-LL(sixthsLen))/splitBy), (HH(sixthsLen) - ((HH(sixthsLen)-LL(sixthsLen))/splitBy)) - buffer);
	}
	
	//long entry
	if(crossUnder(Price, sLowerTZ))
	{
		Entry = sLowerTZ[1] + buffer;
		TakeProfit = (sLowerTZ[1] + buffer) + TP;
		printLim = 0;
		enterLong(ManageTrades, sLowerTZ[1]);
		//printf("nprice = %.5f, sixthsLow = %.5f, pending price = %.5f", price(), sLowerTZ[1], sLowerTZ[1] + buffer);		
		//printf("nFresh lowerTZ calc to %.5f and pending price is %.5f", LL(sixthsLen) + ((HH(sixthsLen)-LL(sixthsLen))/splitBy), (LL(sixthsLen) + ((HH(sixthsLen)-LL(sixthsLen))/splitBy)) + buffer);
	}
	////////////////////////////////////////////////////////

	//plot some stuff 
	plot("Lowest", sLL, 0, BLACK);
	plot("Highest", sHH, 0, BLACK);
	plot("upperTZ", sUpperTZ, 0, RED);
	plot("lowerTZ", sLowerTZ, 0, RED);
}




With this new code there are three problems:
1) TradeVariables[] do not seem to always be set. When one fails to be set, so does the other. Which one goes first and why I dno't know
2) Trades are closing before the end of the simulation. Nowhere in the code are there exit orders sent
3) Trades are opening at the wrong price. These trades seem to be linked to the TradeVariables[] dropping their values or not setting them.

Cheers,
BobbyT