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
Stoploss not hitting per strategy
Collapse
X
-
Hello pyraxic,
Thanks for your reply.
When you are using Calculate.OnEachTick and using live data, the bar references change so that [0] is the currently forming bar.
If you want to use the previous bar then you would change it to [1] which would be the last/just closed bar.
-
Thank you, Paul. What is the best way to set stoploss one tick below the last closed bar? I am usingCode:SetStopLoss(@"Long", CalculationMode.Price, Low[0] - TickSize, false);
Leave a comment:
-
Hello pyraxic,
Thanks for your reply.
What you can do is to create a class level double type variable and store the stop price into it in OnOrderUpdate. Then in the OnBarUpdate() you can add a print statement to print out the time, the stop price, and the close price (current price) (you can comment out the print statement in OnOrderUpdate() as you will now be printing the same thing except in OnbarUpdate()). This then should help you see what is happening on a per-trade basis, to understand where the price is relative to the stop price as well as what the stop price is.
Leave a comment:
-
It gives me output like the following which isn't helpful.
stop price is: 3577.5
stop price is: 3577.5
stop price is: 3577.5
stop price is: 3577.5
stop price is: 3577.25
stop price is: 3577.25
stop price is: 3577.25
stop price is: 3577.25
stop price is: 3577
stop price is: 3577
stop price is: 3577
stop price is: 3577
stop price is: 3576.75
stop price is: 3576.75
stop price is: 3576.75
stop price is: 3576.75
stop price is: 3576.5
stop price is: 3576.5
stop price is: 3576.5
stop price is: 3576.5
stop price is: 3576.25
stop price is: 3576.25
stop price is: 3576.25
stop price is: 3576.25
stop price is: 3576
stop price is: 3576
stop price is: 3576
stop price is: 3576
stop price is: 3575.75
stop price is: 3575.75
stop price is: 3575.75
stop price is: 3575.75
stop price is: 3575.5
stop price is: 3575.5
stop price is: 3575.5
stop price is: 3575.5
stop price is: 3575.25
stop price is: 3575.25
stop price is: 3575.25
stop price is: 3575.25
stop price is: 3575
stop price is: 3575
stop price is: 3575
stop price is: 3575
stop price is: 3575
stop price is: 3575
stop price is: 3575
stop price is: 3578.75
stop price is: 3578.75
stop price is: 3578.75
stop price is: 3578.75
stop price is: 3578.75
stop price is: 3578.75
Leave a comment:
-
Hello pyraxic,
Thanks for your reply.
Everything looks correct.
Please recompile, remove strategy from the chart and then reapply the strategy and enable the strategy.
Leave a comment:
-
Hmm...it's not providing any output in the NinjaScript Output window. Here is my code. Am I missing anything?
Code:protected override void OnBarUpdate() { if (CurrentBar < BarsRequiredToTrade) return; if (BarsInProgress != 0) return; if (CurrentBars[0] < 1) return; if (IsFirstTickOfBar){ TradeTaken = false; } // Enter Long if ((CrossAbove(MACD1.Default, MACD1.Avg, 1)) && Close[0] > Close[1] && TradeTaken == false) { EnterLong(Convert.ToInt32(DefaultQuantity), @"Long"); TradeTaken = true; } SetStopLoss(@"Long", CalculationMode.Price, Low[0] - TickSize, false); // Enter Short if ((CrossBelow(MACD1.Default, MACD1.Avg, 1)) && Close[0] < Close[1] && TradeTaken == false) { EnterShort(Convert.ToInt32(DefaultQuantity), @"Short"); TradeTaken = true; } SetStopLoss(@"Short", CalculationMode.Price, High[0] + TickSize, false); } protected override void OnOrderUpdate(Cbi.Order order, double limitPrice, double stopPrice,int quantity, int filled, double averageFillPrice, Cbi.OrderState orderState, DateTime time, Cbi.ErrorCode error, string comment) { if (order.Name == "Stop loss") { Print ("stop price is: " + order.StopPrice); } }
Leave a comment:
-
Hello pyraxic,
Thanks for your reply.
Yes, it will work in the Strategy analyzer as well as live.
Leave a comment:
-
Originally posted by NinjaTrader_PaulH View PostHello pyraxic,
Thanks for your reply.
I would suggest debugging your strategy with print statements to help understand what is happening with the stop price.
In OnOrderUpdate() you can check to see if the order.Name is "Stop loss" and then print the price it is set to.
Here is an example:
protected override void OnOrderUpdate(Cbi.Order order, double limitPrice, double stopPrice,int quantity, int filled, double averageFillPrice, Cbi.OrderState orderState, DateTime time, Cbi.ErrorCode error, string comment)
{
if (order.Name == "Stop loss")
{
Print ("stop price is: "+order.StopPrice);
}
}
The print statement sends it output to the new>Ninjascript output window.
Leave a comment:
-
Hello pyraxic,
Thanks for your reply.
I would suggest debugging your strategy with print statements to help understand what is happening with the stop price.
In OnOrderUpdate() you can check to see if the order.Name is "Stop loss" and then print the price it is set to.
Here is an example:
protected override void OnOrderUpdate(Cbi.Order order, double limitPrice, double stopPrice,int quantity, int filled, double averageFillPrice, Cbi.OrderState orderState, DateTime time, Cbi.ErrorCode error, string comment)
{
if (order.Name == "Stop loss")
{
Print ("stop price is: "+order.StopPrice);
}
}
The print statement sends it output to the new>Ninjascript output window.
Leave a comment:
-
-
Originally posted by NinjaTrader_PaulH View PostHello pyraxic,
Thanks for your post.
Please check the "log" tab of the control center for any errors related to your strategy.
Is the screenshot from a live trade, or playback with market replay data, or just a historical trade on the chart or a historical trade in the Strategy analyzer?
What Calculate setting does the strategy run with? (Calculate.OnBarClose, Calculate.OnEachTick, Calculate.OnPriceChange)
Do you have any other code that could be setting the stop-loss to another value unexpectedly?
Code:SetStopLoss(@"Long", CalculationMode.Price, Low[0] - TickSize, false);
Leave a comment:
-
Hello pyraxic,
Thanks for your post.
Please check the "log" tab of the control center for any errors related to your strategy.
Is the screenshot from a live trade, or playback with market replay data, or just a historical trade on the chart or a historical trade in the Strategy analyzer?
What Calculate setting does the strategy run with? (Calculate.OnBarClose, Calculate.OnEachTick, Calculate.OnPriceChange)
Do you have any other code that could be setting the stop-loss to another value unexpectedly?
Leave a comment:
-
Stoploss not hitting per strategy
Hello,
I am trying to set the Stop-Loss one tick above the last bar and have the following code.
Code:// Enter Short if ((CrossBelow(MACD1.Default, MACD1.Avg, 1)) && TradeTaken == false && Close[0] < Close[1]) { EnterShort(Convert.ToInt32(DefaultQuantity), @"Short"); TradeTaken = true; } SetStopLoss(@"Short", CalculationMode.Price, High[0] + TickSize, false);
Tags: None
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by bortz, 11-06-2023, 08:04 AM
|
47 responses
1,609 views
0 likes
|
Last Post
by aligator
Today, 07:22 PM
|
||
Started by jaybedreamin, Today, 05:56 PM
|
0 responses
9 views
0 likes
|
Last Post
by jaybedreamin
Today, 05:56 PM
|
||
Started by DJ888, 04-16-2024, 06:09 PM
|
6 responses
19 views
0 likes
|
Last Post
by DJ888
Today, 05:12 PM
|
||
Started by Jon17, Today, 04:33 PM
|
0 responses
6 views
0 likes
|
Last Post
by Jon17
Today, 04:33 PM
|
||
Started by Javierw.ok, Today, 04:12 PM
|
0 responses
16 views
0 likes
|
Last Post
by Javierw.ok
Today, 04:12 PM
|
Leave a comment: