NexusFi: Find Your Edge


Home Menu

 





Help Coding Trailing stoploss in Strategy


Discussion in NinjaTrader

Updated
    1. trending_up 5,756 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?
CME Raises Energy Futures Margins After Iran-War Volatil …
Commodities
May 26 Deadline Dies at 7.5% as US Strikes Iran During C …
Prediction Markets & Event Contracts
June 15 Peace Odds Surge From 3.6% to 12.25% After Trump …
Prediction Markets & Event Contracts
Irans Dual Probability: Guns Quiet at 99.95% While Forma …
Prediction Markets & Event Contracts
Trump Lands in Beijing on CPI Day: Iran Peace Expires To …
Prediction Markets & Event Contracts
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
NexusFi site changelog and issues/problem reporting
8 thanks
Darmok and Jalad at Tanagra
1 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