how can i code that if price cross an indicator to open a position right there before the close of that bar. for example i want to open a long position when price cross 5 EMA and not wait for the close of the candle. any help is greatly appreciated.
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
how to enter a trade when price cross an indicator
Collapse
X
-
how to enter a trade when price cross an indicator
hello
how can i code that if price cross an indicator to open a position right there before the close of that bar. for example i want to open a long position when price cross 5 EMA and not wait for the close of the candle. any help is greatly appreciated.
Tags: None
-
Hello babouin77,
Thanks for your post and welcome to the NinjaTrader forums.
To have your code react to an immediate cross of the moving average and place an immediate order, on live or playback>market replay data, your code would need to run with Calculate.OnPriceChange (or Calculate.OnEachTick).
Please note that in these calculation modes it is entirely possible that the price may cross and uncross many times within the bar because of the price movement. As a result, your code may issue entry orders many times and you will need to add further logic to prevent multiple orders. It is also possible that even though on one price change the moving average may be crossed and your strategy would place an order, subsequent price changes in the opposite direction may make it appear, historically, that a cross never occurred.
The advantage of using Calculate.OnBarClose is that your code is only executed once per bar and the cross conditions would not have changed, the disadvantage is that you would have to wait for the bar to close for execution and placement of the entry order.Paul H.NinjaTrader Customer Service
-
Thanks for your reply. If I do the price change thing, would I put in the code when price close cross over the EMA for example or something else? because I tried to put price change in strategy builder on step 2 then later in the condition i put that price close cross above ... what would the condition be and how would i use the price change in the condition?
Comment
-
Originally posted by NinjaTrader_PaulH View PostHello babouin77,
Thanks for your post and welcome to the NinjaTrader forums.
To have your code react to an immediate cross of the moving average and place an immediate order, on live or playback>market replay data, your code would need to run with Calculate.OnPriceChange (or Calculate.OnEachTick).
Please note that in these calculation modes it is entirely possible that the price may cross and uncross many times within the bar because of the price movement. As a result, your code may issue entry orders many times and you will need to add further logic to prevent multiple orders. It is also possible that even though on one price change the moving average may be crossed and your strategy would place an order, subsequent price changes in the opposite direction may make it appear, historically, that a cross never occurred.
The advantage of using Calculate.OnBarClose is that your code is only executed once per bar and the cross conditions would not have changed, the disadvantage is that you would have to wait for the bar to close for execution and placement of the entry order.
Comment
-
Hello babouin77,
Thanks for your replies.
When you select Calculate.OnPriceChange or Calculate.OnEachTick, the price of Close[0] will be the current price. For the condition of a cross above, I would suggest checking that the previous bar High[1] was less than the previous bar EMA(5)[1] and that the current price Close[0] is greater than the EMA(5)[0].
Forum member Gland1 (Welcome to the NinjaTrader forums!) has offered a solution based on NT7. To clarify a Bool does not actually change the Calculate setting but with the additional coding would permit the action to occur once per bar.
A simpler approach, in the NinjaTrader8 strategy builder, to have a once per bar action when using Calculate.OnEachTick or Calculate.OnPriceChange, is to create an int type variable, for example call it "SavedBar" and to use the system bar counter CurrentBar. In the entry set you would add an entry condition User variables>SavedBar, NOT equal, Misc>Current bar. In the action section (where you place the order) add the action Misc> Set SavedBar and in the SavedBar field click "set" then select Misc>current Bar. The logic here is that as long as SavedBar is not equal to the current bar you can place the entry and once you place the entry to set the savedBar to be equal to the current bar which means the entry condition can not longer be true until the next bar which by then your conditions will have changed.
Paul H.NinjaTrader Customer Service
Comment
-
Originally posted by babouin77 View Postbut how would you define the first close since in the condition it is saying when we click price to choose from, there is no option other than high low close to choose under price. i do not see current price for example
if a "cross" is included in your exit conditions, set a bool var after the Exit() command (for ex --> executedlong = true . This var verifies the Exit and, then, may trigger another var which (eventually) may turn to COBC = true equivalent.
Another workaround may be to write :
if(FirstTickOfBar)
{ counter1 = 0; } // counter1 = int var
counter1++;
in your entry conditions. Then limit the counter to 1 .for ex :
if ( executedlong && counter1 < 2 ) { EnterLong(), etc}.
You may, alternately, add if (Close[0] > Open[0] + x*TickSize
as an additional entry condition (x= int var, close to bar's brick size (if you use Renko type Bars)), something which will certainly decrease the possibility of muliple entries due to "fluctuations" of the driving indicator(s).
All above ideas, are just ideas, and I have not tested them. You may check them.
Thanks - George.Last edited by Gland1; 06-04-2020, 11:36 AM.
Comment
-
Hello Gland1,
Thanks for your contributions and effort to help.
Please note that the thread OP is using the NinjaTrader8 strategy builder which would be the equivalent of the NinjaTrader7 strategy wizard so your coding help, while appreciated, is likely adding confusion to the OP who may or may not have coding knowledge.
You are welcome of course to continue your contrbutions but they would better serve the thread OP if they were specific to NinjaTrader8 Strategy Builder.
Thanks for your understanding and again welcome to the foums!
Paul H.NinjaTrader Customer Service
Comment
-
Hello babouin77,
Thanks for your reply.
Please note that when using Calculate.OnEachTick or Calculate.OnPriceChange what we have talked about will only work on Live data. If you apply to a chart and look at historical trades only these will only be based on Calculate.OnBarClose. It is only when the strategy is working on real time data (or use Playback with market replay data) that you would see entries made immediately at the cross.
Paul H.NinjaTrader Customer Service
Comment
-
one more thing... let us say i am in a long entry and I want to place a stop loss 2 ticks below the low of the long entry... vice versa for the short entry... when i do at the attached it is not working and getting me out... is that will show in real time or it should show backtested?1 Photo
Comment
-
Hello babouin77,
Thanks for your replies.
In the strategy builder, the stops and targets are to be fixed values only, for example select calculationMode of ticks and a value of 10. You cannot assign a dynamically changing value such as an indicator or a price series. This is a limitation of working with the strategy builder.
You have two alternatives:
1) unlock the strategy builder strategy and work directly in Ninjascript where you can then dynamically assign the profit/stops as wish,
2) In the strategy builder do not use the Stops and Targets and instead create additional sets where you submit the orders using other order methods, for example for a short entry you would use "Exit short position by a limit order" (profit target) and "exit short position by a stop order" (stop loss). I would suggest using signal names to tie the orders together for OCO type functionality (you would give the entry order a signal name and use that same name for the other two orders).
Paul H.NinjaTrader Customer Service
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by CortexZenUSA, Today, 12:53 AM
|
0 responses
1 view
0 likes
|
Last Post
by CortexZenUSA
Today, 12:53 AM
|
||
Started by CortexZenUSA, Today, 12:46 AM
|
0 responses
1 view
0 likes
|
Last Post
by CortexZenUSA
Today, 12:46 AM
|
||
Started by usazencortex, Today, 12:43 AM
|
0 responses
5 views
0 likes
|
Last Post
by usazencortex
Today, 12:43 AM
|
||
Started by sidlercom80, 10-28-2023, 08:49 AM
|
168 responses
2,266 views
0 likes
|
Last Post
by sidlercom80
Today, 12:05 AM
|
||
Started by Barry Milan, Yesterday, 10:35 PM
|
3 responses
13 views
0 likes
|
Last Post Yesterday, 11:58 PM |
Comment