![]() |
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
|
|||||||
| General Programming General NinjaScript programming questions. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Member
Join Date: Dec 2008
Location: Kansas City
Posts: 90
Thanks: 0
Thanked 2 times in 2 posts
|
I'm new to NT, and fairly new to programming and have never used C#. Once a buy/sell condition has been met, I'm trying to write the code to set my stop and profit target correctly based on a multiple of the most recent ATR. It doesn't have to adjust the stops (i.e. dynamically modify the stop levels as each subsequent bar comes in) after the original buy/sell order has been filled. Rather, right when the buy/sell order is generated it needs to look back at the last 'x' amount of bars (25 in the code below) to calculate the most recent ATR, multiply it, and then set both the stop and profit limit orders accordingly.
My feeble attempt at this is below (only the main sections of the code included), and I'm getting the CS0102 error saying for the properties section saying that Variable0 and 1 are already defined. If I comment out the 'properties' code, I get CS0019 saying I can't apply an operand '*' to type 'int' and 'Ninjatrader.Indicator.ATR'. Guidance here would be appreciated...Thanks, KCSystemTrader #region Variables section... private int period1 = 25; // Default setting for Period1 private double Variable0 = 0.500; // Default ATR for stop loss private double Variable1 = 1.000; // Default ATR for take profit protected override void Initialize() Section... //Custom code to calculate latest ATR reading and set stop and profit target levels Add(ATR(Period1)); //This adds the ATR to the chart (I think) Variable0 = 3 * ATR(Period1); Variable1 = 6 * ATR(Period1); //Plug Variable0 and Variable1 calculations into stop and profit target SetStopLoss("Buy", CalculationMode.Ticks, Variable0, true); SetStopLoss("Sell", CalculationMode.Ticks, Variable0, true); SetProfitTarget("Buy", CalculationMode.Ticks, Variable1); SetProfitTarget("Sell", CalculationMode.Ticks, Variable1); #region Properties Section... //Custom Code to define minimum allowable values for Variable0 and Variable1 [Description("ATR For Stop Loss")] [Category("Parameters")] public double Variable0 { get { return Variable0; } set { Variable0 = Math.Max(0.200, value); } } [Description("ATR For Take Profit")] [Category("Parameters")] public double Variable1 { get { return Variable1; } set { Variable1 = Math.Max(0.400, value); } } |
|
|
|
|
|
#2 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
|
You cannot do any calculation logic in Initialize(). Everything you want to do needs to be done in OnBarUpdate(). You need to calculate out your ATR value before you submit your entry order.
Josh
NinjaTrader Customer Service |
|
|
|
|
|
#3 | |
|
Member
Join Date: Dec 2008
Location: Kansas City
Posts: 90
Thanks: 0
Thanked 2 times in 2 posts
|
Quote:
//Set Trailing Stop based on ATR Multiple if (Defined == false) { TrailStop = ((ATR(Period1)[1] * ATRMultiple)); if (TrailStop < MinimumTrailStop) { TrailStop = MinimumTrailStop; } SetTrailStop("Buy", CalculationMode.Price, TrailStop, true); SetTrailStop("Sell", CalculationMode.Price, TrailStop, true); Defined = true; } Period1 is a defined int and MinimumTrailStop is a defined double. The code compiles but when I run the strategy a trailstop isn't getting set at all. It just buys/sells and then eventually closes itself out when another buy/sell condition is met. In the variables, I have bool Defined = false; Do I need to reset this to false if I have a flat position or something? How do I do that? Thanks, kcsystemtrader |
|
|
|
|
|
|
#4 |
|
NinjaTrader Customer Service
Join Date: Sep 2008
Location: Germany
Posts: 22,414
Thanks: 252
Thanked 978 times in 961 posts
|
Hi kcsystemtrader, yes you want it to reset, otherwise it will not make it through the trailstop section again...please also take a look at this reference sample - http://www.ninjatrader-support2.com/...ead.php?t=3222
Bertrand
NinjaTrader Customer Service |
|
|
|
|
|
#5 | |
|
Member
Join Date: Dec 2008
Location: Kansas City
Posts: 90
Thanks: 0
Thanked 2 times in 2 posts
|
Quote:
So how do I do that? Do I set a reset based on my position being back to flat or something? I don't really know how to code that. Been programming for about 4 days now. Thanks, |
|
|
|
|
|
|
#6 |
|
Administrator
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
|
Bert will follow up on Monday.
Ray
NinjaTrader Customer Service |
|
|
|
|
|
#7 |
|
Member
Join Date: Dec 2008
Location: Kansas City
Posts: 90
Thanks: 0
Thanked 2 times in 2 posts
|
Okay, thanks. I guess what I need help with is the following:
If a position that was opened by the strategy is closed, then reset the bool "defined" back to false. Or, stated another way, upon any new position being opened by the strategy, reset the bool back to false so that it will run the trail stop code again. So that could either be based on a "if market position = flat", or "if trail stop order = filled", or there are probably quite a few other ways you could code it too. Thanks again, kcsystemtrader |
|
|
|
|
|
#8 |
|
NinjaTrader Customer Service
Join Date: Sep 2008
Location: Germany
Posts: 22,414
Thanks: 252
Thanked 978 times in 961 posts
|
Hi kcsystemtrader,
To reset your bool variable when the strategy is flat you could use - Code:
if (Position.MarketPosition == MarketPosition.Flat) Defined = false;
Bertrand
NinjaTrader Customer Service |
|
|
|
|
|
#9 |
|
Member
Join Date: Dec 2008
Location: Kansas City
Posts: 90
Thanks: 0
Thanked 2 times in 2 posts
|
I'm still having trouble here. I still haven't figured out how to run a calculation on an indicator that is dynamically updating. See below. I can see the "ATRvalue" updating in the output window, but I see nothing in the output window for "TrailStop".
ATRvalue = ATR(120)[0]; Print("The current ATR value is " + ATRvalue.ToString()); TrailStop = ATRvalue * 2; Print("The current TrailStop value is " + TrailStop.ToString()); SetTrailStop("", CalculationMode.Price, TrailStop, false); Once I can at least be sure I'm getting a value for TrailStop, I'll move on to making sure it is getting reset properly with the "Defined" boolean as discussed below based on position being flat. Thanks |
|
|
|
|
|
#10 | |
|
Member
Join Date: Dec 2008
Location: Kansas City
Posts: 90
Thanks: 0
Thanked 2 times in 2 posts
|
Quote:
Figured out what was wrong --- I didn't have an Add(ATR(120)); up in the intialize method. Didn't know that was required in order to reference an indicator in OnBarUpdate(). Thanks, |
|
|
|
|
|
|
#11 |
|
Administrator
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
|
It's not however, if this has resolved your issue then just leave as is.
Ray
NinjaTrader Customer Service |
|
|
|
|
|
#12 | |
|
Member
Join Date: Dec 2008
Location: Kansas City
Posts: 90
Thanks: 0
Thanked 2 times in 2 posts
|
Quote:
I don't think it is a reset problem because it just looks like it is never getting set. |
|
|
|
|
|
|
#13 |
|
NinjaTrader Customer Service
Join Date: Sep 2008
Location: Germany
Posts: 22,414
Thanks: 252
Thanked 978 times in 961 posts
|
Hi kcsystemtrader, great it works now - are you using SetTrailStop together with SetStopLoss and reference the same position? This cannot be done, please see this help guide link - http://www.ninjatrader-support.com/H...TrailStop.html
For modifying your stoploss orders, you may want to review this reference sample here - http://www.ninjatrader-support2.com/...ead.php?t=3222 Have a good weekend!
Bertrand
NinjaTrader Customer Service |
|
|
|
|
|
#14 | |
|
Member
Join Date: Dec 2008
Location: Kansas City
Posts: 90
Thanks: 0
Thanked 2 times in 2 posts
|
Quote:
The problem now is that I have the reset working and the calculation logic working right, but trail stop orders are not being generated. Nothing on the orders tab or in the log. With TraceOrders on, I am seeing the following in the output window: The current ATR value is 0.292274060645679 The current TrailStop value is 0.876822181937038 1/5/2009 9:03:58 AM Entered internal SetStopTarget() method: Type=TrailStop FromEntrySignal='Buy' Mode=Price Value=0.876822181937038 Currency=0 Simulated=False 1/5/2009 9:03:58 AM Entered internal SetStopTarget() method: Type=TrailStop FromEntrySignal='Sell' Mode=Price Value=0.876822181937038 Currency=0 Simulated=False The current value for BarsSinceExit is 337 What does "Entered internal SetStopTarget" mean, and why wouldn't it be an "external" trailing stop order that is sent immediately to the market? As you can see in the trace, I do have Simulated = False. Here is the latest code: //Reset upon going flat if (Position.MarketPosition == MarketPosition.Flat) definedTrailStop = false; // Calculations For Stop Loss if (definedTrailStop == false) { // Prints the current value of an 'x' period ATR using default price type ATRvalue = ATR(ATRPeriod)[0]; Print("The current ATR value is " + ATRvalue.ToString()); // Multiply ATRvalue by ATRMultiple and Set TrailStop TrailStop = ATRvalue * ATRMultiple; if (TrailStop < MinimumTrailStop) { TrailStop = MinimumTrailStop; } Print("The current TrailStop value is " + TrailStop.ToString()); definedTrailStop = true; } //Send trailing stop order to market with last calculated TrailStop SetTrailStop("Buy", CalculationMode.Price, TrailStop, false); SetTrailStop("Sell", CalculationMode.Price, TrailStop, false); //Buy and Sell Conditions here I originally had the SetTrailStop up in the brackets in the ATR calculation logic section, but figured since this logic stops running once a position is entered that maybe the SetTrailStop command would not be seen. So, now it outside of that section of the logic and it is running the SetTrailStop code every update while in a position (and not updating the trailstop price), but still there are no trailing stop orders actually being placed. I've tested real time and it blows right through the stops. Thanks, kcsystemtrader |
|
|
|
|
|
|
#15 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
|
You need to call SetTrailStop before you place your order. After that you should not call it again. It will trail on its own tick by tick. From your code it looks like you are trying to use your own trailing logic and in that case you should not be using SetTrailStop. You should be using ExitLongStop() and just modify that order constantly.
Josh
NinjaTrader Customer Service |
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Indicators and Strategies - Stop Syntax ATR based | Mike060375 | Indicator Development | 1 | 12-01-2008 01:03 PM |
| ATM ATR Trailing stop | net01 | ATM Strategies (Discretionary Trading) | 1 | 10-23-2008 10:34 AM |
| ATR Stop | ATI user | Strategy Development | 8 | 09-29-2008 10:12 AM |
| ATR based Stop Loss | moon_rainz | Strategy Development | 2 | 07-17-2008 11:42 PM |
| Question about source code for ATR | erikp | General Programming | 3 | 06-29-2008 01:22 AM |