Topics
10 May 2019, 10:03
 2445
 11
27 Nov 2018, 07:18
 2793
 6
Replies

alexander.n.fedorov
03 Oct 2018, 13:01 ( Updated at: 21 Dec 2023, 09:20 )


@alexander.n.fedorov

alexander.n.fedorov
03 Oct 2018, 12:48

How about a minimum allowed price change?

 


@alexander.n.fedorov

alexander.n.fedorov
03 Oct 2018, 12:34

Worst comes to worst I will have to 


@alexander.n.fedorov

alexander.n.fedorov
02 Oct 2018, 16:54

I backtest from the first Fondex tick date, which is the 29 th of November

But I recognize the trade, the candles

With you it worked OK

I needed this release, as I could make some very important improvements and philosophy.

I hope it is going to be a bomb!

 


@alexander.n.fedorov

alexander.n.fedorov
02 Oct 2018, 16:46

Yes, that is the trade

I did not have, even I restarted computer a few times

I showed it to my daughter, we looked together, could not understand

May be I shall reinstall the Fondex

By the way, when the beta is avail on other brokers, is the data going to as limited as well?

Regards

Sasha

P.S. Good news anyway, I started to worry about my abilities


@alexander.n.fedorov

alexander.n.fedorov
02 Oct 2018, 16:19

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.RussianStandardTime, AccessRights = AccessRights.None)]
    public class FAN23 : Robot
    {
        #region Parameters public
        [Parameter("Source")]
        public DataSeries Source { get; set; }

        [Parameter("Minimum Equity", DefaultValue = 1500, MinValue = 1000, Step = 100)]
        public double MinEquity { get; set; }


        [Parameter("Bollinger Bands Deviations", DefaultValue = 2.0, MinValue = 1.8, MaxValue = 2.4, Step = 0.1)]
        public double Deviations { get; set; }

        [Parameter("Bollinger Bands Periods", DefaultValue = 40, Step = 1)]
        public int Periods { get; set; }

        [Parameter("MA Periods", DefaultValue = 200, Step = 1)]
        public int MaPeriods { get; set; }

        [Parameter("GridStep in pips", MinValue = 20, MaxValue = 70, DefaultValue = 20, Step = 5)]
        public double GridStep { get; set; }

        [Parameter("Close on profit %", DefaultValue = 1, Step = 1, MinValue = 1, MaxValue = 10)]
        public double ProfitClose { get; set; }

        [Parameter("Tighten steps after ", DefaultValue = 8, MinValue = 3, Step = 1)]
        public int TightenSteps { get; set; }

        [Parameter("Close on Drawdonw %", DefaultValue = 35, Step = 1, MinValue = 5, MaxValue = 50)]
        public double DrawDownClose { get; set; }

        [Parameter("Stop Trading Hour", DefaultValue = 23, Step = 1)]
        public int StopHour { get; set; }

        [Parameter("Stop Trading minutes", DefaultValue = 45, Step = 1)]
        public int StopMinutes { get; set; }

        [Parameter("Open Trades Hour", DefaultValue = 2, Step = 1, MinValue = 0)]
        public int OpenTradesHour { get; set; }
        #endregion

        #region Parameters private

        private const int GridsExponent = 2;
        private string Instance;
        private MovingAverage ma;
        private MovingAverage BBma;
        private double volume, minVolume, accountBalance, equity, profit, gridStep;
        private BollingerBands BB;
        private double shortPrice, longPrice;
        private int totalLongPositions, totalShortPositions;

        #endregion

        #region cBot onStart
        protected override void OnStart()
        {
            accountBalance = 0;
            accountBalance = Account.Balance > accountBalance ? Account.Balance : accountBalance;
            Instance = ToString() + ", " + Symbol.Code + ", " + TimeFrame.ToString() + ", " + Account.BrokerName + ", " + Account.Number.ToString();
            BB = Indicators.BollingerBands(Source, Periods, Deviations, MovingAverageType.Simple);
            ma = Indicators.MovingAverage(Source, MaPeriods, MovingAverageType.Simple);
            BBma = Indicators.MovingAverage(Source, Periods, MovingAverageType.Triangular);
            minVolume = 1000;
            gridStep = GridStep * Symbol.PipSize;
            equity = Account.Equity;
            Print("Instance = {0}, Account Balanace = {1}, Account Equity = {2}, volume = {3}", Instance, Account.Balance, Account.Equity, volume);
        }
        #endregion

        #region onBar
        protected override void OnBar()
        {
            if ((MarketSeries.OpenTime.Last(0).Hour >= StopHour && MarketSeries.OpenTime.Last(0).Minute >= StopMinutes) || (MarketSeries.OpenTime.Last(0).Hour <= OpenTradesHour))
            {
                return;
            }
            TradeManagement();
            if (volume == 0)
            {
                return;
            }
            totalShortPositions = Positions.FindAll(Instance, Symbol, TradeType.Sell).Count();
            totalLongPositions = Positions.FindAll(Instance, Symbol, TradeType.Buy).Count();

            #region short entries
            if (MarketSeries.Close.Last(2) > BB.Top.Last(2))
            {
                if (MarketSeries.Close.Last(1) < BB.Top.Last(1) && MarketSeries.Low.Last(1) > BB.Main.Last(1))
                {
                    if (totalShortPositions == 0)
                    {
                        var orderVolume = volume * Math.Pow(GridsExponent, totalShortPositions);
                        var result = ExecuteMarketOrder(TradeType.Sell, Symbol, orderVolume, Instance);
                        if (result.IsSuccessful)
                        {
                            shortPrice = result.Position.EntryPrice;
                            totalShortPositions = totalShortPositions + 1;
                            Print("Short Price = {0}, gridStep = {1}, MarketSeries.Close.Last(1) = {2}", shortPrice, gridStep, MarketSeries.Close.Last(1));
                        }

                    }
                    if (totalShortPositions > 0)
                    {
                        if (MarketSeries.Close.Last(1) > shortPrice + gridStep)
                        {
                            var orderVolume = volume * Math.Pow(GridsExponent, totalShortPositions);
                            var result = ExecuteMarketOrder(TradeType.Sell, Symbol, orderVolume, Instance);
                            if (result.IsSuccessful)
                            {
                                shortPrice = result.Position.EntryPrice;
                                totalShortPositions = totalShortPositions + 1;
                                Print("Short Price = {0}, gridStep = {1}, MarketSeries.Close.Last(1) = {2}", shortPrice, gridStep, MarketSeries.Close.Last(1));
                            }
                        }
                    }
                }
            }
            #endregion
            #region long entries
            if (MarketSeries.Close.Last(2) < BB.Bottom.Last(2))
            {
                if (MarketSeries.Close.Last(1) > BB.Bottom.Last(1) && MarketSeries.High.Last(1) < BB.Main.Last(1))
                {
                    if (totalLongPositions == 0)
                    {
                        var orderVolume = volume * Math.Pow(GridsExponent, totalLongPositions);
                        var result = ExecuteMarketOrder(TradeType.Buy, Symbol, orderVolume, Instance);
                        if (result.IsSuccessful)
                        {
                            longPrice = result.Position.EntryPrice;
                            totalLongPositions = totalLongPositions + 1;
                            Print("Long Price = {0}, gridStep = {1}, MarketSeries.Close.Last(1) = {2}, totalLongPositions = {3}", longPrice, gridStep, MarketSeries.Close.Last(1), totalLongPositions);
                        }
                    }
                    if (totalLongPositions > 0)
                    {
                        if (MarketSeries.Close.Last(1) < longPrice - gridStep)
                        {
                            var orderVolume = volume * Math.Pow(GridsExponent, totalLongPositions);
                            var result = ExecuteMarketOrder(TradeType.Buy, Symbol, orderVolume, Instance);
                            if (result.IsSuccessful)
                            {
                                longPrice = result.Position.EntryPrice;
                                totalLongPositions = totalLongPositions + 1;
                                Print("Long Price = {0}, gridStep = {1}, MarketSeries.Close.Last(1) = {2}, totalLongPositions = {3}", longPrice, gridStep, MarketSeries.Close.Last(1), totalLongPositions);
                            }
                        }

                    }
                }
            }
            #endregion

            #region hedge difference
            #region hedge with short
            if (totalLongPositions - totalShortPositions >= 4)
            {
                var orderVolume = volume * Math.Pow(GridsExponent, totalShortPositions + 1);
                var result = ExecuteMarketOrder(TradeType.Sell, Symbol, orderVolume, Instance, nullnull);
                if (result.IsSuccessful)
                {
                    totalShortPositions = totalShortPositions + 1;
                }
            }
            #endregion
            #region hedge with long
            if (totalShortPositions - totalLongPositions >= 4)
            {
                var orderVolume = volume * Math.Pow(GridsExponent, totalLongPositions + 1);
                var result = ExecuteMarketOrder(TradeType.Buy, Symbol, orderVolume, Instance, nullnull);
                if (result.IsSuccessful)
                {
                    totalLongPositions = totalLongPositions + 1;
                }
            }
            #endregion
            #endregion

            #region close multiple position on opposite
            #region close long
            if (MarketSeries.High.Last(1) >= BB.Top.Last(1))
            {
                if (totalLongPositions > 0 && totalShortPositions == 0)
                {
                    var positions = Positions.FindAll(Instance, Symbol, TradeType.Buy);
                    foreach (var position in positions)
                    {
                        if (position.NetProfit > 0)
                        {
                            ClosePositionAsync(position);
                            totalLongPositions = totalLongPositions - 1;
                        }
                    }
                    if (totalLongPositions == 0)
                    {
                        longPrice = 0;
                    }
                    longPrice = positions.OrderByDescending(x => x.Id).Last().EntryPrice;
                }
            }
            #endregion

            #region close short
            if (MarketSeries.Low.Last(1) <= BB.Bottom.Last(1))
            {
                if (totalLongPositions == 0 && totalShortPositions > 0)
                {
                    var positions = Positions.FindAll(Instance, Symbol, TradeType.Sell);
                    foreach (var position in positions)
                    {
                        if (position.NetProfit > 0)
                        {
                            ClosePositionAsync(position);
                            totalShortPositions = totalShortPositions - 1;
                        }
                    }
                    if (totalShortPositions == 0)
                    {
                        shortPrice = 0;
                    }
                    shortPrice = positions.OrderByDescending(x => x.Id).Last().EntryPrice;
                }
            }
            #endregion
            #endregion

            #region close bunch of same positions
            #region close longs
            if (totalLongPositions > 0 && totalShortPositions == 0)
            {
                var positions = Positions.FindAll(Instance, Symbol, TradeType.Buy);
                profit = 0;
                foreach (var position in positions)
                {
                    profit = profit + position.NetProfit;
                }
                if (profit > 0)
                {
                    foreach (var position in positions)
                    {
                        ClosePositionAsync(position);
                    }
                }
                longPrice = 0;
            }
            #endregion
            #region close shorts
            if (totalShortPositions > 0 && totalLongPositions == 0)
            {
                var positions = Positions.FindAll(Instance, Symbol, TradeType.Sell);
                profit = 0;
                foreach (var position in positions)
                {
                    profit = profit + position.NetProfit;
                }
                if (profit > 0)
                {
                    foreach (var position in positions)
                    {
                        ClosePositionAsync(position);
                    }
                }
                shortPrice = 0;
            }
            #endregion
        }
        #endregion
        #endregion

        #region tradeManagement
        private void TradeManagement()
        {
            var totalPositions = Positions.FindAll(Instance);
            if (totalPositions.Count() == 0)
            {
                equity = Account.Equity;
                var newVolume = Math.Floor(equity / MinEquity) * minVolume;
                volume = newVolume;
                return;

            }
            if (totalPositions.Count() > 0)
            {
                profit = 0;
                foreach (var position in Positions.FindAll(Instance))
                {
                    profit = profit + position.NetProfit;
                }
                if (profit > equity * ProfitClose / 100 || profit < -equity * DrawDownClose / 100)
                {
                    foreach (var position in Positions.FindAll(Instance))
                    {
                        ClosePositionAsync(position);
                    }
                    Print("closed all");
                    equity = Account.Equity;
                    shortPrice = 0;
                    longPrice = 0;
                }
            }
        }
        #endregion
    }
}


@alexander.n.fedorov

alexander.n.fedorov
02 Oct 2018, 16:18

Just a minute

 


@alexander.n.fedorov

alexander.n.fedorov
02 Oct 2018, 16:10 ( Updated at: 21 Dec 2023, 09:20 )

No, the candlestick I am pointing to is green color, it is bullish, it opens below and closes above BB bottom. Just try to increase the picture


@alexander.n.fedorov

alexander.n.fedorov
02 Oct 2018, 15:56 ( Updated at: 21 Dec 2023, 09:20 )


@alexander.n.fedorov

alexander.n.fedorov
02 Oct 2018, 14:35

Please, help! what do I do wrong?

Regards,

Sasha


@alexander.n.fedorov

alexander.n.fedorov
26 Sep 2018, 12:04

I calculate on Tick availabel margin, before I plasce market order

So, the

"

private void CalculatePosition()

{

}

"

mast check the dynamic leverage

 


@alexander.n.fedorov

alexander.n.fedorov
26 Sep 2018, 11:59

So , it was a misprint in API reference?

 


@alexander.n.fedorov

alexander.n.fedorov
26 Sep 2018, 11:57

Thank you Panagiotis, and let God me be in large positions


@alexander.n.fedorov

alexander.n.fedorov
24 Sep 2018, 10:56

Panagiotis!

Such a pity. Should not be difficult to implement, but very helpful!

Regards

Sasha

P.S. You guys made a fantasic visusalization (I saw it on Fondex). The only problem - there is no data on Fondex


@alexander.n.fedorov

alexander.n.fedorov
17 Sep 2018, 20:50

sorry for misprints . 

"

I coulds use...

"


@alexander.n.fedorov

alexander.n.fedorov
17 Sep 2018, 20:49

Thank you for you very clear answer, Paul

I used to do some programming in VBA for Excel.
If I had and array of data I could loop through the data. It would take some time and processor power if aray is big enoungh

At the same time I could you some standart Excel functions (like "Match" or "Index" for example) and they would work many times faster

As I am new to c# and especially it is virtually impossible to find a textbook for this API,  I was assumning, maybe there is a standart function.

But thank you  anyway!

Regards,

Sasha


@alexander.n.fedorov

alexander.n.fedorov
17 Sep 2018, 14:09

Thank you.


@alexander.n.fedorov

alexander.n.fedorov
17 Sep 2018, 13:06

It is strange . In MT4 very convinient to drag and drop

But thank you anyway


@alexander.n.fedorov

alexander.n.fedorov
06 Sep 2018, 12:57

Hi, Panagiotis,

Thanks for reply ,

Unfortunately it does not work

 

Regards, 

Sahsa


@alexander.n.fedorov

alexander.n.fedorov
06 Sep 2018, 12:56

Hi, Panagiotis, 

Thanks for the reply

Unfortunately, it does not work

Regards, 

Sasha


@alexander.n.fedorov