Category Other  Published on 16/06/2019

Simulator

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.

This cBot is an open source simulator to test your manual and discretional strategies in replay mode.

Put this cBot in visual backtesting mode and use ctrl + left click to open a short position, alt + click to open a long position and ctrl + alt + click to close the position closest to your mouse pointer.

Maybe I'll add support for take profit and stop loss in the future.

No need to thank me, I already know, replay softwares can cost a lot to purchase, and spotware is giving away one for free with a lot of quality history, so thanks them.

Of course, feel free to make suggestions, point out bugs and other stuff that can help.

Changelog 1 - added pending orders! use shift + ctrl + click to set a short pending order on the price where the mouse pointer is (stop or limit is automatically selected depending if the entry price is above or below current price), shift + alt + click to set a long pending order.

Shift + click cancels the pending order with the closes entry price to the mouse pointer.

Changelog 2 - added variable volume! use ctrl + mouse wheel to increment or decrement the volume that you want to use for your positions or pending orders in step of X, where X is the volume parameter of the bot.

Also added a little dashboard to keep track of the number of pips realized.

Changelog 3 - Added Slo-Mo! Use alt+ mouse wheel to slow down time to X ms (milliseconds) per tick, where X is the number specified in the "Slo-Mo Step" parameter.

Changelog 4 - Added static tp/sl; this feature is not so great since theese tp/sl levels cannot be modified... yet. Anyway i added them in case someone needed them.


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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class Simulator : Robot
    {
        [Parameter("Volume", DefaultValue = 1000)]
        public int vol { get; set; }
        [Parameter("Slo-Mo Step (ms)", DefaultValue = 200)]
        public int slomoStep { get; set; }
        [Parameter("TP", DefaultValue = 0)]
        public double tp { get; set; }
        [Parameter("SL", DefaultValue = 0)]
        public double sl { get; set; }
        private int baseVol;
        public int mul = 0;

        protected override void OnStart()
        {
            baseVol = vol;
            Chart.MouseDown += OnChartMouseDown;
            Chart.MouseWheel += OnChartMouseWheel;
        }

        void OnChartMouseWheel(ChartMouseWheelEventArgs obj)
        {
            if (obj.CtrlKey)
            {
                if (obj.Delta > 0)
                    vol += baseVol;
                if (obj.Delta < 0)
                    vol -= baseVol;
                if (vol < baseVol)
                    vol = baseVol;
            }

            if (obj.AltKey)
            {
                if (obj.Delta > 0)
                    mul++;
                if (obj.Delta < 0)
                    mul--;
                if (mul < 0)
                    mul = 0;
                if (mul > 5)
                    mul = 5;
            }
        }

        void OnChartMouseDown(ChartMouseEventArgs obj)
        {
            if (obj.AltKey && !obj.CtrlKey && !obj.ShiftKey)
                ExecuteMarketOrder(TradeType.Buy, Symbol, vol, "", sl, tp);
            if (obj.CtrlKey && !obj.AltKey && !obj.ShiftKey)
                ExecuteMarketOrder(TradeType.Sell, Symbol, vol, "", sl, tp);
            if (obj.AltKey && obj.CtrlKey && !obj.ShiftKey)
                closeClosestPosition(obj.YValue);

            if (obj.ShiftKey && !obj.CtrlKey && obj.AltKey)
            {
                if (obj.YValue > Symbol.Bid)
                    PlaceStopOrder(TradeType.Buy, Symbol, vol, obj.YValue, "", sl, tp);
                else
                    PlaceLimitOrder(TradeType.Buy, Symbol, vol, obj.YValue, "", sl, tp);
            }
            if (obj.ShiftKey && obj.CtrlKey && !obj.AltKey)
            {
                if (obj.YValue > Symbol.Bid)
                    PlaceLimitOrder(TradeType.Sell, Symbol, vol, obj.YValue, "", sl, tp);
                else
                    PlaceStopOrder(TradeType.Sell, Symbol, vol, obj.YValue, "", sl, tp);
            }
            if (!obj.AltKey && !obj.CtrlKey && obj.ShiftKey)
                cancelClosestOrder(obj.YValue);
        }

        protected override void OnTick()
        {
            Chart.DrawStaticText("Dashboard", "Tot. Pips: " + pips() + "\tCurrent Volume: " + vol, VerticalAlignment.Top, HorizontalAlignment.Left, Color.White);
            Chart.DrawStaticText("slomo", "Current Slow Motion Level: " + mul + "\n" + ((slomoStep * mul != 0) ? ((double)slomoStep * mul / 1000).ToString() + " Second(s) per tick" : "No Slo-Mo"), VerticalAlignment.Top, HorizontalAlignment.Right, Color.White);
            Thread.Sleep(slomoStep * mul);
        }

        protected override void OnStop()
        {

        }

        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);
        }

        private void cancelClosestOrder(double price)
        {
            if (PendingOrders.Count == 0)
                return;
            PendingOrder ord = PendingOrders[0];
            foreach (var _ord in PendingOrders)
            {
                if (Math.Abs(price - _ord.TargetPrice) < Math.Abs(price - ord.TargetPrice))
                    ord = _ord;
            }
            CancelPendingOrder(ord);
        }

        private double pips()
        {
            double pips = 0;

            foreach (var pos in Positions)
            {
                pips += pos.Pips;
            }
            foreach (var pos in History)
            {
                pips += pos.Pips;
            }

            return pips;
        }

    }
}


CY
cysecsbin.01

Joined on 10.11.2018 Blocked

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: Simulator.algo
  • Rating: 0
  • Installs: 3276
  • Modified: 13/10/2021 09:54
Comments
Log in to add a comment.
SY
SYUNHUA · 2 years ago

thank you

I have revised the slomode , and 'd like to ask would it update in the future, since a modifiable sl is preferred 

AK
ak9371 · 4 years ago

very very very very thank you

MA
marcushill0123 · 4 years ago

Hello this is amazing! could you put in a way to modify the stops more efficiently instead of a fixed stop? I will be sharing this for sure

CT
ctid241637 · 5 years ago

Let me be the first to thank you anyway! I've been looking for a way to replay bars as well as trade so that I can test discretionary theories.

So, thank you very much.

I only made one adjustment and that was to find/replace White with Black so that I could see the text (my background is currently white) and, other than that, no changes necessary. Excellent!