NexusFi: Find Your Edge


Home Menu

 





Help Coding Trailing stoploss in Strategy


Discussion in NinjaTrader

Updated
    1. trending_up 5,653 views
    2. thumb_up 3 thanks given
    3. group 2 followers
    1. forum 3 posts
    2. attach_file 0 attachments




 
Search this Thread
  #1 (permalink)
Darshiit
India
 
Posts: 29 since Oct 2009
Thanks Given: 32
Thanks Received: 22

Dear Fellow boarders,

This is a wonderful forum for ninja scripting .

I've gone through couple of vids and posts here for how to code a trailing stoploss indicator.

But i m not understanding where i m mistaking.

I tried out many possible things. This is the code Which i find should work. But its not working either on charts or in backtesting.

 
Code
protected override void Initialize()
        {
            
            EntryHandling    =    EntryHandling.UniqueEntries;
            

            CalculateOnBarClose = true;
        }

       
        private void ManageOrders()
        {
            {
                if (Position.MarketPosition == MarketPosition.Flat)
                    return;
            }
            {
                if (Position.MarketPosition == MarketPosition.Long)
                    LongTrails();
            }
            {
                if (Position.MarketPosition == MarketPosition.Short)
                    ShortTrails();
            }
            
        }
        
        private void LongTrails()
        {
            {
                if (Close[0]< Position.AvgPrice )
                {
                    if (Close[0]<Position.AvgPrice - (stopTicks*TickSize))
                        ExitLong();
                }
                
                if (High[0] > Position.AvgPrice + (target1*TickSize))
                {
                    if (Close[0] < Position.AvgPrice + (be*TickSize))
                        ExitLong();
                }
                
                if (High[0] > Position.AvgPrice + ((target1+target2)*TickSize))
                {
                    if (Close[0] < Position.AvgPrice + (target1*TickSize))
                        ExitLong();
                }
                
                if (High[0] > Position.AvgPrice + ((target1+target2+target3)*TickSize))
                {
                    if (Close[0] < Position.AvgPrice + (target2*TickSize))
                        ExitLong();
                }
            }
            
        }
        
        private void ShortTrails()
        {
            {
                if (Close[0]> Position.AvgPrice )
                {
                    if (Close[0]>Position.AvgPrice + (stopTicks*TickSize))
                        ExitShort();
                }
                
                if (Low[0] < Position.AvgPrice - (target1*TickSize))
                {
                    if (Close[0] > Position.AvgPrice - (be*TickSize))
                        ExitShort();
                }
                
                if (Low[0] < Position.AvgPrice - ((target1+target2)*TickSize))
                {
                    if (Close[0] > Position.AvgPrice - (target1*TickSize))
                        ExitShort();
                }
                
                if (Low[0] < Position.AvgPrice - ((target1+target2+target3)*TickSize))
                {
                    if (Close[0] > Position.AvgPrice - (target2*TickSize))
                        ExitShort();
                }
            }
            
        }
        
        protected override void OnBarUpdate()
        {
            
            EntryHandling    =    EntryHandling.UniqueEntries;
            
            
            // Condition set 1
            if (//Condition for  Short Orders// )
            {
                DrawArrowDown("My down arrow" + CurrentBar, false, 0, 0, Color.Red);
                EnterShort(DefaultQuantity, "s");
            }

            // Condition set 2
            if (// Condition for Long Orders//)
            {
                DrawArrowUp("My up arrow" + CurrentBar, false, 0, 0, Color.Lime);
                EnterLong(DefaultQuantity, "b");
            }
            ManageOrders();

        }
where be , target1 , target2 & target3 r declared as integers.

Seniors , Please excuse my ignorance to ninjatrader as i m newbie towards it.

Thanks,
Darshiit


Reply With Quote

Can you help answer these questions
from other members on NexusFi?
New Section 301 Probes Target 16 Trading Partners -- Tra …
Traders Hideout
Synapse Pro - Trade Copier with topstep and MT5 integrat …
NinjaTrader
After $87M Settles NO: Irans Nuclear Redline Sets Up the …
Prediction Markets & Event Contracts
Kalshi Rockets to $22B, Passes Polymarket in Volume -- B …
Prediction Markets & Event Contracts
February NFP Preview -- Why This Mornings Jobs Number Co …
Traders Hideout
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Sober Journey With S&P
24 thanks
2026 Jlab journal
10 thanks
Algo automated / semi-automated trading anyone?
6 thanks
Lady Vols Primer: Trading Volatility Journal
6 thanks
2026 Fire Horse
5 thanks
  #3 (permalink)
Darshiit
India
 
Posts: 29 since Oct 2009
Thanks Given: 32
Thanks Received: 22


I forgot to mention on above post that , Using this code i m able to get initial stop , but not able to get trailing stop.


Reply With Quote
  #4 (permalink)
 bukkan 
Calcutta, India
 
Experience: Intermediate
Platform: ArthaChitra
Posts: 278 since Jun 2009
Thanks Given: 161
Thanks Received: 271

the code dosnt have any code for Traial stop loss. for trial stop you got to set SetStopLoss(...) or SetTraialStop(...)

ExitLong or Exit Short is used to exit the postion and not to set stop.


Reply With Quote
Thanked by:




Last Updated on June 25, 2010


© 2026 NexusFi®, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Downloads - Top
no new posts