Welcome to NexusFi: the best trading community on the planet, with over 150,000 members Sign Up Now for Free
Genuine reviews from real traders, not fake reviews from stealth vendors
Quality education from leading professional traders
We are a friendly, helpful, and positive community
We do not tolerate rude behavior, trolling, or vendors advertising in posts
We are here to help, just let us know what you need
You'll need to register in order to view the content of the threads and start contributing to our community. It's free for basic access, or support us by becoming an Elite Member -- see if you qualify for a discount below.
-- Big Mike, Site Administrator
(If you already have an account, login at the top of the page)
Hello, I need some guidance with a snippet of code. I’m trying to create a closing order using current market price. I cannot wait for the bar to close to complete the calculation. It needs to be current price. For that, I understand that I need to add OnMarketData() to the script. I have successfully done this. I took all the code from under OnBarClose() and moved it below to the OnMarketData() area as I have read in the NT documentation that that OnMarketData() has to be called after OnBarClose(). So we are past that.
I am not understanding how to write the script to do this. I have seen various examples that are close, but nothing that is exact and will compile without errors.
the errror is Operator < Cannot be applied to operands of type 'NinjaTraderData.MarketDataType' and 'Double'
What I need help with is, how do I write the statement that says:
If Current Price (RIGHT NOW) is less than the open of the previous bar, close the trade.
again, I cannot wait for the bar to close to calculate.
That is what I need help with. I am not great with code, and I am learning, so please forgive my lack of expertise. If I am completely using the wrong stuff, please tell me that too. You wont hurt my feelings.
Thank you in advance.
Can you help answer these questions from other members on NexusFi?
I have written many strategies that exit without using OnMarketata().
You may need to switch from CalculateOnBarClose to CalculateOnEachTick. There are at least a couple of ways you can do so.
On the other hand, if you change your order from an ExitLong() to a SetStopLoss(), Ninjatrader should treat it as a stop loss and exit immediately without even changing to CalculateOnEachTick. Place the stop order just below the previous bar's Open.
Much of my coding is, "will this work", and then I try it and see. So, I cannot definitively tell you how to do it, but try using a fairly generic SetStopLoss() order and verify whether it exits right away or not. If it does, futz with the details after you confirm that. If it does not, then, as Roseanne Rosannadanna used to say, "Never mind".
Thank you David. I messed with the settings you suggested. It looks like changing to on each tick from bar close may have resolved this. I didn’t try it before because I was worried that changing the global setting would have also screwed up the entry parameters. But, it seems to be working better, and more where I needed it to. The closing order was giving way too much back waiting for the bar to close to validate the exit. I’m running in the sim this evening and so far so good. Appreciate the response.
Hello Snail 897.
Your code fails with that error because you are comparing objects of two different types. You can compare two prices (which is what you want to do), but comparing a price with a MarketDataType is meaningless so Ninja tells you so.
Are you using NT8? If so, yes you could use some code in OnMarketData() but NT8 provides direct access to the current bid and ask prices. OnMarketData() not needed
Try "if (GetCurrentAsk() < Open[1])
or "if (GetCurrentBid() < Open[1])
If you must use the last price then Iv'e attached an indicator that illustrates how. It prints a line on the Output Window if lastPrice < Open[1]. Note - does not work on historical data.
I found that switching from Barclose to OnTick fixed what I was trying to do, as I was not able to wait for the candle to close before the OnBarUpdate method ran, but your suggestion looks good as well, and thank you for the attachment. I will rewrite it with your suggestions as well and see if there is any behavior difference.
I appropriate the quick response. Have a great day.