New TradeMode: TR_CORRECTIVE (for NFA accounts)

Posted By: AndrewAMD

New TradeMode: TR_CORRECTIVE (for NFA accounts) - 07/27/19 15:42

jcl,

It would be helpful if there was a new TradeMode made available: TR_CORRECTIVE. This would be specifically designed for NFA accounts. The design intent is to synchronize the Pool position with the broker position without requiring any manual intervention.

It's similar to TR_PHANTOM, but with a twist:
* Upon enterLong/enterShort, the Pool does *not* send an entry order to the broker.
* Upon exit, the Pool does *indeed* send an exit order to the broker.

The reasons for this are twofold:
1) The bookkeeping would be better.
2) Corrective actions can and should be fully automatic. It is better if a human does not need to manually intervene with a trading system.

An example use: LotsPool is +80 but my actual position is -40 for reasons unknown. So I enable TR_PHANTOM and then enterShort by 120 lots. Therefore both LotsPool and the actual position are now correctly showing -40. The correction is then documented in my logs.

Moreover, if I stop trading, and I click yes to close all trades, the system will actually close out the -40 lots position correctly instead of shorting by another 80 lots, closing the script at -100 lots.
Posted By: jcl

Re: New TradeMode: TR_CORRECTIVE (for NFA accounts) - 07/29/19 08:19

Although LotsPool is documented as read-only, I think you can modify it and thus easily synchronize the account with no need of a new trade mode. Try it. Let me know if something does not work.
Posted By: AndrewAMD

Re: New TradeMode: TR_CORRECTIVE (for NFA accounts) - 07/29/19 10:04

Let me look into this. Thanks.
Posted By: AndrewAMD

Re: New TradeMode: TR_CORRECTIVE (for NFA accounts) - 08/09/19 01:55

Well, this did work a little bit, but I have found there are problems if the sum of pool trade positions are not equal to LotsPool. For example, if I close Zorro and click "Yes" to close all trades, the pool trades get closed - this is problematic if I modify LotsPool, thus putting the pool trades and LotsPool out of sync.
Posted By: AndrewAMD

Re: New TradeMode: TR_CORRECTIVE (for NFA accounts) - 08/09/19 18:01

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.
Posted By: AndrewAMD

Re: New TradeMode: TR_CORRECTIVE (for NFA accounts) - 08/10/19 02:39

I will test this when the market opens, this looks like a much cleaner implementation:

Code
void sync_pool_to_broker_position(){
	int tm = TradeMode, lp = LotsPool, lv = LotsVirtual, lph = LotsPhantom;
	LotsPool = brokerCommand(GET_POSITION,SymbolTrade);
	int lp_diff = LotsPool - lp;
	if(lp_diff){
		setf(TradeMode,TR_PHANTOM);
		Lots = abs(lp_diff);
		TRADE* pTr;
		if(lp_diff>0)
			pTr = enterLong();
		else if(lp_diff<0)
			pTr = enterShort();
		setf(pTr->flags,TR_NET);
		resf(pTr->flags,TR_PHANTOM);
	}
	TradeMode = tm; LotsVirtual = lv; LotsPhantom = lph;
}

void force_pool_order(){
	if(LotsPool==LotsVirtual) return;
	char algoname[16];
	strcpy(algoname,Algo);
	algo("HACK");
	int tm = TradeMode, lv = LotsVirtual, lph = LotsPhantom;
	setf(TradeMode,TR_PHANTOM);
	TRADE* pTr = enterLong(1);
	setf(pTr->flags, TR_PHANTOM|TR_NET);
	resf(TradeMode,TR_PHANTOM);
	exitTrade(pTr);
	TradeMode = tm; LotsVirtual = lv; LotsPhantom = lph;
	algo(algoname);
}

void set_broker_position(int pos){
	sync_pool_to_broker_position();
	if(LotsVirtual == pos){
		force_pool_order();
		return;
	}
	
	int tm = TradeMode, lv = LotsVirtual;
	int ph_diff = LotsPhantom - LotsVirtual;
	
	// close all virtual trades, this asset. No orders sent to broker.
	setf(TradeMode,TR_PHANTOM);
	for(open_trades){
		if(strcmp(TradeAsset,Asset)) continue;
		if(!TradeIsVirtual) continue;
		resf(ThisTrade->flags,TR_NET);
		exitTrade(ThisTrade);
		setf(ThisTrade->flags,TR_NET);
	}
	LotsVirtual = 0; LotsPhantom = ph_diff;
	if(!pos){
		force_pool_order();
	}
	else{
		resf(TradeMode,TR_PHANTOM);
		Lots = abs(pos);
		if(pos>0)
			enterLong();
		else if(pos<0)
			enterShort();
	}
	TradeMode = tm;
}


EDIT: Don't use this code, it's garbage. I need to give it one more try.
Posted By: OptimusPrime

Re: New TradeMode: TR_CORRECTIVE (for NFA accounts) - 11/20/20 12:40

Hi Andrew: Do you have a new version of this ? Thanks so much!
Optimus
Posted By: AndrewAMD

Re: New TradeMode: TR_CORRECTIVE (for NFA accounts) - 11/20/20 18:54

Use enterTrade(). This feature was added to Zorro after I made this request.

Another comparable new feature is the brokerCommand GET_TRADES. It deletes all of your Zorro-side trades and replaces them all with detected trades on the broker plugin. Obviously, your broker plugin must support this feature.
© 2024 lite-C Forums