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
Strategies do not work in backtest or optimization (Sample strategy works)
Collapse
X
-
Strategies do not work in backtest or optimization (Sample strategy works)
I cannot get strategies that I create to work in backtest or optimization. They do not execute any trades and the results are always 0.00 They work in a chart using market replay data, executing trades. I tried the sample moving average strategy and it worked in backtest and optimization. I thought it might be because I renamed them. So I created a new strategy from the strategy builder without renaming it. Tested it on market replay and then tried it in backtest but it will not execute any trades. I tried removing "State == State.Realtime" from my strategies in case that was causing the problem but there was no change. I've checked online for info but found nothing to help.Tags: None
-
Hello ronaldgreene828,
Conditions with State == State.Realtime in OnBarUpdate() might used only have the conditions evaluate as true in real-time.
f the strategy is applied to a chart, is there data with new bars appearing on the chart the Strategy is applied to?
Is the strategy showing as enabled on the Strategies tab of the Control Center?
If the strategy is in the Strategy Analyzer, is there data appearing on the Chart Display of the Strategy Analyzer?
Importantly, are there errors appearing on the Log tab of the Control Center?
Try testing the Sample MA Crossover strategy included with NinjaTrader on the same chart or Strategy Analyzer using the same instrument, bar type, interval, and date range.
Do you see results with this strategy? (This would confirm for us data is available and the issue is with the logic in the custom script)
If the strategy is getting data and the strategy is enabled or a backtest is run with no errors in the Log tab of the Control Center, then would likely indicate the logic conditions did not evaluate as true or orders are being ignored or cancelled.
In order to better understand how the code is working, it will be necessary to use Print to see how the conditions are evaluating and enable TraceOrders to see if orders are being submitted, ignored, rejected, or cancelled.
Below is a link to a forum post that demonstrates using prints to understand behavior and includes a link to a video recorded using the Strategy Builder to add prints.
https://ninjatrader.com/support/foru...121#post791121
Enable TraceOrders, print the time of the bar and all values used in all of the conditions that submit entry orders.
Let me know if you need any assistance creating a print or enabling TraceOrders.
Save the output from the output window to a text file and provide this with your reply.Last edited by NinjaTrader_ChelseaB; 08-30-2021, 07:08 AM.Chelsea B.NinjaTrader Customer Service
-
Yes, the sample moving average strategy works in backtest and optimization. Custom strategies do not. For custom strategies, bars show in the chart display of the analyzer. It shows enabled in the strategies tab of the Control Center.
Everything compiles in the editor. However, there are errors showing in the Log tab of the Control Center when I run the analyzer with a custom strategy. The errors are for 7 other custom strategies not being tested. All the errors occurred during one backtest on the analyzer. All of the errors say, "Error on calling 'OnStateChange' method: Object reference not set to an instance of an object."
Comment
-
I deleted the 7 old strategies and then tried to run other custom strategies in the analyzer. Now there are no errors but no custom strategies will run in the analyzer. They all run in a chart with the playback connection. The sample moving average executes trades every time in the analyzer. Custom strategies will execute trades in a regular chart, but not in the analyzer. The chart display in the analyzer shows bars and indicators but no trades are executed.
If no custom strategies work in the analyzer, but they all work in a regular chart to execute trades, it doesn't sound like logic in the script. There must be something that is preventing all custom strategies from executing trades.
Comment
-
Hello ronaldgreene828,
Attached is a simple custom script, that is placing trades on my end, if you would like to test your theory all custom scripts cannot be backtested (without making any modifications to the test script).
Does this custom test script also not produce trades while showing data on a chart?
As long as the SampleMACrossover is able to return results on the same data, this would likely indicate an issue with the code in your custom script.
An error of OnStateChange() would prevent the script from processing.
Print(State); in OnStateChange(). Which state is the last state to appear before the error occurs?
Add a print after each line in that state with a different number or letter. What is the last print to appear? What is the line of code below that print?
Attached FilesChelsea B.NinjaTrader Customer Service
Comment
-
Yes, that works in the analyzer. So I decided to try creating a simple strategy also using the strategy builder without opening up the editor. It works also in the analyzer. For custom strategies, I've been naming them and then copying from other strategies and pasting everything below the public class name line and then changing the name to match in State.SetDefaults. I didn't have a problem using the strategy this way in a regular chart. So in pasting from one to another, I'm changing something.
Also, when I include State == State.Realtime in conditions, it doesn't work in the analyzer. I was told by someone here in the forum that I need this in order to execute trades. When I included this, then strategies worked in a regular chart. But now I need to remove this to use the analyzer. What should I do? Is this needed?
Comment
-
Hello ronaldgreene828,
If the custom script I have provided you is producing results, this confirms the issue is with the logic in your script.
Where you have stated:
" I was told by someone here in the forum that I need this in order to execute trades. "
Who specifically told you this? What did they exactly say, word for word?
No, requiring State to be State.Realtime will prevent all logic from being evaluated historically and this means a script cannot be back-tested as backtest is historical and not real-time.
Another way of writing that is if (State == State.Historical) return;, which stops code execution until the State is State.Realtime.
See this example in the help guide:
Code:protected override void OnBarUpdate() { [B]// only process real-time OnBarUpdate events[/B] if (State == State.Historical) return; //rest of logic }
This would be a way of making sure the strategy starts flat (by preventing historical trades) when the strategy is using the wait until flat start behavior and you are not wanting to wait until the historical virtual position is exited.
https://ninjatrader.com/support/foru...541#post811541
For real-time you could use immediately submit (and optionally synchronize account) if you don't want to wait for the historical virtual position to be exited.
Or you could exit on the last historical bar.
With that said, focusing back on the first inquiry at hand, please attach the output from the prints with your next reply.Last edited by NinjaTrader_ChelseaB; 08-30-2021, 07:06 AM.Chelsea B.NinjaTrader Customer Service
Comment
-
I'm not very sharp with code, no other experience and I don't really understand what you want specifically in how to print output, etc. References that you make are not clear to me, but I'm sure many will understand. But you have helped me enough so that I can go ahead and remake any strategy with the strategy builder and then try not to edit outside of OnBarUpdate. Thanks
Comment
-
Ok, you wanted to know who told me about State == State.Realtime. I was getting early entries on market playback. Here is what Ninjatrader_Jesse actually wrote:
"The most simple way is to add a condition to check the state. If you want to only work in realtime you can add the following condition in OnBarUpdate:
if(State != State.Realtime) return;
If you are using the builder you would need to add a condition into each set checking that the Misc -> CurrentState equals Misc -> State -> Realtime."
So I'll edit according to the information you gave me, thanks.
Comment
-
Hello ronaldgreene828,
Please watch the video 'Debugging using prints with the Strategy Builder' that is linked post #2.
Using prints and TraceOrders to get output is a vital part of developing a script that is a required step in understanding behavior. I recommend watching the videos and starting small. Just start by printing the time of the bar.Chelsea B.NinjaTrader Customer Service
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by Tim-c, Today, 08:36 AM
|
4 responses
13 views
0 likes
|
Last Post Today, 02:45 PM | ||
Started by JohnS52, 03-22-2024, 08:06 AM
|
4 responses
28 views
0 likes
|
Last Post
by JohnS52
Today, 02:30 PM
|
||
Started by RaygunWizzle, Yesterday, 08:52 PM
|
2 responses
11 views
0 likes
|
Last Post
by RaygunWizzle
Today, 02:26 PM
|
||
Started by carnitron, 03-25-2024, 01:59 PM
|
11 responses
51 views
0 likes
|
Last Post
by carnitron
Today, 02:18 PM
|
||
Started by Gerik, 12-21-2023, 08:20 AM
|
15 responses
203 views
0 likes
|
Last Post
by Mindset
Today, 02:06 PM
|
Comment