Replies

_internet
29 Mar 2018, 23:21

That's fantastic thank you very much for the code and the advice. I've been looking for that insight about how to best choose which point to open at - the close or the open of the next bar, and what's involved in making the decision. Really helps a lot.


@_internet

_internet
28 Mar 2018, 11:15

Thanks that's great insight. Can I ask what you mean by official way? So if I used your way above, what would the program look like in terms of how it would change?

Didn't know that about OnBar, that makes a lot of sense. I'm wanting to open a position at the closing of the candle which registers the first MA cross. To your knowledge, would this work or would one need to wait to open a position at the open of the second candle after the MA cross is registered? 

Thank you


@_internet

_internet
21 Mar 2018, 18:34

Thank you


@_internet

_internet
21 Mar 2018, 14:44

Hi Panagiotis,

 

Thank you for that. I used the suggestion of the reference window to use the time series option where I want to retrieve the EMA value a certain number of hours ago, i.e. 2, and input what was shown so thought this would work.

It says build succeeded, however gives the error messages

Warning EMA_TEST.cs: Error CS0169: The field 'cAlgo.EMA_TEST.rsi' is never used

Warning EMA_TEST.cs: Error CS0649: Field 'cAlgo.EMA_TEST.EMA_9' is never assigned to, and will always have its default value null

The code I ran is below.

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

namespace cAlgo
{

    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]

    public class EMA_TEST : Robot
    {


        [Parameter()]
        public DataSeries SourceSeries { get; set; }

        [Parameter("EMA_9_period", DefaultValue = 9)]
        public int EMA_9_period { get; set; }

        [Parameter("EMA_3_period", DefaultValue = 3)]
        public int EMA_3_period { get; set; }



        [Parameter("Quantity (Lots)", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]



        public double Quantity { get; set; }
        private RelativeStrengthIndex rsi;

        private ExponentialMovingAverage EMA_9;
        private ExponentialMovingAverage EMA_3;

        private const string label = "Strategy";
        private Position longPosition;
        private Position shortPosition;
        private bool OpenBuyTrade;
        private bool OpenSellTrade;

        protected override void OnStart()
        {
            var EMA_9 = Indicators.ExponentialMovingAverage(SourceSeries, EMA_9_period);
            var EMA_3 = Indicators.ExponentialMovingAverage(SourceSeries, EMA_3_period);




            OpenBuyTrade = false;
            OpenSellTrade = false;

        }

        protected override void OnBar()
        {

            longPosition = Positions.Find(label, Symbol, TradeType.Buy);
            shortPosition = Positions.Find(label, Symbol, TradeType.Sell);


            var currentEMA_9 = EMA_9.Result.Last(1);
            var currentEMA_3 = EMA_3.Result.Last(1);

            var lastEMA_9 = EMA_9.Result.Last(2);
            var lastEMA_3 = EMA_3.Result.Last(2);

            var currentcandleopen = MarketSeries.Open.Last(0);
            if (OpenBuyTrade)
            {
                ExecuteMarketOrder(TradeType.Buy, Symbol, VolumeInUnits, label);
                OpenBuyTrade = false;
            }

            if (OpenSellTrade)
            {
                ExecuteMarketOrder(TradeType.Sell, Symbol, VolumeInUnits, label);
                OpenSellTrade = false;
            }
            if (EMA_3.Result.Last(2) <= EMA_9.Result.Last(2) && EMA_3.Result.Last(1) > EMA_9.Result.Last(1) && longPosition == null)
            {
                OpenBuyTrade = true;

            }
            else if (EMA_3.Result.Last(2) > EMA_9.Result.Last(2) && EMA_3.Result.Last(1) < EMA_9.Result.Last(1) && shortPosition == null)
            {
                OpenSellTrade = true;

            }

            if (longPosition != null && EMA_3.Result.Last(2) >= EMA_9.Result.Last(2) && EMA_3.Result.Last(1) < EMA_9.Result.Last(1))
                ClosePosition(longPosition);

            if (shortPosition != null && EMA_3.Result.Last(2) <= EMA_9.Result.Last(2) && EMA_3.Result.Last(1) > EMA_9.Result.Last(1))
                ClosePosition(shortPosition);
        }

        private long VolumeInUnits
        {
            get { return Symbol.QuantityToVolume(Quantity); }
        }
    }
}

 


@_internet

_internet
21 Mar 2018, 11:13

P.S.

I should mention that the Syntax error above is written as follows: Error AssemblyInfo.cs: Syntax error, ']' expected


@_internet

_internet
21 Mar 2018, 11:12

Here's the code
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo
{

    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]

    public class EMA_Crossover_Strategy : Robot
    {


        [Parameter()]
        public DataSeries SourceSeries { get; set; }





        [Parameter()]
        public DateTime TimeSeries { get; set; }

        //Last(int index);


        [Parameter("EMA_9_period", DefaultValue = 9)]
        public int EMA_9_period { get; set; }

        [Parameter("EMA_3_period", DefaultValue = 3)]
        public int EMA_3_period { get; set; }

        

        [Parameter("Quantity (Lots)", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]

        

        public double Quantity { get; set; }
        private RelativeStrengthIndex rsi;

        //private ExponentialMovingAverage EMA_9;
        //private ExponentialMovingAverage EMA_3;
        
        private const string label = "Strategy";
        private Position longPosition;
        private Position shortPosition;
        private bool OpenBuyTrade;
        private bool OpenSellTrade;

        protected override void OnStart()
        {
            var EMA_9 = Indicators.ExponentialMovingAverage(SourceSeries, EMA_9_period);
            var EMA_3 = Indicators.ExponentialMovingAverage(SourceSeries, EMA_3_period);
            

            

            OpenBuyTrade = false;
            OpenSellTrade = false;

        }

        protected override void OnBar()
        {

            longPosition = Positions.Find(label, Symbol, TradeType.Buy);
            shortPosition = Positions.Find(label, Symbol, TradeType.Sell);


            var currentEMA_9 = EMA_9.Result.Last(1);
            var currentEMA_3 = EMA_3.Result.Last(1);
         
            var lastEMA_9 = EMA_9.Result.Last(2);
            var lastEMA_3 = EMA_3.Result.Last(2);
            
            var currentcandleopen = MarketSeries.Open.Last(0);
            if (OpenBuyTrade)
            {
                ExecuteMarketOrder(TradeType.Buy, Symbol, VolumeInUnits, label);
                OpenBuyTrade = false;
            }

            if (OpenSellTrade)
            {
                ExecuteMarketOrder(TradeType.Sell, Symbol, VolumeInUnits, label);
                OpenSellTrade = false;
            }
            if (EMA_3.Result.Last(2) <= EMA_9.Result.Last(2) && EMA_3.Result.Last(1) > EMA_9.Result.Last(1) && longPosition == null)
            {
                OpenBuyTrade = true;

            }
            else if (EMA_3.Result.Last(2) > EMA_9.Result.Last(2) && EMA_3.Result.Last(1) < EMA_9.Result.Last(1) && shortPosition == null)
            {
                OpenSellTrade = true;

            }

            if (longPosition != null && EMA_3.Result.Last(2) >= EMA_9.Result.Last(2) && EMA_3.Result.Last(1) < EMA_9.Result.Last(1))
                ClosePosition(longPosition);

            if (shortPosition != null && EMA_3.Result.Last(2) <= EMA_9.Result.Last(2) && EMA_3.Result.Last(1) > EMA_9.Result.Last(1))
                ClosePosition(shortPosition);
        }

        private long VolumeInUnits
        {
            get { return Symbol.QuantityToVolume(Quantity); }
        }
    }
}

I'm also getting the error: Error CS1003: Syntax error, ']' expected

and: Error CS1730: Assembly and module attributes must precede all other elements defined in a file except using clauses and extern alias declarations.

Advice would be appreciated, thank you.


@_internet