3 registered members (Ayumi, Akow, AndrewAMD),
1,505
guests, and 9
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
New TradeMode: TR_CORRECTIVE (for NFA accounts)
#477777
07/27/19 15:42
07/27/19 15:42
|
Joined: Feb 2017
Posts: 1,731 Chicago
AndrewAMD
OP
Serious User
|
OP
Serious User
Joined: Feb 2017
Posts: 1,731
Chicago
|
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.
|
|
|
Re: New TradeMode: TR_CORRECTIVE (for NFA accounts)
[Re: AndrewAMD]
#477878
08/09/19 18:01
08/09/19 18:01
|
Joined: Feb 2017
Posts: 1,731 Chicago
AndrewAMD
OP
Serious User
|
OP
Serious User
Joined: Feb 2017
Posts: 1,731
Chicago
|
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. 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.
|
|
|
Re: New TradeMode: TR_CORRECTIVE (for NFA accounts)
[Re: AndrewAMD]
#477879
08/10/19 02:39
08/10/19 02:39
|
Joined: Feb 2017
Posts: 1,731 Chicago
AndrewAMD
OP
Serious User
|
OP
Serious User
Joined: Feb 2017
Posts: 1,731
Chicago
|
I will test this when the market opens, this looks like a much cleaner implementation: 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.
Last edited by AndrewAMD; 08/12/19 19:11. Reason: no good
|
|
|
Re: New TradeMode: TR_CORRECTIVE (for NFA accounts)
[Re: AndrewAMD]
#481922
11/20/20 12:40
11/20/20 12:40
|
Joined: Aug 2018
Posts: 98
OptimusPrime
Junior Member
|
Junior Member
Joined: Aug 2018
Posts: 98
|
Hi Andrew: Do you have a new version of this ? Thanks so much! Optimus
Thanks so much,
OptimusPrime
|
|
|
|