Go Back   NinjaTrader Support Forum > NinjaTrader 8 > Strategy Development

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

NinjaTrader
Reply
 
Thread Tools Display Modes
Old 04-16-2018, 06:55 AM   #16
Rogue_Two
Junior Member
 
Join Date: May 2017
Posts: 17
Thanks: 6
Thanked 0 times in 0 posts
Default

Paul,

I contained two conditions within (IsFirstTickOfBar) and I am sometimes getting the conditions to trigger when the conditions have not fully played out. It seems most of the time I will get a "bar close" behavior but other times the line I have set to cross will fall short of crossing, yet the strategy is executing entries prematurely.

Here's what I have:

if (IsFirstTickOfBar)
{
// Set 1
if (CrossAbove(MACD1.Diff, 0, 1))
{
EnterLong(Convert.ToInt32(Position.Quantity), @"Long Zero");
}

if (CrossAbove(MACD1.Diff, 0, 1))
{
EnterShort(Convert.ToInt32(Position.Quantity), @"Short Zero");
}

Every now and then the entries will not trigger at all.

Does this sound normal?
Last edited by Rogue_Two; 04-16-2018 at 07:05 AM.
Rogue_Two is offline  
Reply With Quote
Old 04-16-2018, 07:18 AM   #17
NinjaTrader_Paul
NinjaTrader Customer Service
 
NinjaTrader_Paul's Avatar
 
Join Date: May 2014
Posts: 3,732
Thanks: 408
Thanked 948 times in 911 posts
Default

Hello Rogue_Two,

Thanks for your reply.

You may want to change the lookback period to 2 from 1 in the crossabove and crossbelow statements.

For a diagnostic process, for the crossabove and crossbelow statements, you may want to add a Draw statement to draw a dot on the chart when the cross above condition is true. This would allow you to see when the statement is true regardless of an order as there may be other reasons that an order may not be placed.
NinjaTrader_Paul is offline  
Reply With Quote
The following user says thank you to NinjaTrader_Paul for this post:
Old 04-16-2018, 08:29 AM   #18
Rogue_Two
Junior Member
 
Join Date: May 2017
Posts: 17
Thanks: 6
Thanked 0 times in 0 posts
Default

I'll try that and see how it goes.

One more question for now.

Is it possible in the strategy to allow me to manually move stops without having them revert back to their original positions as well as not disabling the strategy when they get hit?
Rogue_Two is offline  
Reply With Quote
Old 04-16-2018, 08:53 AM   #19
NinjaTrader_Paul
NinjaTrader Customer Service
 
NinjaTrader_Paul's Avatar
 
Join Date: May 2014
Posts: 3,732
Thanks: 408
Thanked 948 times in 911 posts
Default

Hello Rogue_Two,

Thanks for your reply.

No, in your strategy if you move the stop/target that would be expected and disabling of the strategy can occur.

If you wish to have that capability then you would need to use different strategy methods that interact with an ATM template. There is an example of the coding required in your NinjaTrader called SampleATMstrategy which is well documented internally by comments. In addition, here is a link to the helpguide section on Ninjascript ATMstrategyMethods: https://ninjatrader.com/support/help...gy_methods.htm Basically your strategy would create the entry conditions, then the ATM template would be in play after the entry order is filled. Once a profit or stop is hit, control returns to the ninjascript strategy.

A limitation is that you would not be able to use the strategy in the strategy analyzer as ATMs cannot be backtested (except for using playback with Market replay data).
NinjaTrader_Paul is offline  
Reply With Quote
Old 04-16-2018, 10:58 AM   #20
Rogue_Two
Junior Member
 
Join Date: May 2017
Posts: 17
Thanks: 6
Thanked 0 times in 0 posts
Default

Got it.

So I changed the lookback to 2 and the behavior is still the same. Now I'm finding executions seem to be triggering on touch of the 0 line instead of on "bar close" even though the conditions and entries are contained withing (IsFirstTickOfBar).

While I try and learn how to add in the Draw statements you mentioned, do you have any idea why the entries are getting triggered on touch rather than close?

Could using a Heiken-Ashi Tick chart have anything to do with it?

Thanks for your help and patience Paul!
Last edited by Rogue_Two; 04-16-2018 at 11:17 AM.
Rogue_Two is offline  
Reply With Quote
Old 04-16-2018, 12:43 PM   #21
NinjaTrader_Paul
NinjaTrader Customer Service
 
NinjaTrader_Paul's Avatar
 
Join Date: May 2014
Posts: 3,732
Thanks: 408
Thanked 948 times in 911 posts
Default

Hello Rogue_Two,

You may want to look at your code again as in post #16 it appears you are using CrossAbove for both conditions.
NinjaTrader_Paul is offline  
Reply With Quote
Old 04-16-2018, 12:45 PM   #22
Rogue_Two
Junior Member
 
Join Date: May 2017
Posts: 17
Thanks: 6
Thanked 0 times in 0 posts
Default

Oops, that happened as I was copying it over but in the actual strategy it is correct.
Rogue_Two is offline  
Reply With Quote
Old 04-16-2018, 01:23 PM   #23
NinjaTrader_Paul
NinjaTrader Customer Service
 
NinjaTrader_Paul's Avatar
 
Join Date: May 2014
Posts: 3,732
Thanks: 408
Thanked 948 times in 911 posts
Default

Hello Rogue_Two,

Here is an alternative way to determine if the diff line has crossed the zero line.

Code:
if (IsFirstTickOfBar)
			{
				if (MACD1.Diff[1] > 0 && MACD1.Diff[2] < 0)
				{
					EnterLong(Convert.ToInt32(Position.Quantity), @"Long Zero");
					Draw.Square (this, "testb"+CurrentBar, true, 0, Low[0] - 8 * TickSize, Brushes.Blue);
				}
				
				if (MACD1.Diff[1] < 0 && MACD1.Diff[2] >0)
				{
					EnterLong(Convert.ToInt32(Position.Quantity), @"Long Zero");
					Draw.Dot (this, "testc"+CurrentBar, true, 0, High[0] + 8 * TickSize, Brushes.Magenta);
				}
}
The difference here is that the conditional statements are looking at the closed bars that would not change and does not include the currently forming bar.
NinjaTrader_Paul is offline  
Reply With Quote
Old Today, 02:30 AM   #24
Rogue_Two
Junior Member
 
Join Date: May 2017
Posts: 17
Thanks: 6
Thanked 0 times in 0 posts
Default

My strategy is coming along thanks to you Paul! Although the crosses seem to be triggering my entries correctly on shorter timeframes, but I'm still getting some rogue triggers and even late triggers for some reason on longer tick charts.

***edit***

I will do more testing but it actually seems like the print outs are just moving bars after entries. For example, the "Long Zero" or "Short Zero" are shifting around to other bars when crosses occur.

My stop losses are now moving at least. My long entries stop loss modification is occurring how I want it to in two stages.

Once the market gets to 80 ticks above entry, the stop gets moved +40 ticks into profit and then another modification of the stop loss occurs when the market gets to +160 ticks above entry.

The same behavior is supposed to occur on the short side but only the first stage seems to be trigger the stop to move. I can't seem to get it to move +80 ticks into profit once the market has reached 160 ticks above entry. Can you please take a look at this at your convenience and see if you can spot anything that may be preventing this from occurring?

Thanks Paul!

protected override void OnBarUpdate()
{
if (BarsInProgress != 0)
return;

if (CurrentBars[0] < BarsRequiredToTrade)
return;

if (IsFirstTickOfBar)
{
//Enter Longs
if (MACD1.Diff[1] > 0 && MACD1.Diff[2] < 0)
{
EnterLong(Convert.ToInt32(Position.Quantity), @"Long Zero");
Draw.Square (this, "testb"+CurrentBar, true, 0, Low[0] - 8 * TickSize, Brushes.Blue);
}

//Enter Shorts
if (MACD1.Diff[1] < 0 && MACD1.Diff[2] >0)
{
EnterShort(Convert.ToInt32(Position.Quantity), @"Short Zero");
Draw.Dot (this, "testc"+CurrentBar, true, 0, High[0] + 8 * TickSize, Brushes.Magenta);
}
}


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

// If a long position is open, allow for stop loss modification
if (Position.MarketPosition == MarketPosition.Long && FirstMove)
{
// Once the price is greater than entry price +80 ticks, set stop loss to +40 ticks
if (Close[0] > Position.AveragePrice + 80 * TickSize)
{
SetStopLoss("", CalculationMode.Price, Position.AveragePrice + 40 * TickSize, false);
FirstMove = false;
}
}

else if (Position.MarketPosition == MarketPosition.Long && SecondMove == true)
{
if (Close[0] > Position.AveragePrice + 160 * TickSize)
{
SetStopLoss("", CalculationMode.Price, Position.AveragePrice + 80 * TickSize, false);
SecondMove = false;
}
}

// If a short position is open, allow for stop loss modification
if (Position.MarketPosition == MarketPosition.Short && FirstMove )
{
// Once the price is less than entry price -80 ticks, set stop loss to -40 ticks
if (Close[0] < Position.AveragePrice - 80 * TickSize)
{
SetStopLoss("", CalculationMode.Price, Position.AveragePrice - 40 * TickSize, false);
FirstMove = false;
}

else if (Position.MarketPosition == MarketPosition.Short && SecondMove)
{
if (Close[0] < Position.AveragePrice - 160 * TickSize)
{
SetStopLoss("", CalculationMode.Price, Position.AveragePrice - 80 * TickSize, false);
SecondMove = false;
}
}


}
}

//This section is to help with OCO rejected orders due to fast market movement.
protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
{
if(error == ErrorCode.OrderRejected)
{
if(order.Name == "Stop loss" && Position.MarketPosition == MarketPosition.Long)
{
ExitLong();
Print("Stop Loss Rejected Exiting Long");

}

if(order.Name == "Stop loss" && Position.MarketPosition == MarketPosition.Short)
{
ExitShort();
Print("Stop Loss Rejected Exiting Short");

}

}

if(order.Name == "Stop loss" && order.OrderState == OrderState.Accepted)
{

Print("Stop Loss Accepted");
Last edited by Rogue_Two; Today at 07:41 AM.
Rogue_Two is offline  
Reply With Quote
Old Today, 07:51 AM   #25
NinjaTrader_Paul
NinjaTrader Customer Service
 
NinjaTrader_Paul's Avatar
 
Join Date: May 2014
Posts: 3,732
Thanks: 408
Thanked 948 times in 911 posts
Default

Hello Rogue_Two,

Thanks for your post.

I don't see anything obvious. What you can do is to add print statements in your stop moving area to show the state of the two bools, the position, and the close price.
NinjaTrader_Paul 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
Moving StopLoss and forced orders problem chrisca Strategy Development 3 01-29-2018 08:40 AM
Moving stoploss mikethelen Strategy Development 4 11-10-2016 07:33 AM
Moving StopLoss alibanez Strategy Development 3 07-31-2013 05:15 PM
Less complicated implementation of stoploss moving zooinek Strategy Development 1 04-15-2010 10:10 AM
moving a stoploss stop to breakeven stephenszpak Strategy Development 13 01-03-2010 10:17 PM


All times are GMT -6. The time now is 06:12 PM.

Futures, foreign currency and options trading contains substantial risk and is not for every investor. An investor could potentially lose all or more than the initial investment. Risk capital is money that can be lost without jeopardizing ones financial security or lifestyle. Only risk capital should be used for trading and only those with sufficient risk capital should consider trading. Past performance is not necessarily indicative of future results. View Full Risk Disclosure.

CFTC Rules 4.41 - Hypothetical or Simulated performance results have certain limitations, unlike an actual performance record, simulated results do not represent actual trading. Also, since the trades have not been executed, the results may have under-or-over compensated for the impact, if any, of certain market factors, such as lack of liquidity. Simulated trading programs in general are also subject to the fact that they are designed with the benefit of hindsight. No representation is being made that any account will or is likely to achieve profit or losses similar to those shown.

This website is hosted and operated by NinjaTrader, LLC (“NT”), a software development company which owns and supports all proprietary technology relating to and including the NinjaTrader trading platform. NT is an affiliated company to NinjaTrader Brokerage (“NTB”), which is a NFA registered introducing broker (NFA #0339976) providing brokerage services to traders of futures and foreign exchange products. This website is intended for educational and informational purposes only and should not be viewed as a solicitation or recommendation of any product, service or trading strategy. No offer or solicitation to buy or sell securities, securities derivative or futures products of any kind, or any type of trading or investment advice, recommendation or strategy, is made, given, or in any manner endorsed by any NT affiliate and the information made available on this Web site is not an offer or solicitation of any kind. Specific questions related to a brokerage account should be sent to your broker directly. The content and opinions expressed on this website are those of the authors and do not necessarily reflect the official policy or position of NT or any of its affiliates. 

Vendors along with their websites, products and services, collectively referred to as (“Vendor Content”), are independent persons or companies that are in no manner affiliated with NT or any if its affiliates. NT or any of its affiliates are not responsible for, do not approve, recommend or endorse any Vendor Content referenced on this website and it’s your sole responsibility to evaluate Vendor Content. Please be aware that any performance information provided by a vendor should be considered hypothetical and must contain the disclosures required by NFA Rule 2-29(c). If you are interested in learning more about, or investigating the quality of, any such Vendor Content you must contact the vendor, provider or seller of such Vendor Content. No person employed by, or associated with, NT or any of its affiliates is authorized to provide any information about any such Vendor Content.