Announcement
Collapse
Looking for a User App or Add-On built by the NinjaTrader community?
Visit NinjaTrader EcoSystem and our free User App Share!
Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less
Partner 728x90
Collapse
NinjaTrader
Restrict to trading only 1 time per day per direction.
Collapse
X
-
Restrict to trading only 1 time per day per direction.
As I run a backtest to see the results of my strategy it shows multiple buys and sells during one day as per my criteria. Can I restrict it to only trade once. Thus, after one exit it would no longer take any more long entries. Yet it would still take short entries. And vice versa.Tags: None
-
Works but only for the long trades.
I coded as you suggested and it seems to work correctly (restricts to one trade per day)
This is what I coded:
bool HasTradedLong = false;
If (Bars.FirstBarOfSession)
HasTradedLong = false;
If (tradecondition && !HasTradedLong)
{
HasTradedLong - true;
EnterLong();
}
The above works properly.
However if I add another variable and use it for my short entries it seems not to work. Still works for my long entries but doesn't restrict my short entries to one per day.
This is what I have:
bool HasTradedLong = false;
bool HasTradedShort = false;
If (Bars.FirstBarOfSession)
HasTradedLong = false;
HasTradedShort = false;
If (tradecondition && !HasTradedLong)
{
HasTradedLong = true;
EnterLong();
}
If (tradecondition && !HasTradedShort)
HasTradedShort = true;
EnterShort();
This generates multiple short entries per day.
Also, why do I use Bars.FirstBarOfSession - wouldn't it always return False, as the bar I am going long or short is never the first bar of the session. Yet I am puzzled as to why it works properly for the long entries.
What am I doing wrong?
Thanks
Comment
-
Try one of these guys
http://www.ninjatrader.com/webnew/partners_onlinetrading_NinjaScript.htm
Originally posted by strategy1 View PostI coded as you suggested and it seems to work correctly (restricts to one trade per day)
This is what I coded:
bool HasTradedLong = false;
If (Bars.FirstBarOfSession)
HasTradedLong = false;
If (tradecondition && !HasTradedLong)
{
HasTradedLong - true;
EnterLong();
}
The above works properly.
However if I add another variable and use it for my short entries it seems not to work. Still works for my long entries but doesn't restrict my short entries to one per day.
This is what I have:
bool HasTradedLong = false;
bool HasTradedShort = false;
If (Bars.FirstBarOfSession)
HasTradedLong = false;
HasTradedShort = false;
If (tradecondition && !HasTradedLong)
{
HasTradedLong = true;
EnterLong();
}
If (tradecondition && !HasTradedShort)
HasTradedShort = true;
EnterShort();
This generates multiple short entries per day.
Also, why do I use Bars.FirstBarOfSession - wouldn't it always return False, as the bar I am going long or short is never the first bar of the session. Yet I am puzzled as to why it works properly for the long entries.
What am I doing wrong?
ThanksLast edited by twtrader; 07-22-2008, 09:50 PM. Reason: Do not feel like writing this guys code for hem
Comment
-
strategy1,
You have to consider how an IF statement works. If you don't have brackets, only the line following the IF statement will get executed. If you do have brackets, anything within the brackets will be conditioned by the logic within the IF.
This is true for other conditional logic keywords, such as for(), while(), foreach(), etc.
SO.
If (Bars.FirstBarOfSession)
HasTradedLong = false;
HasTradedShort = false;
means that only HasTradedLong = false will get executed.
HasTradedShort will be executed every bar, hence you will never know you traded short and will always trade short.
if you do this.
If (Bars.FirstBarOfSession)
{
HasTradedLong = false;
HasTradedShort = false;
}
then, the HasTradedLong / HasTradedShort will get executed on the first bar of the session, resetting them. This is what you want.
Google conditional statements C# and you'll find a wealth of infomation.
hope this helps
Anthony
Comment
-
Sorry to dig up this old thread...
If I wanted the strategy to trade three times and have the second and third instances trade only after the first had traded, what would the code look like?
hastradedlong1
hastradedlong2
hastradedlong3
Part two of the question is what is a way to restrict the second Instance from trading until at least 10 minutes after the first?
Comment
-
mainstream,
Assuming you were flat before trading the first time and you are running it on a 1min chart this is what you could try something like this.
Untested code below. Should give you a general framework idea.
Code:if (some trade logic for trade #1) { EnterLong("tradeOne"); tradedOne = true; } if (Position.MarketPosition == MarketPosition.Long) { if (tradedOne == true && tradedTwo != true && BarsSinceEntry("tradeOne") >= 10) { EnterLong("tradeTwo"); tradedTwo = true; } if (tradedTwo == true && BarsSinceEntry("tradeTwo") >= 10) { EnterLong("tradeThree"); tradedThree = true; } } if (some exit conditions) { // close your trades. } if (Position.MarketPosition == MarketPosition.Flat) { if (tradedOne == true) tradedOne = false; if (tradedTwo == true) tradedTwo = false; if (tradedThree == true) tradedThree = false; }
Josh P.NinjaTrader Customer Service
Comment
-
Originally posted by mainstream View PostSorry to dig up this old thread...
If I wanted the strategy to trade three times and have the second and third instances trade only after the first had traded, what would the code look like?
hastradedlong1
hastradedlong2
hastradedlong3
Part two of the question is what is a way to restrict the second Instance from trading until at least 10 minutes after the first?
Code:if (intNumTradesLong < intDailyLimitLong) { // trade entry logic here EnterLong(); intNumTradesLong++; }
Last edited by koganam; 02-18-2011, 03:55 PM.
Comment
-
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by foxtrott21, Today, 08:27 AM
|
0 responses
1 view
0 likes
|
Last Post
by foxtrott21
Today, 08:27 AM
|
||
Started by jaypelle3, Today, 08:26 AM
|
0 responses
1 view
0 likes
|
Last Post
by jaypelle3
Today, 08:26 AM
|
||
Started by linkcou, Today, 07:01 AM
|
2 responses
18 views
0 likes
|
Last Post
by bltdavid
Today, 08:25 AM
|
||
Started by jaypelle3, Today, 08:25 AM
|
0 responses
1 view
0 likes
|
Last Post
by jaypelle3
Today, 08:25 AM
|
||
Started by jaypelle3, Today, 08:22 AM
|
0 responses
2 views
0 likes
|
Last Post
by jaypelle3
Today, 08:22 AM
|
Comment