NexusFi: Find Your Edge


Home Menu

 





need help with live data and closing an order


Discussion in NinjaTrader

Updated
    1. trending_up 1,137 views
    2. thumb_up 4 thanks given
    3. group 3 followers
    1. forum 4 posts
    2. attach_file 1 attachments




 
Search this Thread

need help with live data and closing an order

  #1 (permalink)
 snail897 
Belton, TX
 
Experience: Intermediate
Platform: Ninja Trader, TOS
Trading: Futures, Options
Posts: 4 since Aug 2022
Thanks Given: 2
Thanks Received: 3

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.

My line is this:

“”if ((MarketDataType.Last) < Open[1])””
ExitLong();

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.

Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
What broker to use for trading palladium futures
Commodities
Pivot Indicator like the old SwingTemp by Big Mike
NinjaTrader
MC PL editor upgrade
MultiCharts
ZombieSqueeze
Platforms and Indicators
REcommedations for programming help
Sierra Chart
 
  #2 (permalink)
 
DavidBodhi's Avatar
 DavidBodhi 
Milwaukee, WI, USA
 
Experience: Intermediate
Platform: NinjaTrader
Trading: Equities
Posts: 209 since Oct 2014
Thanks Given: 23
Thanks Received: 207

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".



snail897 View Post
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.

My line is this:

“”if ((MarketDataType.Last) < Open[1])””
ExitLong();

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.


Follow me on Twitter Reply With Quote
Thanked by:
  #3 (permalink)
 snail897 
Belton, TX
 
Experience: Intermediate
Platform: Ninja Trader, TOS
Trading: Futures, Options
Posts: 4 since Aug 2022
Thanks Given: 2
Thanks Received: 3


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.

Thank you..

Started this thread Reply With Quote
Thanked by:
  #4 (permalink)
 DaveS 
Uxbridge UK
 
Experience: Intermediate
Platform: NinjaTrader
Trading: Futures
Posts: 21 since Sep 2012
Thanks Given: 2
Thanks Received: 17

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.

Attached Files
Elite Membership required to download: LastPriceComparison.zip
Reply With Quote
Thanked by:
  #5 (permalink)
 snail897 
Belton, TX
 
Experience: Intermediate
Platform: Ninja Trader, TOS
Trading: Futures, Options
Posts: 4 since Aug 2022
Thanks Given: 2
Thanks Received: 3

Thank you Dave.

Yes, I am using NT8.

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.

snail

Started this thread Reply With Quote




Last Updated on September 5, 2022


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts