It seems like as soon as the first order is placed it is waiting for that order to be executed and resolved before continuing with the rest of my script. Any ideas on what to do? Thanks in advance.
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Can't create simultaneous orders in my script.
Collapse
X
-
Can't create simultaneous orders in my script.
I am trying to create a script that places 2 Stop Market orders simultaneously. My script is able to compile with no errors but when I run the script it is only placing my 1st order then stopping. It doesn't seem to continue to my next order. I even tried to place the commands for both orders in a "while" loop with a simple condition just to kinda "force" both commands for both orders to be placed.
It seems like as soon as the first order is placed it is waiting for that order to be executed and resolved before continuing with the rest of my script. Any ideas on what to do? Thanks in advance.Tags: None
-
Hello jm_test19,
Are you using a value of greater than 1 for entries per direction?
Also it is not advised to use while loops in NinjaScript because that pauses the scripts execution until the loop is broken, you can submit multiple orders at once as long as you use a higher value like 2 for entries per direction.JesseNinjaTrader Customer Service
-
What if I were to place 2 orders simultaneously at the same time in opposite directions??? I'm researching and I can't find anything. I can do this manually in ninjatrader but I would like to code this into my ninjascript strategy.
Also, which loop is best? A "Do While" Loop or "For Loop". It seems like the loop is only acknowledging one entry at a time and waiting to cancel or fill a trade before moving forward.Last edited by jm_test19; 10-27-2024, 08:48 PM.
Comment
-
Hello jm_test19,
That is not possible while using the managed approach, that is called a bracket order and is blocked. You can do that in the unmanaged approach.
A for loop can be used because that ends, while loops have to have specific logic to exit the loop and also block execution so those should not be used.
JesseNinjaTrader Customer Service
Comment
-
I am also having an issue with orders that are coded based on variables that are "variable" (in other words not static).
For example when I declare a variable based on the bid price from the chart and create a entry order based on that variable, when the order is placed it moves up and down as the price moves.
I would like to get the bid price and place it in a variable and have that variable keep that value until the code itself gives it another value. When the order is placed I would like it to stay put sort of speak.
Also, for some reason when I run my script my actual chart functionality seems to freeze up and I can't see my cursor. I can cancel orders from the DOM chart but that's about it. I have to hard close the ninjatrader app altogether.
Thank You for your support so far I hope Im not bugging you too much. I just in case you were wondering, I am not doing any of this on a live account. Im doing all this testing in simulation.
Thanks again.
Comment
-
Hello jm_test19,
For example when I declare a variable based on the bid price from the chart and create a entry order based on that variable, when the order is placed it moves up and down as the price moves.
If that is the case you likely need to either move the setting of the variable into the entry condition so it only happens once before the entry is submitted or make a seperate condition that only happens a single time to calculate the variable.
Freezing would be unexpected, you mentioned loops earlier are you using any while or other type of loops in the script? Those could be causes of freezing as those pause execution.
JesseNinjaTrader Customer Service
Comment
-
I've learned a lot since I last wrote you. I believe I was just declaring my variables in the wrong location. I drastically punched up my code and resolved a lot of errors but now I have 2 kinds of errors I can't seem to make sense of (4 of each).
The first is the "expected }" error CS1513. And second is the "Type or namespace definition, or end-of-file expected" error CS1022 (which has no info about it).
I'm not sure what to do about the second CS1022 errors at all. But with the CS1513 errors, I checked all my brackets and they're all closed appropriately. I'm not sure what to do or what I am missing.
Comment
-
Hello madb123,
Both of those errors go together, it is because the structure of the file is incorrect. You are missing a closing brace somewhere in your code. The second error is because the structure of the file is incorrect, once that is corrected that will go away.JesseNinjaTrader Customer Service
Comment
-
Got it!!! My structure was wrong.
I have a separate question.
If I have a FOR loop in an OnBarUpdate() method and in the middle of that loop there is code that submits an unmanaged order that calls an OnOrderUpdate() method that in turn calls an OnExecutionUpdate() that calls inevitably calls an OnPositionUpdate:
Question#1: When the order stops out where does the code go next?
Question#2: Will it "go back" to the OnBarUpdate() to finish the rest of the code in the loop???
Sorry for how I worded it. Short of sending my code this is the best I could do.
Comment
-
Hello jm_test19,
If you have a for loop in OnBarUpdate, that loop is going to execute well before any other events happen. It's going to take a few milliseconds to iterate that loop, which will happen before any orders are actually sent to the exchange or any events are seen for the orders' status or fills.
OnBarUpdate is executed from top to bottom all at once for each OnBarUpdate event. Your order methods in OnBarUpdate will trigger new orders, but those are going to run separately from OnBarUpdate. They do not block the code in OnBarUpdate to wait. You will very likely see the order of events like the following:
OnBarUpdate
For loop, lets say 3 iterations
Order submitted
Order submitted
Order submitted
OnOrderUpdate status for first order
OnOrderUpdate status for second order
OnOrderUpdate status for third order
and then when the orders fill OnExecutionUpdate
Depending on the series and script settings, you may or may not see other OnBarUpdate events during that time. For example if you are using OnEachTick then OnBarUpdate is called for each tick so that code will continue to be executed while you are getting updates for those orders separately.JesseNinjaTrader Customer Service
Comment
-
Okay so I really don't need a loop at all inside my OnBarUpdate to generate reoccurring orders. I can just create an if(Position.MarketPosition == MarketPosition.Flat && etc.) statement with conditions to control my orders as the code within OnBarUpdate will generate every time the bar is updated.
So now I have 2 questions.
#1. I have variables that are declared outside of the OnBarUpdate() method at the public class level. As these variables change value throughout the code do I have to worry about the variables being reset on every iteration of the OnBarUpdate()? I assume that they wont as they will carry whatever value they accumulate into the next Bar Update. (This is important for my strategy).
#2. Also, can I simply use the CloseStrategy() to close my strategy? See below for example.
protected override void OnBarUpdate()
{
if (condition A)
{
ABC123;
}
else if (condition B)
{
CloseStrategy();
}
}
Comment
-
Hello jm_test19,
Yes you can just place the order methods inline as well, the loop would only be needed if you wanted to do an action multiple times and don't want to have the same code as multiple lines.
The variables at class level are called class level variables and they will retain a value once set and carry it into OnBarUpdate like you want.
You can use CloseStrategy, that will terminate the strategy and close the position and orders it has. That is a cleanup method to stop the strategy.JesseNinjaTrader Customer Service
Comment
-
Okay last question for the day. sorry to be bugging you.
Is there anyway to track if an order has been closed or terminated somehow. similar to like an '"order".OrderState == OrderState.Working' or order.Filled. So I can track when an order has been stopped out or canceled etc.???
I know OnOrderUpdate() will track an orders state. "An order will change state when a change in order quantity, price or state (working to filled) occurs." But I can't find anything that tracks an order from existing to no longer existing. maybe the condition "order != null"
Comment
-
Hello jm_test19,
Yes you can track that by using OnOrderUpdate. When you close an order that is cancelling an order so it will show up as cancelled in OnOrderUpdate. OnOrderUpdate can be used to add logic for all order states and OnExecutionUpdate is filtered for just the executions. There is a table on this page that shows all the possible states.
JesseNinjaTrader Customer Service
Comment
-
I think I got a pretty good logic and the structure seems solid with no errors. Youve been a huge help. But when I enable my strategy and try to run it nothing happens. When I check the log section I get the following messages in the attachment. Ive changed the OnBarUpdate to OnPriceChange as I don't need the computing intensity of OnEachTick. Please help me figure out what I am doing wrong. Thank you.
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by Zek303, 10-20-2024, 06:17 PM
|
4 responses
63 views
0 likes
|
Last Post
by bltdavid
Today, 01:52 PM
|
||
Started by jointkp1, Today, 01:11 PM
|
0 responses
8 views
0 likes
|
Last Post
by jointkp1
Today, 01:11 PM
|
||
Started by warpinator, Today, 01:01 PM
|
0 responses
13 views
0 likes
|
Last Post
by warpinator
Today, 01:01 PM
|
||
Started by C_Hardy, 11-28-2024, 05:44 PM
|
9 responses
66 views
0 likes
|
Last Post
by C_Hardy
Today, 12:41 PM
|
||
Started by kellyboy, Today, 11:29 AM
|
0 responses
9 views
0 likes
|
Last Post
by kellyboy
Today, 11:29 AM
|
Comment