if (CurrentBar < BarsRequiredToTrade) { return; } if (State == State.Historical) { return; } if (Close[1] <= SMA(10)[1] && Close[0] > SMA(10)[0]) { if (PositionAccount.MarketPosition != MarketPosition.Flat) { return; } else { EnterLong(2,"Order"); entryBar = PositionAccount.AveragePrice; prevATRBar = ATR(7)[1]; lowestLow = MIN(Low, MaxLookBackPeriod)[0]; atrX2 = prevATRBar * 2; stopLoss = Instrument.MasterInstrument.RoundToTickSize(lowest Low - atrX2); Target = entryBar + Instrument.MasterInstrument.RoundToTickSize(((entr yBar - stopLoss) * RewardMultiplier)); //SetStopLoss("Order",CalculationMode.Ticks, stopLoss, false); // WORKS //EnterShortStopMarket(stopLoss, "Order"); // DOESN'T WORK } }
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
Why can't I use Enter and Exit Methods for Stops and Targets?
Collapse
X
-
Why can't I use Enter and Exit Methods for Stops and Targets?
I commented what works and what doesn't work.
Code:Tags: None
-
Hello jamestrader21x,
When using the Set methods for targets you always need to call that before the entry to ensure a current price is used.
SetStopLoss("Order",CalculationMode.Ticks, stopLoss, false); // WORKS
EnterLong(2,"Order");
The EnterShortStopMarket doesn't work because that is not being called for each bar and will be expired. You can't call that right after the entry because the entry wont be filled yet, you would need to monitor for a position or the entry fill and then submit it. You can see a sample of using exit methods for exits based on an entry fill here: https://ninjatrader.com/support/help...and_onexec.htm
That also cannot be used in combination with the Set methods, you would have to use one or the other.Last edited by NinjaTrader_Jesse; 05-16-2022, 08:10 AM.JesseNinjaTrader Customer Service
-
-
I appreciate the help! I have one last issue. I'm trying to trail the remainder of the position balance behind a volatility indicator. However, I can't seem to get it to work. This code snippet is from the OnExecutionUpdate(). I can move the stop to break even if target 1 is hit. However, having trouble getting the trail to work right. I'm also posting a picture.
Code:if (Position.MarketPosition == MarketPosition.Long && Position.Quantity == 1) { ExitLongStopMarket(1, PositionAccount.AveragePrice + (TickSize * 4), "Stop","Order"); } else if (Position.MarketPosition == MarketPosition.Short && Position.Quantity == 1) { ExitShortStopMarket(1, PositionAccount.AveragePrice - (TickSize * 4), "Stop","Order"); } //The code below is the code I'm trying to program to trail the volatility indicator if (Position.MarketPosition == MarketPosition.Long && Position.Quantity == 1 && stopOrder.StopPrice < (ATRTrailing(3,13,.0005)[0] - (TickSize * 20))) { ExitLongStopMarket(1, ATRTrailing(3,13,.0005)[0] - (TickSize * 20), "Stop","Order"); } else if (Position.MarketPosition == MarketPosition.Short && Position.Quantity == 1 && stopOrder.StopPrice > ATRTrailing(3,13,.0005)[0] + (TickSize * 20)) { ExitShortStopMarket(1, ATRTrailing(3,13,.0005)[0] + (TickSize * 20), "Stop","Order"); }
Comment
-
I may have it working. I'm using a while loop and ExitLongStopMarket(1, PositionAccount.AveragePrice + (TickSize * 4), "Stop","Order").StopPrice...I saw it adjust with an if statement.
Comment
-
Hello jamestrader21x,
For trailing or breakeven logic you would put that in OnBarUpdate. You don't want to use while loops in NinjaScript, that can lead to the script not working correctly because that pauses the execution by looping forever.
The link I provided in post 2 has a break even in its OnBarUpdate, the trail would be identical to that but the condition which checks if the price exceeds the current order price would be changed to the trail offset values.
There is a strategy builder example of trailing in the following link, that shows the general code to check for a trail and calculate a new price. In your script it would essentially be the same concept
JesseNinjaTrader Customer Service
Comment
-
Thank you for all of the help. I have my strategy working. One last thing. In fast moving markets, my ExitStopMar****rder will cancel itself. It will eventually come back. However, when implementing a trail on a live market, it would cost me money. Here is a snippet of the code. I'm posting the short side code since this is the side in which it happened. I also put in some notes to explain what the code does.
Code:// Trades two short positions. If target one is hit, it moves the stop to 4 ticks below entry (Break even) if (Position.MarketPosition == MarketPosition.Short && Position.Quantity == 1 && stopMovedToBreakEven == false) { ExitShortStopMarket(1, Position.AveragePrice - (TickSize * 2), "Stop","Order"); stopMovedToBreakEven = true; } // If stop has been move to break even, then trail volatility indicator if price continues in favor. if (Position.MarketPosition == MarketPosition.Short && Position.Quantity == 1 && stopMovedToBreakEven == true) { if (Close[0] > currentTriggerPrice) { return; } else { currentTriggerPrice = (Close[0] - (trailFrequency * TickSize)); bearishTrail = ATRTrailing(3,13,.005).Lower[0] + (TickSize * 8); } ExitShortStopMarket(1, bearishTrail, "Stop","Order"); } if (Position.MarketPosition == MarketPosition.Flat) { stopMovedToBreakEven = false; }
Comment
-
Hello jamestrader21x,
Unless you have other logic which holds those orders open then they will be expired when the condition is no longer true and a bar elapses.
I believe you missed one of the concepts that is shown in the sample here: https://ninjatrader.com/support/help...and_onexec.htm
The order needs IsLiveUntilCancelled set to true for this type of use so it doesn't expire after 1 bar.
For example:
stopOrder = ExitLongStopMarket(0, true, stopOrder.Quantity, Position.AveragePrice, "MyStop", "MyEntry");
The first 3 parameters change with this overload, you specify true as IsLiveUntilCancelled when submitting the orders. That will let you avoid having logic in OnBarUpdate which keeps calling the order on each bar.
JesseNinjaTrader Customer Service
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by yertle, Yesterday, 08:38 AM
|
7 responses
28 views
0 likes
|
Last Post
by yertle
Today, 12:15 AM
|
||
Started by bmartz, 03-12-2024, 06:12 AM
|
2 responses
21 views
0 likes
|
Last Post
by bmartz
Today, 12:11 AM
|
||
Started by funk10101, Today, 12:02 AM
|
0 responses
4 views
0 likes
|
Last Post
by funk10101
Today, 12:02 AM
|
||
Started by gravdigaz6, Yesterday, 11:40 PM
|
1 response
8 views
0 likes
|
Last Post Yesterday, 11:49 PM | ||
Started by MarianApalaghiei, Yesterday, 10:49 PM
|
3 responses
11 views
0 likes
|
Last Post Yesterday, 11:33 PM |
Comment