![]() |
|
|||||||
| Strategy Development Support for the development of custom automated trading strategies using NinjaScript. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Junior Member
Join Date: Jul 2012
Posts: 1
Thanks: 1
Thanked 0 times in 0 posts
|
I'm attempting to do an immediate stop and reverse on a parabolicSAR with code that sets the stop to breakeven. I copied the code from the SamplePriceModification.cs file on another help item and am using that. But what occurs now is that after a winning long trade I close the position, go short immediately and get immediately stopped out (instantly) on the backtesting of the strategy. If I comment out the code for moving the stop to B/E on a long trade then when I try and stop and reverse after a winning short trade I get instantly stopped out.
I've tried commenting out each separate chunk of code to narrow it down but it seems to be anything that has to do with the move stop to B/E code Any ideas? Many thanks. protected override void OnBarUpdate() { // Resets the stop loss to the original value when all positions are closed if (Position.MarketPosition == MarketPosition.Flat) { SetStopLoss(CalculationMode.Ticks, StopLoss); } // If a long position is open, allow for stop loss modification to breakeven else if (Position.MarketPosition == MarketPosition.Long) { // Once the price is greater than entry price+30 ticks, set stop loss to breakeven if (Close[0] > Position.AvgPrice + 30 * TickSize) { SetStopLoss(CalculationMode.Price, Position.AvgPrice); } } else if (Position.MarketPosition == MarketPosition.Short) { // Once the price is less than price+30 ticks, set stop loss to breakeven if (Close[0] < Position.AvgPrice - 30 * TickSize) { SetStopLoss(CalculationMode.Price, Position.AvgPrice); } } // Condition set 1 if (CrossBelow(ParabolicSAR(0.02, 0.2, 0.02), Close, 1)) { EnterLong(Contracts, ""); } // Condition set 2 if (CrossAbove(ParabolicSAR(0.02, 0.2, 0.02), Close, 1)) { EnterShort(Contracts, ""); } } |
|
|
|
|
|
#2 |
|
NinjaTrader Customer Service
Join Date: Sep 2009
Location: Denver, CO
Posts: 8,117
Thanks: 249
Thanked 418 times in 415 posts
|
Hi cloudcowboy,
Welcome to the NT forums! Code looks pretty good and it's likely a race condition when reversing -- your stop loss will use the last value set and your "resetting block" when flat will not have a chance to set your stop loss in time. I would add a similar resetting statement to your entry blocks so that the stop loss is set to a static level before entering. As soon as it reads the position, your dynamic adjustments will take over. Code:
if (CrossBelow(ParabolicSAR(0.02, 0.2, 0.02), Close, 1))
{
SetStopLoss(CalculationMode.Ticks, StopLoss);
EnterLong(Contracts, "");
}
// Condition set 2
if (CrossAbove(ParabolicSAR(0.02, 0.2, 0.02), Close, 1))
{
SetStopLoss(CalculationMode.Ticks, StopLoss);
EnterShort(Contracts, "");
}
Ryan M
NinjaTrader Customer Service |
|
|
|
|
The following user says thank you to NinjaTrader_RyanM for this post: |
|
|
|
#3 |
|
Junior Member
Join Date: Dec 2012
Posts: 2
Thanks: 0
Thanked 0 times in 0 posts
|
Dear All,
I have written stoploss based profits (Breakeven trading). While running this script changing of stoploss will not executed. Please me to find exact 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 // This namespace holds all strategies and is required. Do not change it. namespace NinjaTrader.Strategy { /// <summary> /// /// </summary> [Description("")] public class vwap2 : Strategy { #region Variables // Wizard generated variables private int myInput0 = 1; // Default setting for MyInput0 // User defined variables (add any user defined variables below) #endregion private IOrder stopOrder = null; /// <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 = false; SetStopLoss(CalculationMode.Ticks, 50); } /// <summary> /// Called on each bar update event (incoming tick) /// </summary> protected override void OnBarUpdate() { if (CurrentBar < 1) return; if (Position.MarketPosition == MarketPosition.Long) { // Once the price is greater than entry price+10 ticks, set stop loss to breakeven if (stopOrder != null && stopOrder.StopPrice > Position.AvgPrice && Close[0] > Position.AvgPrice + 10 * TickSize) { ChangeOrder(stopOrder, stopOrder.Quantity, stopOrder.LimitPrice, Position.AvgPrice); } } if (Position.MarketPosition == MarketPosition.Short) { // Once the price is greater than entry price+10 ticks, set stop loss to breakeven if (stopOrder != null && stopOrder.StopPrice < Position.AvgPrice && Close[0] < Position.AvgPrice - 10 * TickSize) { ChangeOrder(stopOrder, stopOrder.Quantity, stopOrder.LimitPrice, Position.AvgPrice); } } double vwapval=VWMA(5)[0]; if (vwapval < Open[0] && Open[0]+5*TickSize>Open[0]) { EnterLong(10,"TREND UP"); } if (vwapval > Open[0] && Open[0]-5*TickSize<Open[0]) { EnterShort(10,"TREND DOWN"); } } } } Thanks & Regards, G.Mugavaiselvam |
|
|
|
|
|
#4 |
|
NinjaTrader Customer Service
Join Date: Sep 2008
Location: Germany
Posts: 22,573
Thanks: 262
Thanked 1,018 times in 999 posts
|
mugavai, welcome to our forums - I would not expect that to work, since the SetStopLoss would not expose any IOrder access and the call for ChangeOrder would be limited to working in our unmanaged order submission approach only.
For a sample how to adjust your SetStopLoss value dynamically, please check into this link - http://www.ninjatrader.com/support/f...ead.php?t=3222
Bertrand
NinjaTrader Customer Service |
|
|
|
|
|
#5 |
|
Junior Member
Join Date: Dec 2012
Posts: 2
Thanks: 0
Thanked 0 times in 0 posts
|
Helo Bertrand,
Please give me working script for stoploss and trailing stoploss please..I need to modify stoploss if my trade is in profit...Please Thanks in Advance... G.Mugavaiselvam
Last edited by mugavai; 12-19-2012 at 06:22 AM.
|
|
|
|
|
|
#6 |
|
NinjaTrader Customer Service
Join Date: Apr 2010
Location: Denver, CO, USA
Posts: 4,860
Thanks: 163
Thanked 580 times in 571 posts
|
Hello,
If you refer to the link that Bertrand provided, you can see a script which shows you how to modify the price of a stop loss order. You can apply these same concepts to your script and you should see the results you are looking for.
Matthew
NinjaTrader Customer Service |
|
|
|
|
|
#7 |
|
NinjaTrader Customer Service
Join Date: Sep 2008
Location: Germany
Posts: 22,573
Thanks: 262
Thanked 1,018 times in 999 posts
|
mugavai, please check into the working example I've referred you to and test it out to learn how the code structure would perform, then work the same logic into your custom script please.
Bertrand
NinjaTrader Customer Service |
|
|
|
![]() |
| Tags |
| break even stop, stops |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Changing stop 2 breakeven after 3rd profit target reached | grego | ATM Strategies (Discretionary Trading) | 1 | 06-16-2010 06:18 PM |
| How to set profit target / stop loss based on entry price? | tealover007 | Strategy Development | 4 | 02-01-2010 06:12 PM |
| Moving Profit Target to Breakeven | pipal | ATM Strategies (Discretionary Trading) | 3 | 06-18-2009 07:46 AM |
| Stop to Breakeven | bapovs | ATM Strategies (Discretionary Trading) | 2 | 05-19-2009 03:06 AM |
| set profit...set stoploss | duck_CA | Strategy Development | 1 | 01-15-2009 03:02 AM |