Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Strategy orders at Tick
Collapse
X
-
Strategy orders at Tick
Is there a way stop and limit orders are executed at the moment they appear on the market on a strategy backtesting? Currently they just execute at the end of a candle, living strange situations as entry and sell on the same down bar on a long position....Tags: None
-
Hello zaico, and thank you for your question.
If you change your script's CalculateOnBarClose setting to false in your Initialize section, your OnBarUpdate routine will run once a tick instead of once a bar. I believe this is the setting you were looking for. For any actions you'd prefer to only take once a bar in this mode, we make available the IsFirstTickOfBar boolean.
If you would prefer to take action even more often than upon each fully formed tick, we also expose the method OnMarketData, documented here,
That will execute the instant a new Last, Bid, or Ask price comes in. Please let us know if there are any other ways we can help.Jessica P.NinjaTrader Customer Service
-
If you would like to modify orders every tick, you may set up a 1-tick data series and modify orders within this series. I have attached a script in which the profit target and stop loss are kept 5 ticks on either side of a 14 period SMA every single tick. This script is being provided as an educational aid only and is not intended for live trading.Attached FilesLast edited by NinjaTrader_JessicaP; 12-12-2016, 02:05 PM.Jessica P.NinjaTrader Customer Service
Comment
-
thanks, but that's not exactly what I asked. My orders are not modified, are set and static since the moment I set my limit buy for a long position.
In my strategy I have a condition, and when It appears I set a limit stop and profit target. Let's say profit and stop are 10 points both on each side. I can't do that with what you show since my limits are static.
Comment
-
The advice I gave first, setting CalculateOnBarClose to false, allows you to take every action on each tick except for modifying orders.
The advice I gave second fills that gap, setting up a 1-tick data series, allowing you to modify orders.
If neither of these pieces of advice will cover your use case, would it be possible for you to provide a stripped-down work-in-progress code sample for us to review? I will be happy to either answer questions regarding this code sample or, if a small educational sample would be more appropriate, modify it and return it. Please note that while our staff can not perform development work we are happy to provide demonstrative code samples to the public.Jessica P.NinjaTrader Customer Service
Comment
-
Code:protected override void Initialize() { CalculateOnBarClose = false; ExitOnClose = true; IncludeCommission = true; } protected override void OnBarUpdate() { if (pattern found){ SetStopLoss("", CalculationMode.Ticks, 10, false); SetProfitTarget("", CalculationMode.Ticks, 10); EnterLongLimit(0, true, 1, MIN(Low, 3)[0], "Long Entry"); } }
Comment
-
The following changes will accomplish your goals.
Code:[FONT=Courier New] protected override void Initialize() { CalculateOnBarClose = false; ExitOnClose = true; IncludeCommission = true;[B] Add(PeriodType.Tick, 1); SetProfitTarget(CalculationMode.Ticks, 10); SetStopLoss(CalculationMode.Ticks, 10);[/B] } protected override void OnBarUpdate() {[B] if (BarsInProgress != 1) { return; }[/B] if (pattern found){ EnterLongLimit(0, true, 1, MIN(Low, 3)[0], "Long Entry"); } }[/FONT]
Jessica P.NinjaTrader Customer Service
Comment
-
Thanks. But something is not right. Now the condition is never accomplished.
This is how the code is now:
Code:protected override void Initialize() { CalculateOnBarClose = false; ExitOnClose = true; IncludeCommission = true; Add(PeriodType.Tick, 1); SetProfitTarget(CalculationMode.Ticks, 10); SetStopLoss(CalculationMode.Ticks, 10); } /// <summary> /// Called on each bar update event (incoming tick) /// </summary> protected override void OnBarUpdate() { if (BarsInProgress != 1) { return; } if (Close[0] == 11212){//example EnterLongLimit(0, true, 1, MIN(Low, 3)[0], "Long Entry"); Print("Time: " + Time[0]+" Stochastic: K" + Stochastics(7,14,3).K[0]+" D:"+Stochastics(7,14,3).D[0]); //to check if ever arrives here } }
Last edited by zaico; 12-13-2016, 05:02 AM.
Comment
-
While directly debugging user code is beyond the scope of the services we can provide, I can provide some hints that can help you get started debugging your code. There are two potential reasons why your condition is not met.
I will lead off with the least likely reason. One thing that could occur here is that our example condition is never met. It is better to use ranges when using double precision values. I would like to recommend the following as a replacement for your example clause.
Code:[FONT=Courier New]if (11211.75 <= Close[0] && Close[0] <= 11212.25)[/FONT]
If you would like to test the rest of your code outside of this condition, I would like to instead suggest replacing this by this first. Once you are sure the code outside this condition is working as expected you can then use your own entry condition with confidence in the rest of the code.
Code:[FONT=Courier New]if (Position.MarketPosition == MarketPosition.Flat)[/FONT]
Originally posted by http://ninjatrader.com/support/helpGuides/nt7/enterlonglimit.htmSyntax
EnterLongLimit(double limitPrice)
EnterLongLimit(double limitPrice, string signalName)
EnterLongLimit(int quantity, double limitPrice)
EnterLongLimit(int quantity, double limitPrice, string signalName)
The following method variation is for experienced programmers who fully understand Advanced Order Handling concepts.
EnterLongLimit(int barsInProgressIndex, bool liveUntilCancelled, int quantity, double limitPrice, string signalName)
If you have gone through the above steps, you now have a thorough understanding as far as why your code is not placing trades in this way. This is the solution you should arrive at :
Code:[FONT=Courier New]EnterLongLimit([B]1[/B], true, 1, MIN(Low, 3)[0], "Long Entry");[/FONT]
Jessica P.NinjaTrader Customer Service
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by mattbsea, Today, 05:44 PM
|
0 responses
3 views
0 likes
|
Last Post
by mattbsea
Today, 05:44 PM
|
||
Started by RideMe, 04-07-2024, 04:54 PM
|
6 responses
31 views
0 likes
|
Last Post
by RideMe
Today, 05:31 PM
|
||
Started by tkaboris, Today, 05:13 PM
|
0 responses
2 views
0 likes
|
Last Post
by tkaboris
Today, 05:13 PM
|
||
Started by GussJ, 03-04-2020, 03:11 PM
|
16 responses
3,282 views
0 likes
|
Last Post
by Leafcutter
Today, 04:14 PM
|
||
Started by WHICKED, Today, 12:45 PM
|
2 responses
20 views
0 likes
|
Last Post
by WHICKED
Today, 03:36 PM
|
Comment