Category Trend  Published on 01/11/2019

Pending Order on ADX and MACD

Description

This is an enhanced pending order cBot. Instead of defining a price to enter the market, you define a price range where a trade may open. Order is opened when the current ask price is in the range defined and volatility and trend strength are picking up.

You may set “Open trade on Start” to yes and the order will open immediately after you start the cBot.

How it works

The “Target TP” price used as take profit and the “Pivot SL” price used as the stop loss.

Trade will open on the criteria bellow:

  • If ask price is smaller than target price, buy order is opened. If ask price is bigger than target price, sell order is opened.
  • Ask price must be between target and pivot price.
  • “Distance from Target/Pivot” in pips defines the minimum distance between ask price/pivot price and ask price/target price.
  • ADX value must be greater than 20.
  • If MACD signal line cross above MACD line open sell order. If MACD signal line cross bellow MACD line, open buy order.
  • Volume is calculated depending on the Pivot/SL price you’ve defined and the amount in your account’s currency you want to ris

http://sifneosFX.com

 


using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class PendingOrderonADXandMACD : Robot
    {

        [Parameter("Target TP", DefaultValue = 0)]
        public double p_target_tp { get; set; }

        [Parameter("Pivot SL", DefaultValue = 0)]
        public double p_pivot_sl { get; set; }

        [Parameter("Distance from Target/Pivot", DefaultValue = 20)]
        public double p_distance { get; set; }

        [Parameter("Amount to risk", DefaultValue = 100)]
        public double p_amount { get; set; }

        [Parameter("Open Trade on Start", DefaultValue = false)]
        public bool p_open_trade { get; set; }

        public DirectionalMovementSystem i_adx;
        public MacdCrossOver i_macd;

        public int tp = 0;
        public int sl = 0;

        protected override void OnStart()
        {

            i_adx = Indicators.DirectionalMovementSystem(14);
            i_macd = Indicators.MacdCrossOver(26, 12, 14);
            Chart.DrawHorizontalLine("Target", p_target_tp, Color.Green, 2, LineStyle.Solid);
            Chart.DrawHorizontalLine("Pivot", p_pivot_sl, Color.Red, 2, LineStyle.Solid);

            if (p_open_trade && Positions.FindAll("target", SymbolName).Length == 0)
            {
                //Sell
                if (p_target_tp < Symbol.Ask)
                {
                    tp = (int)Math.Round((Symbol.Ask - p_target_tp) / Symbol.PipSize) * 1;
                    sl = (int)Math.Round((p_pivot_sl - Symbol.Ask) / Symbol.PipSize) * 1;
                    Print("tp: " + tp);
                    ExecuteMarketOrder(TradeType.Sell, SymbolName, getVolume(TradeType.Sell), "target", sl, tp);
                }

                //Buy
                if (p_target_tp > Symbol.Ask)
                {
                    tp = (int)Math.Round((p_target_tp - Symbol.Ask) / Symbol.PipSize);
                    sl = (int)Math.Round((Symbol.Ask - p_pivot_sl) / Symbol.PipSize);
                    ExecuteMarketOrder(TradeType.Buy, SymbolName, getVolume(TradeType.Buy), "target", sl, tp);
                }
                Stop();
            }
        }

        protected override void OnTick()
        {
            if (Positions.FindAll("target", SymbolName).Length > 0)
                return;

            //Sell
            tp = (int)Math.Round((Symbol.Ask - p_target_tp) / Symbol.PipSize) * 1;
            sl = (int)Math.Round((p_pivot_sl - Symbol.Ask) / Symbol.PipSize) * 1;
            if (i_macd.Signal.HasCrossedAbove(i_macd.MACD, 30))
                if ((Symbol.Ask - p_target_tp) / Symbol.PipSize > p_distance)
                    if ((p_pivot_sl - Symbol.Ask) / Symbol.PipSize > p_distance)
                        if (i_adx.ADX.LastValue > 20)
                            if (MarketSeries.Close.IsFalling())
                                if (Symbol.Ask < p_pivot_sl)
                                {
                                    ExecuteMarketOrder(TradeType.Sell, SymbolName, getVolume(TradeType.Sell), "target", sl, tp);
                                    if (!IsBacktesting)
                                        Stop();
                                }

            //Buy
            tp = (int)Math.Round((p_target_tp - Symbol.Ask) / Symbol.PipSize);
            sl = (int)Math.Round((Symbol.Ask - p_pivot_sl) / Symbol.PipSize);
            if (i_macd.Signal.HasCrossedBelow(i_macd.MACD, 30))
                if ((p_target_tp - Symbol.Ask) / Symbol.PipSize > p_distance)
                    if ((Symbol.Ask - p_pivot_sl) / Symbol.PipSize > p_distance)
                        if (i_adx.ADX.LastValue > 20)
                            if (MarketSeries.Close.IsRising())
                                if (Symbol.Ask > p_pivot_sl)
                                {
                                    ExecuteMarketOrder(TradeType.Buy, SymbolName, getVolume(TradeType.Buy), "target", sl, tp);
                                    if (!IsBacktesting)
                                        Stop();
                                }
        }

        double getVolume(TradeType trade_type)
        {
            double sl_pips = 0;
            if (trade_type == TradeType.Sell)
                sl_pips = Math.Round((p_pivot_sl - Symbol.Ask) / Symbol.PipSize);
            else
                sl_pips = Math.Round((Symbol.Ask - p_pivot_sl) / Symbol.PipSize);

            double volume = p_amount / (sl_pips * Symbol.PipSize);
            double normalizedVolume = Symbol.NormalizeVolumeInUnits(volume);

            return Math.Abs(normalizedVolume);

        }
    }
}


SI
sifneosfx

Joined on 22.10.2018

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: Pending Order on ADX and MACD.algo
  • Rating: 3.33
  • Installs: 3681
  • Modified: 13/10/2021 09:54
Comments
Log in to add a comment.
MA
marsoft · 4 years ago

I am looking for a robot for DPO.

https://ctrader.com/algos/indicators/show/2006

 

SI
sifneosfx · 5 years ago

You may post jobs in the Freelance section.

TG
tgjobscv · 5 years ago

Thanks.

You can make pending grid piramyt.

5-10 pending order 10-15 pip after cross price ?

 [Parameter("Max Positions", DefaultValue = 5, MinValue = 1, Step = 15)]
        public int MaxPos { get; set; }

SI
sifneosfx · 5 years ago

ADX shows trend strength, no matter of the direction.

SI
sifneosfx · 5 years ago

You want ADX to be greater than 20, so you know there is a trend .

SI
sifneosfx · 5 years ago

Your welcome! I am on FXpro.

TR
tradermatrix · 5 years ago

Hello
Thank you for sharing your work.
I think line 76 for ADX Sell: if (i_adx.ADX.LastValue> 20) is: (i_adx.ADX.LastValue <20)
Good trade !!
cordially

IA
IandelMar · 5 years ago

Hi Patrick, thank you, Which Broker do you use? Thanks