NinjaTrader Support Forum  

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 10-13-2011, 07:56 AM   #1
Tradingds
Junior Member
 
Join Date: Jun 2011
Posts: 7
Thanks: 0
Thanked 0 times in 0 posts
Default Try to use Trade Counter to limit the orders per direction

Hello everybody

I'm currently developing a trading strategy. I have no previous history in programming, so that's all pretty new to me.It actually worked all just the way it was supposed to (opening/closing trades), but now i tried to add the possibility (with a trade counter, one for short, one for long), to tell the strategy how many trades per direction (long/short) it's supposed to take, before not executing any more trades. But it doesn't take any trades at all now.

I have it set to: CalculateOnBarClose = false;so once it gets the trade signal, it executes the trade. Once the target(s) gets filled it often opens a new one because the trade signal is still there. So sometimes I have 2 trades within one bar. That's OK, but now I'd like to be able to tell the program only to trade for example 3 times and then just to ignore the trade signal.
Once the trade signal (the trade conditions) are not true anymore, it should reset the trade counter immediately, so in case an other signal comes, it will open a new position again (even if it's in the same direction as the trades were before.)
I work with 3 targets (sometime all the same, sometimes different). So those 3 trades for the 3 targets should count as ONE trade taken, not 3.
Below i have a copy of the ninjascript, the way i tried to write it:
Tradingds is offline  
Reply With Quote
Old 10-13-2011, 08:02 AM   #2
Tradingds
Junior Member
 
Join Date: Jun 2011
Posts: 7
Thanks: 0
Thanked 0 times in 0 posts
Default The script:

#[Description("FirtsTry")]
publicclass BMEXTTSv3SOXEasy : Strategy
{
...
...
...
privateint tradeCounterLong= 0;
privateint tradeCounterShort= 0;
privateint maxTradesLong = 3;
privateint maxTradesShort = 3;
privatebool EntryConditionLong;
privatebool EntryConditionShort;



protectedoverridevoid Initialize()
{
CalculateOnBarClose = false;
EntryHandling = EntryHandling.UniqueEntries;
SetTrailStop(CalculationMode.Ticks, trailstop);
}



privatevoid GoLong()
{
tradeCounterLong++;

SetProfitTarget("target1", CalculationMode.Price, Close[0] + (Target1*TickSize));
SetProfitTarget("target2", CalculationMode.Price, Close[0] + (Target2*TickSize));
SetProfitTarget("target3", CalculationMode.Price, Close[0] + (Target3*TickSize));

EnterLong("target1");
EnterLong("target2");
EnterLong("target3");
}

privatevoid GoShort()
{
tradeCounterShort++;

SetProfitTarget("target1", CalculationMode.Price, Close[0] - (Target1*TickSize));
SetProfitTarget("target2", CalculationMode.Price, Close[0] - (Target2*TickSize));
SetProfitTarget("target3", CalculationMode.Price, Close[0] - (Target3*TickSize));

EnterShort("target1");
EnterShort("target2");
EnterShort("target3");
}

protectedoverridevoid OnBarUpdate()
{
EntryConditionLong =
Position.MarketPosition == MarketPosition.Flat
&& ...
&& ...
&& ...
.
.
. ;

EntryConditionShort =
Position.MarketPosition == MarketPosition.Flat
&& ...
&& ...
&& ...
.
.
. ;
//Reset counter for long
if (EntryConditionLong == false)
{
tradeCounterLong = 0;
}

//...and for short
if (EntryConditionShort == false)
{
tradeCounterShort = 0;
}

// entry long
if (EntryConditionLong && (tradeCounterLong < maxTradesLong))
{
GoLong();
}

// entry short
if (EntryConditionShort &&(tradeCounterShort < maxTradesShort))
{
GoShort();
}
}

#region Properties
...
...
...
...
#endregion
}
}

Could somebody please tell me what i did wrong? I tried to apply what i read in other threads here (limit trades per day). But apparently its not working.
Thank you for your help
Tradingds is offline  
Reply With Quote
Old 10-13-2011, 08:30 AM   #3
NinjaTrader_RyanM
NinjaTrader Customer Service
 
NinjaTrader_RyanM's Avatar
 
Join Date: Sep 2009
Location: Denver, CO
Posts: 8,117
Thanks: 249
Thanked 418 times in 415 posts
Default

Hello Tradingds,

Welcome to the NinjaTrader forums. This concept is available in the following sample strategy:
http://www.ninjatrader.com/support/f...ad.php?t=19182

It looks like you are missing the FirstBarOfSession flag designed to reset this counter each day.
NinjaTrader_RyanM is offline  
Reply With Quote
Old 10-13-2011, 09:00 AM   #4
Tradingds
Junior Member
 
Join Date: Jun 2011
Posts: 7
Thanks: 0
Thanked 0 times in 0 posts
Default

Hi Ryan
Thanks for the fast response. I've looked at this sample strategy before and tried to take some of it out. The way I understand FirstBarOfSession is, that that will be when you start the strategy in the morning, is that right?
In my case, it should reset the counter not just every morning, but more important after every time the trade signal (the signal to enter a trade) isn't true anymore. So that could be several times per day.
Tradingds is offline  
Reply With Quote
Old 10-13-2011, 09:09 AM   #5
NinjaTrader_RyanM
NinjaTrader Customer Service
 
NinjaTrader_RyanM's Avatar
 
Join Date: Sep 2009
Location: Denver, CO
Posts: 8,117
Thanks: 249
Thanked 418 times in 415 posts
Default

FirstBarOfSession is true only for the first bar of the session, so if you need certain items reset every day is a good technique for this.

What type of trade limiting are you trying to do with this? Limit trades per day? Per direction?
NinjaTrader_RyanM is offline  
Reply With Quote
Old 10-13-2011, 09:36 AM   #6
Tradingds
Junior Member
 
Join Date: Jun 2011
Posts: 7
Thanks: 0
Thanked 0 times in 0 posts
Default

I'd like to limit trades per direction. For example if there's a downtrend, I get the signal to go short, and enter the first trade. Once the target is filled and the the signal is still good, I'll enter a second short trade and so on. Now I want to give the strategy a certain number of trades to take while the signal is still good. So let's say I'll allow 3 trades, then after the 3rd it should ignore the signal, even if it's still there.
But as soon as the signal to go short is gone -> reset the count so I can go short again the next time I get the signal. So that way there are multiple times per day I need to reset the count
And of course I want the same for long positions too.
Dave
Tradingds is offline  
Reply With Quote
Old 10-13-2011, 09:42 AM   #7
NinjaTrader_RyanM
NinjaTrader Customer Service
 
NinjaTrader_RyanM's Avatar
 
Join Date: Sep 2009
Location: Denver, CO
Posts: 8,117
Thanks: 249
Thanked 418 times in 415 posts
Default

Trades per direction would be handled by properties EntriesPerDirection and EntryHandling, and the signal names of your strategy. These properties work closely together to determine if the strategy accepts further entry signals in a given direction. You should not need a custom counter in this case.
NinjaTrader_RyanM is offline  
Reply With Quote
Old 10-13-2011, 10:24 AM   #8
Tradingds
Junior Member
 
Join Date: Jun 2011
Posts: 7
Thanks: 0
Thanked 0 times in 0 posts
Default

Yeah, I've looked at that before and tried it out as well (just now again). The way I understand it (and it's described in the help guide v7) that's only to use to determent how many positions at the SAME time can be opened if there is more than one signal to trade. But that's only good for the time a position is open, once it closes (like in my case a couple times) it's not doing the job. So that's not it either what I'm looking for.
Tradingds is offline  
Reply With Quote
Old 10-13-2011, 10:32 AM   #9
NinjaTrader_RyanM
NinjaTrader Customer Service
 
NinjaTrader_RyanM's Avatar
 
Join Date: Sep 2009
Location: Denver, CO
Posts: 8,117
Thanks: 249
Thanked 418 times in 415 posts
Default

Yes, that's it. Those properties limits the number of entry signals accepted in the same direction.

Unfortunately it's not clear your plans for this counter, so best suggestion here is to simplify your strategy a bit until it's at a point where you understand its behavior. You can do this by reducing the # of unique signals and conditions. Take advantage of Print statements and TraceOrders output to track all values and follow code flow. This article can help with these techniques:
http://www.ninjatrader.com/support/f...ead.php?t=3418
NinjaTrader_RyanM is offline  
Reply With Quote
Old 10-13-2011, 12:19 PM   #10
Tradingds
Junior Member
 
Join Date: Jun 2011
Posts: 7
Thanks: 0
Thanked 0 times in 0 posts
Default

Alright, thanks for your help. I'll follow that path. :-)
Tradingds is offline  
Reply With Quote
Old 10-13-2011, 02:13 PM   #11
Silver Dragon
Junior Member
 
Join Date: Jul 2009
Posts: 14
Thanks: 13
Thanked 2 times in 2 posts
Default Limit trades to 3

I have beein thinking along the same lines. This will only work if you are talking about consecutive trades. My idea is to have a variable tripped when the trade is triggered. eg shorttrade = 1

The next time a short trade is triggered it adds 1 to it so shorttrade would then = 2 and so on.

In condition statement add: shortrade < 4

shorttrade would be reset when a long trade is entered.


Code:
 
if ( trade conditions here && shorttrade<4)
{
EnterShort(1, "Short 1a")
longtrade = 0;
if (shorttrade = 0)
{
(shorttrade =1;
}
if (shorttrade >0)
{
(shorttrade = shorttrade + 1;
}
}
 
if ( trade conditions here && longtrade < 4)
{
EnterLong(1, "Long 1a")
shorttrade = 0
 
if (longtrade = 0)
{
longtrade = 1;
}
if (longtrade >0)
{
(longtrade = longtrade + 1;
}
}
 
}
Please note I have not tested the code. Just quickly writing my thoughts. Something like this should work if you are doing consecutive trades

SD
Silver Dragon is offline  
Reply With Quote
The following user says thank you to Silver Dragon for this post:
Old 10-13-2011, 06:37 PM   #12
koganam
Senior Member
 
Join Date: Feb 2008
Location: Durham, North Carolina, USA
Posts: 3,199
Thanks: 24
Thanked 1,225 times in 996 posts
Send a message via Skype™ to koganam
Default

Quote:
Originally Posted by Tradingds View Post
I'd like to limit trades per direction. For example if there's a downtrend, I get the signal to go short, and enter the first trade. Once the target is filled and the the signal is still good, I'll enter a second short trade and so on. Now I want to give the strategy a certain number of trades to take while the signal is still good. So let's say I'll allow 3 trades, then after the 3rd it should ignore the signal, even if it's still there.
But as soon as the signal to go short is gone -> reset the count so I can go short again the next time I get the signal. So that way there are multiple times per day I need to reset the count
And of course I want the same for long positions too.
Dave
You will need to set EntriesPerDirection to 3 in the Initialize() method.

ref: http://www.ninjatrader.com/support/h...rdirection.htm
koganam is offline  
Reply With Quote
Old 10-13-2011, 08:28 PM   #13
Silver Dragon
Junior Member
 
Join Date: Jul 2009
Posts: 14
Thanks: 13
Thanked 2 times in 2 posts
Default Limit 3 Trades Per Direction

Lots of mistakes in the code! See attached file for corrected working strategy.


Notes:
  • Tested the short trades for proof of concept. When strategy is enabled "as is" there will be only 3 trades showing. See attached summary.
  • To enable both sides remove commented slashes "//" in front of the code where it says: //Enable after test
  • Remove ExitLong()
  • Add your own code for the strategy.
Note: Run tests on both sides before risking any money! I am making the assumption both sides will work based on the test. Please report back if it does not. Thanks!
  • Enjoy!
SD

Attached Images
File Type: png show 3 trades.png (53.5 KB, 21 views)
Attached Files
File Type: zip Limit3TradesPerDirection.zip (5.9 KB, 21 views)
Silver Dragon is offline  
Reply With Quote
The following user says thank you to Silver Dragon for this post:
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
Direction dependent Limit Order Ducman_nl General Programming 2 01-19-2011 04:27 AM
Two OCO limit orders in same direction - is this possible? Operandi Strategy Development 4 10-20-2010 06:30 AM
One Trade Per Direction Problem 123r34 Strategy Development 23 06-22-2009 11:44 AM
MA direction change counter JamesF Indicator Development 15 06-11-2009 03:50 PM
One Trade per Direction 123r34 Strategy Analyzer 3 02-15-2009 12:43 PM


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