![]() |
|
|||||||
| Strategy Development Support for the development of custom automated trading strategies using NinjaScript. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Senior Member
|
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 |
|
|
|
|
|
#2 |
|
Administrator
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
|
Sure, just call the method with the new price.
Here is a reference sample - http://www.ninjatrader-support.com/v...ead.php?t=3222
Ray
NinjaTrader Customer Service |
|
|
|
|
|
#3 | |
|
Senior Member
|
Quote:
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. |
|
|
|
|
|
|
#4 |
|
Administrator
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
|
Please review the reference sample again and pay attention to the first SetStopLoss() call in OnBarUpdate() which is a reset step.
Ray
NinjaTrader Customer Service |
|
|
|
|
|
#5 |
|
Senior Member
|
|
|
|
|
|
|
#6 |
|
Senior Member
|
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! |
|
|
|
|
|
#7 |
|
Administrator
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
|
NT is smart enough to ignore method calls that don't change anything.
Close[0] is last price.
Ray
NinjaTrader Customer Service |
|
|
|
|
|
#8 |
|
Senior Member
|
|
|
|
|
|
|
#9 |
|
Senior Member
|
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);
}
}
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 |
|
|
|
|
|
#10 |
|
Administrator
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
|
Sorry, you will have to debug your code to figure out what is happening.
Ray
NinjaTrader Customer Service |
|
|
|
|
|
#11 | |
|
Senior Member
|
Quote:
[...] 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 |
|
|
|
|
|
|
#12 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
|
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.
Josh
NinjaTrader Customer Service |
|
|
|
|
|
#13 |
|
Senior Member
Join Date: Apr 2007
Location: , ,
Posts: 153
Thanks: 0
Thanked 2 times in 2 posts
|
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. |
|
|
|
|
The following user says thank you to pdawg for this post: |
|
|
|
#14 |
|
Senior Member
|
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.
|
|
|
|
|
|
#15 | |
|
Senior Member
|
Quote:
Bryan |
|
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
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 |