![]() |
|
|||||||
| Strategy Development Support for the development of custom automated trading strategies using NinjaScript. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Senior Member
Join Date: Jan 2008
Posts: 147
Thanks: 0
Thanked 0 times in 0 posts
|
I had this over in general programming qith a different question but think it might be better here.
I attempted to adda trade counter to this so it doesnt trade if there have been more than 3 trades in the session. It compiles ok, but when I ran it no orders came up after the first 20 minutes as expected. Is there something wrong with the the orders part of this that someone can see? Any help would be much appreciated. Thanks Code:
// // Copyright (C) 2007, NinjaTrader LLC <www.ninjatrader.com>. // NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release. // #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 // This namespace holds all strategies and is required. Do not change it. namespace NinjaTrader.Strategy { /// <summary> /// Simple strategy that monitors for a breakout. /// </summary> [Description("Simple strategy that monitors for a breakout.")] public class SampleBreakoutStrategy : Strategy { #region Variables private double highestHigh = 0; private double lowestLow = 0; private int tradeCounter = 0; #endregion /// <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; } /// <summary> /// Called on each bar update event (incoming tick) /// </summary> protected override void OnBarUpdate() { // Resets the highest high and lowest low at the start of every new session if (Bars.FirstBarOfSession && FirstTickOfBar) { tradeCounter = 0; highestHigh = High[0]; lowestLow = Low[0]; } // Stores the highest high from the first 30 bars if (Bars.BarsSinceSession < 30 && High[0] > highestHigh) highestHigh = High[0]; // Stores the lowest low from the first 30 bars if (Bars.BarsSinceSession < 30 && Low[0] < lowestLow) lowestLow = Low[0]; //Enter Long at highest high + 1 of first 30 min. trade if number of trades today is less than 3 //Enter Short at lowest low - 1 of first 30 min. trade if number of trades today is less than 3 if (tradeCounter < 3) { EnterLongStop(highestHigh + TickSize); tradeCounter++; EnterShortStop(lowestLow - TickSize); tradeCounter++; } } } } |
|
|
|
|
|
#2 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
|
maninjapan,
You cannot submit simultaneous working orders in opposite directions at the same time. Please choose either a long or short. If you want to use the other, you can cancel the first and then submit the second. Also, your counter would not fly in this instance. You need to keep resubmitting your orders to keep them alive and because you are incrementing your counter every single time you keep the order alive, you hit your 3 limit very quickly and then stop keeping it alive.
Josh
NinjaTrader Customer Service |
|
|
|
|
|
#3 |
|
Senior Member
Join Date: Jan 2008
Posts: 147
Thanks: 0
Thanked 0 times in 0 posts
|
ok, well I want t the system not to trade more than 3 times a day, is this not the correct code to be using then?
|
|
|
|
|
|
#4 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
|
Sure, but you are incrementing the counter too early. You should only increment the counter when you actually get filled, not when you simply submit the order. As stated in my previous post, when you keep incrementing on submission, you increment on every single bar that keeps the order alive and then you exceed your limit without even trading once.
Josh
NinjaTrader Customer Service |
|
|
|
|
|
#5 |
|
Senior Member
Join Date: Jan 2008
Posts: 147
Thanks: 0
Thanked 0 times in 0 posts
|
Im currently trying testing the current breakout srtategy and am running it on SB and KC, both are set with the same starting time 8:00am, so I would expect an order on one side or the other the moment the clock ticks past 8:30am. I got a print in the output window with the high/low and an order for SB at 8:31 but didnt get anything from KC until 8:33. and after looking at the high and lows it used it counted the bars all the way up to 8:32.
Am I doing something wrong in the code here? Code:
protected override void Initialize()
{
CalculateOnBarClose = true;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
// Resets the highest high and lowest low at the start of every new session
if (Bars.FirstBarOfSession && FirstTickOfBar)
{
highestHigh = High[0];
lowestLow = Low[0];
}
// Stores the highest high from the first 30 bars
if (Bars.BarsSinceSession < 30 && High[0] > highestHigh)
highestHigh = High[0];
// Stores the lowest low from the first 30 bars
if (Bars.BarsSinceSession < 30 && Low[0] < lowestLow)
lowestLow = Low[0];
//Print the symbol and Highs and Lows of the first 30 minutes
if (Bars.BarsSinceSession == 30 )
PrintWithTimeStamp(Instrument.FullName + " " + "High" + " " + highestHigh + " " + "Low" + lowestLow);
//Setup the Stop in the direction of close to the high/low midpoint
if (Bars.BarsSinceSession >= 30 && Close[0] > (highestHigh + lowestLow)/2)
//Enter Long at highest high + 1 of first 30 min.
EnterLongStop(highestHigh + TickSize);
else if (Bars.BarsSinceSession >= 30 && Close[0] < (highestHigh + lowestLow)/2)
//Enter Short at lowest low - 1 of first 30 min.
EnterShortStop(lowestLow - TickSize);
|
|
|
|
|
|
#6 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
|
What you should do is print on each step of the way and find where you have the bug in your code.
Josh
NinjaTrader Customer Service |
|
|
|
|
|
#7 |
|
Senior Member
Join Date: Jan 2008
Posts: 147
Thanks: 0
Thanked 0 times in 0 posts
|
|
|
|
|
|
|
#8 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
|
maninjapan,
For debugging, please see this tip: http://www.ninjatrader-support2.com/...ead.php?t=3418
Josh
NinjaTrader Customer Service |
|
|
|
|
|
#9 |
|
Senior Member
Join Date: Jan 2008
Posts: 147
Thanks: 0
Thanked 0 times in 0 posts
|
Thanks a lot, Ill go through it. Just one quick question first.
I am getting the highs and lows not only for todays sessions but for the previous 3 or 4 days as well. How do adjust this so its only calling today's High and Low? Code:
if (Bars.BarsSinceSession == 30 )
PrintWithTimeStamp(Instrument.FullName + " " + "High" + " " + highestHigh + " " + "Low" + lowestLow);
Thanks for your help |
|
|
|
|
|
#10 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
|
maninjapan,
When your code processes 3 or 4 days ago, it is processing the data moving forward so during those days the prints came out of, they were actually the current day and you will for sure see them.
Josh
NinjaTrader Customer Service |
|
|
|
|
|
#11 |
|
Senior Member
Join Date: Jan 2008
Posts: 147
Thanks: 0
Thanked 0 times in 0 posts
|
SO I cant adjust this so it just returns the numbers for the current session?
|
|
|
|
|
|
#12 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
|
Current session is relative. Every session is the current session when it is being processed. If you want an absolute session you should just use Time[] comparisons and check dates.
Josh
NinjaTrader Customer Service |
|
|
|
|
|
#13 |
|
Senior Member
Join Date: Jan 2008
Posts: 147
Thanks: 0
Thanked 0 times in 0 posts
|
well, Ive spent about 6 hours on this today, trying to figure out where this strategy is going wrong. And I havent really got anywhere.
I've had a go at attempting to debug with OnOrderUpdate and found out real quick why its labeled advanced..... What I can get in the output window are the highs and lows of what is supposed to be the first 30 bars, but depending on the symbol sometimes it is taking the highs and lows up to the 32nd or 33rd bar before posting in the output window. Does anyone have any comments based on the code below??? OR perhaps an example for dummies on how to post orders to the output window.... |
|
|
|
|
|
#14 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
|
maninjapan,
I suggest you print values on every single bar and take it one step at a time by hand. Print(Time[0] + " " + CurrentBar + " " + highestHigh + " " + lowestLow); Then you want to compare what you were doing by hand versus what was actually done by the strategy.
Josh
NinjaTrader Customer Service |
|
|
|
|
|
#15 |
|
Senior Member
Join Date: Jan 2008
Posts: 147
Thanks: 0
Thanked 0 times in 0 posts
|
just tried this on CAT, it printed every minute with the highs/lows as expected, however the other print that I already had in there myself,
Code:
//Print the symbol and Highs and Lows of the first 30 minutes
if (Bars.BarsSinceSession == 30 )
PrintWithTimeStamp(Instrument.FullName + " " + "High" + " " + highestHigh + " " + "Low" + lowestLow);
The order also appeared at the same time, so it was either 'stuck' for some reason, or my code is wrong (highly probable). The thing is that this worked fine on AAPL which I tried before CAT.... This is the problem that I ahd been experiencing..... Any ideas on this would be greatly appreciated, Thanks |
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Opening Range indicator | mod99 | Indicator Development | 15 | 09-03-2011 02:06 PM |
| Opening range breakout | bradypreston | General Programming | 7 | 02-28-2010 05:13 AM |
| Openning range breakout | Evgeniy_T | Strategy Development | 6 | 10-01-2009 06:14 AM |
| Please help with Opening Range Breakout Strategy | OrderFlow | Strategy Development | 2 | 08-03-2009 04:30 AM |
| Open Range Breakout with Strategy Wizard | SNIP07 | Strategy Development | 1 | 07-07-2009 05:22 AM |