PDA

View Full Version : Strategy based on Fast Stochastics


Trader Rob
10-12-2007, 01:16 PM
Hello,

Because I am not familiar with C#, I would prefer to devellop the following strategy for future trading with the wizzard and wonder whether this is possible.

Conditions for Fast Stochastic to fulfill all for entry Long:
Value of %K > %D
%K is ascending
%D is ascending
(%K and %D) < 25
AND
8:30 < Actual time < 21:45
(Market hours are from 8:00 - 22:00, but I don't want to go in the market at the opening and 15 minutes before closing of the market)

(For short positions I will have to do the contrairy)

At 21:55 I want to close any postion (short or long)

If it is not possible to create with the wizzard I will appreciate a script for this (still basic) strategy.

I am looking forward for any help.

NinjaTrader_Ray
10-12-2007, 01:20 PM
Yes this is possible using the wizard.

You will have to get familiar with the wizard of course. Following is a link to a Tutorial in building a strategy in the wizard.

http://www.ninjatrader-support.com/HelpGuideV6/CreatingTheSrategyViaTheWizard.html

joinfree
10-29-2007, 07:55 AM
here is your code...all the best...god bless

#region Using declarations
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Data;
using NinjaTrader.Indicator;
using NinjaTrader.Strategy;
#endregion

namespace NinjaTrader.Strategy
{
///<summary>
/// Conditions for Fast Stochastic to fulfill all for entry Long: Value of %K > %D, %K is ascending, %D is ascending,(%K and %D) < 25
///</summary>
[Description("Conditions for Fast Stochastic to fulfill all for entry Long: Value of %K > %D, %K is ascending, %D is ascending,(%K and %D) < 25")]
[Gui.Design.DisplayName("Strategy based on Fast Stochastics (http://www.ninjatrader-support.com/vb/showthread.php?p=17614#post17614)")]
publicclass StrategybasedonFastStochastics (http://www.ninjatrader-support.com/vb/showthread.php?p=17614#post17614) : Strategy
{
#region Variables
// Wizard generated variables
privateint myInput0 = 1; // Default setting for MyInput0

#endregion
///<summary>

///</summary>
protectedoverridevoid Initialize()
{
Add(StochasticsFast(3, 14));
SetProfitTarget("", CalculationMode.Ticks, 4);
SetStopLoss("", CalculationMode.Ticks, 4, false);
CalculateOnBarClose = true;
}
///<summary>
/// Called on each bar update event (incoming tick)
///</summary>
protectedoverridevoid OnBarUpdate()
{
// Condition set 1
if (StochasticsFast(3, 14).K[0] > StochasticsFast(3, 14).D[0]
&& StochasticsFast(3, 14).D[0] < 25
&& StochasticsFast(3, 14).K[0] < 25
&& StochasticsFast(3, 14).D[0] > StochasticsFast(3, 14).D[1]
&& StochasticsFast(3, 14).D[2] > StochasticsFast(3, 14).D[3]
&& StochasticsFast(3, 14).K[0] > StochasticsFast(3, 14).K[1]
&& StochasticsFast(3, 14).K[2] > StochasticsFast(3, 14).K[3])
{
EnterLong(DefaultQuantity, "");
}
}
#region Properties
[Description("")]
[Category("Parameters")]
publicint MyInput0
{
get { return myInput0; }
set { myInput0 = Math.Max(1, value); }
}
#endregion
}
}

KBJ
10-29-2007, 09:51 PM
I tweaked it a little and thought you might find it interesting.

Several parameters were added to the strategy so it could be run through the optimizer to see what kind of potential it might have, and although I was able to make it look profitable (see screen shot), it would have to be tested on more than a 6-week time period before I'd want to throw any money at it. But I think you'll agree it's going in the right direction.

Enjoy!


#region Using declarations
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Data;
using NinjaTrader.Indicator;
using NinjaTrader.Strategy;
#endregion

// Found on NinjaTrader Forum 29-Oct-2007
// Ran Optimizer on it and found that it likes these parameters:
// PeriodD=16, PeriodK=17, ProfitTarget=16, StoLongThreshold=15, StopLoss=12
// when used with a 50-tick period from 10-Sep-2007 to 26-Oct-2007 on the ES 12-07 contract.
//
// I have only backtested and NOT live tested this. Caution: performance may vary.
//
// KBJ


namespace NinjaTrader.Strategy
{
///<summary>
/// Conditions for Fast Stochastic to fulfill all for entry Long: Value of %K > %D, %K is ascending, %D is ascending,(%K and %D) < 25
///</summary>
[Description("Conditions for Fast Stochastic to fulfill all for entry Long: Value of %K > %D, %K is ascending, %D is ascending,(%K and %D) < 25")]
[Gui.Design.DisplayName("Strategy based on Fast Stochastics")]
public class FastStochasticsStrategy : Strategy
{
#region Variables
// Wizard generated variables
private int periodD = 3; // Default setting for PeriodD
private int periodK = 14; // Default setting for PeriodK
private int profitTarget = 4; // Default setting for ProfitTarget
private int stopLoss = 4; // Default setting for StopLoss
private int stoLongThreshold = 25; // Default setting for StoLongThreshold
#endregion
///<summary>

///</summary>
protected override void Initialize()
{
Add(StochasticsFast(PeriodD, PeriodK));
SetProfitTarget("", CalculationMode.Ticks, ProfitTarget);
SetStopLoss("", CalculationMode.Ticks, StopLoss, false);
CalculateOnBarClose = true;
}
///<summary>
/// Called on each bar update event (incoming tick)
///</summary>
protected override void OnBarUpdate()
{
// Condition set 1
if ( StochasticsFast(PeriodD, PeriodK).K[0] > StochasticsFast(PeriodD, PeriodK).D[0]
&& StochasticsFast(PeriodD, PeriodK).D[0] < StoLongThreshold
&& StochasticsFast(PeriodD, PeriodK).K[0] < StoLongThreshold
&& StochasticsFast(PeriodD, PeriodK).D[0] > StochasticsFast(PeriodD, PeriodK).D[1]
&& StochasticsFast(PeriodD, PeriodK).D[1] > StochasticsFast(PeriodD, PeriodK).D[2]
&& StochasticsFast(PeriodD, PeriodK).K[0] > StochasticsFast(PeriodD, PeriodK).K[1]
&& StochasticsFast(PeriodD, PeriodK).K[1] > StochasticsFast(PeriodD, PeriodK).K[2]
)
{
EnterLong(DefaultQuantity, "");
}
}

#region Properties
[Description("Period used for calculating the %D of the StochasticsFast indicator.")]
[Category("Parameters")]
public int PeriodD
{
get { return periodD; }
set { periodD = Math.Max(1, value); }
}

[Description("Period used for calculating the %K of the StochasticsFast indicator.")]
[Category("Parameters")]
public int PeriodK
{
get { return periodK; }
set { periodK = Math.Max(1, value); }
}

[Description("Where to take a profit in number of ticks of profit to take.")]
[Category("Parameters")]
public int ProfitTarget
{
get { return profitTarget; }
set { profitTarget = Math.Max(1, value); }
}

[Description("Where to set the stop loss in number of ticks of loss to tolerate.")]
[Category("Parameters")]
public int StopLoss
{
get { return stopLoss; }
set { stopLoss = Math.Max(1, value); }
}

[Description("Look for FastStochastics %K & %D indicators both being below this value before initiating a trade.")]
[Category("Parameters")]
public int StoLongThreshold
{
get { return stoLongThreshold; }
set { stoLongThreshold = Math.Max(1, value); }
}

#endregion
}
}