NinjaTrader Support Forum  

Go Back   NinjaTrader Support Forum > NinjaScript Development Support > Strategy Development

Strategy Development Support for the development of custom automated trading strategies using NinjaScript.

Reply
 
Thread Tools Display Modes
Old 01-24-2008, 05:36 AM   #1
leemiles
Junior Member
 
Join Date: Jan 2008
Posts: 6
Thanks: 0
Thanked 0 times in 0 posts
Default can't get stop losses or profit targets to execute

hello, i am using the market simulation data, and have a very simple strategy developed for testing purposes only. the buy order goes off, but i can not, for the life of me, get any stop loss or profit target to trigger when running this strategy.

Please advise:

// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
///<summary>
/// 4
///</summary>
[Description("4")]
[Gui.Design.DisplayName(
"My custom strategy4")]
publicclass MyCustomStrategy4 : Strategy
{
#region Variables
// Wizard generated variables
privateint myInput0 = 1; // Default setting for MyInput0
// User defined variables (add any user defined variables below)
#endregion
///<summary>
/// This method is used to configure the strategy and is called once before any strategy method is called.
///</summary>
protectedoverridevoid Initialize()
{
SetStopLoss(
"buy", 1);
SetProfitTarget(
"buy", 1);
CalculateOnBarClose =
true;
}
///<summary>
/// Called on each bar update event (incoming tick)
///</summary>
protectedoverridevoid OnBarUpdate()
{
// Condition set 1
if (Close[0] >= 150)
{
EnterLong(DefaultQuantity,
"Buy");
}
}
#region Properties
[Description(
"")]
[Category(
"Parameters")]
publicint MyInput0
{
get { return myInput0; }
set { myInput0 = Math.Max(1, value); }
}
#endregion
}
}
leemiles is offline  
Reply With Quote
Old 01-24-2008, 06:48 AM   #2
Pete S
Senior Member
 
Join Date: Jul 2007
Location: Fairfax, VA
Posts: 216
Thanks: 0
Thanked 0 times in 0 posts
Default

Does that code even compile? I didn't think there was a SetStopLoss() with that signature. In any case, the default mode for SetStopLoss is currency, which means it will trigger when the price goes below one. These are the lines of code from my strategy I use for those two calls in percentage mode; I specify the parameters as whole numbers (e.g. 5 for 5%).

Code:
if (MyProfit > 0)
   SetProfitTarget("", CalculationMode.Percent, MyProfit/100.0);

if (MyStop > 0)
   SetStopLoss("", CalculationMode.Percent, MyStop/100.0, false);
Last edited by Pete S; 01-24-2008 at 06:52 AM. Reason: Typo
Pete S is offline  
Reply With Quote
Old 01-24-2008, 07:05 AM   #3
leemiles
Junior Member
 
Join Date: Jan 2008
Posts: 6
Thanks: 0
Thanked 0 times in 0 posts
Default still not working..

i used part of your code like this:

protectedoverridevoid Initialize()
{
SetProfitTarget(
"", CalculationMode.Percent, 0.50);
SetStopLoss(
"", CalculationMode.Percent, 0.50, false);

}

and still no stop loss or profit target gets executed.

getting very frustrated since i can not begin trading until i can get stop losses and profit targets to actually execute along with the buy/sell order.

i have many strategies ready to implement, and this is the only part that doesn't work....all my long/short strategies actually execute the order as programmed, but i can't seem to get a stop loss or profit target to go along with it.

am i the only one with this issue? is it because i'm using the market replay simulator? will i have this issue with a live $$ account? lord knows i don't want to try that as a test...
leemiles is offline  
Reply With Quote
Old 01-24-2008, 07:39 AM   #4
NinjaTrader_Ray
Administrator
 
NinjaTrader_Ray's Avatar
 
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
Default

C# is case sensitive. Your entry signal name used when submitting an order ("Buy") is different than what you are using when calling SetStopLoss() ("buy"). Also, your syntax is incorrect.

Please try this:

SetStopLoss("Buy", CaclulationMode.Ticks, 20, false);
SetProfitTarget("Buy", CaclulationMode.Ticks, 40);

CalculateOnBarClose =
true;
NinjaTrader_Ray is offline  
Reply With Quote
Old 01-24-2008, 08:32 AM   #5
leemiles
Junior Member
 
Join Date: Jan 2008
Posts: 6
Thanks: 0
Thanked 0 times in 0 posts
Default still not working..sorry ray...

Ray, still not executing a stop loss or profit target. here's my whole strategy....i am apply this for the ticker symbol AAPL...as you can see, it's a very simple strategy...simply buy when price over 125...that executes at the end of the minute bar. but i never get a stop loss or profit target execution...what else can it be??



#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>
/// 4
///</summary>
[Description("4")]
[Gui.Design.DisplayName(
"My custom strategy4")]
publicclass MyCustomStrategy4 : Strategy
{
#region Variables
// Wizard generated variables
privateint myInput0 = 1; // Default setting for MyInput0
// User defined variables (add any user defined variables below)
#endregion
///<summary>
/// This method is used to configure the strategy and is called once before any strategy method is called.
///</summary>

protectedoverridevoid Initialize()
{
SetStopLoss(
"Buy", CaclulationMode.Ticks, 40, false);
SetProfitTarget(
"Buy", CaclulationMode.Ticks, 40);
CalculateOnBarClose =
true;
}
///<summary>
/// Called on each bar update event (incoming tick)
///</summary>
protectedoverridevoid OnBarUpdate()
{
// Condition set 1
if (Close[0] >= 125)
{
EnterLong(DefaultQuantity,
"Buy");
}
}

protectedoverridevoid OnBarUpdate()




#region Properties
[Description(
"")]
[Category(
"Parameters")]
publicint MyInput0
{
get { return myInput0; }
set { myInput0 = Math.Max(1, value); }
}
#endregion
}
}
leemiles is offline  
Reply With Quote
Old 01-24-2008, 10:02 AM   #6
Pete S
Senior Member
 
Join Date: Jul 2007
Location: Fairfax, VA
Posts: 216
Thanks: 0
Thanked 0 times in 0 posts
Default

.5 = 50%

You want to use .05 if you mean 5%.
Pete S is offline  
Reply With Quote
Old 01-24-2008, 10:55 AM   #7
leemiles
Junior Member
 
Join Date: Jan 2008
Posts: 6
Thanks: 0
Thanked 0 times in 0 posts
Default thanks Pete, but that's not it...

ticks...price...percent...no matter what i use, don't get executed.

in latest example, i tried 40 ticks...tried 10 ticks, 1 tick, 100 ticks

tried price of 0.50, 0.005, 0.05, 5, 10, 1, 100

tried percent... 0.01, 0.001, 0.05, 0.005, 0.50, 1, 5, 10, 100.

doesn't matter what i use, no execution.

maybe there is another setting i need to check? it could just be the code is correct but something else isn't selected?
leemiles is offline  
Reply With Quote
Old 01-24-2008, 10:15 PM   #8
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
Default

Please check your syntax.
SetStopLoss("Buy", CaclulationMode.Ticks, 40, false);

It should be "CalculationMode.Ticks".
NinjaTrader_Josh is offline  
Reply With Quote
Old 11-03-2008, 07:55 PM   #9
r2kTrader
Senior Member
 
Join Date: Sep 2008
Posts: 483
Thanks: 0
Thanked 2 times in 2 posts
Default Should that even compile?

Just curious, if CalculationMode is incorrectly typed, should the strategy even compile?

thx

Quote:
Originally Posted by NinjaTrader_Josh View Post
Please check your syntax.
SetStopLoss("Buy", CaclulationMode.Ticks, 40, false);

It should be "CalculationMode.Ticks".
r2kTrader is offline  
Reply With Quote
Old 11-04-2008, 08:56 AM   #10
NinjaTrader_Ben
NinjaTrader Customer Service
 
NinjaTrader_Ben's Avatar
 
Join Date: May 2008
Location: Denver, CO
Posts: 3,157
Thanks: 0
Thanked 3 times in 3 posts
Default

Hello,


This will not compile. You are correct.
NinjaTrader_Ben is offline  
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Stop Losses not working? zoltran General Programming 2 11-08-2007 04:00 AM
Splitting Profit Targets fifty2aces Strategy Development 1 10-10-2007 08:00 AM
Profit Targets & 'Exit on Close' dgregor5 General Programming 2 07-15-2007 01:35 PM
Printing Current StopLoss & Profit Targets Jim-Boulder Strategy Analyzer 1 06-25-2007 11:16 AM
Profit Targets in Strategy Wizard gregw Strategy Development 3 01-24-2007 09:30 AM


All times are GMT -6. The time now is 02:22 PM.