NexusFi: Find Your Edge


Home Menu

 





[newb] question on mult entries in direction of trend


Discussion in NinjaTrader

Updated
    1. trending_up 2,031 views
    2. thumb_up 1 thanks given
    3. group 2 followers
    1. forum 5 posts
    2. attach_file 0 attachments




 
Search this Thread

[newb] question on mult entries in direction of trend

  #1 (permalink)
 jordis 
Mobile, Alabama
 
Experience: Beginner
Platform: NinjaTrader, TOS
Trading: Stocks
Posts: 29 since Sep 2011
Thanks Given: 6
Thanks Received: 3

I haven't really posted on here before, but I've been lurking for a while. I am not new to trading, but I'm a complete beginner when it comes to programming and backtesting. I have a question that should be pretty basic (I hope) for those of you who program in NT regularly. I've tried to search for similar questions on the forum, but I haven't had any luck; so I apologize if something like this has been answered before.

Disclaimer: This is not part of a strategy, it's more of a conceptual question about programming. I understand that this concept would produce horrific results!

Question: How would I go about programming a strategy that (after identifying a trend, in some way or another) would simply enter a new trade in the direction of the trend on the open of every bar, and close these trades at a fixed interval, one after the other?
For example, say I choose to trade long whenever the closing price of a bar is above the 10EMA. So for every bar that opens after the previous bar closes above the 10EMA I want to enter a new long position, hold it for exactly 10 bars, and then sell. This would continue until the price dipped below the 10EMA. So, if the trend lasted for more than 10 bars total then the strategy would enter long trades only until the 11th bar where it would then both open a new long trade as well as close out the initial long trade (and so on).
The concept of how to program this is what is eluding me at the moment. Whenever I try to code this using the strategy builder I end up doing one of 2 things. First, I was getting the consecutive trades to open, but they would all close on the same bar (when my counter is hit from the FIRST entry, not each individual entry). Second, I tried the following code, but no new entry would be opened until the initial entry had been closed (after the 10 bars).

protected override void OnBarUpdate()
{
// Condition set 1
if (CrossAbove(Close, EMA(10), 1))
{
EnterLong(DefaultQuantity, "long");
}

// Condition set 2
if (Close[0] > EMA(10)[0])
{
EnterLong(DefaultQuantity, "long");
}

// Condition set 3
if (BarsSinceEntry() == 10)
{
ExitLong("close", "long");
}
}



So, I realize this is probably basic for most of you, but could someone help explain to me how to code this so I end up with the desired result?

Thanks for any help,
Jordis

Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Exit Strategy
NinjaTrader
MC PL editor upgrade
MultiCharts
REcommedations for programming help
Sierra Chart
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
ZombieSqueeze
Platforms and Indicators
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Just another trading journal: PA, Wyckoff & Trends
31 thanks
Spoo-nalysis ES e-mini futures S&P 500
29 thanks
Tao te Trade: way of the WLD
24 thanks
Bigger Wins or Fewer Losses?
20 thanks
GFIs1 1 DAX trade per day journal
17 thanks
  #3 (permalink)
 
vvhg's Avatar
 vvhg 
Northern Germany
 
Experience: Intermediate
Platform: NT
Trading: FDAX, CL
Posts: 1,583 since Mar 2011
Thanks Given: 1,016
Thanks Received: 2,824



jordis View Post
I haven't really posted on here before, but I've been lurking for a while. I am not new to trading, but I'm a complete beginner when it comes to programming and backtesting. I have a question that should be pretty basic (I hope) for those of you who program in NT regularly. I've tried to search for similar questions on the forum, but I haven't had any luck; so I apologize if something like this has been answered before.

Disclaimer: This is not part of a strategy, it's more of a conceptual question about programming. I understand that this concept would produce horrific results!

Question: How would I go about programming a strategy that (after identifying a trend, in some way or another) would simply enter a new trade in the direction of the trend on the open of every bar, and close these trades at a fixed interval, one after the other?
For example, say I choose to trade long whenever the closing price of a bar is above the 10EMA. So for every bar that opens after the previous bar closes above the 10EMA I want to enter a new long position, hold it for exactly 10 bars, and then sell. This would continue until the price dipped below the 10EMA. So, if the trend lasted for more than 10 bars total then the strategy would enter long trades only until the 11th bar where it would then both open a new long trade as well as close out the initial long trade (and so on).
The concept of how to program this is what is eluding me at the moment. Whenever I try to code this using the strategy builder I end up doing one of 2 things. First, I was getting the consecutive trades to open, but they would all close on the same bar (when my counter is hit from the FIRST entry, not each individual entry). Second, I tried the following code, but no new entry would be opened until the initial entry had been closed (after the 10 bars).

protected override void OnBarUpdate()
{
// Condition set 1
if (CrossAbove(Close, EMA(10), 1))
{
EnterLong(DefaultQuantity, "long");
}

// Condition set 2
if (Close[0] > EMA(10)[0])
{
EnterLong(DefaultQuantity, "long");
}

// Condition set 3
if (BarsSinceEntry() == 10)
{
ExitLong("close", "long");
}
}



So, I realize this is probably basic for most of you, but could someone help explain to me how to code this so I end up with the desired result?

Thanks for any help,
Jordis

1. Condition 1 is entering a trade on the cross over, when you run the strategy with CalculateOnBarClose = true this is not needed. If set to false condition 2 would have to be changed as it would fire on every tick.

2. Condition 2 only can fire when there is no position with the signal name "long" you could instead use "long"+Time[0] to generate a unique signal name.

That way you could also access that 10 bars later as it will then be "long"+Time[10]

hope that helps...

Vvhg

P.s. The wizard is pretty useless, but the documentation in the help file is excellent. It alsohelps a lot to read some code others wrote...

Hic Rhodos, hic salta.
Reply With Quote
Thanked by:
  #4 (permalink)
 jordis 
Mobile, Alabama
 
Experience: Beginner
Platform: NinjaTrader, TOS
Trading: Stocks
Posts: 29 since Sep 2011
Thanks Given: 6
Thanks Received: 3


vvhg View Post
1. Condition 1 is entering a trade on the cross over, when you run the strategy with CalculateOnBarClose = true this is not needed. If set to false condition 2 would have to be changed as it would fire on every tick.

2. Condition 2 only can fire when there is no position with the signal name "long" you could instead use "long"+Time[0] to generate a unique signal name.

That way you could also access that 10 bars later as it will then be "long"+Time[10]

hope that helps...

Vvhg

P.s. The wizard is pretty useless, but the documentation in the help file is excellent. It alsohelps a lot to read some code others wrote...

thanks for the response. I really appreciate the help!

Started this thread Reply With Quote
  #5 (permalink)
 jordis 
Mobile, Alabama
 
Experience: Beginner
Platform: NinjaTrader, TOS
Trading: Stocks
Posts: 29 since Sep 2011
Thanks Given: 6
Thanks Received: 3

Ok, now I've got a new problem!

So I tried working on the code from above to track and sell a position after a set number of minutes.

First, I tried just writing the code to perform the action to the short side, using the below code:

 
Code
 protected override void OnBarUpdate()
        {
			
			{
				ExitShort("c", "short"+Time[14]);
			}
            if (EMA(1)[0] > EMA(10)[0])
            {
                EnterShort(DefaultQuantity, "short"+Time[0]);   
		    }
The results seem to be pretty much what I was trying accomplish, with most positions closing after 15 bars, and the ones that close earlier have Exit Names that are labeled as "Exit on Close" in the backtesting "trades" tab. I can only assume that this means that these trades were closed due to the end of the trading period (end of day maybe?) when my 15 bar limit could not be reached (I should note that I am running this on free downloaded FOREX data, so it's 24hr data in 1 minute bars).

So, after being satisfied for the moment with this result, I tried to add in the long side trades, and that's where I lost it. I tried the following code, hoping for the same result as above, just with long side trades included, but what I got wasn't even really close.

 
Code
 protected override void OnBarUpdate()
        {
			
			{
				ExitShort("c", "short"+Time[14]);
				ExitLong("c2", "long"+Time[14]);
			}
            if (EMA(1)[0] > EMA(10)[0])
            {
                EnterShort(DefaultQuantity, "short"+Time[0]);   
		    }
			else  if (EMA(1)[0] < EMA(10)[0])
            {
                EnterLong(DefaultQuantity, "long"+Time[0]);   
		    }

In the "trades" tab I am seeing a ton of trades with the Exit Name labeled as "Close Position" that are occurring at all hours of the day (ie. no apparent pattern for end of day or anything). The attempted addition of the long side trades even messed up my short side trades that had been successful in the first block of code. Now there are large blocks of trades all closing on (seemingly) random bars all at once.


So, my question is this: what the heck am I missing here? I'm guessing there's something glaringly obvious in my code that explains why I am not achieving what I think I should.


Thanks again for the help. I'm just trying to use trial and error to learn some basic coding, but I'm confusing myself a lot of times and don't know where to turn next!

Started this thread Reply With Quote
  #6 (permalink)
 
vvhg's Avatar
 vvhg 
Northern Germany
 
Experience: Intermediate
Platform: NT
Trading: FDAX, CL
Posts: 1,583 since Mar 2011
Thanks Given: 1,016
Thanks Received: 2,824


jordis View Post
Ok, now I've got a new problem!

So I tried working on the code from above to track and sell a position after a set number of minutes.

First, I tried just writing the code to perform the action to the short side, using the below code:

 
Code
 protected override void OnBarUpdate()
        {
			
			{
				ExitShort("c", "short"+Time[14]);
			}
            if (EMA(1)[0] > EMA(10)[0])
            {
                EnterShort(DefaultQuantity, "short"+Time[0]);   
		    }
The results seem to be pretty much what I was trying accomplish, with most positions closing after 15 bars, and the ones that close earlier have Exit Names that are labeled as "Exit on Close" in the backtesting "trades" tab. I can only assume that this means that these trades were closed due to the end of the trading period (end of day maybe?) when my 15 bar limit could not be reached (I should note that I am running this on free downloaded FOREX data, so it's 24hr data in 1 minute bars).

So, after being satisfied for the moment with this result, I tried to add in the long side trades, and that's where I lost it. I tried the following code, hoping for the same result as above, just with long side trades included, but what I got wasn't even really close.

 
Code
 protected override void OnBarUpdate()
        {
			
			{
				ExitShort("c", "short"+Time[14]);
				ExitLong("c2", "long"+Time[14]);
			}
            if (EMA(1)[0] > EMA(10)[0])
            {
                EnterShort(DefaultQuantity, "short"+Time[0]);   
		    }
			else  if (EMA(1)[0] < EMA(10)[0])
            {
                EnterLong(DefaultQuantity, "long"+Time[0]);   
		    }

In the "trades" tab I am seeing a ton of trades with the Exit Name labeled as "Close Position" that are occurring at all hours of the day (ie. no apparent pattern for end of day or anything). The attempted addition of the long side trades even messed up my short side trades that had been successful in the first block of code. Now there are large blocks of trades all closing on (seemingly) random bars all at once.


So, my question is this: what the heck am I missing here? I'm guessing there's something glaringly obvious in my code that explains why I am not achieving what I think I should.


Thanks again for the help. I'm just trying to use trial and error to learn some basic coding, but I'm confusing myself a lot of times and don't know where to turn next!


When you hold a short position and do EnterLong() NT will first close all short positions and then enter the long. So what you want to do is to exit a short when you have short positions open. So first you check MarketPosition and when its short you do ExitShort() and else you use EnterLong().

Vvhg

Hic Rhodos, hic salta.
Reply With Quote




Last Updated on November 21, 2012


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts