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 05-07-2008, 09:08 AM   #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 Auto-breakeven in strategy

Is there a way to use SetStopLoss() in a strategy to set a default stoploss in the Initialize() tag and then reset that stoploss later in OnBarUpdate() to breakeven at some particular profit level? I tried it and it didn't work -- it just immediately set a breakeven stoploss as soon as the order was filled. I'm looking for an example of it working somewhere. I don't want to manage my profit target and stoploss manually in the OnOrderUpdate() tag.

Thanks!
Bryan
cassb is offline  
Reply With Quote
Old 05-07-2008, 09:45 AM   #2
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

Sure, just call the method with the new price.

Here is a reference sample - http://www.ninjatrader-support.com/v...ead.php?t=3222
NinjaTrader_Ray is offline  
Reply With Quote
Old 05-07-2008, 09:50 AM   #3
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 NinjaTrader_Ray View Post
Sure, just call the method with the new price.

Here is a reference sample - http://www.ninjatrader-support.com/v...ead.php?t=3222
I already tried that in OnBarUpdate() like this:

if (Position.MarketPosition == MarketPosition.Long && GetCurrentBid() >= (Position.AvgPrice + (TickSize * breakeven)))
{
SetStopLoss("MyEntry", CalculationMode.Ticks, 0, false)
}

It just immediately set a breakeven stoploss as soon as the order was filled.
cassb is offline  
Reply With Quote
Old 05-07-2008, 10:09 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

Please review the reference sample again and pay attention to the first SetStopLoss() call in OnBarUpdate() which is a reset step.
NinjaTrader_Ray is offline  
Reply With Quote
Old 05-07-2008, 11:03 AM   #5
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 NinjaTrader_Ray View Post
Please review the reference sample again and pay attention to the first SetStopLoss() call in OnBarUpdate() which is a reset step.
D'oh. Oh yeah... forgot about the reset step, thank you.
cassb is offline  
Reply With Quote
Old 05-07-2008, 11:26 AM   #6
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

Question about that sample code...

// Once the price is greater than entry price+50 ticks, set stop loss to breakeven

So once you do reset the stoploss to breakeven, how do you prevent it from (re)setting the stoploss to breakeven on every OnBarUpdate() call? Seems like that might slow down the processing. I guess I could set a local variable to signal when the breakeven was set and check that, if that's the only way.

Also, a seconary question... if I want to set my breakeven using the last price instead of Close[0], what is the method for that? I can't seem to find it -- I found GetCurrentAsk() and GetCurrentBid(), but not GetLastPrice() (or something like that).

Thanks, Ray!
cassb is offline  
Reply With Quote
Old 05-07-2008, 11:52 AM   #7
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

NT is smart enough to ignore method calls that don't change anything.

Close[0] is last price.
NinjaTrader_Ray is offline  
Reply With Quote
Old 05-07-2008, 01:04 PM   #8
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 NinjaTrader_Ray View Post
NT is smart enough to ignore method calls that don't change anything.

Close[0] is last price.
Good job on the core code then! :-)

And yes... I should have guessed that Close[0] will always be the latest price for a bar. Thanks!
cassb is offline  
Reply With Quote
Old 05-07-2008, 03:03 PM   #9
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

I'm sorry, but it still doesn't seem to work. I copied the code from the sample and modified it a bit. This is what I'm trying to execute:

Code:
            else if (Position.MarketPosition == MarketPosition.Long)
            {
                double breakevenprice = (Position.AvgPrice + (breakeven * TickSize));
                Print("Last price = " + Close[0] + ", PositionAvg = " + Position.AvgPrice + ", breakeven = " + breakevenprice);
                // Once the price is greater than entry price+breakeven ticks, set stop loss to entry price
                if (Close[0] > Position.AvgPrice)
                {
                    Print("Setting Stoploss to " + Position.AvgPrice);
                    SetStopLoss(CalculationMode.Price, Position.AvgPrice);
                }
            }
And this is what I get in the Output window when it runs, and it never does actually change the stoploss price in the SuperDOM.

Last price = 731.7, PositionAvg = 731.6, breakeven = 731.8
Setting Stoploss to 731.6
Last price = 731.8, PositionAvg = 731.6, breakeven = 731.8
Setting Stoploss to 731.6
Last price = 731.8, PositionAvg = 731.6, breakeven = 731.8
Setting Stoploss to 731.6
Last price = 731.8, PositionAvg = 731.6, breakeven = 731.8
Setting Stoploss to 731.6
Last price = 731.8, PositionAvg = 731.6, breakeven = 731.8
Setting Stoploss to 731.6
Last price = 731.8, PositionAvg = 731.6, breakeven = 731.8
Setting Stoploss to 731.6
Last price = 731.8, PositionAvg = 731.6, breakeven = 731.8
Setting Stoploss to 731.6
Last price = 731.8, PositionAvg = 731.6, breakeven = 731.8
Setting Stoploss to 731.6
cassb is offline  
Reply With Quote
Old 05-07-2008, 03:09 PM   #10
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

Sorry, you will have to debug your code to figure out what is happening.
NinjaTrader_Ray is offline  
Reply With Quote
Old 05-07-2008, 08:06 PM   #11
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 NinjaTrader_Ray View Post
Sorry, you will have to debug your code to figure out what is happening.
I'm trying to... but when the SetStopLoss() call executes, it's not changing the stoploss for the working order. Here, I turned on TraceOrder and also put a line in OnOrderUpdate() to Print(order.ToString()) as well. It looks like it's calling your internal SetStopTarget() method over and over, but the actual stop order is not being changed to 731.5, nor is OnOrderUpdate() being called. Is this a bug in the core code?

[...]
Last price = 731.6, PositionAvg = 731.5, breakeven = 731.7
Last price = 731.6, PositionAvg = 731.5, breakeven = 731.7
Last price = 731.7, PositionAvg = 731.5, breakeven = 731.7
Setting Stoploss to 731.5
5/7/2008 9:50:46 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=731.5 Currency=0 Simulated=False
Last price = 731.7, PositionAvg = 731.5, breakeven = 731.7
Setting Stoploss to 731.5
5/7/2008 9:50:46 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=731.5 Currency=0 Simulated=False
Last price = 731.7, PositionAvg = 731.5, breakeven = 731.7
Setting Stoploss to 731.5
5/7/2008 9:50:46 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=731.5 Currency=0 Simulated=False
cassb is offline  
Reply With Quote
Old 05-08-2008, 01:21 AM   #12
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

Not sure what you are seeing. How are you determining that your stop loss is not set to breakeven? If you run this you should be able to watch the order in the Control Center and watch it move up to breakeven.
NinjaTrader_Josh is offline  
Reply With Quote
Old 05-08-2008, 01:28 AM   #13
pdawg
Senior Member
 
Join Date: Apr 2007
Location: , ,
Posts: 153
Thanks: 0
Thanked 2 times in 2 posts
Default

Brian this code works, just put in your strategy code in the appropriate spots, or run it as is to see it work.

Also I have coded a 3 step trail stop strategy if you want something that allows more complex stop movement strategies, like what ATM can provide. Hope it helps.
Attached Files
File Type: zip breakevensample.zip (12.4 KB, 74 views)
pdawg is offline  
Reply With Quote
The following user says thank you to pdawg for this post:
Old 05-08-2008, 07:19 AM   #14
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 Josh View Post
Not sure what you are seeing. How are you determining that your stop loss is not set to breakeven? If you run this you should be able to watch the order in the Control Center and watch it move up to breakeven.
Hi Josh. I do watch the order in SuperDOM and the original stoploss at 10 ticks stays set... it does not move to breakeven when SetStopLoss() is called. I can't debug the core NT code, so I'm not sure how to proceed to figure this out. I guess what I could do is create a simple strategy with just this method and see if I can reproduce it for you and send you the strategy code.
Last edited by cassb; 05-08-2008 at 07:33 AM.
cassb is offline  
Reply With Quote
Old 05-08-2008, 07:19 AM   #15
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 pdawg View Post
Brian this code works, just put in your strategy code in the appropriate spots, or run it as is to see it work.

Also I have coded a 3 step trail stop strategy if you want something that allows more complex stop movement strategies, like what ATM can provide. Hope it helps.
Hey, thanks pdawg! That's kind of you. I'll load it and take a look.

Bryan
cassb 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
Strategy Wizard Trailstop Breakeven daven Suggestions And Feedback 1 04-20-2008 04:55 PM
Example of Auto Breakeven code? Thomas Strategy Development 4 08-17-2007 03:39 PM
Auto breakeven fails? nicko9 ATM Strategies (Discretionary Trading) 3 02-05-2007 03:04 AM
Auto breakeven fails? (2) nicko9 ATM Strategies (Discretionary Trading) 1 02-05-2007 01:36 AM
Sliding auto-breakeven pwald9 ATM Strategies (Discretionary Trading) 3 11-08-2006 06:45 AM


All times are GMT -6. The time now is 12:59 PM.