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
Stop Loss Modification
Collapse
X
-
Hello Serge,
I would suggest simplifying your code with one stop loss modification (just a stop loss 2 ticks above the average entry price) to ensure that this is working as intended.
Once you have done this, work with adding additional stop loss modifications.
The most common method you can use to ensure your code works as expected is through the use of the Print() command.
Can you please clarify what you mean by your stop loss being 0? Is it at the break-even point?
-
Hi Zachary!
I was thinking about my logic stop loss and realized I need to CHANGE stop loss whenever it is the LOWEST LOW SINCE ENTER POSITION.
if (LowestBar(Low,BarsSinceEntry(0,"Short3",0)) <= Position.AvgPrice - (6.0 * TickSize))
SetStopLoss("Short3",CalculationMode.Price, Position.AvgPrice + (2.0 * TickSize),false);
If it is the lowest low since I entered position and it is lower or equal 6 ticks then I want to change my stop loss to 2 * TickSize. I back-tested and it doesn't have errors however it doesn't have good results. It doesn't change stop loss correctly. It immediately changes to 0 stop loss.
So I change my formula to this one
if (Position.MarketPosition == MarketPosition.Flat)
{ SetStopLoss("Short1",CalculationMode.Ticks, stop,false);
SetStopLoss("Short3",CalculationMode.Ticks, stop,false);
SetStopLoss("Short2",CalculationMode.Ticks, stop,false);
}
if (Position.MarketPosition == MarketPosition.Short)
{
if (LowestBar(Low,BarsSinceEntry(0,"Short3",0)) <= Position.AvgPrice - (6.0 * TickSize))
SetStopLoss("Short3",CalculationMode.Price, Position.AvgPrice + (2.0 * TickSize),false);
if (LowestBar(Low,BarsSinceEntry(0,"Short3",0)) <= Position.AvgPrice - (9.0 * TickSize))
SetStopLoss("Short3",CalculationMode.Price, Position.AvgPrice,false);
if (LowestBar(Low,BarsSinceEntry(0,"Short3",0))<= Position.AvgPrice - (12.0 * TickSize))
SetStopLoss("Short3",CalculationMode.Price, Position.AvgPrice - (4.0 * TickSize),false);
}
Can you pls look and see what I got wrong
Thanks,
SergeLast edited by olres7; 11-14-2015, 10:36 AM.
Leave a comment:
-
Hello Serge,
Unfortunately, OnMarketData() will not work with backtesting.
If you would like to test your strategy over historical data, you would need to download Market Replay data and run the strategy over Market Replay so OnMarketData() calls will work.
You can find more information about using Market Replay from the NinjaTrader help guide at this link: https://ninjatrader.com/support/help...ket_replay.htm
If you would like to backtest your strategy, you can Add() a 1 tick Data Series to your strategy. You'll need your stop loss logic to utilize this 1 tick series rather than OnMarketData(). Please take a look at this link on our support forum for further information about backtesting strategies with an intrabar granularity: http://ninjatrader.com/support/forum...ead.php?t=6652
Leave a comment:
-
Thanks for previous post!
I did everything you told me. And I have one question? How to backtest OnMarketData(), because it is a realtime quotes of level 1. Do you how to do that?
Leave a comment:
-
Hello Serge,
You'll want to set your initial stop losses in Initialize(). Placing them in OnBarUpdate() will cause them to be set at the specific price you have specified again if that if statement for your entries is true. According to your script, the initial stop loss is set to 4 ticks.
Additionally, while unrelated, EntryHandling should be set in Initialize() and not OnBarUpdate(). You should remove EntryHandling from OnBarUpdate().
I would suggest simplifying your code and working on your stop losses from this simplified code. Once you have done that, you can allow your strategy to enter in additional orders with their own attached stop losses.
The NinjaTrader support team does not provide coding, debugging, or code modification services.
Here is a link on our support forum providing helpful debugging tips: http://ninjatrader.com/support/forum...58&postcount=1
Leave a comment:
-
Thanks for previous post.
I put that code in my program. Even it compiles good there are still no results.
It doesn't change my stop whenever I have some profit. It stays the same 4 ticks stop loss.
The example of it:
private int myInput0 = 1; // Default setting for MyInput0
private IOrder entryOrders1 = null;
private IOrder entryOrders2 = null;
private IOrder entryOrders3 = null;
private int barNumbOfShorOrder1 = 0;
private int barNumbOfShorOrder2 = 0;
private int barNumbOfShorOrder3 = 0;
private int target = 14;
private int stop = 4;
private bool lastTriggered = false;
private bool breakevenTriggered = false;
private bool firstTriggered = false;
protected override void Initialize()
{
TimeInForce = Cbi.TimeInForce.Day;
ExitOnClose = true;
ExitOnCloseSeconds = 60;
BarsRequired = 159;
ConnectionLossHandling = ConnectionLossHandling.KeepRunning;
EntriesPerDirection = 24;
EntryHandling = EntryHandling.UniqueEntries;
CalculateOnBarClose = true;
}
protected override void OnBarUpdate()
{
EntryHandling = EntryHandling.UniqueEntries;
if(Range()[0]==0 && CurrentBar < 160 ) return;
if(entryOrders1 == null && entryOrders2 == null && entryOrders3 == null &&
Volume[0] > 510 &&
Range()[0] <= 13*TickSize &&
High[0] > MAX(High, 30)[1] )
{
entryOrders1 = EnterShortLimit(0,true,1,High[0]+ (0*TickSize), "Short1");
SetStopLoss("Short1",CalculationMode.Ticks, stop, false);
SetProfitTarget("Short1",CalculationMode.Ticks, target);
barNumbOfShorOrder1 = CurrentBar;
entryOrders2 = EnterShortLimit(0,true,1,High[0] - (2*TickSize), "Short2");
SetStopLoss("Short2",CalculationMode.Ticks, stop, false);
SetProfitTarget("Short2",CalculationMode.Ticks, target);
barNumbOfShorOrder2 = CurrentBar;
entryOrders3 = EnterShortLimit(0,true,1,High[0] - (3*TickSize), "Short3");
SetStopLoss("Short3",CalculationMode.Ticks, stop, false);
SetProfitTarget("Short3",CalculationMode.Ticks, target);
barNumbOfShorOrder3 = CurrentBar;
}
if (entryOrders1 != null && CurrentBar >= barNumbOfShorOrder1 + 1)
{ CancelOrder(entryOrders1);
entryOrders1 = null;}
if (entryOrders2 != null && CurrentBar >= barNumbOfShorOrder2 + 1)
{ CancelOrder(entryOrders2);
entryOrders2 = null; }
if (entryOrders3 != null && CurrentBar >= barNumbOfShorOrder3 + 2)
{ CancelOrder(entryOrders3);
entryOrders3 = null; }
}
protected override void OnMarketData(MarketDataEventArgs e)
{
if (e.MarketDataType == MarketDataType.Last && Position.MarketPosition == MarketPosition.Short)
{
if (e.Price <= Position.AvgPrice - (12.0 * TickSize) && !lastTriggered)
{ SetStopLoss("Short3",CalculationMode.Price, Position.AvgPrice - (6.0 * TickSize),false);
lastTriggered = true;
}
if (e.Price <= Position.AvgPrice - (9.0 * TickSize) && !breakevenTriggered)
{ SetStopLoss("Short3",CalculationMode.Price, Position.AvgPrice,false);
breakevenTriggered = true;
}
if (e.Price <= Position.AvgPrice - (6.0 * TickSize) && !firstTriggered)
{ SetStopLoss("Short3",CalculationMode.Price, Position.AvgPrice + (2.0 * TickSize),false);
firstTriggered = true;
}
}
}
protected override void OnExecution (IExecution execution)
{
if (entryOrders1 != null && entryOrders1 == execution.Order)
{
if (execution.Order.OrderState == OrderState.Filled ||
execution.Order.OrderState == OrderState.PartFilled ||
(execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
{
entryOrders1 = null;
}
}
if (entryOrders2 != null && entryOrders2 == execution.Order)
{
if (execution.Order.OrderState == OrderState.Filled ||
execution.Order.OrderState == OrderState.PartFilled ||
(execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
{
entryOrders2 = null;
}
}
if (entryOrders3 != null && entryOrders3 == execution.Order)
{
if (execution.Order.OrderState == OrderState.Filled ||
execution.Order.OrderState == OrderState.PartFilled ||
(execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
{
entryOrders3 = null;
}
}
}
I spent 1 year to write that program. It works good except Stop Loss Modification. I know that I am very close to the finish
Thanks,
SergeLast edited by olres7; 11-12-2015, 02:01 PM.
Leave a comment:
-
Hello olres7,
If you are going to base your stop losses on the last price, you would need to check if e.MarketDataType is MarketDataType.Last: https://ninjatrader.com/support/help...aeventargs.htm
Also, make sure that you are currently not in a flat position. You can do this by checking if Position.MarketPosition != MarketPosition.Flat.
You'll also want to make sure that you have some way keeping track if each stop loss modification point has triggered as well. Booleans would do the job. The reason why you would want to do this is because, let's say the price gets to 12 ticks above the average entry price and moves the stop loss up 8 ticks above the average entry price. What happens if the price gets below 12 ticks above? The second condition will be true. Without having some way of keeping track that the modification point has already occurred, your stop loss will move down to 9 ticks above the average entry price again.
Here's an example:
Code:private bool lastTriggered = false; private bool breakevenTriggered = false; private bool firstTriggered = false; protected override void OnMarketData(MarketDataEventArgs e) { if (e.MarketDataType == MarketDataType.Last && Position.MarketPosition != MarketPosition.Flat) { if (e.Price >= Position.AvgPrice + 12 * TickSize && !lastTriggered) { SetStopLoss(CalculationMode.Price, Position.AvgPrice + 8 * TickSize); lastTriggered = true; } else if (e.Price >= Position.AvgPrice + 9 * TickSize && !breakevenTriggered) { SetStopLoss(CalculationMode.Price, Position.AvgPrice); breakevenTriggered = true; } else if (e.Price >= Position.AvgPrice + 6 * TickSize && !firstTriggered) { SetStopLoss(CalculationMode.Price, Position.AvgPrice - 2 * TickSize); firstTriggered = true; } } }
Leave a comment:
-
Stop Loss Modification:
I want my stop loss change with a profit of TickSize
private int stopLoss = 5;
private int ProfitTarget =15;
+ 6 ticks of profit move stop loss = stop loss -2 *TickSize
+ 9 ticks of profit move stop loss = stop loss 0
+ 12 ticks of profit move stop loss = stop loss +8 * TickSize
I wrote that part of code however it doesn't work
protected override void OnMarketData(MarketDataEventArgs e)
{
if(e.Price >= Position.AvgPrice - (6.0 * TickSize))
SetStopLoss(CalculationMode.Price, Position.AvgPrice - (2.0*TickSize));
if(e.Price >= Position.AvgPrice - (9.0 * TickSize))
SetStopLoss(CalculationMode.Price, Position.AvgPrice - (0*TickSize));
if(e.Price >= Position.AvgPrice - (12.0 * TickSize))
SetStopLoss(CalculationMode.Price, Position.AvgPrice + (8*TickSize));
}
It doesn't do what I want at all
Leave a comment:
-
Hello olres7,
Can you please clarify how you would like your stop loss to function?
Leave a comment:
-
My entry strategy based on historical and not on realtime. So CalculateOnBar = True.
I want my stop loss baded on realtime update
The code I wrote in OnMarketUpdate doesn't work at all. And i dont know how to fix it.
Leave a comment:
-
Hello olres7,
Thank you for your inquiry.
I wanted to note that it looks like your strategy has CalculateOnBarClose set to true. What this will mean is that OnBarUpdate() will only be called every time a bar closes. If your chart is utilizing five minute bars, for example, then OnBarUpdate() will be called every five minutes.
I would suggest changing this to false if you would like OnBarUpdate() to be called on every new tick rather than at bar close.
Can you please explain what you mean about your SetStopLoss() calls not working well in OnMarketData()? If all three conditions that you specified for your conditional statements are true, then they will all execute as you have made them if statements, rather than if-else if statements. If you would like only one of those conditions to work, you need to utilize else if statements.
An else if statement is formatted like below:
Code:if (something) // do something else if (something else) // do something else
I would suggest taking a look at this article on MSDN on utilizing else if statements: https://msdn.microsoft.com/en-us/library/5011f09h.aspx
Please, let us know if we may be of further assistance.
Leave a comment:
-
Stop Loss Modification
Hi there!
I have one of yours example. It is about stop loss modification
On OnBarUpdate it works but not perfect because it updates every 5 min and sometimes has wrong results
Do you know how to convert your example in OnMarketUpdate?
private int stoplossticks = 5;
private int profittargetticks = 15;
protected override void Initialize()
{ SetStopLoss(CalculationMode.Ticks, stoplossticks);
SetProfitTarget(CalculationMode.Ticks, profittargetticks);
CalculateOnBarClose = true;
}
protected override void OnBarUpdate()
{ if (Position.MarketPosition == MarketPosition.Flat)
{SetStopLoss(CalculationMode.Ticks, stoplossticks); }
else if (Position.MarketPosition == MarketPosition.Long)
{
// Once the price is greater than entry price+50 ticks, set stop loss to breakeven
if (Close[0] > Position.AvgPrice + 50 * TickSize)
{
SetStopLoss(CalculationMode.Price, Position.AvgPrice);
}
}
if (Close[0] > Close[1] && RSI(14, 3)[0] <= 30)
{
EnterLong();
}
}
I did myself a little bit and doesn't work well
protected override void OnMarketData(MarketDataEventArgs e)
{
if(e.Price >= Position.AvgPrice - (6.0 * TickSize))
SetStopLoss(CalculationMode.Price, Position.AvgPrice - (2.0*TickSize));
if(e.Price >= Position.AvgPrice - (9.0 * TickSize))
SetStopLoss(CalculationMode.Price, Position.AvgPrice - (0*TickSize));
if(e.Price >= Position.AvgPrice - (12.0 * TickSize))
SetStopLoss(CalculationMode.Price, Position.AvgPrice + (1*TickSize));
}Tags: None
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by PaulMohn, Today, 03:49 AM
|
0 responses
3 views
0 likes
|
Last Post
by PaulMohn
Today, 03:49 AM
|
||
Started by inanazsocial, Today, 01:15 AM
|
1 response
7 views
0 likes
|
Last Post Today, 02:18 AM | ||
Started by rocketman7, Today, 02:12 AM
|
0 responses
10 views
0 likes
|
Last Post
by rocketman7
Today, 02:12 AM
|
||
Started by dustydbayer, Today, 01:59 AM
|
0 responses
2 views
0 likes
|
Last Post
by dustydbayer
Today, 01:59 AM
|
||
Started by trilliantrader, 04-18-2024, 08:16 AM
|
5 responses
23 views
0 likes
|
Last Post Today, 12:32 AM |
Leave a comment: