By using the information contained in the pages below, you understand and agree with the following terms:

  1. I AGREE that SharkIndicators does not warrant the functionality contained in the provided software code will meet your requirements or the operation of the software in conjunction with BloodHound will be uninterrupted or error-free.
  2. I AGREE that using any of the information, including software code samples, are at my own risk. I shall defend, indemnify and hold SharkIndicators, its employees and associates harmless from any and all claims, damages, or losses resulting from its use.
  3. I AGREE that SharkIndicators is not under any obligation to provide support for the operation of any code derived from these examples and that I assume all associated risks and costs.
  4. I AGREE that in no event will SharkIndicators or its distributors be liable to you or others for any damages, including any lost profit, savings, lost patience or other incidental, or consequential damage.

-- I AGREE --

BloodHound Simple Strategy Example 2


Please note, SharkIndicators does not provide any NinjaScript, programming, or coding support.
This information is provided as-is and for knowledgeable programmers.

Overview


Note:
You must have ‘Developer Extensions’ checked when you ran the SharkIndicators Installer.  That will also install the example code file.
This example illustrates how to use multiple BloodHound logic templates within your strategy. This can be useful if you find the need to utilize BloodHound’s logic capabilities for different purposes.

In this example, we use a logic template for the entries, and another logic template for the exits. We allow the user to select which template to use for each case.

Example Code


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#region Using declarations
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using NinjaTrader.Cbi;
using SharkIndicators.BloodHound;
using SharkIndicators.PlatformServices;
#endregion

namespace NinjaTrader.NinjaScript.Strategies
{
    /// <summary>
    /// This example shows how to get the signal values from BloodHound on 
    /// specific logic templates, chosen by the user, enabling you full control
    /// over both the entry and exit logic using BloodHound. There are at 
    /// minimum two logic templates that you need to specify, one for Entry 
    /// (Entry Logic) and one for Exits (Exit Logic). 
    /// The active logic template becomes irrelevant.
    /// </summary>
    public class SiBloodHoundStrategyExample2 : SiBloodHoundStrategy
    {
        private int _iExitLogicTemplate = -1; // Logic Template of -1 means 
        private int _iEntryLogicTemplate = -1;// nothing has been selected 
        // and will default to the 
        // active logic template
        private double _StopLoss;
        private double _ProfitTarget;
        private CalculationMode _MeasurementUnit;

		
        [TypeConverter(typeof(BloodHoundLogicDropDownSelectorTypeConverter2))]
        [Display(Name = "Exit Logic", GroupName = "BloodHound", Order = 3)]
        public BloodHoundLogicSelectorItem EntryLogicTemplate // these are strings so that NT's optimizer doesn't mess them up
        {
            get
            {
                string name = String.Empty;
                if (BloodHoundTemplate != null && BloodHoundTemplate.HasLogicTemplate(_iEntryLogicTemplate))
                {
                    name = BloodHoundTemplate.GetLogicTemplate(_iEntryLogicTemplate).Name;
                }
                return new BloodHoundLogicSelectorItem() { ID = _iEntryLogicTemplate, Name = name };


            }
            set
            {
                _iEntryLogicTemplate = value.ID;
            }
        }

        [TypeConverter(typeof(BloodHoundLogicDropDownSelectorTypeConverter2))]
        [Display(Name = "Exit Logic", GroupName = "BloodHound", Order = 3)]
        public BloodHoundLogicSelectorItem ExitLogicTemplate // these are strings so that NT's optimizer doesn't mess them up
        {
            get
            {
                string name = String.Empty;
                if (BloodHoundTemplate != null && BloodHoundTemplate.HasLogicTemplate(_iExitLogicTemplate))
                {
                    name = BloodHoundTemplate.GetLogicTemplate(_iExitLogicTemplate).Name;
                }
                return new BloodHoundLogicSelectorItem() { ID = _iExitLogicTemplate, Name = name };
            }
            set
            {
                _iExitLogicTemplate = value.ID;
            }
        }

        [Display(Name = "Measurement Unit", GroupName = "Trade Management", Order = 0, Description = "The unit of measurement for the Profit Target and Stop Loss")]
        public CalculationMode MeasurementUnit
        {
            get { return _MeasurementUnit; }
            set { _MeasurementUnit = value; }
        }

        
        [Display(Name = "Profit Target", GroupName = "Trade Management", Order = 2, Description = "The profit target measured in the units specified by Measurement Unit")]
        public double ProfitTarget
        {
            get { return _ProfitTarget; }
            set { _ProfitTarget = value; }
        }

        [Display(Name = "Stop Loss", GroupName = "Trade Management", Order = 3, Description = "The stop loss measured in the units specified by Measurement Unit")]
        public double StopLoss
        {
            get { return _StopLoss; }
            set { _StopLoss = value; }
        }
		
		
        protected override void OnStateChange()
        {
            base.OnStateChange(); // this is necessary to initialize the 
                                  // BloodHound Template

            if (State == State.Configure)
            {
                // Trade Management
                SetProfitTarget(MeasurementUnit, ProfitTarget);
                SetStopLoss(MeasurementUnit, StopLoss);
            }
            if (State == State.DataLoaded)
            {
		// Add BloodHound plots to chart
		if (BloodHoundTemplate != null && !BloodHoundTemplate.IsEmpty)
		{
		    AddChartIndicator(BloodHound);
		}
            }
        }

        protected override void OnBarUpdate()
        {
            // Do not call this because the base class' default 
            // implementation is to handle entry signals exactly 
            // like below - but we are overriding it to illustrate
            // how the code works.
            // base.OnBarUpdate();

            /* 
             * ENTRY LOGIC
             */

            // need to mark the entries to avoid overfilling with an exit
            bool bPositionPlaced = false;

            // Ensure that we aren't long already
            if (Position.MarketPosition != MarketPosition.Long)
            {
                // see if BloodHound has produced a long signal, using the 
                // logic template specified by EntryLogicTemplate
                if (BloodHound.GetLogicSignalValue(EMarketDirection.Long, _iEntryLogicTemplate, 0))
                {
                    EnterLong();
                    bPositionPlaced = true;
                }
            }
            // otherwise check to see if we aren't short already
            if (Position.MarketPosition != MarketPosition.Short)
            {
                // see if BloodHound has produced a short signal, using the 
                // logic template specified by EntryLogicTemplate
                if (BloodHound.GetLogicSignalValue(EMarketDirection.Short, _iEntryLogicTemplate, 0))
                {
                    EnterShort();
                    bPositionPlaced = true;
                }
            }

            /* 
             * EXIT LOGIC 
             */
            if (!bPositionPlaced) // need to overvoid overfills
            {

                // check if we have a position on, and if it's long
                if (Position.MarketPosition == MarketPosition.Long)
                {
                    // see if there was a signal on the short side for BloodHound,
                    // using the logic template specified by ExitLogicTemplate
                    if (BloodHound.GetLogicSignalValue(EMarketDirection.Short, _iExitLogicTemplate, 0))
                    {
                        // if so, exit the position
                        ExitLong();
                    }
                }

                // check if we have a position on, and if it's short
                if (Position.MarketPosition == MarketPosition.Short)
                {
                    // see if there was a signal on the long side for BloodHound,
                    // using the logic template specified by ExitLogicTemplate
                    if (BloodHound.GetLogicSignalValue(EMarketDirection.Long, _iExitLogicTemplate, 0))
                    {
                        // if so, exit the position
                        ExitShort();
                    }
                }
            }
        }
    }
}


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#region Using declarations
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using NinjaTrader.Cbi;
using SharkIndicators.BloodHound;
using SharkIndicators.PlatformServices;
#endregion

namespace NinjaTrader.NinjaScript.Strategies
{
    /// <summary>
    /// This example shows how to get the signal values from BloodHound on 
    /// specific logic templates, chosen by the user, enabling you full control
    /// over both the entry and exit logic using BloodHound. There are at 
    /// minimum two logic templates that you need to specify, one for Entry 
    /// (Entry Logic) and one for Exits (Exit Logic). 
    /// The active logic template becomes irrelevant.
    /// </summary>
    public class SiBloodHoundStrategyExample2 : SiBloodHoundStrategy
    {
        private int _iExitLogicTemplate = -1; // Logic Template of -1 means 
        private int _iEntryLogicTemplate = -1;// nothing has been selected 
        // and will default to the 
        // active logic template
        private double _StopLoss;
        private double _ProfitTarget;
        private CalculationMode _MeasurementUnit;

		
        [TypeConverter(typeof(BloodHoundLogicDropDownSelectorTypeConverter2))]
        [Display(Name = "Exit Logic", GroupName = "BloodHound", Order = 3)]
        public BloodHoundLogicSelectorItem EntryLogicTemplate // these are strings so that NT's optimizer doesn't mess them up
        {
            get
            {
                string name = String.Empty;
                if (BloodHoundTemplate != null && BloodHoundTemplate.HasLogicTemplate(_iEntryLogicTemplate))
                {
                    name = BloodHoundTemplate.GetLogicTemplate(_iEntryLogicTemplate).Name;
                }
                return new BloodHoundLogicSelectorItem() { ID = _iEntryLogicTemplate, Name = name };


            }
            set
            {
                _iEntryLogicTemplate = value.ID;
            }
        }

        [TypeConverter(typeof(BloodHoundLogicDropDownSelectorTypeConverter2))]
        [Display(Name = "Exit Logic", GroupName = "BloodHound", Order = 3)]
        public BloodHoundLogicSelectorItem ExitLogicTemplate // these are strings so that NT's optimizer doesn't mess them up
        {
            get
            {
                string name = String.Empty;
                if (BloodHoundTemplate != null && BloodHoundTemplate.HasLogicTemplate(_iExitLogicTemplate))
                {
                    name = BloodHoundTemplate.GetLogicTemplate(_iExitLogicTemplate).Name;
                }
                return new BloodHoundLogicSelectorItem() { ID = _iExitLogicTemplate, Name = name };
            }
            set
            {
                _iExitLogicTemplate = value.ID;
            }
        }

        [Display(Name = "Measurement Unit", GroupName = "Trade Management", Order = 0, Description = "The unit of measurement for the Profit Target and Stop Loss")]
        public CalculationMode MeasurementUnit
        {
            get { return _MeasurementUnit; }
            set { _MeasurementUnit = value; }
        }

        
        [Display(Name = "Profit Target", GroupName = "Trade Management", Order = 2, Description = "The profit target measured in the units specified by Measurement Unit")]
        public double ProfitTarget
        {
            get { return _ProfitTarget; }
            set { _ProfitTarget = value; }
        }

        [Display(Name = "Stop Loss", GroupName = "Trade Management", Order = 3, Description = "The stop loss measured in the units specified by Measurement Unit")]
        public double StopLoss
        {
            get { return _StopLoss; }
            set { _StopLoss = value; }
        }
		
		
        protected override void OnStateChange()
        {
            base.OnStateChange(); // this is necessary to initialize the 
                                  // BloodHound Template

            if (State == State.Configure)
            {
                // Trade Management
                SetProfitTarget(MeasurementUnit, ProfitTarget);
                SetStopLoss(MeasurementUnit, StopLoss);
            }
            if (State == State.DataLoaded)
            {
		// Add BloodHound plots to chart
		if (BloodHoundTemplate != null && !BloodHoundTemplate.IsEmpty)
		{
		    AddChartIndicator(BloodHound);
		}
            }
        }

        protected override void OnBarUpdate()
        {
            // Do not call this because the base class' default 
            // implementation is to handle entry signals exactly 
            // like below - but we are overriding it to illustrate
            // how the code works.
            // base.OnBarUpdate();

            /* 
             * ENTRY LOGIC
             */

            // need to mark the entries to avoid overfilling with an exit
            bool bPositionPlaced = false;

            // Ensure that we aren't long already
            if (Position.MarketPosition != MarketPosition.Long)
            {
                // see if BloodHound has produced a long signal, using the 
                // logic template specified by EntryLogicTemplate
                if (BloodHound.GetLogicSignalValue(EMarketDirection.Long, _iEntryLogicTemplate, 0))
                {
                    EnterLong();
                    bPositionPlaced = true;
                }
            }
            // otherwise check to see if we aren't short already
            if (Position.MarketPosition != MarketPosition.Short)
            {
                // see if BloodHound has produced a short signal, using the 
                // logic template specified by EntryLogicTemplate
                if (BloodHound.GetLogicSignalValue(EMarketDirection.Short, _iEntryLogicTemplate, 0))
                {
                    EnterShort();
                    bPositionPlaced = true;
                }
            }

            /* 
             * EXIT LOGIC 
             */
            if (!bPositionPlaced) // need to overvoid overfills
            {

                // check if we have a position on, and if it's long
                if (Position.MarketPosition == MarketPosition.Long)
                {
                    // see if there was a signal on the short side for BloodHound,
                    // using the logic template specified by ExitLogicTemplate
                    if (BloodHound.GetLogicSignalValue(EMarketDirection.Short, _iExitLogicTemplate, 0))
                    {
                        // if so, exit the position
                        ExitLong();
                    }
                }

                // check if we have a position on, and if it's short
                if (Position.MarketPosition == MarketPosition.Short)
                {
                    // see if there was a signal on the long side for BloodHound,
                    // using the logic template specified by ExitLogicTemplate
                    if (BloodHound.GetLogicSignalValue(EMarketDirection.Long, _iExitLogicTemplate, 0))
                    {
                        // if so, exit the position
                        ExitShort();
                    }
                }
            }
        }
    }
}

Discussion


Firstl, be sure to derive your class from the 'SiBloodHoundStrategy' class as in line 22.  In this example, notice that 2 public properties have been declared for the user to select logic templates: EntryLogicTemplate, and ExitLogicTemplate. They are declared as integers (int) and use the BloodHoundLogicDropDownSelectorTypeConverter2 as seen on lines 31 and 52.

Note:
BloodHoundLogicDropDownSelectorTypeConverter2 counterpart BloodHoundLogicDropDownSelectorTypeConverter is designed for WinForms classic property grid, whereas BloodHoundLogicDropDownSelectorTypeConverter2 is designed for the WPFPropertyGrid

Finally the entry and exit logic values are retrieved from BloodHound using the GetLogicSignalValue() method, using the logic templates identified by EntryLogicTemplate and ExitLogicTemplate as seen on lines 131, 142, 160 and 172.


Downloads


NinjaTrader 8

The SharkIndicaotor's Installer will install this file to the ...\NinjaTrader 8\bin\Custom\Strategies\  folder when the Developer Extensions are selected.

NinjaTrader 7

The last version to support Developer Extension is Ver 1.233.6603.  It may be downloaded from the Changelog page.  If you have a newer version installed, it must be uninstalled first and then restart the computer.

Download the import file below and import it into NinjaTrader 7 from the Control Center.  Select the File menu » Utilities » Import NinjaScript.
Download import file for NT 7 (.zip)