SmartQuant Discussion
http://www.smartquant.com/forums/

Help with order reversal code please
http://www.smartquant.com/forums/viewtopic.php?f=60&t=10730
Page 1 of 1

Author:  bean3178 [ Sun May 11, 2014 7:19 pm ]
Post subject:  Help with order reversal code please

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 2342 times ]

Author:  Dr. Anton Fokin [ Sun May 11, 2014 7:35 pm ]
Post subject:  Re: Help with order reversal code please

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

Page 1 of 1 All times are UTC + 3 hours
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/