protected override void Initialize() { EntriesPerDirection = 1; EntryHandling = EntryHandling.UniqueEntries; CalculateOnBarClose = false; } protected override void OnBarUpdate() { if (FirstTickOfBar && Position.MarketPosition == MarketPosition.Flat) { if (ToTime(Time[0]) >= 92500 && ToTime(Time[0]) <= 154500) { if (CrossAbove(Close, 100, 1)) { EnterLong(position1, "Long100a"); EnterLong(position2, "Long100b"); } if (CrossAbove(Close, 40, 1)) { EnterLong(position1, "Long40a"); EnterLong(position2, "Long40b"); } if (CrossBelow(Close, 30, 1)) { EnterShort(position1, "Short30a"); EnterShort(position2, "Short30b"); } if (CrossBelow(Close, 90, 1)) { EnterShort(position1, "Short90a"); EnterShort(position2, "Short90b"); } } } //Profit Targets (position1) if (Position.MarketPosition == MarketPosition.Long) { if (Close[0] >= 110) { ExitLong("Long100a", ""); } if (Close[0] >= 50) { ExitLong("Long40a", ""); } } if (Position.MarketPosition == MarketPosition.Short) { if (Close[0] <= 20) { ExitShort("Short30a", ""); } if (Close[0] <= 80) { ExitShort("Short90a", ""); } } //Stop Losses (position1) if (FirstTickOfBar && Position.MarketPosition == MarketPosition.Long) { if (Close[0] < 90) { ExitLong("Long100a", ""); } if (Close[0] < 30) { ExitLong("Long40a", ""); } } if (FirstTickOfBar && Position.MarketPosition == MarketPosition.Short) { if (Close[0] > 100) { ExitShort("Short90a", ""); } if (Close[0] > 40) { ExitShort("Short30a", ""); } } }
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Why are my positions exiting and re-entering?
Collapse
X
-
Why are my positions exiting and re-entering?
I'm having a problem with my strategy while running backtests. I'm trying to create a position that scales out, and I understand I need to scale in to do this. So I create two different entry signals. The strategy will enter into the position, but before hitting a profit target or a stop I am exiting out of the position. I feel like I'm missing something pretty important and basic here, but I can't figure it out. I'm new to coding and I know this code isn't optimal..
Code:Tags: None
-
Hello IFT20,
This is because you are checking the values of the "Close" price to a static value which is causing your strategy conditions to be true on the next bar which is "Exiting" out of your order.
From your Screenshot I see that the price of the instrument is going to be trading around 130 which is greater than 100 and 40 making your last condition true.
One thing you may want to do as well is change your signal names in your Exit orders. To define an entry signal to exit out you will want to define it as the second string. For example:
ExitShort(string signalName, string fromEntrySignal)
JCNinjaTrader Customer Service
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by Skifree, Today, 03:41 AM
|
3 responses
12 views
0 likes
|
Last Post
by Skifree
Today, 12:03 PM
|
||
Started by traderqz, Yesterday, 09:06 AM
|
5 responses
32 views
0 likes
|
Last Post Today, 12:03 PM | ||
Started by guillembm, Today, 11:25 AM
|
1 response
6 views
0 likes
|
Last Post Today, 12:01 PM | ||
Started by owensd, 04-21-2024, 11:34 PM
|
9 responses
34 views
0 likes
|
Last Post Today, 11:59 AM | ||
Started by trilliantrader, 04-10-2024, 09:33 PM
|
7 responses
25 views
0 likes
|
Last Post Today, 11:56 AM |
Comment