SmartQuant Discussion

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

All times are UTC + 3 hours




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Crossover
PostPosted: Mon Jan 16, 2006 5:30 pm 
Offline

Joined: Tue Aug 05, 2003 3:43 pm
Posts: 6802
Code:
public class Crossover_ATS : ATSComponent
{
   int length1 = 3;
   int length2 = 7;

   Color color1 = Color.Red;
   Color color2 = Color.Yellow;

   SMA sma1;
   SMA sma2;

   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 crossoverExitEnabled = true;
   bool ocaExitEnabled       = true;
   bool stopExitEnabled      = true;

   SingleOrder marketOrder, limitOrder, stopOrder;

   double qty = 100;

   [Category("SMA")]
   public int Length1
   {
      get { return length1; }
      set { length1 = value; }
   }

   [Category("SMA")]
   public int Length2
   {
      get { return length2; }
      set { length2 = value; }
   }

   [Category("SMA")]
   public Color Color1
   {
      get { return color1; }
      set { color1 = value; }
   }

   [Category("SMA")]
   public Color Color2
   {
      get { return color2; }
      set { color2 = value; }
   }

   [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("Exit")]
   public bool CrossoverExitEnabled
   {
      get { return crossoverExitEnabled; }
      set { crossoverExitEnabled = value; }      
   }

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

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

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

   public Crossover_ATS() : base()
   {
      
   }

   public override void Init()
   {
      BarSeries series = DataManager.Bars[Instrument];

      sma1 = new SMA(series, length1, BarData.Close, color1);
      sma2 = new SMA(series, length2, BarData.Close, color2);

        Draw(sma1, 0);
        Draw(sma2, 0);
   }

   public override void OnBar(Bar bar)
   {
      ECross cross = sma1.Crosses(sma2, bar.DateTime);

      if (entryEnabled)
      {
         if (cross == ECross.Above)
         {
            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;
         }
      }
      else
      {
         if (crossoverExitEnabled)
            if (cross == ECross.Below)
            {
               marketOrder = MarketOrder(Instrument, Side.Sell, qty);
               marketOrder.Text = "Crossover Exit";
               marketOrder.Send();
            }
      }
   }

   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  
 
 Post subject:
PostPosted: Sun Jun 04, 2006 6:33 am 
Offline

Joined: Tue Aug 02, 2005 8:30 am
Posts: 45
Hi, Please update this so it will compile. Also, please post the corrections to the tutorial.

Thanks,

Sam


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 04, 2006 5:02 pm 
Offline

Joined: Thu Dec 08, 2005 7:05 pm
Posts: 438
Location: Sydney, Australia
These three strategies listed here are the build in ones, check under ATS components.

-Mark


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 07, 2009 6:17 pm 
Offline

Joined: Mon May 18, 2009 10:10 pm
Posts: 23
Hi,

I would like to go long on crossover in 5 mins timeframe if 60min Fast MA > 60mins Slow MA and 30 min fast MA > 30 min slow MA.
How do you handle this multi timeframe ? Does anyone know how to add 60min and 30min timeframe in above crossover code ?

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 07, 2009 6:31 pm 
Offline

Joined: Thu Sep 20, 2007 6:43 pm
Posts: 122
Location: Poland
Hi :)

jamesbond wrote:
How do you handle this multi timeframe ? Does anyone know how to add 60min and 30min timeframe in above crossover code ?


Look at this: http://www.smartquant.com/forums/viewtopic.php?t=6358

Regards
MaRCHeW

_________________
"Your worst day with OpenQuant is better than your best day with most of the other stuff."


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 07, 2009 8:41 pm 
Offline

Joined: Mon May 18, 2009 10:10 pm
Posts: 23
Hi,

How do I write if Fast MA > Slow MA ? Not FastMA.Crosses (SlowMA,bar).

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 07, 2009 8:47 pm 
Offline

Joined: Tue Aug 05, 2003 3:43 pm
Posts: 6802
if (FastMA.Last > SlowMA.Last)
...

or

if (FastMA[0] > SlowMA[0])
...

Regards,
Anton


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 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:  
Powered by phpBB® Forum Software © phpBB Group