NexusFi: Find Your Edge


Home Menu

 





Help Coding Trailing stoploss in Strategy


Discussion in NinjaTrader

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




 
Search this Thread

Help Coding Trailing stoploss in Strategy

  #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?
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
Pivot Indicator like the old SwingTemp by Big Mike
NinjaTrader
MC PL editor upgrade
MultiCharts
Trade idea based off three indicators.
Traders Hideout
Better Renko Gaps
The Elite Circle
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Diary of a simple price action trader
26 thanks
Just another trading journal: PA, Wyckoff & Trends
24 thanks
Tao te Trade: way of the WLD
21 thanks
My NQ Trading Journal
16 thanks
HumbleTraders next chapter
9 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


© 2024 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 - Privacy Policy - Downloads - Top
no new posts