SmartQuant Discussion

Automated Quantitative Strategy Development, SmartQuant Product Discussion and Technical Support Forums
It is currently Tue Mar 31, 2020 9:16 am

All times are UTC + 3 hours




Post new topic Reply to topic  [ 10 posts ] 
Author Message
PostPosted: Wed Dec 10, 2008 3:05 am 
Offline

Joined: Mon Nov 24, 2008 8:44 pm
Posts: 36
I am referecing Portfolio.HasPosition( Inst ), Positions[ Inst ].GetValue() etc in my strategy, and everything seems to work fine in simulator. Can I still rely on having these info available during live trading? Here is my guess:

If I made trades with OQ in live trading, I can rely on Portfolio object to keep track of them, however, if I shut the strategy down (or OQ crashed) then re-started it...the Portfolio object is no longer reliable/correct, is that correct?

Has anyone tried running OQ strategy non-stop, and if so what is the maximum up-time? Can someone show me some examples of how to "save" state information, so that I can re-start my strategy and it would continue where I left off?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 10, 2008 12:16 pm 
Offline

Joined: Wed Oct 08, 2003 1:06 pm
Posts: 833
Hi,

Just go to Options and check the Persistent option of the Live mode.

All transactions wil be saved in this mode and you will start from not empty portfolio if you restart your strategy after OQ crash.

Regards,
Sergey.


Top
 Profile  
 
 Post subject: Seal the deal!
PostPosted: Wed Dec 10, 2008 10:18 pm 
Offline

Joined: Mon Nov 24, 2008 8:44 pm
Posts: 36
Thanks Sergey!

My OQ demo is going to expire in 1 day, and I was contemplating whether to pay $700+ for it...it works great for me in backtesting, but it is the unknown in live trading that scared me - but this seal the deal for me! I'm placing an order today...

How about SMA, SMD that I've been keeping track of in the strategy...they will be gone after re-start right? Any sample code to store and re-read them?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 10, 2008 10:43 pm 
Offline

Joined: Tue Aug 05, 2003 3:43 pm
Posts: 6817
Hi,

you can pre-load bars stored in OQ historical db to Bars bar series of your strategy in OnStrategyStart. Your indicators will be calculated automatically. There is an example in the FAQ section of this forum. This way you don't need to wait to start live trading.

Regards,
Anton


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 15, 2008 11:34 pm 
Offline

Joined: Wed Apr 27, 2005 4:41 pm
Posts: 609
Location: Helsinki, Finland
We are using IB TWS and have set up a periodic resync of all TWS positions to OQ.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 16, 2008 7:48 am 
Offline

Joined: Mon Nov 24, 2008 8:44 pm
Posts: 36
Can you elaborate? Any sample code or pointer to the right direction is very much appreciated! Does IB have 1 min bar data, and if so, how far back?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 16, 2008 10:58 am 
Offline

Joined: Wed Apr 27, 2005 4:41 pm
Posts: 609
Location: Helsinki, Finland
I'm not by the pc having the code, so I can't post it now.

But basically it's a two phase sync: 1) sync all for TWS to OQ positions and 2) sync all OQ positions with TWS. As these are positions they don't really have anything to do with the TWS bars historic or current. The bar data is a different issue.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 17, 2008 8:44 pm 
Offline

Joined: Wed Apr 27, 2005 4:41 pm
Posts: 609
Location: Helsinki, Finland
OK, here's TWS sync code. All feedback is welcome:
Code:
         // 1st part: Within the try TWS contents get sychronized to the OQ portfolio
         ArrayList iBPositionList = new ArrayList();
            try
            {
                if (IBProviderDisconnect)
                    throw new System.Exception("IB Provider Disconnect.  Please connect and retry.");

                BrokerAccountList Accounts = DataManager.GetBrokerInfo("IB").Accounts;

                if (Portfolio == null)
                    Console.WriteLine("Portfolio is null");

                foreach (BrokerAccount A in Accounts)
                    if (A.Name == Account)
                    {
                        foreach (BrokerPosition P in A.Positions)
                        {
                            if (P.Symbol.StartsWith("IB Conid")) // omit the IB internat id or whatever anyhow not a position
                            {
                                continue;
                            }
                            if (Instruments[P.Symbol] == null)
                            {
                                Console.WriteLine("{0} symbol is not in the instrument list of the strategy.", P.Symbol);
                                continue;
                            }

                            Instrument instrument = InstrumentManager.Instruments[P.Symbol];

                            iBPositionList.Add(P);

                            bool change = false;
                            if (Portfolio.HasPosition(instrument))
                            {
                                double brokerSum = 0;
                                double portfolioSum = 0;
                                double erotus = 0;
                                brokerSum = P.LongQty - P.ShortQty;
                                if (Portfolio.Positions[instrument].Side ==  PositionSide.Long) // if TWS has more add to sum
                                    portfolioSum += Portfolio.Positions[instrument].Qty;
                                else if (Portfolio.Positions[instrument].Side == PositionSide.Short) // if TWS has less subtract sum
                                    portfolioSum -= Portfolio.Positions[instrument].Qty;
                                erotus = brokerSum - portfolioSum;
                                if (erotus > 0) // add to OQ, if TWS has more
                                {
                                    Portfolio.Add(Clock.Now, TransactionSide.Buy, erotus, Instruments[P.Symbol], 0);
                                    change = true;
                                }
                                else if (erotus < 0) // subtract from OQ TWS has less
                                {
                                    Portfolio.Add(Clock.Now, TransactionSide.Sell, erotus, Instruments[P.Symbol], 0);
                                    change = true;
                                }
                            }
                            else // if there was no OQ position at all, just use the TWS amount
                            {
                                if (P.LongQty > 0)
                                {
                                    Portfolio.Add(Clock.Now, TransactionSide.Buy, P.LongQty, Instruments[P.Symbol], 0);
                                    change = true;
                                }
                                if (P.ShortQty > 0)
                                {
                                    Portfolio.Add(Clock.Now, TransactionSide.Sell, P.ShortQty, Instruments[P.Symbol], 0);
                                    change = true;
                                }
                            }
                            if (change)
                                Console.WriteLine(Instruments[P.Symbol].Symbol + " was synchorinized TWS -> OQ");
                        }
                    }


                // 2nd part check OQ positions against TWS positions
                ArrayList positionList = new ArrayList();

                // create a position list which won't change all of the sudden
                foreach (Position position in Portfolio.Positions)
                {
                    positionList.Add(position);
                }


                try
                {

                    foreach (Position position in positionList) // NOTE: this only takes into account positions! Partial fills will be missing!
                    {
                        bool iBAndOQAreEqual = false;
                        foreach (BrokerPosition brokerPosition in iBPositionList)
                        {
                            if (position.Instrument.Symbol == brokerPosition.Symbol && ((brokerPosition.ShortQty != 0 && position.Side == PositionSide.Short && brokerPosition.ShortQty == position.Qty) || (brokerPosition.LongQty != 0 && position.Side == PositionSide.Long && brokerPosition.LongQty == position.Qty)))
                            {
                                iBAndOQAreEqual = true;
                            }
                        }

                        if (iBAndOQAreEqual == false)
                        {
                            // If there's a TWS position which isn't the instrumentlist of strategy, do nothing
                            if (Portfolio.Positions[position.Instrument].Side == PositionSide.Long)
                                Portfolio.Add(Clock.Now, TransactionSide.Sell, position.Qty, position.Instrument, 0);
                            else if (Portfolio.Positions[position.Instrument].Side == PositionSide.Short)
                                Portfolio.Add(Clock.Now, TransactionSide.Buy, position.Qty, position.Instrument, 0);
                            Console.WriteLine(position.Instrument.Symbol + "  was synchronized OQ -> TWS");
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error in sync!!!!!!! ");
                }


As far as I'm aware there's only one major fault: the sync doesn't use any prices, just zero. But we'll fix that sooner or later. Our strategy doesn't need the price info at the positions.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 23, 2008 3:45 pm 
Offline

Joined: Mon Nov 24, 2008 8:44 pm
Posts: 36
Thanks!! I will try that when I have a chance. Do I just put the code at in OnStrategyStart()? With this, do I still need to configure OQ to be in persistent mode (Tool->Options->Configuration->Modes)?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 26, 2008 1:22 am 
Offline

Joined: Wed Apr 27, 2005 4:41 pm
Posts: 609
Location: Helsinki, Finland
Well it depends on how you want your strategy to function.

We do a once a minute resync, which has been good. The partial fills create a bit of confusion and this once a minute resync solves those problems as well.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC + 3 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group