View Full Version : Selling on the Open, and reactivating NT
beauw
10-07-2008, 07:46 AM
Hi, this is either a coding problem, or has something to to do with me restarting NT in the middle of the day. I opened a few hundred positions yesterday, and had to restart ninja trader when the e-mail send function kept going. Basically, I was wondering if when I restart, there's anyway of telling the NT program to work with the already open positions without resubmitting the orders.
Also, what is the best way to sell on the open? I used if BarsSinceEntry==0 then selllong. Is this not going to work? Or is it that I had to restart and it took new positions that threw this off?--Beau
NinjaTrader_Josh
10-07-2008, 08:14 AM
beauw,
When you restart your strategy, especially since it has been running, you need to pay careful attention to syncing issues. Please review this article: http://www.ninjatrader-support.com/vb/showthread.php?t=4033
Options available to you are available in Tools->Options->Strategies->NinjaScript.
if (BarsSinceEntry() == 0) will trigger when you've never traded before. It does not necessarily mean you will trade on the open. There is no order type that will guarantee you a fill at the open. You can place trades on the first tick of the first bar of the session with this condition.
if (Bars.BarsSinceSession && FirstTickOfBar)
beauw
10-07-2008, 10:48 AM
Here's another problem that I just discovered. If both of my entries and my exits are true, then I keep submitting and resubmitting both orders and exits.
How would I structure my program to ignore the days when I am selling on the open, and ignore the sell condition on the days when I am entering? I seem to have both conditions true for NTAP today.
I guess what I'm trying to check for is that the day that I do go long, is not the day that I exit. And the day that I exit, is also not going to be a day to attempt to go long. I was trying to work the datetime values out, but I didn't have any success.
I see the problem of implementing the script. Let's say the script was buy 8% and 10% below the close of yesterday and sell on the open the next day. Well, the basic problem I'm having is that when I get filled on both entries, the minus 8% and the minus 10%, the sell the next day WILL be true even if the entry condition is true the next day, too. So all I've gotten is a bunch of buys and sells one right after the other. There's something in the datatime series that I think would make this possible.
How do I check if the day that I bought was yesterday? And how do I check if the day I sold was today? I don't get from the date time functions how to check today's date. I just get a lot of hard coded values that aren't of any use to me.
NinjaTrader_Josh
10-07-2008, 11:03 AM
Hi beauw,
You can use flags. If your 8% and 10% are true set a flag variable to true. While this flag is true you don't trigger your sell at open. You can do it vice versa also. Afterwards you can just reset the flag and then the system will be ready for another trade.
beauw
10-07-2008, 11:18 AM
Sorry, that doesn't help, because I'm calculating on bar update, not at the close.
What would happen is that I'd have
if long and boolean sell is true
sell
boolean buy is true
if flat and boolean buy is true
buy today
boolean sell is true
The flag doesn't matter in this case because I have no point of reference for resetting it, because it'll get recalculated on every bar update where the buys are true, and recalculated so that the same problem happens.
No, I need a flag that checks todays date, and then for the sell checks if the buy date was yesterday. If it does that then I can reset the program. The flags by themselves aren't the problem.
It's not that I wasn't going to use flags, but that I need to know how to check if the buydate was yesterday. If you can come up with some code for that, I would appreciate. Vice Versa, checking if the sell date was yesterday.
Is this how you'd check it
(f[0]!=DateTime.Now
and setting this in the buy part
f.Set(DateTime.Now);
sell=true;
NinjaTrader_Josh
10-07-2008, 11:26 AM
Not sure I follow. You can specify the initial state of the flag in the Variables region of your code. You don't need to be linked down to one flag. You can use as many flags as you need to get it to work.
If you think using the dates of the trades will work for you, you can definitely check out the Trade objects and access the underlying IExecution to get their time property.
http://www.ninjatrader-support.com/HelpGuideV6/TradeClass.html
beauw
10-07-2008, 11:38 AM
No, you're not understanding. I set that I bought today, right? I mark the date. If I just use a flag, then it'll be true on the next tick and it'll just go straight to submitting the order. If I mark the date, and I check if that was yesterday as precondition to either buying or selling, THEN, I can reset the flags. The flags aren't an issue. Resetting the flags is the issue, BECAUSE THIS IS NOT CALCULATED ON THE CLOSE. This strategy I assume is calculated on every tick. That means the flags will just cycle through and be true anyway. Come on.
NinjaTrader_Josh
10-07-2008, 11:47 AM
Just set in the entry condition that the bool had to be false to begin with. If your buy bool is false then you can buy. When you buy your buy bool is true. When it comes around on the next tick the buy bool is true, it wont buy again. Reset to false after you've exited or whenever you deem necessary.
if (buyLong == false)
{
EnterLong();
buyLong = true;
}
if (some exit condition)
{
ExitLong();
buyLong = false;
}
beauw
10-07-2008, 11:57 AM
Josh, listen to me. That's the problem with my conditions. THEY'RE BOTH TRUE TODAY! They get reset, not tomorrow, when the bar closes, but TODAY on the NEXT TICK,because I'm not calculating on the close, but INTRADAY ON EVERY TICK! This means if both conditions are true, then I'll just keep buying and selling on every single tick. Do you get me?
I need to know DATE FUNCTIONS. I WANT THE FLAG TO BE DEPENDENT ON TODAY'S DATE.
beauw
10-07-2008, 12:12 PM
Josh, my conditions aren't complicated. They're buyatlimit today, sell on the open tomorrow calculated on each tick, not at the close. I just have one condition on the exit and that is to sell on the open, so the exit condition is simply sell on the open the next day.
Basically,
Buy today, sell tomorrow. In the case of NTAP today I have buy yesterday, sell today, and buy today, which puts it in an infinite loop of buying and selling when both conditions are true. I only want to do one or the other. That is, preferrably, buy today, sell tomorrow, wait to repeat the buy today, sell tomorrow process. That's all I'm trying to do.
The system is basically,
buy 8% below the low
sell the next day
There is nothing fancy about the exit, just that it has to be tomorrow.
So, in the case that we have today,
buy 8% below the low from yesterday
sell the next day and the buy is true again then both conditions are true and I'm stuck in an infinite buy sell loop that keeps repeating on each tick.
NinjaTrader_Josh
10-07-2008, 01:32 PM
beauw,
I am listening to you. Why would your conditions both be true today? You can't submit an order to sell 100 bars into the future. To sell tomorrow you can only submit that order on tomorrow's bar. You have your today's buy condition execute, set your bool to disallow of more entry orders from today, tomorrow comes along, sell condition = true, sell order done, reset bool, can enter long again.
Now you bring up DateTimes. This is a completely separate solution. Like I said, you can get DateTime by checking the Trades object for your entry. Then just check against the current Time[0]. If the entry happened yesterday then you know today you can sell.
Alternatively, you can simply set a temp variable that takes on Time[0] as you enter today. Tomorrow you check ToDay(Time[0]) against ToDay(temp variable). If it is greater than temp variable's return then you know it is tomorrow and that you can sell.
twtrader
10-07-2008, 07:09 PM
...snip...
Buy today, sell tomorrow. In the case of NTAP today I have buy yesterday, sell today, and buy today, which puts it in an infinite loop of buying and selling when both conditions are true. I only want to do one or the other. That is, preferrably, buy today, sell tomorrow, wait to repeat the buy today, sell tomorrow process. That's all I'm trying to do.
... snip ...
.
beauw,
I am wondering, if you have a daily strategy, why are you processing every tick? Why not just use daily bars ?
beauw
10-08-2008, 01:48 PM
beauw,
When you restart your strategy, especially since it has been running, you need to pay careful attention to syncing issues. Please review this article: http://www.ninjatrader-support.com/vb/showthread.php?t=4033
Options available to you are available in Tools->Options->Strategies->NinjaScript.
if (BarsSinceEntry() == 0) will trigger when you've never traded before. It does not necessarily mean you will trade on the open. There is no order type that will guarantee you a fill at the open. You can place trades on the first tick of the first bar of the session with this condition.
if (Bars.BarsSinceSession && FirstTickOfBar)
This bit of code I did add to the program, and it still kept submitting the orders. Does this method not work when the chart is not calculated on the close?
beauw
10-08-2008, 01:49 PM
beauw,
I am wondering, if you have a daily strategy, why are you processing every tick? Why not just use daily bars ?
I am on daily bars, it's just there's ten entries and there's not enough buying power to submit them all. That's why. They have to "get close" to the price before I submit them. The problem is I can't get the program to wait until the bar closes before submitting the exits. And the bit of code that would get the orders price on the open doesn't work.
NinjaTrader_Josh
10-08-2008, 02:27 PM
beauw,
Please use the techniques from my latest post. That code snippet works fine for limiting trades on the first bar of a session and the first tick of that bar. There are no "sessions" when you are using daily bars.
beauw
10-08-2008, 03:41 PM
beauw,
Please use the techniques from my latest post. That code snippet works fine for limiting trades on the first bar of a session and the first tick of that bar. There are no "sessions" when you are using daily bars.
Then, exactly what I told you happened, occured. Thus the code does not work.
The new code doesn't make any sense either. I've attempted to decipher the time functions, and I don't know how to set one. I also don't know how to use the date or time functions, and apparently esignal crash NT now, so I don't even have data to work with, and you're only solution is re-install everything, which I'm not going to do.
Come up with something else.
Be specific about how to set the date and time functions.
I want to mark today's date, and I want to check if today is greater than that date. The code you provided I don't understand.
beauw
10-08-2008, 03:43 PM
A better question, Josh is, can I use getcurrentAsk() functions if the bar is only updating on the close? Will this still work without the rest of the program running? It's been my experience in the past that that doesn't work.
NinjaTrader_Josh
10-08-2008, 04:03 PM
beauw,
The code examples and techniques I've posted should work in theory. It is up to you to tweak it to work in your environment. As a matter of bandwidth, we cannot custom program everything for you. We can give you ideas and tips, but you have to run with it on your own. We can help you along the way, but you need to help us too. When you come back saying it doesn't work there is nothing more I can do for you. On the other hand, if you posted your code we could then help you systematically examine it for potential pitfalls.
CalculateOnBarClose has no bearing on GetCurrentAsk() working or not. GetCurrentAsk() is a real-time property. If you try to access it at the end of the bar, it WILL return you what the present ask is at that exact moment in time. If you access it every single tick it will give it to you at that tick.