Welcome to 4XLab.NET : Forex Lab Community Server Sign in | Join | Faq

Automated Trading Systems

Started by asimon at 07-29-2006 8:19 AM. Topic has 1 replies.

Print Search
Sort Posts:    
   07-29-2006, 8:19 AM
asimon is not online. Last active: 1/23/2010 8:15:16 PM asimon

Top 10 Posts
Joined on 03-07-2006
Posts 140
Source Code: Four Percent Model

This is the source code for the trading system used in the tutorial "Finding A Profitable System With The Built-In Optimizer".

This code implements the 4 Percent Model, developed by Ned Davis and popularized by Martin Zweig in the book Winning on Wall Street. This is a very simple trend following model. It selects an initial direction when the price raises or falls by a specified percentage. Once an initial direction is set, it stays on the trade until the price retraces from the maximum or minimum by the specified percentage.


   Report 
   07-29-2006, 8:23 AM
asimon is not online. Last active: 1/23/2010 8:15:16 PM asimon

Top 10 Posts
Joined on 03-07-2006
Posts 140
Source Code: Four Percent Model

//
// 4X Lab.NET © Copyright 2005-2006 ASCSE LLC
// http://www.4xlab.net
// email: 4xlab@4xlab.net
//
using System;
namespace _4XLab.NET
   {
      // add bollinger bands and try to express the percentage of change
      // based on volatility


      public class fPercentageChange : ForexObject
      {
      double iPercentage;
      double iMin;
      double iMax;
      bool FirstTick;
      sTick Tick;
      double Spread;


      public fPercentageChange()
      {
      }


      public override void Init(string pParameters)
      {
         this.InitializeParameters(pParameters,"PERCENT:0.001;");
         iPercentage = PParser.GetDouble("PERCENT",0);
         Framework.TickServer.RegisterTickListener("System","*",
this);
         Framework.WriteGraphLine("InTrade,Margin,Tick,Min,MinTh,Max,MaxTh");
         FirstTick =
true;
      }

      public override void ReceiveTick(sTick pTick)
      {
         Spread = pTick.Ask - pTick.Bid;
         Tick = pTick;
         DecisionFunction();
      }

      public override void ReceiveCandle(sCandle pCandle, int pPeriod, string pCBTitle)
      {
      }

      private void DecisionFunction()
      {
         if (FirstTick)
         {
            FirstTick =
false;
            iMin = Tick.Bid;
            iMax = Tick.Bid;
         }
         else
         {
            if (Framework.Account.InTrade()==1)
            {
               if (Tick.Bid > iMax)
               {
                  iMax = Tick.Bid;
               }
         
               if ((iMax-Tick.Bid) > iPercentage*iMax)
               {
                  Framework.Account.ExitTrade();
                  Framework.Account.EnterTrade(-1);
                  iMin = Tick.Bid;
                  iMax = 0;
               }
            }

            if (Framework.Account.InTrade()==-1)
            {
               if (Tick.Bid < iMin)
               {
                  iMin = Tick.Bid;
               }
      
               if ((Tick.Bid-iMin) > iPercentage*iMin)
               {
                  Framework.Account.ExitTrade();
                  Framework.Account.EnterTrade(1);
                  iMax = Tick.Bid;
                  iMin = 0;
               }
            }

            if (Framework.Account.InTrade()==0)
            {
               if ((Tick.Bid-iMin) > iPercentage*iMin)
               {
                  Framework.Account.EnterTrade(1);
                  iMax = Tick.Bid;
                  iMin = 0;
               }


               if ((iMax-Tick.Bid) > iPercentage*iMax)
               {
                  Framework.Account.EnterTrade(-1);
                  iMin = Tick.Bid;
                  iMax = 0;
               }
            }
         }

         double logInTrade = Framework.Account.InTrade();
         double logMargin = Framework.Account.GetAccount().C;
         double MinTh = iMin + iMin*iPercentage;
         double MaxTh = iMax - iMax*iPercentage;
         Framework.WriteGraphLine(logInTrade+","+logMargin+","+Tick.Bid+","+iMin+","+MinTh+","+iMax+","+MaxTh);
      }

      public override bool Finished()
      {
         bool returnv = true;
         return returnv;
      }

      public override string Title()
      {
         return "% Trend Change";
      }

   }
}


   Report 
4XLab.NET : For... » 4XLab.NET » Automated Tradi... » Source Code: Four Percent Model

Powered by Community Server, by Telligent Systems