Skip to content

DirectionalMovementSystem

interface cAlgo.API.Indicators

DesktopCLICloud

The Welles Wilder's Directional Movement Indicator calculation.

Definition

public interface DirectionalMovementSystem

Namespace: cAlgo.API.Indicators · Group: Indicators · Implements: StandardIndicator

Remarks

Welles Wilder's Directional Movement System uses three indicators to determine whether the market is trending, and in which direction, and sends trading signals accordingly. A buy signal occurs when +DI line crosses above -DI line. A sell signal occurs when -DI line crosses below +DI line.

Examples

Example 1

//...

public partial class SampleADX : Indicator
{
    private DirectionalMovementSystem _dms;
    private double _dIplus;
    private double _dIminus;

    protected override void Initialize()
    {
        _dms = Indicators.DirectionalMovementSystem(Period);
    }

    public override void Calculate(int index)
    {
        _dIplus = _dms.DIPlus[index];
        _dIminus = _dms.DIMinus[index];

        if (_dIminus > _dIplus)
        {
            Sell[index] = Bars.ClosePrices[index] + Symbol.TickSize*100;
        }
        else
        {
            Buy[index] = Bars.ClosePrices[index] - Symbol.TickSize*100;
        }
    }
}

//...
import clr
clr.AddReference("cAlgo.API")

from cAlgo.API import *

class Test():
    def initialize(self):
        self.dms = api.Indicators.DirectionalMovementSystem(api.Period)

    def calculate(self, index):
        if self.dms.DIMinus[index] > self.dms.DIPlus[index]:
            api.Sell[index] = api.Bars.ClosePrices[index] + api.Symbol.TickSize * 100
        else:
            api.Buy[index] = api.Bars.ClosePrices[index] - api.Symbol.TickSize * 100
{
  "IsOverlay": true,
  "Parameters": [
    {
      "Name": "Period",
      "DisplayName": "ADX Period",
      "Type": "Integer",
      "DefaultValue": 14
    }
  ],
  "LineOutputs": [
    {
      "Name": "Buy",
      "PlotType": "Points",
      "Color": "Green",
      "Thickness": 4
    },
    {
      "Name": "Sell",
      "PlotType": "Points",
      "Color": "Red",
      "Thickness": 4
    }
  ]
}

Example 2

using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo.Robots
{
    // This sample cBot shows how to use the Directional Movement System indicator
    public partial class DirectionalMovementSystemSample : Robot
    {
        private double _volumeInUnits;

        private DirectionalMovementSystem _directionalMovementSystem;

        public Position[] BotPositions
        {
            get
            {
                return Positions.FindAll(Label);
            }
        }

        protected override void OnStart()
        {
            _volumeInUnits = Symbol.QuantityToVolumeInUnits(VolumeInLots);

            _directionalMovementSystem = Indicators.DirectionalMovementSystem(20);
        }

        protected override void OnBar()
        {
            if (_directionalMovementSystem.ADX.Last(1) < 25) return;

            if (_directionalMovementSystem.DIPlus.Last(1) > _directionalMovementSystem.DIMinus.Last(1) && _directionalMovementSystem.DIPlus.Last(2) <= _directionalMovementSystem.DIMinus.Last(2))
            {
                ClosePositions(TradeType.Sell);

                ExecuteMarketOrder(TradeType.Buy, SymbolName, _volumeInUnits, Label, StopLossInPips, TakeProfitInPips);
            }
            else if (_directionalMovementSystem.DIPlus.Last(1) < _directionalMovementSystem.DIMinus.Last(1) && _directionalMovementSystem.DIPlus.Last(2) >= _directionalMovementSystem.DIMinus.Last(2))
            {
                ClosePositions(TradeType.Buy);

                ExecuteMarketOrder(TradeType.Sell, SymbolName, _volumeInUnits, Label, StopLossInPips, TakeProfitInPips);
            }
        }

        private void ClosePositions(TradeType tradeType)
        {
            foreach (var position in BotPositions)
            {
                if (position.TradeType != tradeType) continue;

                ClosePosition(position);
            }
        }
    }
}
import clr

clr.AddReference("cAlgo.API")

# Import cAlgo API types
from cAlgo.API import *

# Import trading wrapper functions
from robot_wrapper import *

class DirectionalMovementSystemSample():
    def on_start(self):
        self.volumeInUnits = api.Symbol.QuantityToVolumeInUnits(api.VolumeInLots)
        self.directionalMovementSystem = api.Indicators.DirectionalMovementSystem(20)

    def on_bar_closed(self):
        if self.directionalMovementSystem.ADX.Last(0) < 25:
            return

        if self.directionalMovementSystem.DIPlus.Last(0) > self.directionalMovementSystem.DIMinus.Last(0) and self.directionalMovementSystem.DIPlus.Last(1) <= self.directionalMovementSystem.DIMinus.Last(1):
            self.close_positions(TradeType.Sell)
            api.ExecuteMarketOrder(TradeType.Buy, api.SymbolName, self.volumeInUnits, api.Label, api.StopLossInPips, api.TakeProfitInPips)
        elif self.directionalMovementSystem.DIPlus.Last(0) < self.directionalMovementSystem.DIMinus.Last(0) and self.directionalMovementSystem.DIPlus.Last(1) >= self.directionalMovementSystem.DIMinus.Last(1):
            self.close_positions(TradeType.Buy)
            api.ExecuteMarketOrder(TradeType.Sell, api.SymbolName, self.volumeInUnits, api.Label, api.StopLossInPips, api.TakeProfitInPips)

    def get_bot_positions(self):
        return api.Positions.FindAll(api.Label)

    def close_positions(self, tradeType):
        for position in self.get_bot_positions():
            if position.TradeType != tradeType:
                continue
            api.ClosePosition(position)
{
  "AccessRights": "None",
  "TimeZone": "UTC",
  "Parameters": [
    {
      "Name": "VolumeInLots",
      "DisplayName": "Volume (Lots)",
      "Type": "Double",
      "DefaultValue": 0.01
    },
    {
      "Name": "StopLossInPips",
      "DisplayName": "Stop Loss (Pips)",
      "Type": "Double",
      "DefaultValue": 10
    },
    {
      "Name": "TakeProfitInPips",
      "DisplayName": "Take Profit (Pips)",
      "Type": "Double",
      "DefaultValue": 10
    },
    {
      "Name": "Label",
      "Type": "String",
      "DefaultValue": "Sample"
    }
  ]
}

Properties

Name Description
ADXDesktopCLICloudIndicatorDataSeries The Average Directional Movement Index (ADX) indicates whether the market is trending or ranging.
DIMinusDesktopCLICloudIndicatorDataSeries The Negative Direction Indicator (-DI) indicates downward trend movement.
DIPlusDesktopCLICloudIndicatorDataSeries The Positive Direction Indicator (+DI) indicates upward trend movement.

Inherited members

From StandardIndicator: AddToChart, Symbol, TimeFrame, Bars