SmartQuant Discussion

Automated Quantitative Strategy Development, SmartQuant Product Discussion and Technical Support Forums
It is currently Sat Apr 04, 2020 1:34 am

All times are UTC + 3 hours




Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Sun May 11, 2014 7:19 pm 
Offline

Joined: Sat Jul 13, 2013 3:57 am
Posts: 52
Hey all, I'm testing OpenQuant 2014. I took the Spread strategy that was included and added bollinger bands to it. I simply want to reverse the order at the upper and lower bollinger bands. I added the following code:

Code:
      protected override void OnTrade(Instrument instrument, Trade trade)
      {
         
         // Order logic
         if (bbl.Count > 1 && bbu.Count > 1 && ema.Count > 1)
         {
            if (!HasPosition(instrument)) {      
               if (trade.Price <= bbl.Ago(1))
                  Buy(instrument, Qty, "Buy spread");
               else if (trade.Price >= bbu.Ago(1))
                  Sell(instrument, Qty, "Sell spread");
            }
            else {
               if (trade.Price <= bbl.Ago(1) && Position.Side == PositionSide.Short)
                  Buy(instrument, 2*Qty, "Reverse spread to long");
               else if (trade.Price >= bbu.Ago(1) && Position.Side == PositionSide.Long)
                  Sell(instrument, 2*Qty, "Reverse spread to short");   
            }
            
         }
         
      }


When I run the backtest, I see that multiple orders are getting placed int he same direction. I would expect to see the buy and sell sides alternating. Any idea why this is happening? When I manually output the position info on each bar, I see:

AAPL vs MSFT -100 Short
AAPL vs MSFT -100 Short
AAPL vs MSFT -100 Short
AAPL vs MSFT -100 Short
AAPL vs MSFT -100 Short
AAPL vs MSFT -100 Short
AAPL vs MSFT -100 Short
AAPL vs MSFT -100 Short
AAPL vs MSFT -100 Short
AAPL vs MSFT -100 Short
AAPL vs MSFT -100 Short
AAPL vs MSFT -100 Short
AAPL vs MSFT -100 Short
AAPL vs MSFT -100 Short
AAPL vs MSFT -100 Short
AAPL vs MSFT -100 Short
AAPL vs MSFT -100 Short
AAPL vs MSFT -100 Short
AAPL vs MSFT -100 Short
AAPL vs MSFT -100 Short
AAPL vs MSFT -100 Short
AAPL vs MSFT -100 Short
AAPL vs MSFT -100 Short
AAPL vs MSFT -100 Short
AAPL vs MSFT -100 Short
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT -300 Short
AAPL vs MSFT -300 Short
AAPL vs MSFT -300 Short
AAPL vs MSFT -300 Short
AAPL vs MSFT -300 Short
AAPL vs MSFT -300 Short
AAPL vs MSFT -300 Short
AAPL vs MSFT -300 Short
AAPL vs MSFT -300 Short
AAPL vs MSFT -300 Short
AAPL vs MSFT -300 Short
AAPL vs MSFT -300 Short
AAPL vs MSFT -300 Short
AAPL vs MSFT -300 Short
AAPL vs MSFT -300 Short
AAPL vs MSFT -300 Short
AAPL vs MSFT -300 Short
AAPL vs MSFT -300 Short
AAPL vs MSFT -300 Short
AAPL vs MSFT -300 Short
AAPL vs MSFT -300 Short
AAPL vs MSFT -300 Short
AAPL vs MSFT -300 Short
AAPL vs MSFT -300 Short
AAPL vs MSFT -300 Short
AAPL vs MSFT -300 Short
AAPL vs MSFT 300 Long
AAPL vs MSFT 300 Long
AAPL vs MSFT 300 Long
AAPL vs MSFT 300 Long
AAPL vs MSFT 300 Long
AAPL vs MSFT 300 Long
AAPL vs MSFT 300 Long
AAPL vs MSFT 300 Long
AAPL vs MSFT 300 Long
AAPL vs MSFT 300 Long
AAPL vs MSFT 300 Long
AAPL vs MSFT 300 Long
AAPL vs MSFT 300 Long
AAPL vs MSFT 300 Long
AAPL vs MSFT 300 Long
AAPL vs MSFT 300 Long
AAPL vs MSFT -100 Short
AAPL vs MSFT -100 Short
AAPL vs MSFT -100 Short
AAPL vs MSFT -100 Short
AAPL vs MSFT -100 Short
AAPL vs MSFT -100 Short
AAPL vs MSFT 300 Long
AAPL vs MSFT 300 Long
AAPL vs MSFT 300 Long
AAPL vs MSFT 300 Long
AAPL vs MSFT 300 Long
AAPL vs MSFT 300 Long
AAPL vs MSFT 300 Long
AAPL vs MSFT 300 Long
AAPL vs MSFT 300 Long
AAPL vs MSFT 300 Long
AAPL vs MSFT -300 Short
AAPL vs MSFT -300 Short
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT -300 Short
AAPL vs MSFT -300 Short
AAPL vs MSFT -300 Short
AAPL vs MSFT -300 Short
AAPL vs MSFT -300 Short
AAPL vs MSFT -300 Short
AAPL vs MSFT -300 Short
AAPL vs MSFT -300 Short
AAPL vs MSFT -300 Short
AAPL vs MSFT -300 Short
AAPL vs MSFT -300 Short
AAPL vs MSFT -300 Short
AAPL vs MSFT -300 Short
AAPL vs MSFT -300 Short
AAPL vs MSFT -300 Short
AAPL vs MSFT -300 Short
AAPL vs MSFT -300 Short
AAPL vs MSFT -300 Short
AAPL vs MSFT -300 Short
AAPL vs MSFT -300 Short
AAPL vs MSFT -300 Short
AAPL vs MSFT -300 Short
AAPL vs MSFT -300 Short
AAPL vs MSFT -300 Short
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT -100 Short
AAPL vs MSFT -100 Short
AAPL vs MSFT -100 Short
AAPL vs MSFT -100 Short
AAPL vs MSFT -100 Short
AAPL vs MSFT -100 Short
AAPL vs MSFT -100 Short
AAPL vs MSFT -100 Short
AAPL vs MSFT -100 Short
AAPL vs MSFT -100 Short
AAPL vs MSFT -100 Short
AAPL vs MSFT -100 Short
AAPL vs MSFT -100 Short
AAPL vs MSFT -100 Short
AAPL vs MSFT -100 Short
AAPL vs MSFT -100 Short
AAPL vs MSFT -100 Short
AAPL vs MSFT -100 Short
AAPL vs MSFT -100 Short
AAPL vs MSFT -100 Short
AAPL vs MSFT -100 Short
AAPL vs MSFT -100 Short
AAPL vs MSFT -100 Short
AAPL vs MSFT -100 Short
AAPL vs MSFT -100 Short
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long
AAPL vs MSFT -900 Short
AAPL vs MSFT -900 Short
AAPL vs MSFT -900 Short
AAPL vs MSFT -900 Short
AAPL vs MSFT -700 Short
AAPL vs MSFT 100 Long
AAPL vs MSFT 100 Long


Attachments:
orders.PNG
orders.PNG [ 54.63 KiB | Viewed 2346 times ]
Top
 Profile  
 
PostPosted: Sun May 11, 2014 7:35 pm 
Offline

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

HasPosition returns false if you don't have a position, i.e. position.Qty = 0. I might be wrong, but I don't see a place in your code where you reverse position. Your if (!HasPosition) statement can do anything depending on prices, including pyramiding that you observe. It doesn't really reverse the spread. Though I might be wrong - it's Sunday evening :)

Regards,
Anton


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

All times are UTC + 3 hours


Who is online

Users browsing this forum: Google [Bot] 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