NinjaTrader Support Forum  
X

Attention!

This website will be down for maintenance from Friday May 24th at 6PM MDT until Saturday May 25th at 11AM MDT. We apologize for the inconvenience. If you need assistance during this time, please email sales@ninjatrader.com


Go Back   NinjaTrader Support Forum > NinjaScript Development Support > Strategy Development

Strategy Development Support for the development of custom automated trading strategies using NinjaScript.

Reply
 
Thread Tools Display Modes
Old 03-16-2010, 05:31 PM   #1
suprsnipes
Senior Member
 
Join Date: Aug 2008
Posts: 233
Thanks: 19
Thanked 5 times in 5 posts
Default position.AvgPrice causing problems within strategy

Hi,

I'm having some intermittent trouble with the following;

I am using this line of code to save the prior trade as a private double in the variables section.

Code:
    
    protected override void OnPositionUpdate(IPosition position)
        {
            if (position.MarketPosition == MarketPosition.Long) priorTrade = position.AvgPrice;
            Print(Instrument.FullName + " " + position.AvgPrice);
        }
Then I am using the priorTrade within my logic to prevent subsequent trades at the same price as the previous trade.

I loaded the strategy today which should have placed a long limit to buy, when it didn't I removed this line of code below referencing the prior trade and reloaded the strategy which placed the order.

Code:
priorTrade != gNumb().Line4[0]
Could anyone offer any help with this issue?

Regards,
suprsnipes
suprsnipes is offline  
Reply With Quote
Old 03-17-2010, 05:27 AM   #2
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,404
Thanks: 252
Thanked 974 times in 957 posts
Default

suprsnipes, when do you reset the price level to not take a trade, daily weekly?

Not sure I fully follow, but to me it seems your logic is working as this trade as perhaps at the same price level as the last one saved to the corresponding variable?
NinjaTrader_Bertrand is offline  
Reply With Quote
Old 03-18-2010, 04:34 AM   #3
suprsnipes
Senior Member
 
Join Date: Aug 2008
Posts: 233
Thanks: 19
Thanked 5 times in 5 posts
Default

Hi Bertrand,

The variable priorTrade is only stored for the session as far as I know...does the variable reset when you stop the strategy?

My logic is as you have mentioned. I'm using the priorTrade to ensure if all other conditions are met AFTER the strategy has already entered a long position at the same price level as the priorTrade then my strategy will ignore the signal.

I am testing another strategy with the same priorTrade code where I am NOT running into the same problem.

The main issue is the strategy won't place any trades whatsoever, but as soon as I remove the line of code regarding the priorTrade the strategy runs as expected...can you offer any further suggestions?
suprsnipes is offline  
Reply With Quote
Old 03-18-2010, 06:23 AM   #4
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,404
Thanks: 252
Thanked 974 times in 957 posts
Default

suprsnipes, well if you do not reset it explicitly at Session break it may still be filled from a prior session as the strategy cycles through the historical OnBarUpdate() part as you start it up. So you would need to debug the values being set by the strategy for this variable as you already determined it works as expected without this involved in your conditions.

http://www.ninjatrader-support2.com/...ead.php?t=3418
NinjaTrader_Bertrand is offline  
Reply With Quote
Old 03-18-2010, 06:47 AM   #5
suprsnipes
Senior Member
 
Join Date: Aug 2008
Posts: 233
Thanks: 19
Thanked 5 times in 5 posts
Default

Thanks for the quick reply (as always),

Bertrand could you please let me know where you would suggest I place the code to reset the priorTrade variable?

Regards,
suprsnipes
suprsnipes is offline  
Reply With Quote
Old 03-18-2010, 07:11 AM   #6
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,404
Thanks: 252
Thanked 974 times in 957 posts
Default

You're welcome, you could place it at the session break then -

http://www.ninjatrader-support.com/H...sionBreak.html
NinjaTrader_Bertrand is offline  
Reply With Quote
Old 03-18-2010, 05:22 PM   #7
suprsnipes
Senior Member
 
Join Date: Aug 2008
Posts: 233
Thanks: 19
Thanked 5 times in 5 posts
Default

Hi Bertand,

After pointing me in the right direction which I really appreciate by the way, to reset the priorTrade variable at the start of each session this is what I come up with.

Code:
 protected override void OnBarUpdate()
        {
            // At the start of a new session
            if (Bars.SessionBreak)
            {
            // Reset the priorTrade variable
                priorTrade = 0;
            }  
         }
I'll be testing this over the next few days but it appears as though the above code has solved the problem.

Learning heaps by the way...and it's great the you guys at NT offer support to inexperienced programmers willing to have a go.

Best regards,
suprsnipes
suprsnipes is offline  
Reply With Quote
Old 03-19-2010, 05:00 AM   #8
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,404
Thanks: 252
Thanked 974 times in 957 posts
Default

You're welcome, great this helped you made it work.
NinjaTrader_Bertrand 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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Position.AvgPrice = Order Fill Price? tonyh Strategy Development 1 03-13-2010 12:28 PM
Position.AvgPrice Faspomy Strategy Development 7 02-16-2010 10:52 AM
Position.AvgPrice Real Value daven Automated Trading 1 10-30-2008 09:31 AM
Why Position.AvgPrice gives not exact price pmaglio Strategy Development 8 07-24-2008 12:32 PM
Position.AvgPrice MrBaffalo Strategy Development 1 05-16-2007 09:01 AM


All times are GMT -6. The time now is 08:28 PM.