Equity Millipede

by cysecsbin.01 in category Trend at 30/06/2019
Description

Follow my cTrader Telegram group at https://t.me/cTraderCommunity; it's a new community but it will grow fast, plus everyone can talk about cTrader indicators and algorithm without restrictions, though it is not allowed to spam commercial indicators to sell them. There's also a Discord Server now @ https://discord.gg/5GAPMtp

This bot opens a position for each side when conditions are met, then it is up to the user to manage it.

This was a request from one of the users of the telegram group, he based his logic on the famous "Equity Millipede" thread on forex factory.

In this bot, you can choose which open prices should be use to build the opening logic, a position, either buy or sell, is then opened when the price is > open price + boundary for every TF selected in case of a long position and < open price - boudary in case of a short position.

This bot can be backtested in Visual Mode by using ctrl + click to activate/deactivate it (so that it works only when wanted) and ctrl + alt + click to close the position closes to the cursor

For any bug report or suggestion contact me by joining the telegram group linked above or by commenting below

Download
168 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 EquityMillipede : Robot
    {
        [Parameter("Month Open Enabled", DefaultValue = true)]
        public bool monthly { get; set; }
        [Parameter("Monthly Boundary", DefaultValue = 50)]
        public int monthlyBnd { get; set; }

        [Parameter("Weekly Open Enabled", DefaultValue = true)]
        public bool weekly { get; set; }
        [Parameter("Weekly Boundary", DefaultValue = 50)]
        public int weeklyBnd { get; set; }

        [Parameter("Daily Open Enabled", DefaultValue = true)]
        public bool daily { get; set; }
        [Parameter("Daily Boundary", DefaultValue = 20)]
        public int dailyBnd { get; set; }

        [Parameter("4 Hour Open Enabled", DefaultValue = true)]
        public bool hour4 { get; set; }
        [Parameter("4 Hour Boundary", DefaultValue = 10)]
        public int hour4Bnd { get; set; }

        [Parameter("Hour Open Enabled", DefaultValue = true)]
        public bool hourly { get; set; }
        [Parameter("Hour Boundary", DefaultValue = 5)]
        public int hourBnd { get; set; }

        [Parameter("m15 Open Enabled", DefaultValue = true)]
        public bool m15ly { get; set; }
        [Parameter("m15 Boundary", DefaultValue = 5)]
        public int m15Bnd { get; set; }

        [Parameter("London Open Enabled", DefaultValue = true)]
        public bool london { get; set; }
        [Parameter("London Boundary", DefaultValue = 10)]
        public int londonBnd { get; set; }

        [Parameter("X Base Units", Group = "Volume", DefaultValue = 1)]
        public int volBU { get; set; }
        [Parameter("Every Y Cash in Balance", Group = "Volume", DefaultValue = 1000)]
        public int volF { get; set; }

        [Parameter("Max Spread", DefaultValue = 10)]
        public int spread { get; set; }

        private MarketSeries M, W, D, H4, H, m15;
        private bool activated = true;

        protected override void OnStart()
        {
            if (monthly)
                M = MarketData.GetSeries(TimeFrame.Monthly);
            if (weekly)
                W = MarketData.GetSeries(TimeFrame.Weekly);
            if (daily)
                D = MarketData.GetSeries(TimeFrame.Daily);
            if (hour4)
                H4 = MarketData.GetSeries(TimeFrame.Hour4);
            if (hour4)
                H = MarketData.GetSeries(TimeFrame.Hour);
            if (m15ly)
                m15 = MarketData.GetSeries(TimeFrame.Minute15);

            if (RunningMode == RunningMode.VisualBacktesting)
                Chart.MouseDown += OnChartMouseDown;

        }

        void OnChartMouseDown(ChartMouseEventArgs obj)
        {
            if (obj.CtrlKey && !obj.AltKey && !obj.ShiftKey)
                activated = activated ? false : true;
            if (obj.AltKey && obj.CtrlKey && !obj.ShiftKey)
                closeClosestPosition(obj.YValue);
        }

        private void closeClosestPosition(double price)
        {
            if (Positions.Count == 0)
                return;
            Position pos = Positions[0];
            foreach (var _pos in Positions)
            {
                if (Math.Abs(price - _pos.EntryPrice) < Math.Abs(price - pos.EntryPrice))
                    pos = _pos;
            }
            ClosePosition(pos);
        }
        protected override void OnTick()
        {
            if (Math.Abs(Bid - Ask) > spread * Symbol.PipSize)
                return;

            bool longCheck = true;
            bool shortCheck = true;

            if (RunningMode == RunningMode.VisualBacktesting)
                Chart.DrawStaticText("act", "Bot is " + (activated ? "active" : "inactive"), VerticalAlignment.Top, HorizontalAlignment.Center, Color.White);

            if (activated)
            {
                if (Positions.Find("Millipede Long") == null)
                {
                    if (monthly)
                        longCheck = Bid > M.Open.LastValue + monthlyBnd * Symbol.PipSize ? longCheck : false;
                    if (weekly)
                        longCheck = Bid > W.Open.LastValue + weeklyBnd * Symbol.PipSize ? longCheck : false;
                    if (daily)
                        longCheck = Bid > D.Open.LastValue + dailyBnd * Symbol.PipSize ? longCheck : false;
                    if (hour4)
                        longCheck = Bid > H4.Open.LastValue + hour4Bnd * Symbol.PipSize ? longCheck : false;
                    if (hourly)
                        longCheck = Bid > H.Open.LastValue + hourBnd * Symbol.PipSize ? longCheck : false;
                    if (m15ly)
                        longCheck = Bid > m15.Open.LastValue + m15Bnd * Symbol.PipSize ? longCheck : false;
                    if (london)
                    {
                        DateTime londonOpen;
                        DateTime.TryParse(MarketSeries.OpenTime.LastValue.Day.ToString() + "/" + MarketSeries.OpenTime.LastValue.Month.ToString() + "/" + MarketSeries.OpenTime.LastValue.Year.ToString() + " " + "07:00:00", out londonOpen);
                        longCheck = Bid > MarketSeries.Open[MarketSeries.OpenTime.GetIndexByTime(londonOpen)] + londonBnd ? longCheck : false;
                    }
                    if (longCheck)
                        ExecuteMarketOrder(TradeType.Buy, Symbol.Name, setVolume(), "Millipede Long");
                }
                if (Positions.Find("Millipede Short") == null)
                {
                    if (monthly)
                        shortCheck = Bid < M.Open.LastValue - monthlyBnd * Symbol.PipSize ? shortCheck : false;
                    if (weekly)
                        shortCheck = Bid < W.Open.LastValue - weeklyBnd * Symbol.PipSize ? shortCheck : false;
                    if (daily)
                        shortCheck = Bid < D.Open.LastValue - dailyBnd * Symbol.PipSize ? shortCheck : false;
                    if (hour4)
                        shortCheck = Bid < H4.Open.LastValue - hour4Bnd * Symbol.PipSize ? shortCheck : false;
                    if (hourly)
                        shortCheck = Bid < H.Open.LastValue - hourBnd * Symbol.PipSize ? shortCheck : false;
                    if (m15ly)
                        shortCheck = Bid < m15.Open.LastValue - m15Bnd * Symbol.PipSize ? shortCheck : false;
                    if (london)
                    {
                        DateTime londonOpen;
                        DateTime.TryParse(MarketSeries.OpenTime.LastValue.Day.ToString() + "/" + MarketSeries.OpenTime.LastValue.Month.ToString() + "/" + MarketSeries.OpenTime.LastValue.Year.ToString() + " " + "07:00:00", out londonOpen);
                        longCheck = Bid > MarketSeries.Open[MarketSeries.OpenTime.GetIndexByTime(londonOpen)] + londonBnd ? longCheck : false;
                    }
                    if (shortCheck)
                        ExecuteMarketOrder(TradeType.Sell, Symbol.Name, setVolume(), "Millipede Short");
                }
            }
        }

        private double setVolume()
        {
            return Symbol.NormalizeVolumeInUnits((volBU * Symbol.VolumeInUnitsMin) * (int)(Account.Balance / volF));
        }

        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}
Comments
5