Announcement
Collapse
Looking for a User App or Add-On built by the NinjaTrader community?
Visit NinjaTrader EcoSystem and our free User App Share!
Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less
Partner 728x90
Collapse
NinjaTrader
NT hangs when I run a strategy
Collapse
X
-
How to check for local PC time
Hi Jesse. Thanks for your reply. I do agree with you that from the logic the code should be executing without a hitch but this is not the case in real time execution. I actually tried printing out each execution of the for loop as you suggested and that revealed that indeed there is a problem.
I have a question with regards to how to check the local PC time. I wrote a condition stating that
if(BarsInProgress == 1)
{
if((ToTime(Time[0]) >= 070000 && ToTime(Time[0]) <= 235959) || (ToTime(Time[0]) >= 000000 && ToTime(Time[0]) <= 061500))
TradeTime = true;
else
TradeTime = false;
}
For your reference, I have attached my most recent code. These are lines 81 to 87. I enabled this strategy at around 6:30 am local PC time and hence I was expecting the variable TradeTime to be set at false. However, it was set at true. On closer investigation, I noticed that the last bar in the previous trading session (the trading session had already ended) was around 5:58 am as you can see from the screenshot and this time falls into the TradeTime = true condition. It seems that (ToTime(Time[0]) doesn't actually use PC time but rather the time stamp of the last bar. Is this assumption true? If that is the case, how do I get local PC time? It is crucial for the proper operation of my strategy. Please let me know. Thanks.
PS: The settings for the data series the same as last time
-
Hello mbesha,
For your question regarding Bars.BarsSinceSession.
BarsSinceSession simply gets the amount of bars from the first bar after the last session break and subtracts that from your current bar to come out with the amount of bars since the session. This does work historically.
I am not sure of exactly the results you are expecting but currently it seems that you have used this correct.
You are setting your variable I to the amount of bars in between your current bar and the session start.
Then reducing the count by 1 for every iteration until you have reached 0, each iteration is used as a bars ago value which is based on the bars count between the current bar and the session start.
If the results are not correct I would advise to just take the for loop and create a test strategy or indicator to test just that loop. This would help so that you can print all data and not really worry about what else is going on in the script. you could also change your order logic that is in the loop to DrawDot or something to verify the conditions are happening when you think they should.
Please let me know if I may be of additional assistance.
Leave a comment:
-
Hi Jesse. Thanks for checking it out. It still doesn't seem to be working the way I expect it to even when I use within my time window.
I have a couple of questions I'd like to ask. My intention is to have the for loop run until the first bar in the session is reached. To achieve this I tried using Bars.BarsSinceSession but I am not too sure if this also works with historical data. Does it work with historical data? I want the CheckBars() method to work even with historical data, for example, on Sunday, it should be able to use Friday's data to calculate in readiness for the market open on Monday. Is it possible to use Bars.FirstBarOfSession to achieve my goal? Or is there something like Bar[i] (i being a variable) that I can use to do the same?
Please let me know.
Leave a comment:
-
Hello mbesha,
Thank you for the question.
I looked the strategy over, CheckBars method is working, I'm using all the same settings as you have pictured.
The only thing I have changed was the time in your check so that I could test it this morning
Code:if(ToTime(Time[0]) >= 140000 && ToTime(Time[0]) < 150000)
Code:if(ToTime(Time[0]) >= 060000 && ToTime(Time[0]) < 150000)
I placed a print statement at the top of the CheckBars method and ran it, then worked my way down to the end of the method with the print statement, running it each time I had moved it to check that there were no hold ups.
The print worked all the way through, were you testing this outside your time window in the script? Otherwise I see the method is being used.
Please let me know if I may be of additional assistance.
Leave a comment:
-
Question as to why code is executing as expected
Hi Jesse. I have a quick question for you. I am writing my own custom strategy and I don't know why the code at the very beginning of my OnBarUpdate() method isn't being executed (lines 82 - 93). If you refer to my code which has been attached, the code I am talking about is called CheckBars(). Feel free to modify the time condition for the code to test it. I have tried all I can to make the CheckBars() work but it doesn't seem to even though the strategy is working fine. I have attached a screenshot of the data settings which I am using.
I have also noticed that for lines 198 - 200, the values for the day and date that written using the streamwriter are for the previous day rather than the current day. Why is this so even though the code explicitly states Time[0] ?
Please advise and thanks for the many times you've helped me out before.
Leave a comment:
-
Thanks a lot Jesse. It's so good working with you. I will try to modify the code of my strategy accordingly and will get back to you if there's any lingering problem. Highly appreciate it.
Leave a comment:
-
Hello mbesha,
Instead of trying to make your strategy produce trades to test this, I will provide a test script that shows how to use the stream writer correctly.
The issue you are running into is most likely that you are not closing the writer between writes. What this means is that the first time it writes (creates the file) it is not closed so the file is locked, being locked your script can not edit the file.
So what I have put in the script is simple, it writes the current bar to a new line in the txt file using your path.
What I do is close the writer after each write that way the next loop through it will have access.
In OnTermination, I check if there are any open streams, if so close them to unlock the file. Next I dispose the stream writer before exiting.
Please let me know if I may be of additional assistance.Attached Files
Leave a comment:
-
Thanks for the quick response
I have attached a screenshot of the parameters I am using and have also attached my latest code. Please use this latest one to do your testing.
I actually included a method called OnTermination() at the very end of my code to close the streamwriter but maybe I haven't used it in the right way. Please take a look and let me know what you think. Many thanks.
Leave a comment:
-
Hello mbesha,
Thanks for the question,
I have taken a look at your code and I see where you are using the streamwriter. One thing I noticed right away is that you are not closing the streamwriter and opening a new one for subsequent writes, this may be where you are running into a problem.
I would like to test the script but I am unable to get it to place a trade, can you please provide me with all the parameters you are using along with the instrument you are using this on so that I can test this?
I look forward to being of further assistance.
Leave a comment:
-
Question about how to implement the streamwriter in the strategy
As always, many thanks for your ever kind support and useful assistance. I have stumbled on a new problem and I was wondering if you could help me out. I am trying to have the P/L written in a text file using the StreamWriter but it don't seem to be working. Is there something wrong with my code or logic for the StreamWriter? I have attached my latest code for reference. Thanks in advance.Attached Files
Leave a comment:
-
Hello mbesha,
Thank you for the screenshots,
I loaded the strategy today but was unable to get it to perform in a chart or backtest using the settings you provided. I tried some other variations to the same outcome.
I did notice in the output where you have "Right day to trade?" this never becomes true using the above settings, so that may possibly be something to look into.
I also took a look through the code to see if I could see anything specific preventing this from running but with the complexity of the code as it stands I don't believe I would be able to narrow it down or test this in a reasonable amount of time.
For this item what I might recommend because you know the code better than I, would be to eliminate any unnecessary portions of the code and to leave the logic in place to simply test the logic and order handling.
With just that portion of code remaining it should be quite a bit easier to see what exactly is happening using Print statements.
I am sorry I don't have a better answer for you but I do not see anything that sticks out to me other than the print statement you have already set.
Please let me know if I may be of additional assistance.
Leave a comment:
-
-
Hello mbesha,
Thank you for the reply,
I would be happy to take a look into this today to see if I can locate a reason for this.
Could you please tell me what settings you are using when creating the chart and also what you are using for the backtest so that I can try and recreate this on my end?
If you could take a screenshot of the dataseries window for the chart showing how your chart is set up and also one of the backtest settings window I would appreciate that.
I look forward to being of further assistance.
Leave a comment:
-
Wrong file
Please ignore the file I attached in my previous message and use this one instead. Thanks.Attached Files
Leave a comment:
-
Problem with backtesting
Hi Jesse. Thanks for all your help up until now. I have encountered one new problem. I try backtesting my strategy but it doesn't seem to execute trades at all. Is there something I need to modify in the code for the strategy to work in the Strategy Analyzer for backtesting? Please advise. I have attached my latest code for your reference. Thanks in advance.Attached Files
Leave a comment:
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by tsantospinto, 04-12-2024, 07:04 PM
|
6 responses
98 views
0 likes
|
Last Post
by tsantospinto
Today, 08:57 AM
|
||
Started by rocketman7, Today, 02:12 AM
|
5 responses
25 views
0 likes
|
Last Post Today, 08:56 AM | ||
Started by ZenCortexReal, Today, 08:54 AM
|
0 responses
1 view
0 likes
|
Last Post Today, 08:54 AM | ||
Started by ZenCortexReal, Today, 08:52 AM
|
0 responses
0 views
0 likes
|
Last Post Today, 08:52 AM | ||
Started by trilliantrader, 04-18-2024, 08:16 AM
|
7 responses
28 views
0 likes
|
Last Post Today, 07:56 AM |
Leave a comment: