Announcement
Collapse
No announcement yet.
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
-
It probably makes more sense to use a counter that you test before each trade and increment on each trade. Then when you reach the designated number of trades, you just never enter the trade entry code block.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 Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
648 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
369 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
109 views
0 likes
|
Last Post
by Mindset
02-09-2026, 11:44 AM
|
||
|
Started by Geovanny Suaza, 02-02-2026, 12:30 PM
|
0 responses
573 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
575 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|

Comment