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 12-15-2008, 09:11 PM   #1
cassb
Senior Member
 
Join Date: Nov 2007
Location: Victor, NY
Posts: 577
Thanks: 5
Thanked 4 times in 4 posts
Send a message via Yahoo to cassb Send a message via Skype™ to cassb
Default Dynamic Stoploss

I want to be able to place a market order and set the stoploss for that order based on some criteria. Right now, this is how I'm doing it:

Code:
            myEntry = EnterLong(contracts,"Going Long");  // Enter long market order
            if (target > 0) SetProfitTarget(CalculationMode.Ticks, target);
            double stop = <some criteria that returns a stop price or 0>;
            if (stop > 0)  SetStopLoss(CalculationMode.Ticks, stop);
            else SetStopLoss(CalculationMode.Ticks, 0);
But when an order is created, there is no stoploss set. The profit target works fine, but why is the stoploss not working?
Price-Dynamics.com
A NinjaTrader Official Partner

Visit http://www.price-dynamics.com to learn more about the Rhythm Relay trading system. Yes, you can discretionary or auto-trade with 75%+ accuracy!

Use our Contact Us page to request free access to our daily Trader's Lounge where you can see the system in action in a live account!
cassb is offline  
Reply With Quote
Old 12-16-2008, 05:25 AM   #2
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,377
Thanks: 252
Thanked 966 times in 949 posts
Default

Hi, did you use TraceOrders to debug? http://www.ninjatrader-support.com/H...aceOrders.html

You also want to use Print() statements to debug your conditions - http://www.ninjatrader-support2.com/...ead.php?t=3418

To see sample code how to change the price of a StopLoss, please see this reference samlpe - http://www.ninjatrader-support2.com/...ead.php?t=3222
NinjaTrader_Bertrand is offline  
Reply With Quote
Old 12-16-2008, 06:20 AM   #3
mrlogik
Certified NinjaScript Consultant
 
mrlogik's Avatar
 
Join Date: Sep 2006
Location: New York, USA
Posts: 774
Thanks: 1
Thanked 7 times in 5 posts
Default

cassb,

Also, check the Log tab in your control center.

What you have should work.
"You look closely enough, you can find everything has a ... weak spot where it can break, sooner or later"

PureLogikTrading
mrlogik is offline  
Reply With Quote
Old 12-16-2008, 07:33 AM   #4
cassb
Senior Member
 
Join Date: Nov 2007
Location: Victor, NY
Posts: 577
Thanks: 5
Thanked 4 times in 4 posts
Send a message via Yahoo to cassb Send a message via Skype™ to cassb
Default

Quote:
Originally Posted by mrlogik View Post
cassb,

Also, check the Log tab in your control center.

What you have should work.

Good thinking, Mr. I do see an error in the log:

Calculated stop order price for strategy 'TJ_BPS ' was smaller/equal 0. No stop order placed.


I see my mistake now. I should be using CalculationMode.Price, not Ticks. D'oh -- it's always something simple isn't it?

Thanks!
Bryan
Price-Dynamics.com
A NinjaTrader Official Partner

Visit http://www.price-dynamics.com to learn more about the Rhythm Relay trading system. Yes, you can discretionary or auto-trade with 75%+ accuracy!

Use our Contact Us page to request free access to our daily Trader's Lounge where you can see the system in action in a live account!
cassb is offline  
Reply With Quote
Old 12-16-2008, 07:37 AM   #5
mrlogik
Certified NinjaScript Consultant
 
mrlogik's Avatar
 
Join Date: Sep 2006
Location: New York, USA
Posts: 774
Thanks: 1
Thanked 7 times in 5 posts
Default

you got it!
"You look closely enough, you can find everything has a ... weak spot where it can break, sooner or later"

PureLogikTrading
mrlogik is offline  
Reply With Quote
Old 04-26-2011, 11:17 AM   #6
J_o_s
Senior Member
 
Join Date: Aug 2008
Location: Netherlands
Posts: 159
Thanks: 17
Thanked 5 times in 5 posts
Default

I've got the same problem with an dynamic stop loss, and I've written an basic strategy (with the use of this example: http://www.ninjatrader.com/support/f...ead.php?t=3222) and I still receive this error:

Code:
**NT** Calculated stop order price for strategy 'TestStrat' was smaller/equal 0. No stop order placed.
My strategy:
PHP Code:
#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

namespace NinjaTrader.Strategy
{
    [
Description("Enter the description of your strategy here")]
    public class 
TestStrat Strategy
    
{
        protected 
override void Initialize()
        {
            
CalculateOnBarClose true;
        }

        protected 
override void OnBarUpdate()
        {
            if(
Position.MarketPosition == MarketPosition.Flat)
            {
                
SetStopLoss("EnterLong"CalculationMode.Price0false);    
            }
            
            if(
CrossAbove(EMA(20), EMA(50), 1))
            {
                
SetStopLoss("EnterLong"CalculationMode.PriceEMA(50)[0] - ATR(14)[0], false);
                
EnterLong("EnterLong");    
                Print(
"EnterLong. Stoploss value is: " + (EMA(50)[0] - ATR(14)[0]));
            }
            
            if (
Position.MarketPosition == MarketPosition.Long)
            {
                
SetStopLoss("EnterLong"CalculationMode.PercentEMA(50)[0] - ATR(14)[0], false);
                Print(
"Adjust Stoploss. Stoploss value is: " + (EMA(50)[0] - ATR(14)[0]));
            }
            
            if(
CrossBelow(EMA(20), EMA(50), 1))
            {
                
ExitLong("ExitLong""EnterLong");
            }
                    
        }
    }

The output window output:
Code:
EnterLong. Stoploss value is: 202,539517134472
**NT** Calculated stop order price for strategy 'TestStrat' was smaller/equal 0. No stop order placed.
Adjust Stoploss. Stoploss value is: 202,786202213329
Adjust Stoploss. Stoploss value is: 202,982148979163
Adjust Stoploss. Stoploss value is: 203,229759331933
Adjust Stoploss. Stoploss value is: 203,319350754973
Adjust Stoploss. Stoploss value is: 203,429408226915
I would love to hear any suggestion as how to solve this, also partly because I've already encountered numerous difficulties with a simple dynamic stop, so I would love to hear what's the best way to do this.

Regards,
J_o_s 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
ATR Stoploss RSTLNE Strategy Development 3 09-01-2008 04:47 PM
Problem re-setting StopLoss Burga1 Strategy Development 27 05-24-2008 10:30 PM
EnterLongLimit and stoploss ct Strategy Development 1 12-31-2007 09:18 PM
Cancel a StopLoss? Burga1 Strategy Development 3 12-07-2007 04:03 PM
Setting StopLoss via Automation MarlinTrader Automated Trading 1 02-03-2005 04:33 AM


All times are GMT -6. The time now is 11:29 AM.