Replies

varunthakur.outlook
01 Nov 2016, 10:58

Thank you

First of all thank you, lucian.

Below is my code, it does work; however, see if you can suggest any improvements.

 

//#reference: ..\Indicators\HeikenAshi.algo

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 HAichimoku : Robot
    {


        [Parameter("Buy", DefaultValue = true)]
        public bool Buy { get; set; }

        [Parameter("Sell", DefaultValue = true)]
        public bool Sell { get; set; }


        [Parameter("First Volume", DefaultValue = 1, MinValue = 1, Step = 1)]
        public int FirstVolume { get; set; }


        [Parameter("Max Spread", DefaultValue = 3)]
        public double MaxSpread { get; set; }

        [Parameter("TP", DefaultValue = 1, MinValue = 1)]
        public int TP { get; set; }


        [Parameter("SL", DefaultValue = 0, MinValue = 0)]
        public int SL { get; set; }

        [Parameter("MaxPipsSlippage", DefaultValue = 3, MinValue = 1)]
        public int MPS { get; set; }




        //[Parameter("MaxNegativeGrossProfit", DefaultValue = 100, MinValue = 100)]
        //public double MaxNegativeGrProfit { get; set; }



        private bool LossMoreThanUserWants;



        //private string Label = Convert.ToString(Symbol.Code);
        //private Position position; //to check position by position

        private double sp_d;



        private HeikenAshi HAJ;
        private int index;



        //--------------------------------------------------------------------------------------
        private IchimokuKinkoHyo _ichi;
        private bool ichiHAJSell = false;
        private bool ichiHAJBuy = false;



        //------------------------------------------------------------------------------------------


        //private int takeProfitInPips => Math.Round(TP / Symbol.PipSize, 1);

        protected override void OnStart()
        {

            HAJ = Indicators.GetIndicator<HeikenAshi>(1);

            _ichi = Indicators.IchimokuKinkoHyo(9, 26, 52);

        }


//---------------------------------------------------------------------------------------------------------------------------------------------------------



//---------------------------------------------------------------------------------------------------------------------------------------------------------------




        protected override void OnError(Error error)
        {
            if (error.Code == ErrorCode.NoMoney)
            {

                Print("openning stopped because: not enough money");
            }
        }

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++



        protected override void OnBar()
        {

            index = MarketSeries.Close.Count - 2;

            double HAJCloseValue = HAJ.xClose[index];
            double HAJOpenValue = HAJ.xOpen[index];
            double HAJLowValue = HAJ.xLow[index];
            double HAJHighValue = HAJ.xHigh[index];

            double MktSeriesCloseValue = MarketSeries.Close.LastValue;


            sp_d = (Symbol.Ask - Symbol.Bid) / Symbol.PipSize;

            if ((HAJCloseValue < _ichi.SenkouSpanA[index]) && (HAJCloseValue < _ichi.SenkouSpanB[index]) && (_ichi.TenkanSen.LastValue < _ichi.KijunSen.LastValue) && (HAJCloseValue > (_ichi.ChikouSpan[index - 26])))
            {
                if ((HAJOpenValue == HAJHighValue))
                {
                    if (sp_d <= MaxSpread)
                    {
                        Open_24Sell();
                    }
                }
            }




            if ((HAJ.xClose[index] > _ichi.SenkouSpanA[index]) && (HAJ.xClose[index] > _ichi.SenkouSpanB[index]) && (_ichi.TenkanSen.LastValue > _ichi.KijunSen.LastValue) && (HAJ.xClose[index] < (_ichi.ChikouSpan[index - 26])))
            {
                if (HAJ.xOpen[index] == HAJ.xLow[index])
                {
                    if (sp_d <= MaxSpread)
                    {
                        Open_24Buy();
                    }
                }
            }



            ChartObjects.DrawText("SpreadTxt", "spread " + Convert.ToString(Math.Round(sp_d, 2)), StaticPosition.TopCenter, Colors.Tomato);


        }


        protected override void OnStop()
        {
            // ChartObjects.RemoveAllObjects();
        }



        private void Open_24Buy()
        {


            OrderSend(TradeType.Buy, FirstVolume);


        }


        private void Open_24Sell()
        {

            OrderSend(TradeType.Sell, FirstVolume);

        }


        private void OrderSend(TradeType TrdTp, long iVol)
        {

            if (iVol > 0)
            {
                TradeResult result = ExecuteMarketOrder(TrdTp, Symbol, iVol, Convert.ToString(Symbol.Code), SL, TP, MPS, Convert.ToString(Symbol.Code));


            }


        }


    }

}




 


@varunthakur.outlook