Welcome to NexusFi: the best trading community on the planet, with over 150,000 members Sign Up Now for Free
Genuine reviews from real traders, not fake reviews from stealth vendors
Quality education from leading professional traders
We are a friendly, helpful, and positive community
We do not tolerate rude behavior, trolling, or vendors advertising in posts
We are here to help, just let us know what you need
You'll need to register in order to view the content of the threads and start contributing to our community. It's free for basic access, or support us by becoming an Elite Member -- see if you qualify for a discount below.
-- Big Mike, Site Administrator
(If you already have an account, login at the top of the page)
I'm currently developing an automated 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 at this point...
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 trade because the signal is still there. So sometimes I have 2 trades within one bar. That's OK, but like i said before, 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) is not true anymore, it should reset the trade counter immediately, so in case an other signal comes a (few) bar(s) later , 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:
#region Using declarations
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Data;
using NinjaTrader.Indicator;
using NinjaTrader.Gui.Chart;
using NinjaTrader.Strategy;
#endregion
// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
///<summary>
/// Enter the description of your strategy here
///</summary>
[Description("")]
publicclass BMEXTTSv3SOXEasy : Strategy
{
...
...
...
privateint tradeCounterLong= 0; // This variable represents the number of trades taken.
privateint tradeCounterShort= 0; // This variable represents the number of trades taken.
privateint maxTradesLong = 3; // This variable sets the maximum number of trades to take.
privateint maxTradesShort = 3; // This variable sets the maximum number of trades to take.
privatebool EntryConditionLong;
privatebool EntryConditionShort;
///<summary>
/// This method is used to configure the strategy and is called once before any strategy method is called.
///</summary>
protectedoverridevoid Initialize()
{
CalculateOnBarClose = false;
EntryHandling = EntryHandling.UniqueEntries;
SetTrailStop(CalculationMode.Ticks, trailstop);
}
// Reset the tradeCounter value of each session.
//...for long
if (EntryConditionLong == false)
{
tradeCounterLong = 0;
}
//...and for short
if (EntryConditionShort == false)
{
tradeCounterShort = 0;
}
// Entry Condition set 1 (long)
if (EntryConditionLong && (tradeCounterLong < maxTradesLong))
{
GoLong();
}
// Entry Condition set 2 (short)
if (EntryConditionShort &&(tradeCounterShort < maxTradesShort))
{
GoShort();
}
}
#region Properties
[Description("Maximum number of trades to take per Direction")]
[Category("Max Trades per Direction")]
publicint MaxTradesShort
{
get { return maxTradesShort; }
set { maxTradesShort = Math.Max(1, value); }
}
[Description("Maximum number of trades to take per Direction")]
[Category("Max Trades per Direction")]
publicint MaxTradesLong
{
get { return maxTradesLong; }
set { maxTradesLong = Math.Max(1, value); }
}
...
...
...
...
#endregion
}
}
Could somebody please tell me what i did wrong? Thank you all for your help in advance!
Dave
Can you help answer these questions from other members on NexusFi?
If you only want one entry per bar, just keep track of BarsSinceEntry or set your own flag. If you use BarsSinceEntry you need to check for -1 which means 'null' or not yet set, and then any other value is the number of bars since the last entry. It would need to be > 0 to not be an intrabar multiple entry.
Setting a flag would be something like
if (MarketPosition[0] != 0) entrybar = CurrentBar;
then test that entrybar != CurrentBar in your entry code.
I have written C# in a couple years so above code needs some tweaking.
I posted this in the NT forum as well. The attached strategy is a example where it will limit 3 consecutive short trades in row.
The trick is to have a trigger that will update when the trade is executed. Once it gets to a certain number of trades it will not execute until the trigger is reset.
In the below code the trades will be triggered everytime CCI20 crosses above zero. Each time it does the longtrade variable will be increased by 1. The code will only allow 3 long trades until the condition is reset. The condition is reset when SMA(20) crosses below SMA(89).
Let me know if this makes sense. I have attached proof of concept strategy. When run it will limit the strategy to 3 trades.