Reset left side of += when request historical data at IB

UniversalGoldmine

Junior member
Messages
26
Likes
0
When running this API code for IB to request historical data, it produces dublettes of each line for each symbol.

As I understand this has to do with += ?
I wonder if there is a way to reset the left side of the += after the first loop ?
Code:
            List<String> Fill = new List<String>();
            Fill.Add("MSFT");
            Fill.Add("IBM");
            for (int i = 0; i < 2; i++)
            {
                Tws1.reqHistoricalData(i, Fill[i].ToString(), "STK", "", 0, "", "", "SMART", "usd", 0, "", "300 S", "1 min", "TRADES", 1, 1);
                Tws1.historicalData += new AxTWSLib._DTwsEvents_historicalDataEventHandler(this.historicalData);

            }
 
Last edited:
There is no need, or? Take the += OUT OF THE LOOP. YOu hook up the event handler, THEN start requesting data. Hooking up the event handler multiple times is simply not necessary. This is a pure programming error ;)
 
There is no need, or? Take the += OUT OF THE LOOP. YOu hook up the event handler, THEN start requesting data. Hooking up the event handler multiple times is simply not necessary. This is a pure programming error ;)

It worked as you said if I take the += out of the loop like this:
Code:
            Tws1.historicalData -= new AxTWSLib._DTwsEvents_historicalDataEventHandler(this.historicalData);
            List<String> Fill = new List<String>();
            Fill.Add("MSFT");
            Fill.Add("IBM");
            for (int i = 0; i < 2; i++)
            {
                Tws1.reqHistoricalData(i, Fill[i].ToString(), "STK", "", 0, "", "", "SMART", "usd", 0, "", "300 S", "1 min", "TRADES", 1, 1);

            }
            Tws1.historicalData += new AxTWSLib._DTwsEvents_historicalDataEventHandler(this.historicalData);

But if I run this code right after again, there will be dublettes of the lines.
So they are still in memory because of +=.

So what I wonder is if this is the correct method to empy the memory from data, though it do works ?
 
Well, first of - the "out of the loop" should be BEFORE the reqhistorical data - otherwise you MAY miss a line or two.

Second, make sure this is ALL you do - no other "+=" anywhere in code. THis should be set up ONCE in a proper provider pattern for the Tws API. Second. Tws1.historicalData does not store ANY data - it is a pure event. It keeps no memory. When it is raised, all registered handlers get called for every raising of the event.

If you ge t multiple calls, you have multiple handlers registered.
 
Top