![]() |
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
|
|||||||
| Strategy Development Support for the development of custom automated trading strategies using NinjaScript. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Senior Member
Join Date: Oct 2008
Location: Dallas, TX
Posts: 120
Thanks: 9
Thanked 2 times in 2 posts
|
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, "");
}
}
|
|
|
|
|
|
#2 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
|
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
NinjaTrader Customer Service |
|
|
|
|
|
#3 | |
|
Senior Member
Join Date: Oct 2008
Location: Dallas, TX
Posts: 120
Thanks: 9
Thanked 2 times in 2 posts
|
Quote:
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. |
|
|
|
|
|
|
#4 |
|
NinjaTrader Customer Service
Join Date: Jun 2009
Location: Denver, CO
Posts: 3,149
Thanks: 10
Thanked 89 times in 81 posts
|
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.
Austin
NinjaTrader Customer Service |
|
|
|
|
|
#5 | ||
|
Senior Member
Join Date: Oct 2008
Location: Dallas, TX
Posts: 120
Thanks: 9
Thanked 2 times in 2 posts
|
Quote:
Quote:
|
||
|
|
|
|
|
#6 |
|
Senior Member
Join Date: Oct 2008
Location: Dallas, TX
Posts: 120
Thanks: 9
Thanked 2 times in 2 posts
|
Nevermind... I just found exitonclose in the strategy options. I didn't realize it was an external parameter to the code.
|
|
|
|
|
|
#7 |
|
NinjaTrader Customer Service
Join Date: Jun 2009
Location: Denver, CO
Posts: 3,149
Thanks: 10
Thanked 89 times in 81 posts
|
Correct. Sorry for not understanding what you wanted to do. Did changing Exit on close resolve the early exit issue?
Austin
NinjaTrader Customer Service |
|
|
|
|
|
#8 |
|
Senior Member
Join Date: Oct 2008
Location: Dallas, TX
Posts: 120
Thanks: 9
Thanked 2 times in 2 posts
|
|
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
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 |