Pending Order on ADX and MACD

by sifneosfx in category Trend at 01/11/2019
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

 

Download
397 downloads
How to install
Warning! Executing the following cBot may result in loss of funds. Use it at your own risk.
Notification Publishing copyrighted material is strictly prohibited. If you believe there is copyrighted material in this section you may use the Copyright Infringement Notification form to submit a claim.
Formula / Source Code
Language: C#
Trading Platform: cAlgo
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);

        }
    }
}
Comments

BullittTraders - November 02, 2019 @ 10:27

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

TraDerMaTriX - November 02, 2019 @ 12:07

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

sifneosfx - November 02, 2019 @ 17:33

Your welcome! I am on FXpro.

sifneosfx - November 02, 2019 @ 17:35

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

sifneosfx - November 02, 2019 @ 17:37

ADX shows trend strength, no matter of the direction.

5