Welcome to NexusFi: the best trading community on the planet, with over 150,000 members Sign Up Now for Free
Genuine reviews from real traders, not fake reviews from stealth vendors
Quality education from leading professional traders
We are a friendly, helpful, and positive community
We do not tolerate rude behavior, trolling, or vendors advertising in posts
We are here to help, just let us know what you need
You'll need to register in order to view the content of the threads and start contributing to our community. It's free for basic access, or support us by becoming an Elite Member -- see if you qualify for a discount below.
-- Big Mike, Site Administrator
(If you already have an account, login at the top of the page)
Broker: Tradestation/Tradestation, NinjaTrader, FXCM and Tallinex
Trading: ES, CL, EUR/USD, TF
Posts: 173 since Aug 2009
Thanks Given: 105
Thanks Received: 61
hmm, actually I am still new to Ninjascript. It seems like Position.AvgPrice is the average representation of the fill price. from the documentation
{
// Raise stop loss to breakeven when there is at least 10 ticks in profit
if (Close[0] >= Position.AvgPrice + 10 * TickSize)
ExitLongStop(Position.Quantity, Position.AvgPrice);
}
So, we probably don't need IOrder, but I hear that most professionals will only use them with real live strategies. We can always take IOrder out and replace _entry.AvgFillPrice with Position.AvgPrice and just add a simple SMA cross strategy or something.
hmm, actually I am still new to Ninjascript. It seems like Position.AvgPrice is the average representation of the fill price. from the documentation
{
// Raise stop loss to breakeven when there is at least 10 ticks in profit
if (Close[0] >= Position.AvgPrice + 10 * TickSize)
ExitLongStop(Position.Quantity, Position.AvgPrice);
}
So, we probably don't need IOrder, but I hear that most professionals will only use them with real live strategies. We can always take IOrder out and replace _entry.AvgFillPrice with Position.AvgPrice and just add a simple SMA cross strategy or something.
Broker: Tradestation/Tradestation, NinjaTrader, FXCM and Tallinex
Trading: ES, CL, EUR/USD, TF
Posts: 173 since Aug 2009
Thanks Given: 105
Thanks Received: 61
Hey calhawk01
I think this code pretty much works.
I did not use IOrders. You can always take out the reason to get long or short. You can also play with the drawLine things.
However, I believe it works or at least it is a general outline to what you were looking for
take care
Spencer
#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.Gui.Chart;
using NinjaTrader.Strategy;
#endregion
// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
/*
Author: Spencer Davis
This is an outline Trade Management strategy for using dynamic stops and targets
*/
[Description("A strategy that trails the stops")]
public class sdTM : Strategy
{
//periods
private int fastPeriod = 8;
private int slowPeriod = 34;
//trade management
private int stopLoss = 18;
private int target1 = 10;
private int target2 = 20;
private int target3 = 30;
private SMA fastSMA;
private SMA slowSMA;
private bool drawStop = false;
protected override void Initialize()
{
CalculateOnBarClose = true;
EntryHandling = EntryHandling.UniqueEntries;
fastSMA = SMA(FastPeriod);
slowSMA = SMA(SlowPeriod);
Add(slowSMA);
Add(fastSMA);
fastSMA.Plots[0].Pen.Width = 2;
fastSMA.Plots[0].Pen.Color = Color.Green;
slowSMA.Plots[0].Pen.Width = 2;
slowSMA.Plots[0].Pen.Color = Color.Red;
}
protected override void OnBarUpdate()
{
if (CurrentBar < 20) return; //make sure there is enough data
//buy conditions
if (CrossAbove(SMA(FastPeriod), SMA(SlowPeriod), 1)&& Flat)
GoLong();
else if (CrossBelow(SMA(FastPeriod),SMA(SlowPeriod),1) && Flat)
GoShort();
ManageTrade();
}
private void ManageTrade()
{
if(Long)
{
if (!drawStop)
this.DrawLine("StopLine"+CurrentBar, true, 0, (Position.AvgPrice - StopLoss*TickSize), -8, (Position.AvgPrice -StopLoss*TickSize), Color.Red, DashStyle.Dash, 2);
if (High[0] >= (Position.AvgPrice + Target1*TickSize) && High[0] < (Position.AvgPrice + Target2*TickSize))
{
SetStopLoss("EntryLong", CalculationMode.Price, Position.AvgPrice, false);
this.DrawLine("BreakEvenLong"+CurrentBar,true,0,(Position.AvgPrice + Target1*TickSize), -8, (Position.AvgPrice + Target1*TickSize), Color.Gray, DashStyle.Dash, 2);
}
if (High[0] >= (Position.AvgPrice + Target2*TickSize)&& High[0] < (Position.AvgPrice + Target3*TickSize))
{
SetStopLoss("EntryLong", CalculationMode.Price, Position.AvgPrice + Target1*TickSize, false);
this.DrawLine("Target1Long"+CurrentBar,true,0,(Position.AvgPrice + Target2*TickSize), -8, (Position.AvgPrice + Target2*TickSize), Color.Green, DashStyle.Dash, 2);
}
if (High[0] >= (Position.AvgPrice + Target3*TickSize))
{
ExitLong(1, "Exiting Long", "EntryLong");
this.DrawLine("Target2Long"+CurrentBar,true,0,(Position.AvgPrice + Target3*TickSize), -8, (Position.AvgPrice + Target3*TickSize), Color.Green, DashStyle.Dash, 2);
}
drawStop = true;
}
if(Short)
{
if (!drawStop)
this.DrawLine("StopLine"+CurrentBar, true, 0, (Position.AvgPrice + StopLoss*TickSize), -8, (Position.AvgPrice + StopLoss*TickSize), Color.Red, DashStyle.Dash, 2);
if (Low[0] <= (Position.AvgPrice - Target1*TickSize)&& Low[0] > (Position.AvgPrice - Target2*TickSize))
{
SetStopLoss("EntryShort", CalculationMode.Price, Position.AvgPrice, false);
this.DrawLine("BreakEvenShort"+CurrentBar,true,0,(Position.AvgPrice - Target1*TickSize), -8, (Position.AvgPrice - Target1*TickSize), Color.Gray, DashStyle.Dash, 2);
}
if (Low[0] <= (Position.AvgPrice - Target2*TickSize)&& Low[0] > (Position.AvgPrice - Target3*TickSize))
{
SetStopLoss("EntryShort", CalculationMode.Price, Position.AvgPrice - Target1*TickSize, false);
this.DrawLine("Target1Long"+CurrentBar,true,0,(Position.AvgPrice - Target2*TickSize), -8, (Position.AvgPrice - Target2*TickSize), Color.Green, DashStyle.Dash, 2);
}
if (Low[0] <= (Position.AvgPrice - Target3*TickSize))
{
ExitShort(1, "Exiting Short", "EntryShort");
this.DrawLine("Target2Long"+CurrentBar,true,0,(Position.AvgPrice - Target3*TickSize), -8, (Position.AvgPrice - Target3*TickSize), Color.Green, DashStyle.Dash, 2);
}
drawStop = true;
}
}
private void GoLong()
{
SetStopLoss("EntryLong", CalculationMode.Ticks, StopLoss, false);
EnterLong(1, "EntryLong");
drawStop = false;
}
private void GoShort()
{
SetStopLoss("EntryShort", CalculationMode.Ticks, StopLoss, false);
EnterShort(1, "EntryShort");
drawStop = false;
}
//returns current state of Flat, Long, or Short
#region TradeState
private bool Flat { get{ return Position.MarketPosition == MarketPosition.Flat; } }
private bool Long { get{ return Position.MarketPosition == MarketPosition.Long; } }
private bool Short { get{ return Position.MarketPosition == MarketPosition.Short; } }
#endregion
#region Properties
[Description("Fast Period")]
[GridCategory("Parameters")]
public int FastPeriod
{
get { return fastPeriod; }
set { fastPeriod = Math.Max(1, value); }
}
[Description("Slow Period")]
[GridCategory("Parameters")]
public int SlowPeriod
{
get { return slowPeriod; }
set { slowPeriod = Math.Max(1, value); }
}
[Description("Stop Loss")]
[GridCategory("Parameters")]
public int StopLoss
{
get { return stopLoss; }
set { stopLoss = Math.Max(1, value); }
}
[Description("1st Target")]
[GridCategory("Parameters")]
public int Target1
{
get { return target1; }
set { target1 = Math.Max(1, value); }
}
[Description("2nd Target")]
[GridCategory("Parameters")]
public int Target2
{
get { return target2; }
set { target2 = Math.Max(1, value); }
}
[Description("3rd Target")]
[GridCategory("Parameters")]
public int Target3
{
get { return target3; }
set { target3 = Math.Max(1, value); }
}
#endregion
}
}
In this video tutorial I show how to create a strategy from scratch (not using the wizard, which I never use).
The strategy contains a few optimizable parameters such as SMA length, EMA length, HMA length, three different targets with custom tick settings on each, a stop size, and the option to move target 2 to breakeven after target 1 is hit, as well as move target 3 to breakeven after target 2 is hit.
[Mike
I got the following Recursive Property error when I compiled the strategy (see code below):
Your strategy likely holds one or more recursive properties which could cause NinjaTrader to crash: EMAlength HMALENGTH TARGET1 TARGET2 TARGET3 BE2 BE3
Do you want to edit these properties?
I have attached the code I created. Please note I did not use the SMA. Can anyone please point out what I typed in wrong?
Thank you.
JLW
#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.Gui.Chart;
using NinjaTrader.Strategy;
#endregion
// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
/// <summary>
/// Enter the description of your strategy here
/// </summary>
[Description("Create a strategy with optimizable variables")]
public class testbmt : Strategy
{
private int emalength = 20;
private int hmalength = 10;
private int target1 = 12;
private int target2 = 10;
private int target3 = 18;
/// <summary>
/// This method is used to configure the strategy and is called once before any strategy method is called.
/// </summary>
protected override void Initialize()
{
CalculateOnBarClose = true;
EntryHandling = EntryHandling.UniqueEntries;
}
Broker: Tradestation/Tradestation, NinjaTrader, FXCM and Tallinex
Trading: ES, CL, EUR/USD, TF
Posts: 173 since Aug 2009
Thanks Given: 105
Thanks Received: 61
jlwade123
I got the following Recursive Property error when I compiled the strategy (see code below):
Your strategy likely holds one or more recursive properties which could cause NinjaTrader to crash: EMAlength HMALENGTH TARGET1 TARGET2 TARGET3 BE2 BE3
Do you want to edit these properties?
I have attached the code I created. Please note I did not use the SMA. Can anyone please point out what I typed in wrong?
Thank you.
JLW
#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.Gui.Chart;
using NinjaTrader.Strategy;
#endregion
// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
/// <summary>
/// Enter the description of your strategy here
/// </summary>
[Description("Create a strategy with optimizable variables")]
public class testbmt : Strategy
{
private int emalength = 20;
private int hmalength = 10;
private int target1 = 12;
private int target2 = 10;
private int target3 = 18;
/// <summary>
/// This method is used to configure the strategy and is called once before any strategy method is called.
/// </summary>
protected override void Initialize()
{
CalculateOnBarClose = true;
EntryHandling = EntryHandling.UniqueEntries;
}
This will probably give you a bunch of errors.
(....) are for functions and arguments
{....} is for blocks of code --- everything that goes inside of a method, class, namespace
You mention in the video your running ninja on a virtual machine. Could you amplify on this. I guess more specifically
I am wondering if this gives some enhanced level of security for your code etc. From who, Ninja, Hackers etc.
and what virtual machine do you recommend?
You mention in the video your running ninja on a virtual machine. Could you amplify on this. I guess more specifically
I am wondering if this gives some enhanced level of security for your code etc. From who, Ninja, Hackers etc.
and what virtual machine do you recommend?
Thanks
Jerry
I am not sure if those are the reasons Big Mike does it, but the following advantages come to my mind when running it on a Virtual Machine:
1. You can run a 2. instance of NinjaTrader
2. Assuming you are connecting to a demo account on your VM: Your testing of strategies etc. won't interfere with your live trading accounts (depending on how you test this could happen if you test it on your primary NT connected to your live accounts)
Hence: you completely keep your live trading and your research separated
You mention in the video your running ninja on a virtual machine. Could you amplify on this. I guess more specifically
I am wondering if this gives some enhanced level of security for your code etc. From who, Ninja, Hackers etc.
and what virtual machine do you recommend?
Thanks
Jerry
I use Amazon S3 for free to house my Ninja data backup. I have been thinking increasing storage/memory from Amazon to use as a sandbox for my strategy development. Amazon is pretty cheap.