Here is an example implementation of flattening a position. It works, but I suspect I might be messing with Zorro's bookkeeping.

This is intrabar only. You cannot run this code from run(). Also, it assumes one Asset and one Algo.

As you can see, not only do i need to override LotsPool, but I also need to override LotsVirtual and LotsPhantom to keep the system sane. Finally, the net position of open virtual trades, phantom trades, and pool trades need to match LotsVirtual, LotsPhantom, and LotsPool, respectively.

Code
LotsPool = (int)brokerCommand(GET_POSITION,SymbolTrade);
//exit all virtual trades without sending to broker
setf(TradeMode,TR_PHANTOM);
for(open_trades){
	if(TradeAlgo != "foo") continue;
	if(!TradeIsVirtual) continue;
	ThisTrade->flags &= ~TR_NET;
	printf("\nExiting virtual trade, no send to broker, id: %d",TradeID);
	exitTrade(ThisTrade);
	ThisTrade->flags |= TR_NET;
}
//inject phantom trade and convert it to regular virtual.
setf(TradeMode,TR_PHANTOM);
Lots=1;
printf("\ncheckpoint enterlong");
TRADE* pTr = enterLong();
resf(TradeMode,TR_PHANTOM);
LotsVirtual = 1;
LotsPhantom = 1;
pTr->flags |= TR_PHANTOM|TR_NET;
Lots=1;
printf("\ncheckpoint exittrade");
exitTrade(pTr); // <--about to try this
LotsPool = (int)brokerCommand(GET_POSITION,SymbolTrade);

//cancel all virtual trades
for(open_trades){
	if(TradeAlgo != "foo") continue;
	if(!TradeIsVirtual) continue;
	printf("\nCanceling virtual trade id: %d",TradeID);
	cancelTrade(ThisTrade);
}

// cancel remaining pool trades
for(open_trades){
	if(TradeAlgo != "foo") continue;
	if(!TradeIsPool) continue;
	printf("\nCanceling pool trade id: %d",TradeID);
	cancelTrade(ThisTrade);
}
LotsPool = (int)brokerCommand(GET_POSITION,SymbolTrade);


Erm, it's probably too verbose. I think I can rewrite this into a simpler void set_position(int pos) function.