NinjaTrader Support Forum  
X

Attention!

This website will be down for maintenance from Friday May 24th at 6PM MDT until Saturday May 25th at 11AM MDT. We apologize for the inconvenience. If you need assistance during this time, please email sales@ninjatrader.com


Go Back   NinjaTrader Support Forum > Application Technical Support > ATM Strategies (Discretionary Trading)

ATM Strategies (Discretionary Trading) Support for Advanced Trade Management, AutoTrail, AutoBreakeven, Stop Strategy and Simulated Stops etc...

Reply
 
Thread Tools Display Modes
Old 11-26-2008, 12:03 AM   #1
dargente
Member
 
Join Date: Oct 2008
Posts: 54
Thanks: 0
Thanked 0 times in 0 posts
Default Autotrail stoploss not executing

I am having trouble with the stoploss modification to autotrail. I have downloaded SamplePriceModification.zip and compiled it based on my specs, but the modification never gets executed when running a strategy. It seems to work when running a backtest, not entirely sure though. Is there something I'm missing? Lets say I simply want to lock in 4 ticks profit once 13 ticks profit is reached, is the following correct?


// Resets the stop loss to the original value when all positions are closed
if (Position.MarketPosition == MarketPosition.Flat)
{
SetStopLoss(CalculationMode.Ticks, stoplossticks);
}

// 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 + # ticks, set stop loss to breakeven
if (Close[4] > Position.AvgPrice + 13 * TickSize)
{
SetStopLoss(CalculationMode.Price, Position.AvgPrice);
}
}

// If a short position is open, allow for stop loss modification to breakeven
else if (Position.MarketPosition == MarketPosition.Short)
{
// Once the price is less than entry price - # ticks, set stop loss to breakeven
if (Close[-4] > Position.AvgPrice - 13 * TickSize)
{
SetStopLoss(CalculationMode.Price, Position.AvgPrice);
}
dargente is offline  
Reply With Quote
Old 11-26-2008, 07:31 AM   #2
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
Default

dargente,

The problem with your code is when you access Close[]. You go Close[-4]. This does not work. The indexing is for barsAgo. 0 means the current bar. 1 means the previous bar. 2 means the bar after that and so on.
NinjaTrader_Josh is offline  
Reply With Quote
Old 11-26-2008, 09:04 PM   #3
dargente
Member
 
Join Date: Oct 2008
Posts: 54
Thanks: 0
Thanked 0 times in 0 posts
Default

ok figured it out thanks. besides the close[0] error, i was basing the modified stop loss on the current price rather than on avg price.
Last edited by dargente; 11-27-2008 at 07:44 AM. Reason: should be ok now
dargente is offline  
Reply With Quote
Old 11-27-2008, 12:23 PM   #4
dargente
Member
 
Join Date: Oct 2008
Posts: 54
Thanks: 0
Thanked 0 times in 0 posts
Default still problems

it looks like the modified stop loss is being activated on the long side, but something is definitely wrong with the short side. or are the stoploss mods that i have for the long positions actually being applied to shorts as well? do i need to separately identify these? how can i allocate different stop loss orders for each long/short positions?
Code:
            // Resets the stop loss to the original value when all positions are closed
            if (Position.MarketPosition == MarketPosition.Flat)
            {
                SetStopLoss(CalculationMode.Ticks, stoplossticks);
            }
            
            // 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 + # ticks, set stop loss to breakeven
                if (Close[0] > Position.AvgPrice + 10 * TickSize)
                {
                    SetStopLoss(CalculationMode.Price, Position.AvgPrice * TickSize);
                }
                
                if (Close[0] > Position.AvgPrice + 20 * TickSize)
                {
                    SetStopLoss(CalculationMode.Price, Position.AvgPrice + 10 * TickSize);
                }

            // If a short position is open, allow for stop loss modification to breakeven
            else if (Position.MarketPosition == MarketPosition.Short)
                
  
// Once the price is less than entry price + # ticks, set stop loss to breakeven
if (Close[0] < Position.AvgPrice - 10 * TickSize) { SetStopLoss(CalculationMode.Price, Position.AvgPrice * TickSize); } if (Close[0] < Position.AvgPrice - 20 * TickSize) { SetStopLoss(CalculationMode.Price, Position.AvgPrice - 10 * TickSize);
Last edited by dargente; 11-27-2008 at 12:25 PM.
dargente is offline  
Reply With Quote
Old 11-27-2008, 02:25 PM   #5
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
Default

Your hunch is correct. If you want separate stops for longs and shorts I suggest you use the fromEntrySignal parameter for the stop losses and tie them to specific long/short entries.
NinjaTrader_Josh is offline  
Reply With Quote
Old 11-29-2008, 09:55 AM   #6
dargente
Member
 
Join Date: Oct 2008
Posts: 54
Thanks: 0
Thanked 0 times in 0 posts
Default retraceable stop loss orders

not sure if with this code the stops are tied to specific longs or shorts, but the exits seem to be working correctly, except... my stop losses are retraceable, meaning depending on the OnBarClose my stop loss orders may move in any direction, instead of being locked in and only increasing as next target has been reached. its funny to see this happen when it has a beneficial effect, but i'd like to have more control over the exits to minimize loss. perhaps SetStopLoss is not the best solution? can you offer any insight as to how i can optimize this strategy? thanks

update:
think i need to use

if (Position.MarketPosition == MarketPosition.Long)
ExitLongStop(yourParametersHere);

will look into that, just not sure how to configure it for multiple exits...

Code:

protected override void Initialize()
        {
            EntriesPerDirection = 1;
            EntryHandling = EntryHandling.AllEntries;
            SetStopLoss(CalculationMode.Ticks, stoplossticks);
            SetProfitTarget(CalculationMode.Ticks, profittargetticks);

            CalculateOnBarClose = true;
        }

        /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
        protected override void OnBarUpdate()
            
        {
                // Entry 1
            if (Position.MarketPosition == MarketPosition.Flat)
            if ...
                    EnterLong("Long Entry 1");

            // Entry 2
            if (Position.MarketPosition == MarketPosition.Flat)
            if ...
                EnterShort("Short Entry 1");
            
            // Entry 3
            if (Position.MarketPosition == MarketPosition.Flat)
                if ...
                    EnterLong("Long Entry 2");

            // Entry 4
            if (Position.MarketPosition == MarketPosition.Flat)
            if ...
                EnterShort("Short Entry 2");

            // Exit 1
            // Resets the stop loss to the original value when all positions are closed
            if (Position.MarketPosition == MarketPosition.Flat)
            {
                SetStopLoss(CalculationMode.Ticks, stoplossticks);
            }
            
            // If a long position is open, allow for stop loss modification to breakeven +-
            if (Position.MarketPosition == MarketPosition.Long)
                
                // Once the price is greater than entry price + # ticks, set stop loss to breakeven
                if (Close[0] > Position.AvgPrice + 10 * TickSize)

                {
                    SetStopLoss(CalculationMode.Price, Position.AvgPrice - 5 * TickSize);
                }
                
                // Once the price is greater than entry price + # ticks, set stop loss to breakeven +-
                if (Close[0] > Position.AvgPrice + 20 * TickSize)

                {
                    SetStopLoss(CalculationMode.Price, Position.AvgPrice + 10 * TickSize);
                }
            }
                
            // Exit 2
            // Resets the stop loss to the original value when all positions are closed
            if (Position.MarketPosition == MarketPosition.Flat)
            {
                SetStopLoss(CalculationMode.Ticks, stoplossticks);
            }
            
            // If a short position is open, allow for stop loss modification to breakeven +-
            else if (Position.MarketPosition == MarketPosition.Short)
                
                
                // Once the price is greater than entry price + # ticks, set stop loss to breakeven
                if (Close[0] < Position.AvgPrice - 10 * TickSize)

                {
                    SetStopLoss(CalculationMode.Price, Position.AvgPrice + 5 * TickSize);
                }
                
                
                // Once the price is less than entry price + # ticks, set stop loss to breakeven +-
                if (Close[0] < Position.AvgPrice - 20 * TickSize)

                {
                    SetStopLoss(CalculationMode.Price, Position.AvgPrice - 10 * TickSize);
                }
Code:
        #region Properties
        /// <summary>
        /// </summary>
        [Description("Numbers of ticks away from entry price for the Stop Loss order")]
        [Category("Parameters")]
        public int StopLossTicks
        {
            get { return stoplossticks; }
            set { stoplossticks = Math.Max(0, value); }
        }
        
        /// <summary>
        /// </summary>
        [Description("Number of ticks away from entry price for the Profit Target order")]
        [Category("Parameters")]
        public int ProfitTargetTicks
        {
            get { return profittargetticks; }
            set { profittargetticks = Math.Max(0, value); }
        }
        

        #endregion
Last edited by dargente; 11-29-2008 at 12:08 PM. Reason: toiling
dargente is offline  
Reply With Quote
Old 11-29-2008, 01:54 PM   #7
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
Default

The reason your stop orders are moving is because you keep resubmitting. Your condition is simply if you are long or if you are short. On each OnBarUpdate() in which that condition is true you end up resubmitting your stop loss at a different price. To prevent this I suggest you use some flag variables.

Code:
if (Position.MarketPosition == MarketPosition.Long && stopPlaced == false)
{
     SetStopLoss(...);
     stopPlaced = true;
}

if (Position.MarketPosition == MarketPosition.Flat && stopPlaced == true)
{
     stopPlaced = false;
}
Something like that. You can change and tweak for it to fit your needs.
NinjaTrader_Josh is offline  
Reply With Quote
Old 12-09-2008, 09:45 AM   #8
dargente
Member
 
Join Date: Oct 2008
Posts: 54
Thanks: 0
Thanked 0 times in 0 posts
Default

hi josh,

can you give a little more specifics on this; i'm having trouble figuring it out.
dargente is offline  
Reply With Quote
Old 12-09-2008, 09:54 AM   #9
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
Default

Your original condition was if you are long -> do something. This condition is evaluated to true on every single bar. This is why your stops keep moving because you keep changing it on every single bar. What you need to do is only submit it once and stop submitting it after that. That is what my prior post shows you how to do.
NinjaTrader_Josh is offline  
Reply With Quote
Old 12-09-2008, 10:33 AM   #10
dargente
Member
 
Join Date: Oct 2008
Posts: 54
Thanks: 0
Thanked 0 times in 0 posts
Default

i appreciate your help. the error message states:

The name 'stopPlaced' does not exist in the current context
dargente is offline  
Reply With Quote
Old 12-09-2008, 10:48 AM   #11
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
Default

That is because you need to create it as a bool variable first.
NinjaTrader_Josh is offline  
Reply With Quote
Old 02-05-2009, 08:10 AM   #12
edgeliner
Senior Member
 
Join Date: Oct 2007
Location: Southy Florida
Posts: 263
Thanks: 16
Thanked 1 time in 1 post
Default

And how would you do that?
edgeliner is offline  
Reply With Quote
Old 02-05-2009, 08:14 AM   #13
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
Default

In the variables section

private bool someVariable = false;
NinjaTrader_Josh is offline  
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
autotrail on manualorder vk2205 Automated Trading 3 08-28-2008 08:43 AM
Ticks, AutoTrail, Autobreakeven? knvb8 ATM Strategies (Discretionary Trading) 1 05-07-2008 01:42 AM
Autotrail in strategy nybangali Strategy Development 1 12-11-2007 09:36 AM
AutoTrail Error Jappenzeller ATM Strategies (Discretionary Trading) 3 06-03-2007 06:12 PM
Setting up autotrail strategy T4M ATM Strategies (Discretionary Trading) 5 05-01-2007 01:02 AM


All times are GMT -6. The time now is 03:10 PM.