Fibonnaci Retracement Bot

Created at 31 Aug 2024, 19:30
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
TH

thecaffeinatedtrader

Joined 22.07.2020

Fibonnaci Retracement Bot
31 Aug 2024, 19:30


Hello,

I am looking to test out a new strategy I have tested this on a small data set manually and seems to be fairly profitable. I would like to build a bot so I can test it over a longer data set and then if it works, run it on a full-time basis. 
If someone can help create the following rules strictly for entries, stops, and take profits it would be greatly appreciated.

The Rules:

  • Use the Fibonacci Retracement tool to measure from the high to the low of the previous candle
  • Use 25%, 50%, and 75% as the levels between 0 - 100%
  • The previous candle's close, whether a bullish or bearish candle will determine the entry direction
    • If the previous candle closed bullish, we look to go long
    • If the previous candle closed bearish, we look to go short
  • We will use limit entries at the 25%, 50%, and 75% retracement levels
  • If the previous candle was bearish
    • we measure from High to Low
    • We then place our limit entries
      • The 25% retracement take profit will be at 0%
      • The 50% retracement take profit will be at 25%
      • The 75% retracement take profit will be at 50% 
        • All stops go to 100% retracement
  • If the previous candle was bullish
    • we measure from Low to High
    • We then place our limit entries
      • The 25% retracement take profit will be at 0%
      • The 50% retracement take profit will be at 25%
      • The 75% retracement take profit will be at 50% 
        • All stops go to 100% retracement
  • The remaining code for position sizing and trading times are depicted below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class FibonacciRetracementStrategy : Robot
    {
        // Parameters
        [Parameter("Source", DefaultValue = "Close")]
        public DataSeries SourceSeries { get; set; }
        
        [Parameter("Instance Name", DefaultValue = "Instance Name", Group = "InstanceName")]
        public string InstanceName { get; set; }
        
        [Parameter("Open", DefaultValue = 23, Group = "Open Trades")]
        public double StartTrading1 { get; set; }

        [Parameter("Close", DefaultValue = 24, Group = "Open Trades")]
        public double StopTrading1 { get; set; }
        
        [Parameter("Open", DefaultValue = 0, Group = "Open Trades")]
        public double StartTrading2 { get; set; }

        [Parameter("Close", DefaultValue = 20, Group = "Open Trades")]
        public double StopTrading2 { get; set; } 

        [Parameter("Long Take Profit", DefaultValue = 30, Step = 1, Group = "Risk Management")]
        public int LongTakeProfit { get; set; }
        
        [Parameter("Long Stop Loss", DefaultValue = 30, Step = 1, Group = "Risk Management")]
        public int LongStopLoss { get; set; }
        
        [Parameter("Short Take Profit", DefaultValue = 30, Step = 1, Group = "Risk Management")]
        public int ShortTakeProfit { get; set; }
        
        [Parameter("Short Stop Loss", DefaultValue = 30, Step = 1, Group = "Risk Management")]
        public int ShortStopLoss { get; set; }
        
        [Parameter("Risk %", DefaultValue = 1, MinValue = 0.01, Step = 0.01, Group = "Risk Management")]
        public double RiskPerTrade { get; set; }



        private DateTime _starttrading1;
        private DateTime _stoptrading1;
        private DateTime _starttrading2;
        private DateTime _stoptrading2;


        protected override void OnStart()
        {
            _starttrading1 = Server.Time.Date.AddHours(StartTrading1);
            _stoptrading1 = Server.Time.Date.AddHours(StopTrading1);
            _starttrading2 = Server.Time.Date.AddHours(StartTrading2);
            _stoptrading2 = Server.Time.Date.AddHours(StopTrading2);
        }
        
        
        protected override void OnTick()
        {
            {
            var currentHours1 = Server.Time.TimeOfDay.TotalHours;
            bool istimecorrect1 = currentHours1 > StartTrading1 && currentHours1 < StopTrading1;
            var currentHours2 = Server.Time.TimeOfDay.TotalHours;
            bool istimecorrect2 = currentHours2 > StartTrading2 && currentHours2 < StopTrading2;
            if (!istimecorrect1 && !istimecorrect2)
            return; 
                {
                double Freemargin = Account.FreeMargin;
                    {
                    // Check for buy entry
                    if  (Freemargin >= Account.Equity/3)
                        {
                        if ( ** Long Entry Rules Go Here ** )
                            {
                            ExecuteMarketOrder(TradeType.Buy, SymbolName, LongGetVolume(LongStopLoss), InstanceName, LongStopLoss, LongTakeProfit);
                            }
                    // Check for sell entry
                    if (Freemargin >= Account.Equity/3)
                        {
                        if ( ** Short Entry Rules Go Here ** )
                            {
                            ExecuteMarketOrder(TradeType.Sell, SymbolName, ShortGetVolume(ShortStopLoss), InstanceName, ShortStopLoss, ShortTakeProfit);
                            }
                        }
                    }
                }
            }
        }
        
        
        private double LongGetVolume(double? LongStopLoss = null)
        {
            double costPerPip = (double)((int)(Symbol.PipValue * 10000000)) / 100;

            // Change this to Account.Balance if you want to
            double baseNumber = Account.FreeMargin;

            double sizeInLots = Math.Round((baseNumber * RiskPerTrade / 100) / (LongStopLoss.Value * costPerPip), 2);

            var result = Symbol.QuantityToVolumeInUnits(sizeInLots);

            if (result > Symbol.VolumeInUnitsMax)
            {
                result = Symbol.VolumeInUnitsMax;
            }
            else if (result < Symbol.VolumeInUnitsMin)
            {
                result = Symbol.VolumeInUnitsMin;
            }
            else if (result % Symbol.VolumeInUnitsStep != 0)
            {
                result = result - (result % Symbol.VolumeInUnitsStep);
            }
            return result;
        }
  
  
        private double ShortGetVolume(double? ShortStopLoss = null)
        {
            double costPerPip = (double)((int)(Symbol.PipValue * 10000000)) / 100;

            // Change this to Account.Balance if you want to
            double baseNumber = Account.FreeMargin;

            double sizeInLots = Math.Round((baseNumber * RiskPerTrade / 100) / (ShortStopLoss.Value * costPerPip), 2);

            var result = Symbol.QuantityToVolumeInUnits(sizeInLots);

            if (result > Symbol.VolumeInUnitsMax)
            {
                result = Symbol.VolumeInUnitsMax;
            }
            else if (result < Symbol.VolumeInUnitsMin)
            {
                result = Symbol.VolumeInUnitsMin;
            }
            else if (result % Symbol.VolumeInUnitsStep != 0)
            {
                result = result - (result % Symbol.VolumeInUnitsStep);
            }
            return result;
        }
    }
}

@thecaffeinatedtrader
Replies

PanagiotisCharalampous
01 Sep 2024, 05:48

Hi there,

If you are looking to hire somebody, feel free to contact me at development@clickalgo.com

Best regards,

Panagiotis


@PanagiotisCharalampous

thecaffeinatedtrader
05 Sep 2024, 00:42

RE: Fibonnaci Retracement Bot

PanagiotisCharalampous said: 

Hi there,

If you are looking to hire somebody, feel free to contact me at development@clickalgo.com

Best regards,

Panagiotis

Thanks, I appreciate it but really all I need to know is if it is even possible to use the Fibonacci Retracement tool in Cbot. 
And if so what are the basics needed to know, I can do the rest and set my own levels, entry and exit points etc.. 

I just don't know the basics to get it to even use this tool. If you could help with that, it would be much appreciated!


@thecaffeinatedtrader