Populating stop orders with predefined conditional TP and SL

PUdPUd since: 24 Jan 2021;

  01 Apr 2021, 23:54
Populating stop orders with predefined conditional TP and SL

Hello, I have some calculations that tell me how to trade an instrument that is conditional on the value of other instruments. If the other instrument is below or between values (i.e. <5, >=5 & <7, etc) then I would like to trade a predefined TP and SL for a long and short position (there are around 8 pairs of TP and SL calculations in total). I get the error messages Error CS0103: The name 'TargetLongMath' does not exist in the current context. and the same error message for TargetShortMath. Sorry for the simple question. 

Also, I get multiple Warnings that Internals.MarketSeries is obsolete and to 'Use Bars instead' but I can't seem to make it work for accessing data from other instruments. Following on from that I get warning messages to use Bars.HighPrices instead. Can someone point me towards proper syntax? 

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 MyRobot : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

//The source of the MarketSeries is obsolete: 'Use Bars Instead' Warning message
        private MarketSeries series2, series3;
        private Symbol symbol2, symbol3;

        [Parameter(DefaultValue = "EURUSD")]
        public string Symbol2 { get; set; }

        [Parameter(DefaultValue = "GBPUSD")]
        public string Symbol3 { get; set; }

        protected override void OnStart()
        {
            var dailyBars = MarketData.GetBars(TimeFrame.Daily);
            dailyBars.BarOpened += OnDailyBarsBarOpened;
        }

        void OnDailyBarsBarOpened(BarOpenedEventArgs obj)
        {
            //Get some data

var Lag1USDCADHigh = obj.Bars.HighPrices.Last(1);

            symbol2 = Symbols.GetSymbol(Symbol2);
            series2 =  GetSeries(symbol2, TimeFrame.Daily);
//Source of second warning message to use Bars.ClosePrices Instead but how to apply to external data??            var EURUSDYDaysClose = series2.Close.Last(0);

            var symbol3 = Symbols.GetSymbol(Symbol3);
            series3 = GetSeries(symbol3, TimeFrame.Daily);
            var Lag1GBPUSDHigh = series3.High.Last(0);

//Predifined TP and SL for Long and Short positions
            var 1LongMath = -1 + (2 * Lag1GBPUSDHigh);
            var 1ShortMath = -3 + (4 * EURUSDYDaysClose) + (5 * Lag1USDCADHigh );
            var 1LongSL = -0.6;
            var 1ShortSL = 7;
                         
            var 2LongMath = -2 + (3 * Lag1GBPUSDHigh);
            var 2ShortMath = -4 + (5 * EURUSDYDaysClose) + (6 * Lag1USDCADHigh );
            var 2LongSL = -0.7;
            var 2ShortSL = 8;

            var 3LongMath = -3 + (4 * Lag1GBPUSDHigh);
            var 3ShortMath = -5 + (6 * EURUSDYDaysClose) + (7 * Lag1USDCADHigh );
            var 3LongSL = -0.8;
            var 3ShortSL = 9;

//Choose which TP and SL to use based on the value of another instrument

            if (EURUSDYDaysClose < 0.5)
            {
                var TargetLongMath = 1LongMath;
                var LongSL = 1LongSL;
                var TargetShortMath = 1ShortMath;
                var ShortSL = 1ShortSL;
            }

            else
            {
                if ((EURUSDYDaysClose > 0.5) && (EURUSDYDaysClose < 0.7)
                {
                var TargetLongMath = 2LongMath;
                var LongSL = 2LongSL;
                var TargetShortMath = 2ShortMath;
                var ShortSL = 2ShortSL;

                }

                else if ((EURUSDYDaysClose > 0.7) && (EURUSDYDaysClose < 0.9))
                {
                var TargetLongMath = 3LongMath;
                var LongSL = 3LongSL;
                var TargetShortMath = 3ShortMath;
                var ShortSL = 3ShortSL;

                }


                //Round down the estimated profit target level, calculate target pips, round down target pips
                var TargetLong = (Math.Round(TargetLongMath, 3));
                var TargetPipslong = TargetLong - Symbol.Bid * Symbol.PipSize;
                var TargetPipsLong = (Math.Round(TargetPipslong, 3));


              //As above but for shorts
                var TargetShort = (Math.Round(TargetShortMath, 3));
                var TargetPipsshort = TargetShort - Symbol.Ask * Symbol.PipSize;
                var TargetPipsShort = (Math.Round(TargetPipsshort, 3));


                foreach (var position in Positions)
                {
                    ClosePosition(position);
                }

                        var risk = 0.95;

                        double Volume = (long)Math.Ceiling(Account.Equity / 100) * risk;                 
                        Volume = Symbol.NormalizeVolumeInUnits(Volume, RoundingMode.Down);

                        if (Volume > 100)
                            Volume = 100;
                      
                        var SLBid = (Math.Round(Symbol.Bid + LongSL, 2));
                        var SLAsk = (Math.Round(Symbol.Ask ShortSL, 2));

                        var TPBidProp = TargetPipsLong;
                        var TPBid = (Math.Round(TPBidProp * Symbol.PipSize, 2));

                        var TPAskProp = TargetPipsShort;
                        var TPAsk = (Math.Round(TPAskProp * Symbol.PipSize, 2));

                        PlaceStopOrder(TradeType.Buy, SymbolName, Volume, Symbol.Bid * Symbol.PipSize, "Buy", SLBid, TPBid, );
                        PlaceStopOrder(TradeType.Sell, SymbolName, Volume, Symbol.Ask * Symbol.PipSize, "Sell", SLAsk, TPAsk);
                    

                    }
                }


            }
        }




        protected override void OnTick()
        {
            // Put your core logic here
        }

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

amusleh's avatar

amusleh since: 01 Mar 2021;

  02 Apr 2021, 11:26

Hi,

You code is full of errors, please learn the basic of C# before trying to create an indicator or cBot.

You can't start your variable names in C# with a number, this is not allowed, and when you post code please use the code snippet option of editor.

We can't develop your cBot for you, we only can answer questions here.

I made some changes on your cBot code but it still has some errors as I don't had access to some of your variables and I don't know what are those variables.

using cAlgo.API;
using cAlgo.API.Internals;
using System;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class MyRobot : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

        //The source of the MarketSeries is obsolete: 'Use Bars Instead' Warning message
        private Bars series2, series3;

        private Symbol symbol2, symbol3;

        [Parameter(DefaultValue = "EURUSD")]
        public string Symbol2 { get; set; }

        [Parameter(DefaultValue = "GBPUSD")]
        public string Symbol3 { get; set; }

        protected override void OnStart()
        {
            var dailyBars = MarketData.GetBars(TimeFrame.Daily);
            dailyBars.BarOpened += OnDailyBarsBarOpened;
        }

        protected override void OnTick()
        {
            // Put your core logic here
        }

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

        private void OnDailyBarsBarOpened(BarOpenedEventArgs obj)
        {
            //Get some data

            var Lag1USDCADHigh = obj.Bars.HighPrices.Last(1);

            symbol2 = Symbols.GetSymbol(Symbol2);
            series2 = MarketData.GetBars(TimeFrame.Daily, Symbol2);
            //Source of second warning message to use Bars.ClosePrices Instead but how to apply to external data??            var EURUSDYDaysClose = series2.Close.Last(0);

            var symbol3 = Symbols.GetSymbol(Symbol3);
            series3 = MarketData.GetBars(TimeFrame.Daily, Symbol3);
            var Lag1GBPUSDHigh = series3.HighPrices.Last(0);

            //Predifined TP and SL for Long and Short positions
            var longMath = -1 + (2 * Lag1GBPUSDHigh);
            var shortMath = -3 + (4 * EURUSDYDaysClose) + (5 * Lag1USDCADHigh);
            var longSL = -0.6;
            var shortSL = 7.0;

            var longMath = -2 + (3 * Lag1GBPUSDHigh);
            var shortMath = -4 + (5 * EURUSDYDaysClose) + (6 * Lag1USDCADHigh);
            var longSL = -0.7;
            var shortSL = 8;

            var longMath = -3 + (4 * Lag1GBPUSDHigh);
            var shortMath = -5 + (6 * EURUSDYDaysClose) + (7 * Lag1USDCADHigh);
            var longSL = -0.8;
            var shortSL = 9;

            //Choose which TP and SL to use based on the value of another instrument

            double TargetLongMath = 0;

            if (EURUSDYDaysClose < 0.5)
            {
                TargetLongMath  = longMath;
                var LongSL = longSL;
                var TargetShortMath = shortMath;
                var ShortSL = shortSL;
            }
            else
            {
                if ((EURUSDYDaysClose > 0.5) && (EURUSDYDaysClose < 0.7)
                {
                    TargetLongMath = longMath;
                    var LongSL = longSL;
                    var TargetShortMath = shortMath;
                    var ShortSL = shortSL;
                }
                else if ((EURUSDYDaysClose > 0.7) && (EURUSDYDaysClose < 0.9))
                {
                    TargetLongMath = longMath;
                    var LongSL = longSL;
                    var TargetShortMath = shortMath;
                    var ShortSL = shortSL;
                }

                //Round down the estimated profit target level, calculate target pips, round down target pips
                var TargetLong = (Math.Round(TargetLongMath, 3));
                var TargetPipslong = TargetLong - Symbol.Bid * Symbol.PipSize;
                var TargetPipsLong = (Math.Round(TargetPipslong, 3));

                //As above but for shorts
                var TargetShort = (Math.Round(TargetShortMath, 3));
                var TargetPipsshort = TargetShort - Symbol.Ask * Symbol.PipSize;
                var TargetPipsShort = (Math.Round(TargetPipsshort, 3));

                foreach (var position in Positions)
                {
                    ClosePosition(position);
                }

                var risk = 0.95;

                double Volume = (long)Math.Ceiling(Account.Equity / 100) * risk;
                Volume = Symbol.NormalizeVolumeInUnits(Volume, RoundingMode.Down);

                if (Volume > 100)
                    Volume = 100;

                var SLBid = Math.Round(Symbol.Bid + longSL, 2);
                var SLAsk = Math.Round(Symbol.Ask + shortSL, 2);

                var TPBidProp = TargetPipsLong;
                var TPBid = (Math.Round(TPBidProp * Symbol.PipSize, 2));

                var TPAskProp = TargetPipsShort;
                var TPAsk = (Math.Round(TPAskProp * Symbol.PipSize, 2));

                PlaceStopOrder(TradeType.Buy, SymbolName, Volume, Symbol.Bid * Symbol.PipSize, "Buy", SLBid, TPBid);
                PlaceStopOrder(TradeType.Sell, SymbolName, Volume, Symbol.Ask * Symbol.PipSize, "Sell", SLAsk, TPAsk);
            }
        }
    }
}

That's all we can do here in forum, if you need more than this please ask a consultant to help you or post a job request.

Community Developer | Spotware

PUdPUd since: 24 Jan 2021;

  02 Apr 2021, 13:31
RE:

Thank you for the syntax changes Amusleh, much appreciated. None of my variables start with a number so I am not sure what you refer to above.

My only outstanding problem is being able to select the predefined TP and SL from each of the model coefficients I have developed depending on the level of GBPUSD yesterday. Depending on the level of GBPUSD the cbot needs to select the appropriate TP and SL, some levels have the same TP and SL and some don't, and some have a combination of new and old TP and SL, hence the repetition. If there is a better way to select this than using if, else statements please say so so that I can investigate. If I could afford someone to code it for me I would!

If anyone can advise on why my I am getting Error CS0103: The name 'TargetLongMath' does not exist in the current context and the same for TargetShortMath that would be much appreciated.

Updated code here with all variables: 

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 aNarrowestCI : Robot

    {

        [Parameter(DefaultValue = 0.0)]

        public double Parameter { get; set; }

        private Bars series2, series3;

        private Symbol symbol2, symbol3;

        [Parameter(DefaultValue = "EURUSD")]

        public string Symbol2 { get; set; }

        [Parameter(DefaultValue = "GBPUSD")]

        public string Symbol3 { get; set; }

        protected override void OnStart()

        {

            var dailyBars = MarketData.GetBars(TimeFrame.Daily);

            dailyBars.BarOpened += OnDailyBarsBarOpened;

        }

        void OnDailyBarsBarOpened(BarOpenedEventArgs obj)

        {

            var Lag1USDCADHigh = obj.Bars.HighPrices.Last(1);

            var Lag1USDCADOpen = obj.Bars.OpenPrices.Last(1);

            var Lag1USDCADLow = obj.Bars.LowPrices.Last(1);

            var Lag1USDCADOpenHigh = Lag1USDCADHigh - Lag1USDCADOpen;

            var Lag1USDCADOpenLow = Lag1USDCADOpen - Lag1USDCADLow;

            var Lag1USDCADVolume = obj.Bars.TickVolumes.Last(1);

            var Lag1USDCADPropOH = (Lag1USDCADOpenHigh / Lag1USDCADOpen) * 100;

            var symbol2 = Symbols.GetSymbol(Symbol2);

            series2 = MarketData.GetBars(TimeFrame.Daily, Symbol2);

            var GBPUSDYDaysClose = series2.ClosePrices.Last(0);

            var Lag1GBPUSDHigh = series2.HighPrices.Last(0);

            var Lag1GBPUSDLow = series2.LowPrices.Last(0);

            var Lag1GBPUSDOpen = series2.OpenPrices.Last(0);

            var Lag1GBPUSDOpenLow = Lag1GBPUSDOpen - Lag1GBPUSDLow;

            var Lag1GBPUSDOpenHigh = Lag1GBPUSDHigh - Lag1GBPUSDOpen;

            var Lag1GBPUSDPropHighLow = ((Lag1GBPUSDHigh - Lag1GBPUSDLow) / Lag1GBPUSDOpen) * 100;

            var symbol3 = Symbols.GetSymbol(Symbol3);

            series3 = MarketData.GetBars(TimeFrame.Daily, Symbol3);

            var Lag1EURUSDHigh = series3.HighPrices.Last(0);

            var Lag1EURUSDOpen = series3.OpenPrices.Last(0);

            var Lag1EURUSDVolume = series3.TickVolumes.Last(1);

            // < 1.10

            //Long Model 5

            var FM5LongMath = -1 + (2 * Lag1USDCADHigh) + (3* Lag1USDCADOpenLow) + (4 * Lag1GBPUSDOpenLow) + (-5 * Lag1GBPUSDPropHighLow) + (6 * Lag1EURUSDHigh);

            //Short Model 2

            var FM2ShortMath = -7 + (8 * Lag1USDCADLow) + (9 * Lag1USDCADOpenHigh);

            var FM5LongSL_15 = -10;

            var FM2ShortSL_15 = 9;

            //>= 1.10 GBPUSDYDaysClose < 1.20         

            //Long Model 5

            var FM5LongSL_15_20 = -8;

            //Short Model 3

            var FM3ShortMath = 7 + (6 * Lag1USDCADLow) + (5 * Lag1USDCADOpenHigh) + (-4 * Lag1GBPUSDOpen);

            var FM3ShortSL_15_20 = 3;

            //>= 1.20 GBPUSDYDaysClose < 1.25

            //Long Model 5

            var FM5LongSL_20_25 = 2;

            //Short Model 2

            var FM2ShortSL_20_25 = 1;

            //  >= 1.25 GBPUSDYDaysClose < 1.30

            //Long Model 5

            var FM5LongSL_25_30 = -1;

            //Short Model 2

            var SM2ShortMath = -2 + (3 * Lag1USDCADOpen) + (4 * Lag1USDCADPropOH);

            var SM2ShortSL_25_30 = 5;

            //>= 1.30 GBPUSDYDaysClose < 1.45

            //Long Model 5

            var FM5LongSL_30_45 = -6;

            //Short Model 4

            var FM4ShortMath = 7 + (8 * Lag1USDCADLow) + (9 * Lag1USDCADOpenHigh) + (-8 * Lag1GBPUSDOpen) + (-1 * Lag1USDCADOpenLow);

            var FM4ShortSL_30_45 = 2;

            //GBPUSDYDaysClose >= 1.45

            //Long Model 1

            var BM1LongMath = -3 + (4 * GBPUSDYDaysClose) + (5 * Lag1USDCADOpen) + (-6 * Lag1USDCADVolume) + (-7 * Lag1GBPUSDOpenHigh) + (8 * Lag1EURUSDVolume) + (9 * Lag1USDCADOpenHigh) + (8 * Lag1USDCADOpenLow) + (7 * Lag1GBPUSDPropHighLow) + 6 * Lag1EURUSDOpen);

            var BM1LongSL_45 = -54.13;

            //Short Model 3

            var FM3ShortSL_45 = 102.914;

            if (GBPUSDYDaysClose < 15)

            {

                var TargetLongMath = FM5LongMath;

                // var LongSL = FM5LongSL_15;

                //var TargetShortMath = FM2ShortMath;

                // var ShortSL = FM2ShortSL_15;

            }

            else

            {

                if ((GBPUSDYDaysClose >= 15) && (GBPUSDYDaysClose < 20))

                {

                    var TargetLongMath = FM5LongMath;

                    //var LongSL = FM5LongSL_15_20;

                    //var TargetShortMath = FM3ShortMath;

                    //var ShortSL = FM3ShortSL_15_20;

                }

                else if ((GBPUSDYDaysClose >= 20) && (GBPUSDYDaysClose < 25))

                {

                    var TargetLongMath = FM5LongMath;

                    // var LongSL = FM5LongSL_20_25;

                    //var TargetShortMath = FM2ShortMath;

                    //var ShortSL = FM2ShortSL_20_25;

                }

                else if ((GBPUSDYDaysClose >= 25) && (GBPUSDYDaysClose < 30))

                {

                    var TargetLongMath = FM5LongMath;

                    // var LongSL = FM5LongSL_25_30;

                    //var TargetShortMath = SM2ShortMath;

                    // var ShortSL = SM2ShortSL_25_30;

                }

                else if ((GBPUSDYDaysClose >= 30) && (GBPUSDYDaysClose < 45))

                {

                    var TargetLongMath = FM5LongMath;

                    // var LongSL = FM5LongSL_30_45;

                    //var TargetShortMath = FM4ShortMath;

                    //var ShortSL = FM4ShortSL_30_45;

                }

                else if (GBPUSDYDaysClose >= 45)

                {

                    var TargetLongMath = BM1LongMath;

                    // var LongSL = BM1LongSL_45;

                    //var TargetShortMath = FM3ShortMath;

                    //var ShortSL = FM3ShortSL_45;

                }

                //Long structure

                //var Targetlong = TargetLongMath;

                var TargetLong = (Math.Round(TargetLongMath, 3));

                var TargetPipslong = TargetLong - Symbol.Bid * Symbol.PipSize;

                var TargetPipsLong = (Math.Round(TargetPipslong, 3));

                //Short structure

                var TargetShort = (Math.Round(TargetShortMath, 3));

                var TargetPipsshort = TargetShort - Symbol.Ask * Symbol.PipSize;

                var TargetPipsShort = (Math.Round(TargetPipsshort, 3));

                foreach (var position in Positions)

                {

                    ClosePosition(position);

                }

                if (TargetPipsLong > 0)

                {

                    if (TargetPipsShort > 0)

                    {

                        var risk = 0.95;

                        double Volume = (long)Math.Ceiling(Account.Equity / 100) * risk;

                        double MaxVolume = Volume / 100;

                        Volume = Symbol.NormalizeVolumeInUnits(Volume, RoundingMode.Down);

                        if (Volume > 100)

                            Volume = 100;

                        var SLprop = 0.0;

                        var SLBid = (Math.Round(Symbol.Bid * SLprop, 2));

                        var SLAsk = (Math.Round(Symbol.Ask * SLprop, 2));

                        var TPBidProp = TargetPipsLong;

                        var TPBid = (Math.Round(TPBidProp * Symbol.PipSize, 2));

                        var TPAskProp = TargetPipsShort;

                        var TPAsk = (Math.Round(TPAskProp * Symbol.PipSize, 2));

                        PlaceStopOrder(TradeType.Buy, SymbolName, Volume, Symbol.Bid + 3 * Symbol.PipSize, "OpenStopBuy", SLBid, TPBid, Server.Time.AddHours(23));

                        PlaceStopOrder(TradeType.Sell, SymbolName, Volume, Symbol.Ask - 3 * Symbol.PipSize, "OpenStopSell", SLAsk, TPAsk, Server.Time.AddHours(23));

                    }

                }

            }

        }

        protected override void OnTick()

        {

            // Put your core logic here

        }

        protected override void OnStop()

        {

            // Put your deinitialization logic here

        }

    }

}

afhacker's avatar

afhacker since: 15 Oct 2015;

  02 Apr 2021, 15:20

The numbers appeared on variable names because I copied the code, please use the code snippet of editor to post code as I do.

The variables are not defined, that's why you are getting that error, you have to first define a variable then use it.

Here is the code:

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 aNarrowestCI : Robot

    {

        [Parameter(DefaultValue = 0.0)]

        public double Parameter { get; set; }

        private Bars series2, series3;

        private Symbol symbol2, symbol3;

        [Parameter(DefaultValue = "EURUSD")]

        public string Symbol2 { get; set; }

        [Parameter(DefaultValue = "GBPUSD")]

        public string Symbol3 { get; set; }

        protected override void OnStart()

        {

            var dailyBars = MarketData.GetBars(TimeFrame.Daily);

            dailyBars.BarOpened += OnDailyBarsBarOpened;

        }

        void OnDailyBarsBarOpened(BarOpenedEventArgs obj)

        {

            var Lag1USDCADHigh = obj.Bars.HighPrices.Last(1);

            var Lag1USDCADOpen = obj.Bars.OpenPrices.Last(1);

            var Lag1USDCADLow = obj.Bars.LowPrices.Last(1);

            var Lag1USDCADOpenHigh = Lag1USDCADHigh - Lag1USDCADOpen;

            var Lag1USDCADOpenLow = Lag1USDCADOpen - Lag1USDCADLow;

            var Lag1USDCADVolume = obj.Bars.TickVolumes.Last(1);

            var Lag1USDCADPropOH = (Lag1USDCADOpenHigh / Lag1USDCADOpen) * 100;

            var symbol2 = Symbols.GetSymbol(Symbol2);

            series2 = MarketData.GetBars(TimeFrame.Daily, Symbol2);

            var GBPUSDYDaysClose = series2.ClosePrices.Last(0);

            var Lag1GBPUSDHigh = series2.HighPrices.Last(0);

            var Lag1GBPUSDLow = series2.LowPrices.Last(0);

            var Lag1GBPUSDOpen = series2.OpenPrices.Last(0);

            var Lag1GBPUSDOpenLow = Lag1GBPUSDOpen - Lag1GBPUSDLow;

            var Lag1GBPUSDOpenHigh = Lag1GBPUSDHigh - Lag1GBPUSDOpen;

            var Lag1GBPUSDPropHighLow = ((Lag1GBPUSDHigh - Lag1GBPUSDLow) / Lag1GBPUSDOpen) * 100;

            var symbol3 = Symbols.GetSymbol(Symbol3);

            series3 = MarketData.GetBars(TimeFrame.Daily, Symbol3);

            var Lag1EURUSDHigh = series3.HighPrices.Last(0);

            var Lag1EURUSDOpen = series3.OpenPrices.Last(0);

            var Lag1EURUSDVolume = series3.TickVolumes.Last(1);

            // < 1.10

            //Long Model 5

            var FM5LongMath = -1 + (2 * Lag1USDCADHigh) + (3* Lag1USDCADOpenLow) + (4 * Lag1GBPUSDOpenLow) + (-5 * Lag1GBPUSDPropHighLow) + (6 * Lag1EURUSDHigh);

            //Short Model 2

            var FM2ShortMath = -7 + (8 * Lag1USDCADLow) + (9 * Lag1USDCADOpenHigh);

            var FM5LongSL_15 = -10;

            var FM2ShortSL_15 = 9;

            //>= 1.10 GBPUSDYDaysClose < 1.20         

            //Long Model 5

            var FM5LongSL_15_20 = -8;

            //Short Model 3

            var FM3ShortMath = 7 + (6 * Lag1USDCADLow) + (5 * Lag1USDCADOpenHigh) + (-4 * Lag1GBPUSDOpen);

            var FM3ShortSL_15_20 = 3;

            //>= 1.20 GBPUSDYDaysClose < 1.25

            //Long Model 5

            var FM5LongSL_20_25 = 2;

            //Short Model 2

            var FM2ShortSL_20_25 = 1;

            //  >= 1.25 GBPUSDYDaysClose < 1.30

            //Long Model 5

            var FM5LongSL_25_30 = -1;

            //Short Model 2

            var SM2ShortMath = -2 + (3 * Lag1USDCADOpen) + (4 * Lag1USDCADPropOH);

            var SM2ShortSL_25_30 = 5;

            //>= 1.30 GBPUSDYDaysClose < 1.45

            //Long Model 5

            var FM5LongSL_30_45 = -6;

            //Short Model 4

            var FM4ShortMath = 7 + (8 * Lag1USDCADLow) + (9 * Lag1USDCADOpenHigh) + (-8 * Lag1GBPUSDOpen) + (-1 * Lag1USDCADOpenLow);

            var FM4ShortSL_30_45 = 2;

            //GBPUSDYDaysClose >= 1.45

            //Long Model 1

            var BM1LongMath = -3 + (4 * GBPUSDYDaysClose) + (5 * Lag1USDCADOpen) + (-6 * Lag1USDCADVolume) + (-7 * Lag1GBPUSDOpenHigh) + (8 * Lag1EURUSDVolume) + (9 * Lag1USDCADOpenHigh) + (8 * Lag1USDCADOpenLow) + (7 * Lag1GBPUSDPropHighLow) + (6 * Lag1EURUSDOpen);

            var BM1LongSL_45 = -54.13;

            //Short Model 3

            var FM3ShortSL_45 = 102.914;

            double TargetLongMath = 0;
            double TargetShortMath = 0;

            if (GBPUSDYDaysClose < 15)

            {

                TargetLongMath = FM5LongMath;

                // var LongSL = FM5LongSL_15;

                //var TargetShortMath = FM2ShortMath;

                // var ShortSL = FM2ShortSL_15;

            }

            else

            {

                if ((GBPUSDYDaysClose >= 15) && (GBPUSDYDaysClose < 20))

                {

                    TargetLongMath = FM5LongMath;

                    //var LongSL = FM5LongSL_15_20;

                    //var TargetShortMath = FM3ShortMath;

                    //var ShortSL = FM3ShortSL_15_20;

                }

                else if ((GBPUSDYDaysClose >= 20) && (GBPUSDYDaysClose < 25))

                {

                    TargetLongMath = FM5LongMath;

                    // var LongSL = FM5LongSL_20_25;

                    //var TargetShortMath = FM2ShortMath;

                    //var ShortSL = FM2ShortSL_20_25;

                }

                else if ((GBPUSDYDaysClose >= 25) && (GBPUSDYDaysClose < 30))

                {

                    TargetLongMath = FM5LongMath;

                    // var LongSL = FM5LongSL_25_30;

                    //var TargetShortMath = SM2ShortMath;

                    // var ShortSL = SM2ShortSL_25_30;

                }

                else if ((GBPUSDYDaysClose >= 30) && (GBPUSDYDaysClose < 45))

                {

                    TargetLongMath = FM5LongMath;

                    // var LongSL = FM5LongSL_30_45;

                    //var TargetShortMath = FM4ShortMath;

                    //var ShortSL = FM4ShortSL_30_45;

                }

                else if (GBPUSDYDaysClose >= 45)

                {

                    TargetLongMath = BM1LongMath;

                    // var LongSL = BM1LongSL_45;

                    //var TargetShortMath = FM3ShortMath;

                    //var ShortSL = FM3ShortSL_45;

                }

                //Long structure

                //var Targetlong = TargetLongMath;

                var TargetLong = (Math.Round(TargetLongMath, 3));

                var TargetPipslong = TargetLong - Symbol.Bid * Symbol.PipSize;

                var TargetPipsLong = (Math.Round(TargetPipslong, 3));

                //Short structure

                var TargetShort = (Math.Round(TargetShortMath, 3));

                var TargetPipsshort = TargetShort - Symbol.Ask * Symbol.PipSize;

                var TargetPipsShort = (Math.Round(TargetPipsshort, 3));

                foreach (var position in Positions)

                {

                    ClosePosition(position);

                }

                if (TargetPipsLong > 0)

                {

                    if (TargetPipsShort > 0)

                    {

                        var risk = 0.95;

                        double Volume = (long)Math.Ceiling(Account.Equity / 100) * risk;

                        double MaxVolume = Volume / 100;

                        Volume = Symbol.NormalizeVolumeInUnits(Volume, RoundingMode.Down);

                        if (Volume > 100)

                            Volume = 100;

                        var SLprop = 0.0;

                        var SLBid = (Math.Round(Symbol.Bid * SLprop, 2));

                        var SLAsk = (Math.Round(Symbol.Ask * SLprop, 2));

                        var TPBidProp = TargetPipsLong;

                        var TPBid = (Math.Round(TPBidProp * Symbol.PipSize, 2));

                        var TPAskProp = TargetPipsShort;

                        var TPAsk = (Math.Round(TPAskProp * Symbol.PipSize, 2));

                        PlaceStopOrder(TradeType.Buy, SymbolName, Volume, Symbol.Bid + 3 * Symbol.PipSize, "OpenStopBuy", SLBid, TPBid, Server.Time.AddHours(23));

                        PlaceStopOrder(TradeType.Sell, SymbolName, Volume, Symbol.Ask - 3 * Symbol.PipSize, "OpenStopSell", SLAsk, TPAsk, Server.Time.AddHours(23));

                    }

                }

            }

        }

        protected override void OnTick()

        {

            // Put your core logic here

        }

        protected override void OnStop()

        {

            // Put your deinitialization logic here

        }

    }

}

Last, your bot will not work as you expect, please learn C# basics before writing a cBot/indicator.

http://algodeveloper.com/

PUdPUd since: 24 Jan 2021;

  02 Apr 2021, 15:32
RE:

Thanks for your thoughts Afhacker. Your suggested amendment gives the following error: Error CS0136: A local variable named 'TargetLongMath' cannot be declared in this scope because it would give a different meaning to 'TargetLongMath', which is already used in a 'parent or current' scope to denote something else

amusleh's avatar

amusleh since: 01 Mar 2021;

  02 Apr 2021, 22:09
RE: RE:

PUdPUd said:

Thanks for your thoughts Afhacker. Your suggested amendment gives the following error: Error CS0136: A local variable named 'TargetLongMath' cannot be declared in this scope because it would give a different meaning to 'TargetLongMath', which is already used in a 'parent or current' scope to denote something else

We provide help here for cTrader and cTrader automate, not for C#, as I said in my previous posts please start learning C# basics: 

Community Developer | Spotware

sureshbabudasari since: 03 May 2021;

  03 May 2021, 18:52
RE: RE: RE:

amusleh said:

PUdPUd said:

Thanks for your thoughts Afhacker. Your suggested amendment gives the following error: Error CS0136: A local variable named 'TargetLongMath' cannot be declared in this scope because it would give a different meaning to 'TargetLongMath', which is already used in a 'parent or current' scope to denote something else

We provide help here for cTrader and cTrader automate, not for C#, as I said in my previous posts please start learning C# basics: 

Even the following c# course is useful to learn c# from basic to advanced level.

https://www.tutlane.com/tutorial/csharp