SmartQuant Discussion

Automated Quantitative Strategy Development, SmartQuant Product Discussion and Technical Support Forums
It is currently Tue May 21, 2019 6:29 pm

All times are UTC + 3 hours




Post new topic Reply to topic  [ 1 post ] 
Author Message
PostPosted: Wed Mar 21, 2007 1:33 pm 
Offline

Joined: Tue Aug 05, 2003 3:43 pm
Posts: 6812
The original Turtle system was made famous because it was supposed to show that anyone could become a good commodities trader—all you needed was to follow some specific rules that implemented a good trading system.

The implementation shown below is based on Altucher’s book, which uses its own choice of 22 week (100 day) and 55 week (385 day) moving averages. This is what he calls a “slow turtle� strategy that is intended to catch very long (and major) trends. He does this by using very long moving averages. The original Turtle system used shorter averages. (It is worth saying that there are many variations of the Turtle system on the Internet.) There are three interesting things about this single-state (always in the market) implementation. It uses very long moving averages (100 and 385 days). It only trades on Monday mornings (in the
OnBarOpen method). And it reverses existing positions by buying or selling a trade quantity that is twice the size of the existing position.

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

using System.Drawing;

public class MyStrategy : Strategy
{
   // quantity to buy on a trade
   [Parameter("Order quantity (number of contracts to trade)")]   
   double Qty = 100;
   
   [Parameter("Bar Block Size")]
   int BarBlockSize = 6;
   
   [Parameter("Length of SMA in blocks (weeks)", "SMA")]
   int FastSMALength = 22;
   
   [Parameter("Length of SMA in blocks (weeks)", "SMA")]
   int SlowSMALength = 55;

   int positionInBlock = 0;
   bool buyOnNewBlock;
   bool sellOnNewBlock;
   
   // two moving averages
   SMA fastSMA;
   SMA slowSMA;
   
   public override void OnStrategyStart()
   {      
      // set up the fast average
      fastSMA = new SMA(Bars, FastSMALength * 7, Color.Yellow);      
      Draw(fastSMA, 0);
      // set up the slow average
      slowSMA = new SMA(Bars, SlowSMALength * 7, Color.Pink);      
      Draw(slowSMA, 0);
   }

   public override void OnBarOpen(Bar bar)
   {
      // calc quantity to reverse a position
      double orderQty = 2 * Qty;

      if (!HasPosition)
         orderQty = Qty;

      if (positionInBlock == 0)
      {
         if (buyOnNewBlock)
         {
            Buy(orderQty, "Reverse to Long");

            buyOnNewBlock = false;
         }

         if (sellOnNewBlock)
         {
            Sell(orderQty, "Reverse to Short");            

            sellOnNewBlock = false;
         }
      }
   }

   public override void OnBar(Bar bar)
   {
      // if our SMAs contain the current bar date
      if (fastSMA.Contains(bar.DateTime) && slowSMA.Contains(bar.DateTime))
      {
         // see which one is above the other
         Cross cross = fastSMA.Crosses(slowSMA, bar);

         if (cross == Cross.Above)
            buyOnNewBlock = true;

         if (cross == Cross.Below)
            sellOnNewBlock = true;
      }

      positionInBlock = (positionInBlock++) % BarBlockSize;
   }
}


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