This is the code that reverses a position:

Code

Lots = 2; //1 lot to close previous, 1 to open new in opposite direction
OrderLimit = Close[0] - Spread/2; //order between bid/ask
Spread = 0;  //spread to zero because limit orders
TRADE* t = enterLong();
if(t){
	t->nLotsTarget = 1;
	t->nLots = 1;       //semantically, trade is only 1 lot
	t->fSpread = 0; 
	t->fSlippage = 0;
	TRADE tmp;
	memcpy(&tmp, lasttrade, sizeof(tmp));    //lasttrade is pointer to previous trade that gets cancelled and re-entered
	cancelTrade(lasttrade);
	tmp.fExitPrice = t->fEntryPrice;
	tmp.fResult = tmp.nLots * (tmp.fEntryPrice-tmp.fExitPrice);        //lasttrade was a short trade
	tmp.fSpread = 0;   
	tmp.fSlippage = 0;
	tmp.flags ^= TR_OPEN; //toggle closed
	tmp.flags ^= TR_SIMULATED; //Zorro doesn't set this automatically upon enterTrade(), not sure what it does exactly
	tmp.nBarClose = Bar;
	tmp.tExitDate = t->tEntryDate;
	enterTrade(&tmp);
	lasttrade = t;
}



Commission is also set to zero earlier. I don't think it's that I forgot something, I suspect Zorro adjusts the internal performance metrics at the enterLong() call before I can change any properties of the trade.

Last edited by MegaTanker; 06/11/22 06:53.