Risk Disclosure

Futures, foreign currency and options trading contains substantial risk and is not for every investor. An investor could potentially lose all or more than the initial investment. Risk capital is money that can be lost without jeopardizing ones financial security or lifestyle. Only risk capital should be used for trading and only those with sufficient risk capital should consider trading. Past performance is not necessarily indicative of future results. View full risk disclosure.

BloodHound Simple Strategy Example 2


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 SharkIndicator, 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 of operation.
  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.

Overview


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


#region Using declarations
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Xml.Serialization;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.Drawing.Design;
using System.Windows.Forms.Design;
using NinjaTrader.Cbi;
using NinjaTrader.Data;
using NinjaTrader.Indicator;
using NinjaTrader.Strategy;
using SharkIndicators.BloodHound;
using SharkIndicators.PlatformServices;
#endregion

namespace NinjaTrader.Strategy
{
    /// <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 irrelavent.
    /// </summary>
    public class SiBloodHoundStrategyExample2 : SiBloodHoundStrategy
    {
        #region Fields

        private int _ExitLogicTemplate = -1; // Logic Template of -1 means 
        private int _EntryLogicTemplate = -1;// nothing has been selected 
        // and will default to the 
        // active logic template
        private double _StopLoss;
        private double _ProfitTarget;
        private CalculationMode _MeasurementUnit;
        #endregion
        #region Properties

        [TypeConverter(typeof(BloodHoundLogicDropDownSelectorTypeConverter))]
        [NinjaTrader.Gui.Design.DisplayName("Entry Logic")]
        [GridCategory("\rBloodHound")]
        public int EntryLogicTemplate
        {
            get { return _EntryLogicTemplate; }
            set { _EntryLogicTemplate = value; }
        }

        [TypeConverter(typeof(BloodHoundLogicDropDownSelectorTypeConverter))]
        [NinjaTrader.Gui.Design.DisplayName("Exit Logic")]
        [GridCategory("\rBloodHound")]
        public int ExitLogicTemplate
        {
            get { return _ExitLogicTemplate; }
            set { _ExitLogicTemplate = value; }
        }

        [GridCategory("\rTrade Management")]
        [Gui.Design.DisplayName("Measurement Unit")]
        [Description("The unit of measurement for the Profit Target and Stop Loss")]
        public CalculationMode MeasurementUnit
        {
            get { return _MeasurementUnit; }
            set { _MeasurementUnit = value; }
        }

        [GridCategory("\rTrade Management")]
        [Gui.Design.DisplayName("Profit Target")]
        [Description("The profit target measured in the units specified by Measurement Unit")]
        public double ProfitTarget
        {
            get { return _ProfitTarget; }
            set { _ProfitTarget = value; }
        }

        [GridCategory("\rTrade Management")]
        [Gui.Design.DisplayName("Stop Loss")]
        [Description("The stop loss measured in the units specified by Measurement Unit")]
        public double StopLoss
        {
            get { return _StopLoss; }
            set { _StopLoss = value; }
        }


        #endregion
        protected override void Initialize()
        {
            base.Initialize(); // this is necessary to initialize the 
            // BloodHound Template

            // Add your Initialize code here
            // Trade Management
            SetProfitTarget(MeasurementUnit, ProfitTarget);
            SetStopLoss(MeasurementUnit, StopLoss);


        }

        protected override void OnStartUp()
        {
            base.OnStartUp(); // this is necessary to startup the 
            // BloodHound Template

            // Add your startup code below
        }

        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, EntryLogicTemplate, 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, EntryLogicTemplate, 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, ExitLogicTemplate, 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, ExitLogicTemplate, 0))
                    {
                        // if so, exit the position
                        ExitShort();
                    }
                }


            }
        }
    }
}

Discussion


Firstly be sure to derive your class from the SiBloodHoundStrategy class as in line 30.

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 BloodHoundLogicDropDownSelectorTypeConverter as seen on lines 44 and 53.

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 132, 143, 161 and 173.

Example File


Note: You must have ‘Developer Extensions’ checked when you ran the SharkIndicators Installer before you can import these strategies.

Download the raw .cs example code using the link below:
Download example code file (.cs)

Place it the following directory:

…/My Documents/NinjaTrader 7/bin/Custom/Strategy

OR

Download the import file below and import it into NinjaTrader using File » Utilities » Import NinjaScript.
Download import file (.zip)