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 > NinjaScript Development Support > Strategy Development

Strategy Development Support for the development of custom automated trading strategies using NinjaScript.

Reply
 
Thread Tools Display Modes
Old 07-21-2009, 03:05 PM   #1
texasnomad
Senior Member
 
Join Date: Oct 2008
Location: Dallas, TX
Posts: 120
Thanks: 9
Thanked 2 times in 2 posts
Default PSAR flips not working correctly

Does anyone have any idea why this code makes my trades exit early? I just want it to buy & exit short when PSAR flips down and sell & exit long when psar flips up.

Code:
        protected override void Initialize()
        {

            CalculateOnBarClose = true;
        }

        /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
        protected override void OnBarUpdate()
        {
            // Condition set 1
            if (ParabolicSAR(0.02, 0.2, 0.02)[2] > Close[2]
                && ParabolicSAR(0.02, 0.2, 0.02)[1] < Close[1])
            {
                ExitShort("", "");
                EnterLong(Lots, "");
            }

            // Condition set 2
            if (ParabolicSAR(0.02, 0.2, 0.02)[2] < Close[2]
                && ParabolicSAR(0.02, 0.2, 0.02)[1] > Close[1])
            {
                ExitLong("", "");
                EnterShort(Lots, "");
            }
        }
Attached Images
File Type: png notworkingright.PNG (26.0 KB, 22 views)
texasnomad is offline  
Reply With Quote
Old 07-21-2009, 03:11 PM   #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

Looks correct to me. You are checking 1 bar ago and 2 bars ago for the flip. When the flip is identified it places the trades. The trades execute on the next bar because the signal bar is already closed in backtesting.
NinjaTrader_Josh is offline  
Reply With Quote
Old 07-21-2009, 03:58 PM   #3
texasnomad
Senior Member
 
Join Date: Oct 2008
Location: Dallas, TX
Posts: 120
Thanks: 9
Thanked 2 times in 2 posts
Default

Quote:
Originally Posted by NinjaTrader_Josh View Post
Looks correct to me. You are checking 1 bar ago and 2 bars ago for the flip. When the flip is identified it places the trades. The trades execute on the next bar because the signal bar is already closed in backtesting.
Josh,

But if you look at the screenshot, it shows the trades closing well before a PSAR flip occurs. I don't understand what's wrong with the exits.
texasnomad is offline  
Reply With Quote
Old 07-21-2009, 04:06 PM   #4
NinjaTrader_Austin
NinjaTrader Customer Service
 
NinjaTrader_Austin's Avatar
 
Join Date: Jun 2009
Location: Denver, CO
Posts: 3,149
Thanks: 10
Thanked 89 times in 81 posts
Default

Texasnomad, what Josh was saying is that you're checking the PSAR values from one bar back and two bars back, NOT the current bar and one bar back. Changing the values from 2 and 1 to 1 and 0 would fix this.

In addition, the early exits are caused by ExitOnClose being set to true as indicated by "Exit on close" on the chart.

You can try changing these settings and see if the exits/entries are closer to where they should be.
NinjaTrader_Austin is offline  
Reply With Quote
Old 07-21-2009, 04:18 PM   #5
texasnomad
Senior Member
 
Join Date: Oct 2008
Location: Dallas, TX
Posts: 120
Thanks: 9
Thanked 2 times in 2 posts
Default

Quote:
Originally Posted by NinjaTrader_Austin View Post
Texasnomad, what Josh was saying is that you're checking the PSAR values from one bar back and two bars back, NOT the current bar and one bar back. Changing the values from 2 and 1 to 1 and 0 would fix this.
I don't want the strategy to use the current bar. I want to see 2 bars to the left with PSAR above the price, 1 bar to the left with PSAR below the price, then I'll buy at the open of the current bar. This is what this code does, correct?

Quote:
In addition, the early exits are caused by ExitOnClose being set to true as indicated by "Exit on close" on the chart.
How do I eliminate ExitOnClose? I don't see any variables or functions of that name in my code.
texasnomad is offline  
Reply With Quote
Old 07-21-2009, 04:19 PM   #6
texasnomad
Senior Member
 
Join Date: Oct 2008
Location: Dallas, TX
Posts: 120
Thanks: 9
Thanked 2 times in 2 posts
Default

Nevermind... I just found exitonclose in the strategy options. I didn't realize it was an external parameter to the code.
texasnomad is offline  
Reply With Quote
Old 07-21-2009, 04:22 PM   #7
NinjaTrader_Austin
NinjaTrader Customer Service
 
NinjaTrader_Austin's Avatar
 
Join Date: Jun 2009
Location: Denver, CO
Posts: 3,149
Thanks: 10
Thanked 89 times in 81 posts
Default

Quote:
Originally Posted by texasnomad View Post
I don't want the strategy to use the current bar. I want to see 2 bars to the left with PSAR above the price, 1 bar to the left with PSAR below the price, then I'll buy at the open of the current bar. This is what this code does, correct?
Correct. Sorry for not understanding what you wanted to do. Did changing Exit on close resolve the early exit issue?
NinjaTrader_Austin is offline  
Reply With Quote
Old 07-21-2009, 04:32 PM   #8
texasnomad
Senior Member
 
Join Date: Oct 2008
Location: Dallas, TX
Posts: 120
Thanks: 9
Thanked 2 times in 2 posts
Default

Quote:
Originally Posted by NinjaTrader_Austin View Post
Correct. Sorry for not understanding what you wanted to do. Did changing Exit on close resolve the early exit issue?
Yes - everything works as designed now. Thanks for your timely help.
texasnomad 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
Tradestation ATI NTMarketposition not working correctly. strad739 Automated Trading 1 04-02-2008 09:11 AM
Market replay not working correctly rokafella Historical NinjaTrader 6.5 Beta Threads 3 03-29-2008 03:42 PM
Multi Time Frame PSAR question kgillis23 Strategy Development 5 03-12-2008 10:43 PM
Plotting "n" day charts - not working correctly stevestrading Suggestions And Feedback 1 09-08-2007 03:32 AM
PSAR?? tt_ninja Charting 1 01-28-2007 11:24 AM


All times are GMT -6. The time now is 04:23 PM.