Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

i need help in creating a custom trailing stop

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    i need help in creating a custom trailing stop

    good day to everyone,


    coding in ninjascript is really not the easiest thing in the world. in nt7 i have been able to achieve some fairly basic things like having indicators change colors, setting a fixed width for them, modifying some sample strategies to create some elementary strategies of my own and so on.


    the most complicated piece of code i have tried to put together would be a custom kind of trailing stop. this stop would be progressively moved in case price went in my favor but not tick by tick (which follows price too tightly and leads to most winning trades being killed prematurely) but rather in medium sized increments that could be adjusted for every instrument and even optimized if need be. such a piece of code turns out to be quite a challenge in ninjascript, i had to include iorder execution and other special commands and even needed the help of a programmer friend of mine in order to be able to finish this short script and get it to compile.


    i would appreciate the assistance of nt's client support personnel or other users to modify this code as necessary so that it:

    1 - would open a long trade if the conditions for an entry are present
    2 - places an initial stop loss order a number of ticks below my entry price
    3 - moves this stop as indicated if the position is still open and price is moving favorably
    4 - after the last increment (pf06), introduces a regular trailing stop (tstops01 ticks behind) to follow my position if it continues to move favorably
    5 - in case any of the multiple stops was triggered, the strategy should reset all values, cancel all orders and go back to its initial state to wait for the entry conditions to be present again to open another position


    this is the code i developed for nt7, it does compile and run but it does not deliver the desired results at all. one of the malfunctions that can occur is for the strategy to place one single first trade and then zero further trades even when the conditions for subsequent entries are present. i only include the long commands as i think i would be able to invert these commands for short trades on my own once the strategy works as intended.


    i would also like to know what kind of modifications would be necessary for a similar custom trailing stop to work on nt8 as i intend to perform some extensive tests on both nt7 and nt8.


    thanks a lot, all the best, regards.


    Code:
    
    #region Variables
            
            
            private int pf01= 10;
            private int pf02= 16;
            private int pf03= 20;
            private int pf04= 24;
            private int pf05= 30;        
            private int pf06= 45;
            private int ps01 = 1;
            private int stops01 = 6;
            private int stops02 = 2;
            private int stops03 = 6;
            private int stops04 = 10;
            private int stops05 = 14;
            private int stops06 = 20;
            private int tstops01 = 15;
            
            private IOrder p01 = null;
                   
            private double pp01;
                    
            
            #endregion
    
    
    
    
    
    protected override void OnBarUpdate()
            
            {
            
            if ( CrossAbove(SMA(Fast), SMA(Slow), 1) && Position.MarketPosition == MarketPosition.Flat && p01 == null )
            {
            p01 = EnterLong(ps01, "p01");
            SetStopLoss("p01", CalculationMode.Price, pp01 - stops01 * TickSize, false);
            }
                        
            if ( Position.GetProfitLoss(Close[0], PerformanceUnit.Points) > pf01*TickSize && Position.MarketPosition == MarketPosition.Long)
            {
            SetStopLoss("p01", CalculationMode.Price, pp01 + stops02 * TickSize, false);
            }
            
            if ( Position.GetProfitLoss(Close[0], PerformanceUnit.Points) > pf02*TickSize && Position.MarketPosition == MarketPosition.Long)
            {
            SetStopLoss("p01", CalculationMode.Price, pp01 + stops03 * TickSize, false);
            }
            
            if ( Position.GetProfitLoss(Close[0], PerformanceUnit.Points) > pf03*TickSize && Position.MarketPosition == MarketPosition.Long)
            {
            SetStopLoss("p01", CalculationMode.Price, pp01 + stops04 * TickSize, false);
            }
            
            if ( Position.GetProfitLoss(Close[0], PerformanceUnit.Points) > pf04*TickSize && Position.MarketPosition == MarketPosition.Long)
            {
            SetStopLoss("p01", CalculationMode.Price, pp01 + stops05 * TickSize, false);
            }
            
            if ( Position.GetProfitLoss(Close[0], PerformanceUnit.Points) > pf05*TickSize
            {
            
            SetStopLoss("p01", CalculationMode.Price, pp01 + stops06 * TickSize, false);
            }
            
            
            
            if (CrossBelow(SMA(Fast), SMA(Slow), 1))
            
            EnterShort(ps01, "p01");
            
            
            
            
            }
            protected override void OnExecution(IExecution execution)
            {
                
            if(Position.MarketPosition == MarketPosition.Flat)
                
            p01 = null;
            pp01 = 0;
                   
            
            if(p01 != null && p01 == execution.Order && Position.MarketPosition == MarketPosition.Long)
            pp01 = execution.Price;
                
                      
                    
            }
    Last edited by rtwave; 01-10-2018, 01:23 PM.

    #2
    Hello rtwave,

    Thank you for the post.

    While our support cannot create items for you, we can provide assistance in providing learning resources or can try to explain tough topics.

    For this item, I can provide some suggestions on steps you can take to address your concerns.

    1 - would open a long trade if the conditions for an entry are present
    It looks like you already have some conditions defined so it would appear you are already past this step. If the entries are not as you are expecting as you have noted, this would be a good place to stop and address this specifically before moving on. You may benefit from removing your stop logic until you get the entry logic happening as you want.

    You noted that a single trade is placed but then no future trades happen. Can you tell me, are you referring to the EnterShort or is this the EnterLong you are expecting to happen? I don't see that you have any Prints or other debugging in the script, are you certain the condition had become true?
    I would likely suggest to enable TraceOrders and also use Prints to find out if your conditions are happening as a starting point, we have a short guide to debugging here: https://ninjatrader.com/support/foru...749#post413749

    2 - places an initial stop loss order a number of ticks below my entry price
    It appears you already have this in place as well except that this should likely go in OnExecution rather than OnBarUpdate for the initial stop if you need the fill price of the entry order. In a situation where the OnBarUpdate logic was called before you assign the fill price to your variable, that could lead to unexpected results. To submit protective orders based on an entry price, you could see the following example that uses OnExecution to wait for an entry and then submit the stop based on the entry price: https://ninjatrader.com/support/foru...ead.php?t=7499 In the example the ExitLongStop could be replaced with the type of protective order you want such as SetStopLoss.

    3 - moves this stop as indicated if the position is still open and price is moving favorably
    It appears you have this in place as well if the current logic is not working as expected this would be a good case to use Prints to find out why. Please see the link from #1 about debugging techniques.

    4 - after the last increment (pf06), introduces a regular trailing stop (tstops01 ticks behind) to follow my position if it continues to move favorably
    It does appear you have this as well, again this would be a case for Printing if this is not becoming true or working as expected.

    5 - in case any of the multiple stops was triggered, the strategy should reset all values, cancel all orders and go back to its initial state to wait for the entry conditions to be present again to open another position
    For this, are you planning on having multiple entries or a single entry with more than 1 quantity? I would likely suggest to wait on this item specifically until you have worked through the other items you have noted. A multi entry or multi stop strategy will be more complex so it would be advisable to start simple with a single entry and single stop and get that working as you would expect. Once the greater logic is working as expected, you could explore the EntriesPerDirection for multiple entries, or the example shown in #2 if you need to break the stop into multiple stops for the quantity of the entry.


    Regarding NT8, there are some differences between the format of NT7 and NT8 scripts. I would likely suggest to review the sample in #2 as there is a NT7 and NT8 version that do the same thing. You could compare the differences here to see generally what would be different. NT8 does have more features so there are more things you can do with NinjaScript overall. If you are familliar with NT7 and not NT8, I would likely suggest to start in NT7 as it is more simple and then once you have a working example migrate that to NT8.

    I look forward to being of futher assistance.
    JesseNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by elirion, Today, 01:36 AM
    0 responses
    2 views
    0 likes
    Last Post elirion
    by elirion
     
    Started by gentlebenthebear, Today, 01:30 AM
    0 responses
    2 views
    0 likes
    Last Post gentlebenthebear  
    Started by samish18, Yesterday, 08:31 AM
    2 responses
    9 views
    0 likes
    Last Post elirion
    by elirion
     
    Started by Mestor, 03-10-2023, 01:50 AM
    16 responses
    389 views
    0 likes
    Last Post z.franck  
    Started by rtwave, 04-12-2024, 09:30 AM
    4 responses
    31 views
    0 likes
    Last Post rtwave
    by rtwave
     
    Working...
    X