Simple Moving Average Add-on [B4 Copilot Pro]

barbaros

Administrator
Staff member
Here is a simple SMA plugin for B4 CoPilot Pro. It is hard coded to be SMA 50. Change it as you see fit.

Code:
#region Using declarations
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Gui;
using NinjaTrader.Gui.Chart;
using NinjaTrader.Gui.SuperDom;
using NinjaTrader.Gui.Tools;
using NinjaTrader.Data;
using NinjaTrader.NinjaScript;
using NinjaTrader.Core.FloatingPoint;
using NinjaTrader.NinjaScript.DrawingTools;
#endregion

namespace NinjaTrader.NinjaScript.Indicators.B4Signals.Development.SMAPlugin
{
    public class SMAPlugin : Indicator
    {
        
        #region B4 CoPilot Pro Integration Variables
        [NinjaScriptProperty]
        [Display(Name="B4 Entry Signals Enabled", Order=1, GroupName="B4 CoPilot Pro Integration")]
        public bool B4UseEntrySignals
        { get; set; }
        
        [NinjaScriptProperty]
        [Display(Name="B4 Confirmation Signals Enabled", Order=2, GroupName="B4 CoPilot Pro Integration")]
        public bool B4UseConfirmations
        { get; set; }
        
        private Series<bool> B4BuySignal;
        private Series<bool> B4SellSignal;
        private Series<int> B4ConfirmationDirection;
        
        [NinjaScriptProperty]
        [Browsable(false)]
        [XmlIgnore]
        public Series<bool> B4BuySignals
        {
            get { return B4BuySignal; }
            set {}
        }
        
        [NinjaScriptProperty]
        [Browsable(false)]
        [XmlIgnore]
        public Series<bool> B4SellSignals
        {
            get { return B4SellSignal; }
            set {}
        }
        
        [NinjaScriptProperty]
        [Browsable(false)]
        [XmlIgnore]
        public Series<int>    B4ConfirmationDirections
        {
            get { return B4ConfirmationDirection; }
            set {}
        }
        #endregion
        
        protected override void OnStateChange()
        {
            if (State == State.SetDefaults)
            {
                Description                                    = @"B4 CoPilot Pro Plugin - SMA";
                Name                                        = "B4 CoPilot Pro Plugin - SMA";
                Calculate                                    = Calculate.OnBarClose;
                IsOverlay                                    = true;
                DisplayInDataBox                            = true;
                DrawOnPricePanel                            = true;
                DrawHorizontalGridLines                        = true;
                DrawVerticalGridLines                        = true;
                PaintPriceMarkers                            = true;
                ScaleJustification                            = NinjaTrader.Gui.Chart.ScaleJustification.Right;
                //Disable this property if your indicator requires custom values that cumulate with each new market data event.
                //See Help Guide for additional information.
                IsSuspendedWhileInactive                    = true;
                
                B4UseEntrySignals  = true;    // By default, allow trade signals to be processed by B4 CoPilot Pro
                B4UseConfirmations = true;    // By default, allow confirmation direction to be processed by B4 CoPilot Pro
            }
            else if (State == State.Configure)
            {
                #region B4 CoPilot Pro Integration Initalization
                B4BuySignal = new Series<bool>(this);
                B4SellSignal = new Series<bool>(this);
                B4ConfirmationDirection = new Series<int>(this);
                #endregion
            }
        }

        protected override void OnBarUpdate()
        {
            // Example for SMA
            if (CurrentBar < 50) return; // Do not execute until enough bars are loaded
            SMA smaValue = SMA(50);         // Calculate SMA value
            
            // Set Buy Signals
            // assign true for triggering buy entry
            // otherwise assign false
            B4BuySignals[0] = CrossAbove(Close, smaValue, 1); // Trigger a buy signal when close
                                                              // closes above the SMA
            
            // Set Sell Signals
            // assign true for triggering sell entry
            // otherwise assign false
            B4SellSignals[0] = CrossBelow(Close, smaValue, 1); // Trigger a sell signal when close
                                                               // crosses below the SMA
            
            // Set Confirmation Direction
            // 0 : netural, no trade
            // 1 : bullish
            // -1 : bearish
            B4ConfirmationDirection[0] = Close[0] > smaValue[0] ? 1 :  // Allow longs when close above SMA
                                         Close[0] < smaValue[0] ? -1 : // Allow shorts when close below SMA
                                         0;                               // no trade
        }
        
    }
}
 
Top