Can't Import ZigZag indicator calulation for making the strategy in cbot

Created at 09 May 2023, 23:15
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!
AS

ashish.sah.np

Joined 19.12.2022

Can't Import ZigZag indicator calulation for making the strategy in cbot
09 May 2023, 23:15


I am trying to use this indicator in my script for placing the trade. SwingZigZag Indicator | Algorithmic Forex Trading | cTrader Community

But I am not able to open any trade. What am i missing? Can anyone please suggest me for my below code.

using System;
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 SwingZigZagStrategy : Robot
    {
        [Parameter("Period", DefaultValue = 20, MinValue = 1)]
        public int Period { get; set; }

        [Parameter("Risk Reward Ratio", DefaultValue = 2)]
        public double RiskRewardRatio { get; set; }

       

        private SwingZigZag zigzag;

        protected override void OnStart()
        {
            zigzag = Indicators.GetIndicator<SwingZigZag>(MarketSeries.High, MarketSeries.Low, Period);
        }

        protected override void OnBar()
        {
            if (Trade.IsExecuting)
                return;

            double lastSwingHigh = zigzag.SwingHigh.Last(1);
            double lastSwingLow = zigzag.SwingLow.Last(1);
            Print(lastSwingLow);
            // Check for short position
            if (!double.IsNaN(lastSwingHigh) && Symbol.Ask < lastSwingHigh)
            {
                double takeProfitPrice = Math.Max(zigzag.SwingLow.Last(2), zigzag.SwingLow.Last(3));
                if (double.IsNaN(takeProfitPrice))
                    return;

                double takeProfitPips = (int)((lastSwingHigh - takeProfitPrice) / Symbol.PipSize) * RiskRewardRatio;
                double stopLossPips = (int)((lastSwingHigh - Symbol.Ask) / Symbol.PipSize);

                ExecuteMarketOrder(TradeType.Sell, SymbolName, 1000, "SwingZigZagStrategy", stopLossPips, takeProfitPips);
            }
            // Check for long position
            else if (!double.IsNaN(lastSwingLow) && Symbol.Bid > lastSwingLow)
            {
                double takeProfitPrice = Math.Max(zigzag.SwingHigh.Last(2), zigzag.SwingHigh.Last(3));
                if (double.IsNaN(takeProfitPrice))
                    return;

                double takeProfitPips = (int)((takeProfitPrice - lastSwingLow) / Symbol.PipSize) * RiskRewardRatio;
                double stopLossPips = (int)((Symbol.Bid - lastSwingLow) / Symbol.PipSize);

                ExecuteMarketOrder(TradeType.Buy, SymbolName, 1000, "SwingZigZagStrategy", stopLossPips, takeProfitPips);
            }
        }

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

@ashish.sah.np
Replies

firemyst
14 May 2023, 10:33

What error messages, if any, do you receive?

Have you tried putting lots of Print statements through your code to see what values are written in the logging tab?

Ex:



Print("LSH {0}, SA {1}", lastSwingHigh, Symbol.Ask);

if (!double.IsNaN(lastSwingHigh) && Symbol.Ask < lastSwingHigh)
            {
                double takeProfitPrice = Math.Max(zigzag.SwingLow.Last(2), zigzag.SwingLow.Last(3));

Print("TPP: {0}, {1}, {2}", takeProfitPrice, zigzag.SwingLow.Last(2), zigzag.SwingLow.Last(3));

                if (double.IsNaN(takeProfitPrice))
                    return;

                double takeProfitPips = (int)((lastSwingHigh - takeProfitPrice) / Symbol.PipSize) * RiskRewardRatio;
                double stopLossPips = (int)((lastSwingHigh - Symbol.Ask) / Symbol.PipSize);

Print("TPP {0}, SLP {1}", takeProfitPips, stopLossPips);

                ExecuteMarketOrder(TradeType.Sell, SymbolName, 1000, "SwingZigZagStrategy", stopLossPips, takeProfitPips);
            }

 


@firemyst