TMF not executed at each ticks

Posted By: JohanAu

TMF not executed at each ticks - 10/31/17 12:23

Hello everyone,

I am familiar with zorro since last year but live trading since fews weeks now and I would like to report something curious.

I am using Zorro 1.72 and I have observed the following behavior.

According to the documentation of zorro (which is great by the way) :

"All stop, profit, trail, or entry limits are handled by software and controlled at each tick. They are not sent to the broker's server (except for the 'safety net' stop given by StopFactor) and thus not visible to the broker, this way preventing "stop hunting" or similar practices. This also steps around NFA Compliance Rule 2-43(b) that does not allow US citizens to place stop or profit targets"

It works well when live trading only one asset :
- When the take profit is hit, the trade is closed immediately.

But when I use that same algo but with multiple assets in a loop ( while(loop(Assets)) :

- when the take profit is hit, the trade is closed as next bar.
- The only exception is for the asset that is selected in the GUI (see attached files). in that way is works fine.

Is it a behavior that is wanted ? or is it a feature for Zorro S user only ? or is it a bug ?
Please let me know that you think.

This cause trouble because since it close the trade at the next bar, the profit can be quite different from expected.

Best regards,
Johan


Attached picture MultipleAssetsTMF.png
Posted By: jcl

Re: TMF not executed at each ticks - 11/01/17 09:51

No, Stops and TPs must work in the same way with single and with multiple assets.

If your script behaves different dependent on whether you select the asset by script or with the scrollbox, then I would first suspect some wrong initialization. Make sure that you set the parameters for the right asset, after and not before selecting it. And set Stop and TP before entering the trade, not after it.

Posted By: JohanAu

Re: TMF not executed at each ticks - 11/02/17 07:36

Hi Jcl,

Thank for your hints,

According to your comment, I have added the is(INITRUN) check when loading the assetList (which was not the case).

I am using now the following script for test purpose :

Code:
function run()
{
set(PLOTNOW);
set(LOGFILE);
set(PRELOAD);
Verbose=7;
StartDate = 20150615;
EndDate = 20171001;
Weekend=0;

if(is(INITRUN)) {
		assetList("AssetsStrategy.csv"); // load asset list
}

while (asset(loop(Assets)))
{

BarPeriod = 15;
MaxLong = 4;
var winprofit;
set(NFA);
vars Price = series(price());
var NbDevUp=1.7;
var NbDevDn=1.7;
BBands((series(1000*priceClose())),20,NbDevUp,NbDevDn,MAType_SMA);
vars upperBand = series(rRealUpperBand/1000);
vars lowerBand = series(rRealLowerBand/1000);
plot("Bollinger1",rRealUpperBand/1000,BAND1,BLACK);
plot("Bollinger2",rRealLowerBand/1000,BAND2,GREY);
set(PLOTNOW);
set(TICKS);

printf("n Asset  : %s ",Asset);
printf("n price  : %.8f ",Price[0]);
printf("n UpperBand  : %.8f ",upperBand[0]);
printf("n LowerBand  : %.8f ",lowerBand[0]); 

if (crossUnder(Price, lowerBand) && NumOpenLong <MaxLong)
	{

	Lots = 1;
    TakeProfit = 1.0375*price();
	winprofit = (WinLong - LossLong);
	
	printf("n Asset  : %s ",Asset);
	printf("n pip cost : %.8f ",PIPCost);
	printf("n pip  : %.8f ",PIP);
	printf("n commission cost: %.8f ",CommissionCost);
	printf("n commission : %.8f ",Commission);
	printf("n lot amount : %.8f ",LotAmount);
	printf("n lot  : %.8f ",Lots);
	printf("n spread  : %.8f ",Spread);
	printf("n price  : %.8f ",Price[0]);
	printf("n UpperBand  : %.8f ",upperBand[0]);
	printf("n LowerBand  : %.8f ",lowerBand[0]);
	printf("n win - loss in btc : %.8f ",winprofit);
	
	enterLong();
  }	
	
}
	
plot("$ Balance",Balance,NEW,BLUE); 
plot("Equity",Equity,0,GREEN); 

}



It seems that the issue is still present, maybe I have misunderstood something with the BarPeriod (but normally should not affect TMFs).




Posted By: jcl

Re: TMF not executed at each ticks - 11/02/17 09:00

Yes, that looks quite wrong. I wonder why you get no runtime error with this script.

The order of commands does matter in programming. First set all script parameters, such as the bar period, TICKS, etc, and then load the asset. Not the other way around. Otherwise the asset is not loaded in TICKS mode, but in low resolution. The INITRUN clause is not needed.

You can see in workshop 6 in the manual how a portfolio script should look like.
Posted By: JohanAu

Re: TMF not executed at each ticks - 11/13/17 09:25

Hi Jcl,

Thank you for your response.
I have modified the function according to your comments :

Code:
function TradeBBands()
{
var NbDevUp=1.7;
var NbDevDn=1.7;
var MaxTradePerAsset = 4;

vars Price = series(price());
BBands((series(1000*priceClose())),20,NbDevUp,NbDevDn,MAType_SMA);
vars upperBand = series(rRealUpperBand/1000);
vars lowerBand = series(rRealLowerBand/1000);
Lots = 1;

if (crossUnder(Price, lowerBand) && NumOpenLong <MaxTradePerAsset)
	{
  TakeProfit = 1.0375*price();
	enterLong();
  }		
}


function run()
{
set(LOGFILE+PRELOAD+NFA+TICKS);
BarPeriod = 15;
Weekend = 0;
Verbose=7;
Capital = 0.009;
StartDate = 20150615;
EndDate = 20171001;

assetList("AssetsToUse.csv"); // load asset list

while (asset(loop(Assets)))
{
TradeBBands();	
}	
}



Tested it again and the issue is still present.
The trade is close at the right time only for one asset.
I don't know what variable I can check to diagnose the faulty element. Do you have any tips ?

BR, Johan
Posted By: jcl

Re: TMF not executed at each ticks - 11/13/17 10:39

Looks better now. At least I see no obvious reason for a delayed exit. But I would not use price() for a TakeProfit. That means the limit can be already below the current price.

For diagnose, use a TMF that prints the current time, asset, TradeProfitLimit, and TradePriceClose. That should give insight in the trade exit behavior.
© 2024 lite-C Forums