Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
How to test stop loss and target strategies
Collapse
X
-
How to test stop loss and target strategies
I have discovered that using SetProfitTarget and SetStopLoss functions in a strategy only work in real time. Is there a way to perform backtesting for stops and targets?Tags: None
-
Hello ShruggedAtlas,
SetStopLoss and SetProfitTarget do work in backtest as well as historical data.
These should be set before the order is placed.
Attached is an example made with the Strategy Wizard to demonstrate.
Please run this in the Strategy Analyzer or add to a chart. Ensure you have historical data showing in the backtest or chart.
The strategy will only enter a new order if the strategy is in a flat position.
This means after the first order, there will only be a second if the stop loss or profit target closes the order. (or the ExitOnClose)Attached FilesChelsea B.NinjaTrader Customer Service
-
I assume this is an example stategy? I'm trying to figure out what to do with this file to allow me to open it in the Wizard or put it on a chart. I'll see if there is info on this.
Comment
-
Hello ShruggedAtlas,
Yes, the attachment is an example strategy.
Follow these steps to import the NinjaScript:
1. Download the script to your desktop, keep it in the compressed .zip file.
2. From the Control Center window select the menu File > Utilities > Import NinjaScript
3. Select the downloaded .zip file
4. NinjaTrader will then confirm if the import has been successful.
Critical *Note that on any files that say "File already exists on your PC" that start with an "@" symbol are the ones that came preloaded inside of NinjaTrader so you would say "No" so that you do not override those files.
Once imported, to view the strategy in the Strategy Wizard:- Click Tools -> Edit NinjaScript -> Strategy... -> StopLossTargetExampleWizard -> OK
To add the strategy to a chart:- Right-click the Chart -> select Strategies...
- Select StopLossTargetExampleWizard from the list -> click the New button
- In the parameters on the right -> set Enabled to True
- Click OK
To backtest this in the Strategy Analyzer:- Click File -> New -> Strategy Analyzer...
- Right-click an instrument from your instrument lists -> select Backtest
- Click Run Backtest
(You must have a connection with historical data to backtest a script. If you click the Charts tab in the results, there must be data on this chart)Last edited by NinjaTrader_ChelseaB; 02-26-2015, 01:43 PM.Chelsea B.NinjaTrader Customer Service
Comment
-
Ok I see now how this works. I was applying my strategy to my chart and seeing what the performance was...this is totally different apparently to backtesting. i was assuming it was the same thing.
thank you!
Comment
-
Hello ShruggedAtlas,
Applying a strategy to a chart that has historical data on it and enabling will process the script historically which essentially the same as a backtest. (Ensure the strategy is enabled)
This script, if applied to a chart with data, will have historical trades that are exited by the stop loss and profit target. You will be able to view the "backtest results" for the days to load period on the chart by right-clicking the chart -> selecting Strategy Performance -> the instance of the strategy -> Historical.Chelsea B.NinjaTrader Customer Service
Comment
-
Still not working
I have applied my strategy and backtested. Many of the losing trades show losses well beyond my stop value. My stop is set at 20 ticks. 1 YM contract = $100 loss.
my results show some losing trades of several hundred dollars. Whats going on here?
here is my code: am I applying the stop condition properly or is there some other trick to this?
HTML Code:public class RICK3 : Strategy { #region Variables private int stopLoss = 20; // Default setting for StopLoss private int profitTarget = 4; // Default setting for ProfitTarget private int barsToExit = 12; // Default setting for BarsToExit private int entryOffset = 10; // Default setting for EntryOffset private int timeframe2 = 30; private int timeframe3 = 150; private int rsiHigh = 70; private int rsiLow = 30; #endregion protected override void Initialize() { SetProfitTarget("Profit", CalculationMode.Ticks, 40); SetStopLoss("Loss", CalculationMode.Ticks, 20, false); Add(PeriodType.Minute, timeframe2); Add(PeriodType.Minute, timeframe3); CalculateOnBarClose = true; } protected override void OnBarUpdate() { if (CurrentBars[0] <= BarsRequired || CurrentBars[1] <= BarsRequired || CurrentBars[2] <= BarsRequired) return; if(BarsInProgress != 0) return; // Entry Condition if(CUMRSI(BarsArray[1],2,3)[0] < rsiLow && CUMRSI(2,3)[0] < rsiLow) { EnterLongLimit(DefaultQuantity, Close[0] - EntryOffset * TickSize, "Long"); } // Exit Condition if(CUMRSI(BarsArray[1],2,3)[0] > rsiHigh && CUMRSI(2,3)[0] > rsiHigh)// || BarsSinceEntry(0,"Long",0) > barsToExit) ExitLong(); }
Last edited by ShruggedAtlas; 02-26-2015, 04:18 PM.
Comment
-
Hello ShruggedAtlas,
At what price was the entry filled?
At what price was the stop loss submitted?
At what price did the stop loss fill?
Is this in the Strategy Analyzer or on a Chart?
If on a chart, you can find this information by right-clicking the chart -> selecting Strategy Performance -> instance name -> Historical & Real-time.
In the Strategy Analyzer and the chart performance report click the executions tab to see the fill price, click the orders tab to see the submission price.Last edited by NinjaTrader_ChelseaB; 02-26-2015, 04:33 PM.Chelsea B.NinjaTrader Customer Service
Comment
-
This was in strategy Analyser - after the backtest I pulled up the list of trades...chose the worst loss and then pulled up the chart from within strategy analyser.
From the included picture you can see there was no stop applied. my code is identical to the one supplied earlier today as an example so I don't see what i'm doing wrong.
Based on the picture I've included..the order executed at 17330 long so the stop should have been placed automatically at 17310. As you can see, the traded exited at close rather than at the stop.
I looked at trades and execution in strategy analyzer and they are identical to the what the chart shows. no stop was implemented
Is this related in some way to the multi-timeframe aspects of my strategy? I'm implementing this on a 5 minute chart and using 5 and 30 minute data for entry and exit. (ignore the 150 minute - that's for later)Last edited by ShruggedAtlas; 02-26-2015, 04:28 PM.
Comment
-
Hi ShruggedAtlas,
In my example I do not use signal names.
You have chosen to use signal names. However the fromEntrySignal in the stop loss does not match your entry order.
Your entry order uses the signalName "Long".
The profit target however is attached to "Profit".
There is no entry order that uses the signal name Profit, so this profit target is not attached to anything. If you remove the fromEntrySignal, then this profit target will be applied to all orders and not a specific order using the signalName Profit.
EnterLongLimit(int quantity, double limitPrice, string signalName)
http://www.ninjatrader.com/support/h...rlonglimit.htm
SetProfitTarget(string fromEntrySignal, CalculationMode mode, double value)
http://www.ninjatrader.com/support/h...ofittarget.htm
Your stop loss is attached to "Loss". There is not an entry signal named Loss. This stop loss is not applied to any orders.
Try using no fromEntrySignal.
SetProfitTarget(CalculationMode.Ticks, 40);
SetStopLoss(CalculationMode.Ticks, 20, false);
Or if you want your stop loss to be applied to a specific entry, use the signal name of that entry in the fromEntrySignal of the stop loss.Chelsea B.NinjaTrader Customer Service
Comment
-
Ok now i see why those labels are there...to connect the stops to the entry. It works just perfectly now.
thanks!
Comment
-
I discovered this problem yesterday in fact...Once the list of available strategies gets too long it won't fit in the window and NT has apparently not built in a scrolling function in that panel of that window. It's an annoying flaw but easily fixed. Just delete any old strategies you don't think you're likely to use until you are able to fit in the list.
Actually, i just tested this....my comment only refers to applying a strategy using the strategies tab in control panel...on that one you can't resize the window.
When you apply a strategy directly to a chart, you can resize the window and view all the strategies.
Comment
-
Hello Donaldm1,
Thanks for your post and welcome to the Forum!
When you right mouse click on the chart and select strategies (or if you select the strategy icon at the top of the chart) you will have a "strategies" window with a vertical scroll bar so that you can scroll through the list of available strategies on the left upper side of the window (only if you have more strategies than can be shown in the window) Please advise if you do not see this.
When you right mouse click on the strategies tab and select "new strategy" the new strategy window appears. At the top of this window is a drop downlist that will show all available strategies again with a scroll bar (if you have more strategies than will be shown in the list window).
Please see attached examples for clarification.Paul H.NinjaTrader Customer Service
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by Yasharandom, Today, 07:45 AM
|
0 responses
4 views
0 likes
|
Last Post
by Yasharandom
Today, 07:45 AM
|
||
Started by llanqui, Yesterday, 09:59 AM
|
4 responses
23 views
0 likes
|
Last Post
by llanqui
Today, 07:22 AM
|
||
Started by smartromain, 03-13-2024, 01:42 AM
|
5 responses
95 views
0 likes
|
Last Post
by AndreiBig
Today, 06:08 AM
|
||
Started by Noerclou, Today, 04:55 AM
|
0 responses
5 views
0 likes
|
Last Post
by Noerclou
Today, 04:55 AM
|
||
Started by ThoriSten, Today, 03:56 AM
|
0 responses
6 views
0 likes
|
Last Post
by ThoriSten
Today, 03:56 AM
|
Comment