Welcome to NexusFi: the best trading community on the planet, with over 150,000 members Sign Up Now for Free
Genuine reviews from real traders, not fake reviews from stealth vendors
Quality education from leading professional traders
We are a friendly, helpful, and positive community
We do not tolerate rude behavior, trolling, or vendors advertising in posts
We are here to help, just let us know what you need
You'll need to register in order to view the content of the threads and start contributing to our community. It's free for basic access, or support us by becoming an Elite Member -- see if you qualify for a discount below.
-- Big Mike, Site Administrator
(If you already have an account, login at the top of the page)
[newb] question on mult entries in direction of trend
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
Can you help answer these questions from other members on NexusFi?
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...
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:
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.
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().