IB wrong stop

Posted By: vinsom

IB wrong stop - 07/14/17 21:52

Hi,
I'm getting some wrong stop values with IB.
Happens randomly, now happened with MNST, happened before with other stocks.

I calculate the stop and the trail with the lines below:

Stop=priceClose()-(3*ATR(30));
Trail=ATR(2);

I also tried previously with this line below but getting same errors too:
Stop=3*ATR(30);

This is extracted from the log:

[MNST::L8573] Trail 7@50.4100 Stop 20042.13 at 11:14:42
[MNST::L8573] Trail 7@50.4100 Stop 38037.55 at 11:40:07
Stop 38037.5547 hit at 30823.4707/30823.470730823.4707/30823.4707
BrokerSell MNST: 71774 ms

Anyone from the Zorro team has any idea why is this happening ?
Log file is attached(renamed to .h to allow upload)

Thanks
Vincenzo

Attached File
MVOreal.h  (92 downloads)
Posted By: jcl

Re: IB wrong stop - 07/18/17 06:36

Can you post the script? Otherwise it's hard to tell where the stop became wrong.
Posted By: vinsom

Re: IB wrong stop - 07/19/17 11:02

Hi Jcl,
There is only one line of code where I set the Stop equal to a value.
And looks like this is happening only in live trading with IB.
I checked the test log and all stops hit have normal values.

Script is below: Is just the Get Rich slowly modified for trading:

#define LEVERAGE 2 // 1:4 leverage
#define DAYS 252 // 1 year
#define NN 300 // max number of assets

string Names[NN];
vars Returns[NN];
var Means[NN];
var Covariances[NN][NN];
var Weights[NN];

//////////////////////////////////////////////////////

function run()
{
BarPeriod = 1440;
LookBack = DAYS;
NumYears = 5;
Verbose = 30;
set(PRELOAD); // allow extremely long lookback period



var TotalCapital = slider(1,1000,0,10000,"Capital","Total capital to distribute");
var VFactor = slider(2,10,0,100,"Risk","Variance factor");

int N = 0;
assetList("AssetsZ8NasdaqStocks.csv");
while (asset(loop(Assets)))
{
Names[N] =Asset;

Returns[N] = series((priceClose(0)-priceClose(1))/priceClose(1));
if(N++ >= NN) break;
}


int i,j;
for(i=0; i<N; i++)
{
Means[i] = Moment(Returns[i],LookBack,1);
for(j=0; j<N; j++)
Covariances[N*i+j] = Covariance(Returns[i],Returns[j],LookBack);
}

var BestVariance = markowitz(Covariances,Means,N,0.5);
var MinVariance = markowitzReturn(0,0);
markowitzReturn(Weights,MinVariance+VFactor/100.*(BestVariance-MinVariance));

for(i=0; i<N; i++)
{
asset(Names[i]);
Stop=priceClose()-(3*ATR(30));
Trail=ATR(2);
MarginCost = priceClose()/LEVERAGE;
int Position = TotalCapital*Weights[i]/MarginCost;

//printf("n%s: %d Contracts at %.0f$",Names[i],Position,priceClose());

if(tdm() == 1 )
{
if(Position>LotsPool)
{
enterLong(Position-LotsPool);//Position-LotsPool);

}
else if(Position<LotsPool)
{
exitLong(0,0,LotsPool-Position);//0,0,LotsPool-Position);
}
}
}

}



THanks
Vincenzo
Posted By: jcl

Re: IB wrong stop - 07/21/17 08:27

I see indeed no problem in the code at first glance. The stop is initially ok and then wrong trailed due to a wrong price for MSFT.

Which Zorro version is that? Do you trade via TWS or via Gateway?
Posted By: vinsom

Re: IB wrong stop - 07/21/17 10:01

Hi Jcl,
Is Zorro 1.58.
And I'm using the Gateway.
For the possible wrong price, is there an outlier control in the IB plugin ?

Thanks
VIncenzo
Posted By: jcl

Re: IB wrong stop - 07/21/17 11:31

Not in the plugin, but Zorro has an outlier control. It does not look like an outlier, but more like a permanently wrong price. Can you contact Support, and send them the .diag file if you have one?
Posted By: vinsom

Re: IB wrong stop - 07/21/17 12:01

Just sent to support.
Thanks
Vincenzo
© 2024 lite-C Forums