SmartQuant Discussion

Automated Quantitative Strategy Development, SmartQuant Product Discussion and Technical Support Forums
It is currently Mon Feb 16, 2026 9:03 am

All times are UTC + 3 hours




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: Gap
PostPosted: Mon Jan 16, 2006 5:29 pm 
Offline

Joined: Tue Aug 05, 2003 3:43 pm
Posts: 6817
Code:
public class Gap_ATS : ATSComponent
{

   bool   down  = false;
   double close = 0;
   double gap   = 0.05;

   double stopOCALevel  = 0.98;
   double limitOCALevel = 1.05;

   int OCACount = 0;

   ATSStop     stop;
   double   stopLevel = 0.05;
   StopType stopType  = StopType.Trailing;
   StopMode stopMode  = StopMode.Percent;

   bool entryEnabled    = true;

   bool ocaExitEnabled  = true;
   bool timeExitEnabled = true;
   bool stopExitEnabled = true;

   SingleOrder marketOrder, limitOrder, stopOrder;

   int barCount;
   int barsToExit = 10;

   double qty = 100;

   [Category("OCA")]
   public double StopOCALevel
   {
      get { return stopOCALevel; }
      set { stopOCALevel = value; }      
   }

   [Category("OCA")]
   public double LimitOCALevel
   {
      get { return limitOCALevel; }
      set { limitOCALevel = value; }      
   }

   [Category("Stop")]
   public double StopLevel
   {
      get { return stopLevel; }
      set { stopLevel = value; }      
   }

   [Category("Stop")]
   public StopType StopType
   {
      get { return stopType; }
      set { stopType = value; }      
   }

   [Category("Stop")]
   public StopMode StopMode
   {
      get { return stopMode; }
      set { stopMode = value; }      
   }

   [Category("Entry")]
   public double GapLevel
   {
      get { return gap; }
      set { gap = value; }      
   }

   [Category("Exit")]
   public bool OCAExitEnabled
   {
      get { return ocaExitEnabled; }
      set { ocaExitEnabled = value; }      
   }

   [Category("Exit")]
   public bool TimeExitEnabled
   {
      get { return timeExitEnabled; }
      set { timeExitEnabled = value; }      
   }

   [Category("Exit")]
   public bool StopExitEnabled
   {
      get { return stopExitEnabled; }
      set { stopExitEnabled = value; }      
   }

   [Category("Exit")]
   public int BarsToExit
   {
      get { return barsToExit; }
      set { barsToExit = value; }      
   }

   [Category("Money")]
   public double Qty
   {
      get { return qty; }
      set { qty = value; }      
   }

   public Gap_ATS() : base()
   {

   }

   public override void Init()
   {
   
   }

   public override void OnBarOpen(Bar bar)
   {
      if (entryEnabled)
      {
         if (down)
            if (bar.Open / close - 1 > gap)
            {
               marketOrder = MarketOrder(Instrument, Side.Buy, qty);
               marketOrder.Text = "Entry";
               marketOrder.Send();

               if (ocaExitEnabled)
               {
                  limitOrder = LimitOrder(Instrument, Side.Sell, qty, limitOCALevel * bar.Close);
                  limitOrder.OCAGroup = "OCA " + Instrument.Symbol + " " + OCACount;
                  limitOrder.Text     = "Limit OCA " + OCACount;

                  stopOrder = StopOrder(Instrument, Side.Sell, qty, stopOCALevel * bar.Close);
                  stopOrder.OCAGroup = "OCA " + Instrument.Symbol + " " + OCACount;
                  stopOrder.Text     = "Stop OCA " + OCACount;

                  limitOrder.Send();
                  stopOrder .Send();

                  OCACount++;
               }

               entryEnabled = false;

               barCount = 0;
            }
      }
      else
      {
         barCount++;

         if (timeExitEnabled && barCount > barsToExit)
         {
            marketOrder = MarketOrder(Instrument, Side.Sell, qty);
            marketOrder.Text = "Time Exit";
            marketOrder.Send();
         }
      }
   }

   public override void OnBar(Bar bar)
   {
      if (bar.Close < bar.Open)
         down = true;
      else
         down = false;
      
      close = bar.Close;
   }

   public override void OnPositionOpened()
   {
      if (stopExitEnabled)
         stop = SetStop(stopLevel, stopType, stopMode);
   }

   public override void OnPositionClosed()
   {
      // cancel OCA

      if (ocaExitEnabled && !(limitOrder.IsFilled || limitOrder.IsCancelled))
         limitOrder.Cancel();

      // can entry again now

      entryEnabled = true;
   }

   public override void OnStopExecuted(ATSStop stop)
   {
      marketOrder = MarketOrder(Instrument, Side.Sell, qty);
      marketOrder.Text = "Stop Exit";
      marketOrder.Send();
   }
}


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

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:  
Powered by phpBB® Forum Software © phpBB Group