SetStopLoss and IB Data

Questions about MultiCharts and user contributed studies.
allenlowe
Posts: 37
Joined: 29 Jun 2011
Has thanked: 2 times
Been thanked: 3 times

SetStopLoss and IB Data

Postby allenlowe » 14 Sep 2011

Hi,

I think there is some bugs in the setstoploss and setpercenttrailing working with IB data. I am using TWS to trade HSIF. In my data source setting, I already set generate new tick if volume change. Also, in my chart, I am using the 1 minute trade price based on volume change. My chart showing data correctly and filter out all the zero price and zero volume correctly.
My stop loss and trailing strategy is very simple.

Code: Select all

SetStopContract;
SetStopLoss(50 * 145);
setpercenttrailing( 50 * 195, 5);


Basically my stop loss is 145 points and trailing at 5% start with 195 points profit. On 9/9/2011, my auto trade system generated a sells order at 19,991. The system immediately generate a stoploss buy order at 20135 which is correct because I set the trading cost for a round trip at $60 (around 1 point). I put my stop loss coding at the beginning of my signal. I guess that’s why each time the same stop loss order will regenerate which there is a new order placed (even that order is not executed).

The problem is immediately after the closing at noon, the system keep on generating buy order at 20135 and 999. The 999 trailing order is correct in calculation if the price is zero (Int(19991 *50 *0.5)/50) = 999. It should not be a problem since all orders are rejected by the system.
However, just at the opening in the afternoon, an order of 999 is generated and accepted by IB. This cause my strategy unexpected closed.

I believe that this is due to the data used by SetStopLoss and trailing is direct from IB without go through the custom setting such as filter zero price and based on trade volume. Will MC address this in the future?
Also, there is no way to turn off the setstoploss, I just amend my code to temporary increase the stop loss and trailing to a level that will not trigger during lunch time. I am not sure it can address the issue or not. I will test it tomorrow. Any better idea to fix the problem?

Code: Select all

Condition99 = Time of this bar < 1159 or time of this bar > 1331 ;
If Condition99 then begin
    SetStopContract;
    SetStopLoss(50* LossTarget);
    setpercenttrailing( 50 * PTarget, 5);
end
Else begin
    SetStopContract;
    SetStopLoss(50* 30000);
    setpercenttrailing( 50 * 30000, 5);
end;
Attachments
3.GIF
Screen Shot 1
3.GIF (99.9 KiB) Viewed 1118 times

arjfca
Posts: 1270
Joined: 23 Nov 2010
Has thanked: 720 times
Been thanked: 222 times

Re: SetStopLoss and IB Data

Postby arjfca » 14 Sep 2011

Hello,

I did not look at your code logic, but here is my 2 cents opinion.

If I recall, SetStopLoss is a dollars$ (or Euro) value. It determine the real money that you are ready to loss. It is not directly linked to a price target

These setstoploss values, once sent, are stored on IB server. IB will react immediately upon reaching these target. If your instrument value drop by the SetStopLoss value, then the position is liquidated

To deactivate, maybe try to put setstoploss = 0

This is my understanding on how it work

Martin

User avatar
Henry MultiСharts
Posts: 8438
Joined: 25 Aug 2011
Has thanked: 1207 times
Been thanked: 2703 times

Re: SetStopLoss and IB Data

Postby Henry MultiСharts » 14 Sep 2011

Hello Allen.
There should be no issues with setstoploss and setpercenttrailing working with IB data. Here is what we recommend you to do:
If you don’t want to generate and send the orders during the closing at noon - in your script don't send the order during this period. You need to put a condition at the beginning of the script to make it work during only specified time:

Code: Select all

    if time>=   and time <= then
    your script
    end

Please let me know if this solution helped you or not.

allenlowe
Posts: 37
Joined: 29 Jun 2011
Has thanked: 2 times
Been thanked: 3 times

Re: SetStopLoss and IB Data

Postby allenlowe » 14 Sep 2011

Thanks, I never try to set stop loss to zero.

Hi Henry,

Did u see the attached gif file? It is the actual order generated by the code I quote. Also, your coding not work, I have tested the same logic today. Unlike other coding, the setpercenttrailing will valid until the related order is closed or the setpercenttrailing is amended. That's why I need to have a setpercenttrailing to 30000 pionts so that it will never trigger.

User avatar
TJ
Posts: 6551
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 966 times
Been thanked: 1892 times

Re: SetStopLoss and IB Data

Postby TJ » 14 Sep 2011

allenlowe wrote:Hi,

I think there is some bugs in the setstoploss and setpercenttrailing working with IB data. I am using TWS to trade HSIF. In my data source setting, I already set generate new tick if volume change. Also, in my chart, I am using the 1 minute trade price based on volume change. My chart showing data correctly and filter out all the zero price and zero volume correctly.
My stop loss and trailing strategy is very simple.

Code: Select all

SetStopContract;
SetStopLoss(50 * 145);
setpercenttrailing( 50 * 195, 5);


Basically my stop loss is 145 points and trailing at 5% start with 195 points profit. On 9/9/2011, my auto trade system generated a sells order at 19,991. The system immediately generate a stoploss buy order at 20135 which is correct because I set the trading cost for a round trip at $60 (around 1 point). I put my stop loss coding at the beginning of my signal. I guess that’s why each time the same stop loss order will regenerate which there is a new order placed (even that order is not executed).

The problem is immediately after the closing at noon, the system keep on generating buy order at 20135 and 999. The 999 trailing order is correct in calculation if the price is zero (Int(19991 *50 *0.5)/50) = 999. It should not be a problem since all orders are rejected by the system.
However, just at the opening in the afternoon, an order of 999 is generated and accepted by IB. This cause my strategy unexpected closed.

I believe that this is due to the data used by SetStopLoss and trailing is direct from IB without go through the custom setting such as filter zero price and based on trade volume. Will MC address this in the future?
Also, there is no way to turn off the setstoploss, I just amend my code to temporary increase the stop loss and trailing to a level that will not trigger during lunch time. I am not sure it can address the issue or not. I will test it tomorrow. Any better idea to fix the problem?

Code: Select all

Condition99 = Time of this bar < 1159 or time of this bar > 1331 ;
If Condition99 then begin
    SetStopContract;
    SetStopLoss(50* LossTarget);
    setpercenttrailing( 50 * PTarget, 5);
end
Else begin
    SetStopContract;
    SetStopLoss(50* 30000);
    setpercenttrailing( 50 * 30000, 5);
end;


see post #3
viewtopic.php?f=16&t=6929

allenlowe
Posts: 37
Joined: 29 Jun 2011
Has thanked: 2 times
Been thanked: 3 times

Re: SetStopLoss and IB Data

Postby allenlowe » 14 Sep 2011

Hi TJ,

Thanks but my code is at the beginning not inside the strategy. The problem is that the stop loss and stoppercenttrailing execute when it got a zero price noise from IB data even I set the price based on volume change. That's why I believe the setpercenttrailing function got the data from IB directly instead of go through the MC filter.

User avatar
TJ
Posts: 6551
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 966 times
Been thanked: 1892 times

Re: SetStopLoss and IB Data

Postby TJ » 14 Sep 2011

allenlowe wrote:Hi TJ,

Thanks but my code is at the beginning not inside the strategy. The problem is that the stop loss and stoppercenttrailing execute when it got a zero price noise from IB data even I set the price based on volume change. That's why I believe the setpercenttrailing function got the data from IB directly instead of go through the MC filter.

did I say strategy?

allenlowe
Posts: 37
Joined: 29 Jun 2011
Has thanked: 2 times
Been thanked: 3 times

Re: SetStopLoss and IB Data

Postby allenlowe » 14 Sep 2011

Sorry TJ,

I mean condition, my code is right after variable declaration.

User avatar
TJ
Posts: 6551
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 966 times
Been thanked: 1892 times

Re: SetStopLoss and IB Data

Postby TJ » 14 Sep 2011

allenlowe wrote:Sorry TJ,

I mean condition, my code is right after variable declaration.


Do you mean your following codes are not inside a condition?

Condition99 = Time of this bar < 1159 or time of this bar > 1331 ;

If Condition99 then begin
SetStopContract;
SetStopLoss(50* LossTarget);
setpercenttrailing( 50 * PTarget, 5);
end
Else begin
SetStopContract;
SetStopLoss(50* 30000);
setpercenttrailing( 50 * 30000, 5);
end;



ps. the setstops/trailings should be at the very end of your code.

allenlowe
Posts: 37
Joined: 29 Jun 2011
Has thanked: 2 times
Been thanked: 3 times

Re: SetStopLoss and IB Data

Postby allenlowe » 14 Sep 2011

Hi TJ,

Not in any condition as I mentioned, I don't think end of code or beginning makes any different as I am using minute bar. Also, if u look at my gif file, the code works perfect until the market close for lunch. Also, based on the calculation I shown above, that is cause by a zero price tick. However, I already set price based on volume change and filter all zero price.

That's why I believe the setpercenttrailing did not solve the problem of IB random data bug that MC used to have.
These users thanked the author allenlowe for the post:
TJ

User avatar
TJ
Posts: 6551
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 966 times
Been thanked: 1892 times

Re: SetStopLoss and IB Data

Postby TJ » 14 Sep 2011

allenlowe wrote:Hi,

...
Also, there is no way to turn off the setstoploss, I just amend my code to temporary increase the stop loss and trailing to a level that will not trigger during lunch time. I am not sure it can address the issue or not. I will test it tomorrow. Any better idea to fix the problem?

Code: Select all

Condition99 = Time of this bar < 1159 or time of this bar > 1331 ;
If Condition99 then begin
    SetStopContract;
    SetStopLoss(50* LossTarget);
    setpercenttrailing( 50 * PTarget, 5);
end
Else begin
    SetStopContract;
    SetStopLoss(50* 30000);
    setpercenttrailing( 50 * 30000, 5);
end;



try this:

Code: Select all

Condition99 = Time of this bar < 1159 or time of this bar > 1331 ;
If Condition99 then begin

    Stop.Loss = 50* LossTarget;
    percent.trailing = 50 * PTarget;

end
Else begin

    Stop.Loss = 50* 30000;
    percent.trailing = 50 * 30000;

end;

SetStopContract;
SetStopLoss( Stop.loss);
Setpercenttrailing( Percent.trailing, 5);
These users thanked the author TJ for the post:
Henry MultiСharts

allenlowe
Posts: 37
Joined: 29 Jun 2011
Has thanked: 2 times
Been thanked: 3 times

Re: SetStopLoss and IB Data

Postby allenlowe » 14 Sep 2011

Thanks, will try tomorrow

User avatar
TJ
Posts: 6551
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 966 times
Been thanked: 1892 times

Re: SetStopLoss and IB Data

Postby TJ » 14 Sep 2011

TJ wrote:...
ps. they should be at the very end of your code.


allenlowe wrote:Hi TJ,

... I don't think end of code or beginning makes any different as I am using minute bar....


as you wish.

HarryHindsight
Posts: 14
Joined: 04 Sep 2010
Has thanked: 1 time
Been thanked: 4 times

Re: SetStopLoss and IB Data

Postby HarryHindsight » 14 Sep 2011

Have you checked the session's are correct in Quote manager ?
I thought they had changed the morning session to close at 12pm.
http://www.hkex.com.hk/eng/market/typho ... nghour.htm
If you get a dirty tick outside market hours that could upset your calculations.

allenlowe
Posts: 37
Joined: 29 Jun 2011
Has thanked: 2 times
Been thanked: 3 times

Re: SetStopLoss and IB Data

Postby allenlowe » 14 Sep 2011

Yes, they do change the time. That's why I already set the price based on volume change and filter all zero price and zero volume. I didn't set the time in Quote manager because I need to merge the data before HKEX change their opening hour with data after change for backtesting. My data on the chart doesn't have any dirty tick except the set percent trailing.

But thanks for your advice, I just set the opening hour for 2011 Sep exact match with HKEX and let's see whether it solve the problem.

allenlowe
Posts: 37
Joined: 29 Jun 2011
Has thanked: 2 times
Been thanked: 3 times

Re: SetStopLoss and IB Data

Postby allenlowe » 14 Sep 2011

Dear All,

Thanks for all advice. Just confirmed that change the opening hours in Quote Manager doesn't work. I still got strange orders after opening hour.

Now is the lunch time of HKFE. I am going to try my original time exclusion with TJ's modification. Will let you all know the result.

allenlowe
Posts: 37
Joined: 29 Jun 2011
Has thanked: 2 times
Been thanked: 3 times

Re: SetStopLoss and IB Data

Postby allenlowe » 15 Sep 2011

Hi TJ,

The code is not working. The setpercenttrail order immediate fill once it turn on by normal parameter. I believe this is because the setpercenttrail engine calculate the maxium contract profit based on the dirty zero price during lunch which never exist.

In backtesting, the setpercenttrail works fine. Even on realtime, the charts shows that I suppose to close the contract at the right timing.

I test the maxcontractprofit parameter, it seems work fine on backtesting again. I am not sure on realtime. I am going to re-write the setpercenttrail function based on some of the existing codes in this forum, will let all know whether it works or not.

User avatar
TJ
Posts: 6551
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 966 times
Been thanked: 1892 times

Re: SetStopLoss and IB Data

Postby TJ » 15 Sep 2011

Code: Select all

SetStopContract;
SetStopLoss(50 * 145);
setpercenttrailing( 50 * 195, 5);


5% trailing on HSI ???

That's hardly enough for spread !

If there is a gap opening in the afternoon session, the trail will be taken for sure.

Did you backtest with bid/ask?

allenlowe
Posts: 37
Joined: 29 Jun 2011
Has thanked: 2 times
Been thanked: 3 times

Re: SetStopLoss and IB Data

Postby allenlowe » 15 Sep 2011

Actually most of my order close within 100 points. This setting just safeguard for volatile market situation like last few weeks.

I am using trade price instead of bid/ask.

Yes, backtesting work perfect.

allenlowe
Posts: 37
Joined: 29 Jun 2011
Has thanked: 2 times
Been thanked: 3 times

Re: SetStopLoss and IB Data

Postby allenlowe » 15 Sep 2011

Also, I have check the trail stop order placed, it was based on a zero price tick.

User avatar
Henry MultiСharts
Posts: 8438
Joined: 25 Aug 2011
Has thanked: 1207 times
Been thanked: 2703 times

Re: SetStopLoss and IB Data

Postby Henry MultiСharts » 15 Sep 2011

The issue has been confirmed.
The reason is zero tick price that comes from IB side and we will do our best to reproduce it in our environment.
Allen please collect the logs after the session break and send them to support@multicharts.com for further investigation.

You can find log files here "%USERPROFILE%\Local Settings\Application Data\TS Support\MultiCharts\".
You should paste this line in Start --> Run and in the window that will pop up make a zip-folder.
Or you can click on the Windows Start button-> Programs -> MultiCharts -> make a right click on Logs-> then click on Properties.
Go to the Shortcut tab and click on "Open file location".
In the appeared window please compress to Zip folder called "Logs".
These users thanked the author Henry MultiСharts for the post:
TJ

allenlowe
Posts: 37
Joined: 29 Jun 2011
Has thanked: 2 times
Been thanked: 3 times

Re: SetStopLoss and IB Data

Postby allenlowe » 15 Sep 2011

But I already disable the setpercenttrailing, is my log still useful for you? Or can I just backup the log on 9/9/2011. Can u tell me how to backup?

User avatar
Henry MultiСharts
Posts: 8438
Joined: 25 Aug 2011
Has thanked: 1207 times
Been thanked: 2703 times

Re: SetStopLoss and IB Data

Postby Henry MultiСharts » 16 Sep 2011

We have reproduced the issue in our environment. We confirm it as a bug. The fix is scheduled for MultiCharts 7.01

The logs are erased on the start of MultiCharts. If you have closed MC after reproducing the issue-the logs won’t have sufficient information. The instructions how to collect logs can be found in my previous reply http://www.multicharts.com/discussion/viewtopic.php?f=1&t=9188#p43638.
These users thanked the author Henry MultiСharts for the post:
allenlowe

allenlowe
Posts: 37
Joined: 29 Jun 2011
Has thanked: 2 times
Been thanked: 3 times

Re: SetStopLoss and IB Data

Postby allenlowe » 23 Apr 2012

The set percentagetrailing seems still not working. I watch the data on realtime today. The percentage trailing generate an signal immediate on the chart window. However, there is no order generated in the order and tracker window. I wait for one minute and finally close the position manually.

It seems to me that the percentage trailing will not work after lunch break of the stock exchange.

User avatar
Henry MultiСharts
Posts: 8438
Joined: 25 Aug 2011
Has thanked: 1207 times
Been thanked: 2703 times

Re: SetStopLoss and IB Data

Postby Henry MultiСharts » 24 Apr 2012

allenlowe wrote:The set percentagetrailing seems still not working. I watch the data on realtime today. The percentage trailing generate an signal immediate on the chart window. However, there is no order generated in the order and tracker window. I wait for one minute and finally close the position manually.
It seems to me that the percentage trailing will not work after lunch break of the stock exchange.

Are you referring to real-time trading or real-time calculation? Was auto trading turned on on your chart?
The described situation can happen only if there is real-time strategy calculation and the automated order execution is not enabled.

allenlowe
Posts: 37
Joined: 29 Jun 2011
Has thanked: 2 times
Been thanked: 3 times

Re: SetStopLoss and IB Data

Postby allenlowe » 24 Apr 2012

I certainly enabled auto trade, the order was entered by my strategies and the connection was still on when I was watching it happened. For the real-time strategy, I didn't know there is a button for real-time strategy or real-time calculation. Was I missing something?

As you may aware, I am the one who found this error and certainly I used this command before and would like to use it again in the future if it fixed.

allenlowe
Posts: 37
Joined: 29 Jun 2011
Has thanked: 2 times
Been thanked: 3 times

Re: SetStopLoss and IB Data

Postby allenlowe » 24 Apr 2012

Is it possible because of the session break? The HKF exchange has changed the trading time, MC has updated it for HSI but not the MHI. I used MHI to test my strategy and therefore I have to create my own session to test my strategy.

User avatar
Henry MultiСharts
Posts: 8438
Joined: 25 Aug 2011
Has thanked: 1207 times
Been thanked: 2703 times

Re: SetStopLoss and IB Data

Postby Henry MultiСharts » 26 Apr 2012

allenlowe wrote:I certainly enabled auto trade, the order was entered by my strategies and the connection was still on when I was watching it happened. For the real-time strategy, I didn't know there is a button for real-time strategy or real-time calculation. Was I missing something?

As you may aware, I am the one who found this error and certainly I used this command before and would like to use it again in the future if it fixed.

Hello Allen,

When you apply signals to the chart - your strategy is calculated on real time data and the calculation results are plotted on the chart (this is what I meant by real-time calculation). No orders are sent to the broker. Strategy orders are sent to the broker only when you turn on the auto trading (real-time trading).

We have double checked the original issue you have reported-we confirm that it is fixed in the latest versions of MultiCharts.
allenlowe wrote:Is it possible because of the session break? The HKF exchange has changed the trading time, MC has updated it for HSI but not the MHI. I used MHI to test my strategy and therefore I have to create my own session to test my strategy.

It should not be affected by the session settings, but it is recommended to use the correct settings. Please set the proper session settings and if you still have the issue - please come to our live chat Monday-Friday 6:30 am - 4 pm EST to demonstrate this issue. We will do our best to help you.

allenlowe
Posts: 37
Joined: 29 Jun 2011
Has thanked: 2 times
Been thanked: 3 times

Re: SetStopLoss and IB Data

Postby allenlowe » 27 Apr 2012

Thanks a lot.

Don't know why but the error didn't reproduce anymore. I leave it at the moment. One quick question about a code time_s

If I turned on the intra-bar order generation, is that time_s will jump every second same even I am using a minute bar?

User avatar
JoshM
Posts: 2080
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1525 times
Been thanked: 1483 times
Contact:

Re: SetStopLoss and IB Data

Postby JoshM » 27 Apr 2012

allenlowe wrote:One quick question about a code time_s

If I turned on the intra-bar order generation, is that time_s will jump every second same even I am using a minute bar?

If you use time_s intrabar, it returns the time of the last trade (so it will updated during the time that the bar is active).
These users thanked the author JoshM for the post:
Henry MultiСharts

User avatar
Henry MultiСharts
Posts: 8438
Joined: 25 Aug 2011
Has thanked: 1207 times
Been thanked: 2703 times

Re: SetStopLoss and IB Data

Postby Henry MultiСharts » 27 Apr 2012

allenlowe wrote:If I turned on the intra-bar order generation, is that time_s will jump every second same even I am using a minute bar?

You can use CurrentTime_s if you need to operate with the computer's current time including seconds.


Return to “MultiCharts”