SmartQuant Discussion

Automated Quantitative Strategy Development, SmartQuant Product Discussion and Technical Support Forums
It is currently Sun May 20, 2018 1:02 pm

All times are UTC + 3 hours




Post new topic Reply to topic  [ 8 posts ] 
Author Message
PostPosted: Wed Mar 21, 2007 2:00 pm 
Offline

Joined: Tue Aug 05, 2003 3:43 pm
Posts: 6802
This example is like the basic Bollinger Band example above, but it adds three refinements. First, it demands that prices really punch through the lower Bollinger limit before buying. If 0 is the low limit, and 100 the high limit, this strategy buys only if prices are 20% of the band width below the lower limit. Second, it exits when a profit target of 15% is reached. Third, it exits after 4 days if the profit target has not been reached.

Code:
using OpenQuant.API;
using OpenQuant.API.Indicators;

using System.Drawing;

public class MyStrategy : Strategy
{
   [Parameter("Order quantity (number of contracts to trade)")]
   double Qty = 100;

   [Parameter("Bollinger Band level to go long")]
   double BLevel = -20;

   [Parameter("Percent profit target")]
   double ProfitPercent = 15;

   [Parameter("Max number of bars, while position is active")]
   int MaxDuration = 4;

   [Parameter("Length of Bollinger Band")]
   int BLength = 10;

   [Parameter("Order of Bollinger Band")]
   double BOrder = 1.5;

   B b;
   int barsFromEntry = 0;
   double exitPrice;
   Order sellOrder;
   Order buyOrder;

   public override void OnStrategyStart()
   {
      // set up bollinger bands
      BBL bbl = new BBL(Bars, BLength, BOrder, Color.Pink);

      // set up a series for B (breakout force) value
      b = new B(Bars, BLength, BOrder, Color.Yellow);

      Draw(bbl, 0);
      Draw(b, 2);
   }

   public override void OnBar(Bar bar)
   {
      // good practice to check if a series has the date you are interested in before you try
      // to use it
      if (b.Contains(bar))
      {
         // if we don't have a position and prices are below the lower band, open a long position
         if (!HasPosition)
         {
            if (b[bar.DateTime] * 100 <= BLevel)
            {
               buyOrder = BuyOrder(Qty, "Entry");
               buyOrder.Send();
            }
         }
         else
         {
            barsFromEntry++;

            // if we _have_ reached the exit day (4 days after entry), cancel the profit target
            // sell order, and issue a new market order to close the position now.
            if (barsFromEntry == MaxDuration)
            {
               barsFromEntry = 0;

               // cancel existing sell order if there is one
               if (sellOrder != null)
                  sellOrder.Cancel();

               Sell(Qty, "Exit (Max Duration)");
            }
         }
      }
   }

   public override void OnPositionOpened()
   {
      // when a position is opened, calculate profit target
      exitPrice = buyOrder.AvgPrice * (1 + ProfitPercent / 100);

      // cancel existing sell order if there is one
      if (sellOrder != null)
         sellOrder.Cancel();

      // issue a new sell limit order at the profit target price
      sellOrder = SellLimitOrder(Qty, exitPrice, "Exit (Profit Target)");
      sellOrder.Send();
   }

   public override void OnPositionClosed()
   {
      barsFromEntry = 0;
   }
}



Top
 Profile  
 
PostPosted: Mon Jan 03, 2011 9:43 pm 
Offline

Joined: Thu Jul 23, 2009 2:02 pm
Posts: 22
Hi, Could anyone help me how to add stop limit order in this code ? Currently it only send target exit limit order (SellLimitOrder) in OmPositionOpened() but I would like to add stop limit order for downside risk. I added stop order code but it doesn't seem to to work..
Thanks.

public override void OnPositionOpened()
{
// when a position is opened, calculate profit target
exitPrice = buyOrder.AvgPrice * (1 + ProfitPercent / 100);
// cancel existing sell order if there is one
if (sellOrder != null)
sellOrder.Cancel();
// issue a new sell limit order at the profit target price
sellOrder = SellLimitOrder(Qty, exitPrice, "Exit (Profit Target)");
sellOrder.Send();

/*** stop Order added by me **/

stopPrice = buyOrder.AvgPrice - ticks_stop
if (stopOrder != null)
stopOrder.Cancel();
stopOrder = SellStopOrder(Qty, stopPrice, "Exit(Stop Loss)" + OCACount);
stopOrder.Send();

}


Top
 Profile  
 
PostPosted: Mon Jan 03, 2011 10:04 pm 
Offline

Joined: Tue Aug 05, 2003 3:43 pm
Posts: 6802
I've inserted your code into the demo example and it works (at least it sends stop orders, I can see them in the order manager). Could you check that your ticks_stop value is correct? I used something like 10... Otherwise why do you say it doesn't work?

Regards,
Anton


Top
 Profile  
 
PostPosted: Tue Jan 04, 2011 12:15 am 
Offline

Joined: Thu Jul 23, 2009 2:02 pm
Posts: 22
Hi, If i add my stop order code, I keep getting filled on max duration logic.

Code:
using OpenQuant.API;
using OpenQuant.API.Indicators;

using System.Drawing;

public class MyStrategy : Strategy
{
   [Parameter("Order quantity (number of contracts to trade)")]
   double Qty = 1;

   [Parameter("Bollinger Band level to go long")]
   double BLevel = -10;

   [Parameter("Percent profit target")]
   double ProfitPercent = 15;

   [Parameter("Max number of bars, while position is active")]
   int MaxDuration = 10;

   [Parameter("Length of Bollinger Band")]
   int BLength = 10;

   [Parameter("Order of Bollinger Band")]
   double BOrder = 1.5;

   B b;
   int barsFromEntry = 0;
   double exitPrice;
   double stopPrice;
   Order sellOrder;
   Order buyOrder;
   Order stopOrder;

   public override void OnStrategyStart()
   {
      // set up bollinger bands
      BBL bbl = new BBL(Bars, BLength, BOrder, Color.Pink);

      // set up a series for B (breakout force) value
      b = new B(Bars, BLength, BOrder, Color.Yellow);

      Draw(bbl, 0);
      Draw(b, 2);
   }

   public override void OnBar(Bar bar)
   {
      // good practice to check if a series has the date you are interested in before you try
      // to use it
      if (b.Contains(bar))
      {
         // if we don't have a position and prices are below the lower band, open a long position
         if (!HasPosition)
         {
            if (b[bar.DateTime] * 100 <= BLevel)
            {
               buyOrder = BuyOrder(Qty, "Entry");
               buyOrder.Send();
            }
         }
         else
         {
            barsFromEntry++;

            // if we _have_ reached the exit day (4 days after entry), cancel the profit target
            // sell order, and issue a new market order to close the position now.
            if (barsFromEntry == MaxDuration)
            {
               barsFromEntry = 0;

               // cancel existing sell order if there is one
               if (sellOrder != null)
                  sellOrder.Cancel();

               Sell(Qty, "Exit (Max Duration)");
            }
         }
      }
   }

   public override void OnPositionOpened()
   {
      // when a position is opened, calculate profit target
      exitPrice = buyOrder.AvgPrice + 0.0005;

      // cancel existing sell order if there is one
      if (sellOrder != null)
         sellOrder.Cancel();

      // issue a new sell limit order at the profit target price
      sellOrder = SellLimitOrder(Qty, exitPrice, "Exit (Profit Target)");
      sellOrder.Send();
      
      
      /*** stop Order added by me ****************************/
      stopPrice = buyOrder.AvgPrice - 0.0005;
      if (stopOrder != null)
         stopOrder.Cancel();   
      
      stopOrder = SellStopOrder(Qty, stopPrice, "Exit(Stop Loss)" );   
      stopOrder.Send();
      //<-------------------------------------------------------
         
   }

   public override void OnPositionClosed()
   {
      barsFromEntry = 0;
   }
   
}


Attachments:
fill.jpg
fill.jpg [ 177.1 KiB | Viewed 6063 times ]
Top
 Profile  
 
PostPosted: Tue Jan 04, 2011 12:54 am 
Offline

Joined: Tue Aug 05, 2003 3:43 pm
Posts: 6802
I think there is something wrong with your logic. At one point (around 12:08) you entry with a buy market order and then both your exits (sell stop and limit orders) get executed, so that instead of going flat you revert your position. But you still have a position, even if it's a short position, so that HasPosition returns true and you start sending multiple max duration exit orders.

Regards,
Anton


Top
 Profile  
 
PostPosted: Tue Jan 04, 2011 2:09 am 
Offline

Joined: Thu Jul 23, 2009 2:02 pm
Posts: 22
Then what would be other ways to add stop limit order ?
Thanks.


Top
 Profile  
 
PostPosted: Tue Jan 04, 2011 11:36 am 
Offline

Joined: Tue Aug 05, 2003 3:43 pm
Posts: 6802
Cancel all other exit orders when one of them gets filled.


Top
 Profile  
 
PostPosted: Tue Jan 04, 2011 11:54 am 
Offline

Joined: Tue Aug 05, 2003 3:43 pm
Posts: 6802
You can use your own strategy logic for this or you can use OCA groups. See Breakout with multiple exits example.

Regards,
Anton


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 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