Go Back   NinjaTrader Support Forum > Application Technical Support > Automated Trading

Automated Trading Support for automated trading systems using NinjaScript. Support for our ATI (Automated Trading Interface) used to link an external application such as TradeStation and eSignal to NinjaTrader.

Reply
 
Thread Tools Display Modes
Old 04-02-2012, 08:14 AM   #1
tonynt
Senior Member
 
Join Date: Jun 2009
Posts: 911
Thanks: 28
Thanked 24 times in 11 posts
Default flat_or_notflat

Hello,

in the attached screenshot you can see when a trade is closed with white background coloring. The next blue upbar (with blue blackground) there should be a new trade referring conditions but there is no entry. The reason is the marketpositionflat condition. Only the next bar would be the entry (as I know from testing).

Can I change the "Position.MarketPosition == MarketPosition.Flat" (with COBC=true) so that entry is done already next bar after exit if conditions are filled? Do I have to change maybe somehow from onBarUpdate to OnExecution?

Thanks
Tony
Attached Images
File Type: jpg flatornot.jpg (13.6 KB, 8 views)
tonynt is offline  
Reply With Quote
Old 04-02-2012, 08:28 AM   #2
NinjaTrader_Matthew
NinjaTrader Customer Service
 
NinjaTrader_Matthew's Avatar
 
Join Date: Apr 2010
Location: Denver, CO, USA
Posts: 5,722
Thanks: 189
Thanked 778 times in 744 posts
Default

Hello,

Position.MarketPosition would only be evaluated the next time the bar closes on OnBarUpdate().

If you would like to improve the timing of this, you can either use another method such as OnExecution() to check your market position, or you can also try adding a higher resolution time period to your strategy and evaluating your position on that BarsArray.

For the second option, please take a look at our refernece sample on Entering on one time frame and exiting on another:

http://www.ninjatrader.com/support/f...ead.php?t=5787
NinjaTrader_Matthew is offline  
Reply With Quote
Old 04-02-2012, 09:39 AM   #3
tonynt
Senior Member
 
Join Date: Jun 2009
Posts: 911
Thanks: 28
Thanked 24 times in 11 posts
Default

Hello Matthew,

thank you for your reply. I work with BarsArray. Maybe I didnīt translate correctly my question before. How can I do in OnExecution? Would this be different to onbarupdate ("flat") so that the entry is done the next bar after exits of the trade before - if conditions filled?

Thanks
Tony



Quote:
Originally Posted by NinjaTrader_Matthew View Post
Hello,

Position.MarketPosition would only be evaluated the next time the bar closes on OnBarUpdate().

If you would like to improve the timing of this, you can either use another method such as OnExecution() to check your market position, or you can also try adding a higher resolution time period to your strategy and evaluating your position on that BarsArray.

For the second option, please take a look at our refernece sample on Entering on one time frame and exiting on another:

http://www.ninjatrader.com/support/f...ead.php?t=5787
tonynt is offline  
Reply With Quote
Old 04-02-2012, 01:04 PM   #4
NinjaTrader_Matthew
NinjaTrader Customer Service
 
NinjaTrader_Matthew's Avatar
 
Join Date: Apr 2010
Location: Denver, CO, USA
Posts: 5,722
Thanks: 189
Thanked 778 times in 744 posts
Default

Hello,

Yes, you can check your position in OnExecution() which will update faster than OnBarUpdate().

This means if your entry condition is true, but is not executing because the MarketPosition is flagged in OnBarUpdate, you can instead put this check in OnExecution which should return as Flat as soon as the previous order has exited.
NinjaTrader_Matthew is offline  
Reply With Quote
Old 04-02-2012, 02:14 PM   #5
tonynt
Senior Member
 
Join Date: Jun 2009
Posts: 911
Thanks: 28
Thanked 24 times in 11 posts
Default

Thank you for your reply. Sorry for my bad translation to english maybe. I meant how this can be done in onexecution-section so that the entry referring conditions in onbarupdate-section is done?

Thank you
Tony



Quote:
Originally Posted by NinjaTrader_Matthew View Post
Hello,

Yes, you can check your position in OnExecution() which will update faster than OnBarUpdate().

This means if your entry condition is true, but is not executing because the MarketPosition is flagged in OnBarUpdate, you can instead put this check in OnExecution which should return as Flat as soon as the previous order has exited.
tonynt is offline  
Reply With Quote
Old 04-02-2012, 03:07 PM   #6
koganam
Senior Member
 
Join Date: Feb 2008
Location: http://omegasigmaindicators.com
Posts: 4,652
Thanks: 31
Thanked 1,930 times in 1,576 posts
Send a message via Skype™ to koganam
Default

Quote:
Originally Posted by tonynt View Post
Thank you for your reply. Sorry for my bad translation to english maybe. I meant how this can be done in onexecution-section so that the entry referring conditions in onbarupdate-section is done?

Thank you
Tony
I think that maybe the answer to your question is the same one as this: http://www.ninjatrader.com/support/f...ad.php?t=48849
koganam is offline  
Reply With Quote
The following user says thank you to koganam for this post:
Old 04-02-2012, 04:09 PM   #7
tonynt
Senior Member
 
Join Date: Jun 2009
Posts: 911
Thanks: 28
Thanked 24 times in 11 posts
Default

Koganam,

thank you very much for your help!

But as you write the first 2 methods are more cpu-intensive, therefore I use the COBC=true.

With the multidataseries I know I have 5 add(...) I would understand but this is not how I have to do it.

Do you know how one can do in the script so that the "flat-condition" is in OnExecution but this work "together" with OnBarUpdate-conditions? Maybe to put all conditions to OnExecution? Or how to refer from one to other? "Conditions in onbarupdate and flat in onexecution, then do...."

Thank you
Tony

Quote:
Originally Posted by koganam View Post
I think that maybe the answer to your question is the same one as this: http://www.ninjatrader.com/support/f...ad.php?t=48849
tonynt is offline  
Reply With Quote
Old 04-02-2012, 11:21 PM   #8
tonynt
Senior Member
 
Join Date: Jun 2009
Posts: 911
Thanks: 28
Thanked 24 times in 11 posts
Default

Hello NinjaSupport,

as I have a lifetime-license on NT I think it should be possible to give a short answer to the question how one can do the conditions in onbarupdate-section but doing the marketposition.flat condtion in onexecution-section. How can the conditions each section refer to the other section?

I could not found on your support sites.

Thanks
Tony
tonynt is offline  
Reply With Quote
Old 04-03-2012, 04:58 AM   #9
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 25,448
Thanks: 418
Thanked 1,602 times in 1,567 posts
Default

Tony, unfortuantely I think our problem here is still one of understand and being clear exactly what you wish to achieve - OnExecution() will fire everytime you receive an execution, if that's the execution taking you flat > then this would be seen earlier in OnExecution() as compared to the position update that will follow then in OnBarUpdate() and OnPositionUpdate(). Those are simply different event timings seen as the event progresses through the NT framework - the question remaining would, what are you looking to do with the earlier known execution event compared to the positon one showing you're flat? Sending a reversal order out?
NinjaTrader_Bertrand is offline  
Reply With Quote
Old 04-03-2012, 12:20 PM   #10
tonynt
Senior Member
 
Join Date: Jun 2009
Posts: 911
Thanks: 28
Thanked 24 times in 11 posts
Default

Hello,

no, I dont want to send a reversal order out. The entry would be at this reversal bar in the screenshot (but no matter if reversal bar or not, this is to explain in this screenshot where the new entry should have been only)

When I work with COBC=true then the entry is not done at a bar when exit of strategy has been the bar before. Please see the attached screenshot. Strategy doesnīt enter the next blue upbar not because its an upbar, but maybe 3 conditions are true there. One of the condition is "&&Position.MarketPosition == MarketPosition.Flat"

I found out that the "flat-condition" is the problem and when I remove then the entry is done this bar (where there is flat!). So I asked in the forum if it could be done to check the flat in onexecution section. This was confirmed, but I donīt know how one can work with the conditions in both sections, the onbarupdate (eg "MACD, SMA....") and checking if flat in onexecution.

I simply want to have eg "if Rising(MACD(BarsArray[3],12,26,9).Avg) == true && "....flat..." but checking flat in onexecution then Enterlong (but checking in onexecution as there is no enterlong when the exit has been the bar before and checking flat is in onbarupdate with COBCtrue)


I think this is more to explain than to code and you know now what I mean

Thank you
Tony


Quote:
Originally Posted by NinjaTrader_Bertrand View Post
Tony, unfortuantely I think our problem here is still one of understand and being clear exactly what you wish to achieve - OnExecution() will fire everytime you receive an execution, if that's the execution taking you flat > then this would be seen earlier in OnExecution() as compared to the position update that will follow then in OnBarUpdate() and OnPositionUpdate(). Those are simply different event timings seen as the event progresses through the NT framework - the question remaining would, what are you looking to do with the earlier known execution event compared to the positon one showing you're flat? Sending a reversal order out?
tonynt is offline  
Reply With Quote
Old 04-03-2012, 04:05 PM   #11
NinjaTrader_Matthew
NinjaTrader Customer Service
 
NinjaTrader_Matthew's Avatar
 
Join Date: Apr 2010
Location: Denver, CO, USA
Posts: 5,722
Thanks: 189
Thanked 778 times in 744 posts
Default

Tony,

Here is a basic example of how you can set a bool flag in OnExeuction to be evaluated in OnBarUpdate

First, we need to create a global boolean variable that can be accessed from both OnBarUpdate and OnExecution
Code:
#region Variables // create isFlat bool bool isFlat = true; #endregion
Next, we will examine the current market position in OnExeuction. As soon as an exeuction is received, it will check your market position and if this is returned as anything but flat, it will set the isFlat to false, indicating that you are in position. Otherwise, the isFlat flag will return to true indicating your market position is flat

Code:
  
protectedoverridevoid OnExecution(IExecution execution) { // Checks if the current strategy position is not flat if (Position.MarketPosition != MarketPosition.Flat) { // sets isFlat bool to false (market position is NOT flat) This will dynamically update in OnBarUpdate isFlat = false; } // otherwise set the flag to true (market position is flat) else isFlat = true; }
Finally, we can workin OnBarUpdate which will return the current market position flag that we set in OnExeuction

Code:
     
protectedoverridevoid OnBarUpdate() { //Checks for a condition and returns the current state of the isFlat bool set in OnExecution if Rising(MACD(BarsArray[3],12,26,9).Avg) == true && && isFlat == true) { EnterLong(""); } }
Please let me know if you have any questions on this.
Last edited by NinjaTrader_Matthew; 04-03-2012 at 05:21 PM.
NinjaTrader_Matthew is offline  
Reply With Quote
Old 04-04-2012, 02:11 AM   #12
tonynt
Senior Member
 
Join Date: Jun 2009
Posts: 911
Thanks: 28
Thanked 24 times in 11 posts
Default

Finally we got it!

THANK YOU!!!



Quote:
Originally Posted by NinjaTrader_Matthew View Post
Tony,

Here is a basic example of how you can set a bool flag in OnExeuction to be evaluated in OnBarUpdate

First, we need to create a global boolean variable that can be accessed from both OnBarUpdate and OnExecution
Code:
#region Variables // create isFlat bool bool isFlat = true; #endregion
Next, we will examine the current market position in OnExeuction. As soon as an exeuction is received, it will check your market position and if this is returned as anything but flat, it will set the isFlat to false, indicating that you are in position. Otherwise, the isFlat flag will return to true indicating your market position is flat

Code:
  
protectedoverridevoid OnExecution(IExecution execution) { // Checks if the current strategy position is not flat if (Position.MarketPosition != MarketPosition.Flat) { // sets isFlat bool to false (market position is NOT flat) This will dynamically update in OnBarUpdate isFlat = false; } // otherwise set the flag to true (market position is flat) else isFlat = true; }
Finally, we can workin OnBarUpdate which will return the current market position flag that we set in OnExeuction

Code:
     
protectedoverridevoid OnBarUpdate() { //Checks for a condition and returns the current state of the isFlat bool set in OnExecution if Rising(MACD(BarsArray[3],12,26,9).Avg) == true && && isFlat == true) { EnterLong(""); } }
Please let me know if you have any questions on this.
tonynt is offline  
Reply With Quote
Old 04-22-2012, 10:08 AM   #13
jp_kettunen
Member
 
Join Date: Dec 2008
Location: Sipoo, Finland
Posts: 93
Thanks: 1
Thanked 0 times in 0 posts
Default IExecution.MarketPosition on Sim account

If I first buy (position = Long) and then sell (position = Flat) on Sim account and inspect inside OnExecution() the values of IExecution.MarketPosition, I find that after the execution of the entry order it returns (correctly) "Long", but after the execution of the exit order it returns (erroneously) "Short". Order qty is in both cases 1, and IPosition.MarketPosition confirms "Flat". Using the latest NT release. Haven't yet tested this in live trading, the other properties have been working fine so far. Sim-specific thing?!?!
jp_kettunen is offline  
Reply With Quote
Old 04-23-2012, 07:14 AM   #14
NinjaTrader_Matthew
NinjaTrader Customer Service
 
NinjaTrader_Matthew's Avatar
 
Join Date: Apr 2010
Location: Denver, CO, USA
Posts: 5,722
Thanks: 189
Thanked 778 times in 744 posts
Default

Hello,

IExecution.MarketPosition only tells you what type of execution occurred - whether it was a buy or a sell. There is not a flat object returned in this function.

IExecution method and properties

To check whether the strategy is currently long, short or flat, please use position.MarketPosition.

This would be the case in live and simulation.
NinjaTrader_Matthew is offline  
Reply With Quote
Old 04-23-2012, 07:32 AM   #15
jp_kettunen
Member
 
Join Date: Dec 2008
Location: Sipoo, Finland
Posts: 93
Thanks: 1
Thanked 0 times in 0 posts
Default

Ok, understand. Perhaps a bit misleading name for that property, since the same information can be retrieved from IExecution.Order.OrderAction.
jp_kettunen is offline  
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -6. The time now is 05:55 AM.

FULL RISK DISCLOSURE: Futures trading contains substantial risk and is not for every investor. An investor could potentially lose all or more than the initial investment. Risk capital is money that can be lost without jeopardizing ones financial security or lifestyle. Only risk capital should be used for trading and only those with sufficient risk capital should consider trading. Past performance is not necessarily indicative of future results. View Full Risk Disclosure.