Replies

cerradus@gmail.com
19 Sep 2018, 14:25

RE:

Panagiotis Charalampous said:

Hi Allan,

Ok I understand now. My suggestion is to program your logic on OnTick() and keep track that an order has been placed in order not to create multiple subsequent orders being placed. You can use a flag for this and reset it whenever you want new orders to be placed.

Best Regards,

Panagiotis

Hi Panagiotis,

got it, I'll do it!! With the API "Positions.Count" is it possible to count the positions by type or only count the total? That would solve my problem.

 

Best Regards,

Allan


@cerradus@gmail.com

cerradus@gmail.com
18 Sep 2018, 21:21

RE:

Panagiotis Charalampous said:

Hi Allan,

I noticed that the code is executed in OnBar() so I would not expect accuracy in the order placement. It is still not clear to me why you do not place the limit order at the exact price you want since you already know the desired entry price beforehand.

Best Regards,

Panagiotis

Dear Panagiotis,

The desired logic is as follows: if an indicator points to a purchase the theft launches the order. But instead go up, the price goes down and then the indicator again points to a purchase. However, the robot should not allow buy again if it is 15 pips (for example) of the previous purchase. Put another way, the robot can not allow successive trades with a distance less than that defined in step. I've seen this mechanism in a robot on this site, the Smart Grid.

Even so, I thank you very much for your clarifications, because I learned a lot.

Greetings,
Allan.


@cerradus@gmail.com

cerradus@gmail.com
18 Sep 2018, 13:31

RE:

Panagiotis Charalampous said:

Hi cerradus@gmail.com,

Can you post your full cBot code? Also, is there a reason that you don't just place the limit order on position opening but you wait the price reach that level? 

Best Regards,

Panagiotis

Dear Panagiotis,

There is a robot here on the site (Norse Piprunner) that has this mechanism that does not allow nearby trades. The problem is that, in addition to the high complexity of the code, it negotiates without indicators making grid purchases and sales.

Follow the code of my bot:

//Brazil - 09/2018 - HS
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 pumpjack : Robot
    {


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

        [Parameter("Take Profit", DefaultValue = 100)]
        public int _takeProfit { get; set; }

        [Parameter("Label", DefaultValue = "HS")]
        public string label { get; set; }

        [Parameter("Pending Hours", DefaultValue = 15, MinValue = 1)]
        public int expira { get; set; }

        [Parameter("Step Last", DefaultValue = 100, MinValue = 1)]
        public int step { get; set; }

        [Parameter("Auto Stop", DefaultValue = 300, MinValue = 1)]
        public int autostop { get; set; }

        [Parameter("TSMA Period", DefaultValue = 14, MinValue = 1, Step = 1)]
        public int TSMAperiod { get; set; }


        private TimeSeriesMovingAverage tsmaopen;
        private TimeSeriesMovingAverage tsmaclose;


        protected override void OnStart()
        {

            tsmaopen = Indicators.TimeSeriesMovingAverage(MarketSeries.Open, TSMAperiod);
            tsmaclose = Indicators.TimeSeriesMovingAverage(MarketSeries.Close, TSMAperiod);

        }


        protected override void OnBar()
        {
            foreach (var position in Positions)
            {
                if (position.Pips < -autostop)
                {
                    ClosePosition(position);
                }
            }

            var posi = Positions.Find(label, Symbol);

            if (posi == null || posi.TradeType == TradeType.Buy && (Symbol.Bid - posi.EntryPrice) / Symbol.PipSize > step)

                if (tsmaclose.Result.LastValue > tsmaopen.Result.LastValue)
                {
                    PlaceLimitOrder(TradeType.Buy, Symbol, Volume, Symbol.Bid, label, null, _takeProfit, Server.Time.AddHours(expira));
                }



            if (posi == null || posi.TradeType == TradeType.Sell && (posi.EntryPrice - Symbol.Bid) / Symbol.PipSize > step)


                if (tsmaclose.Result.LastValue < tsmaopen.Result.LastValue)
                {
                    PlaceLimitOrder(TradeType.Sell, Symbol, Volume, Symbol.Bid, label, null, _takeProfit, Server.Time.AddHours(expira));

                }

        }

    }

}


Best regards,

Allan


@cerradus@gmail.com

cerradus@gmail.com
17 Sep 2018, 23:45 ( Updated at: 21 Dec 2023, 09:20 )

RE:

Panagiotis Charalampous said:

Hi cerradus@gmail.com,

The conditions are wrong in both cases. In the case of Buy position < should become > and in the case of Sell position Symbol.Bid - posi.EntryPrice should become posi.EntryPrice - Symbol.Bid.

Let me know if this helps.

Best Regards,

Panagiotis

Dear Panagiotis,

I made the corrections as per the code below. However, it continues to make trades very close and in excess, even with the addition of the crossing of the Average Time Series Moving Average.

protected override void OnBar()
        {
            var posi = Positions.Find(label, Symbol);

           
            if (posi == null || posi.TradeType == TradeType.Buy && (Symbol.Bid - posi.EntryPrice) / Symbol.PipSize > step)


                if (tsmaclose.Result.LastValue > tsmaopen.Result.LastValue)
                {
                    PlaceLimitOrder(TradeType.Buy, Symbol, Volume, Symbol.Bid, label, null, null, Server.Time.AddHours(expira));
                }


            if (posi == null || posi.TradeType == TradeType.Sell && (posi.EntryPrice - Symbol.Bid) / Symbol.PipSize > step)


                if (tsmaclose.Result.LastValue < tsmaopen.Result.LastValue)
                {
                    PlaceLimitOrder(TradeType.Sell, Symbol, Volume, Symbol.Bid, label, null, null, Server.Time.AddHours(expira));

                }
        }

In the image below it is possible to check the backtest whose distance was set at 28 pips (step) in the optimization.

Best regards,

Allan

 


@cerradus@gmail.com

cerradus@gmail.com
13 Sep 2018, 19:35 ( Updated at: 21 Dec 2023, 09:20 )

RE: RE:

cerradus@gmail.com said:

Panagiotis Charalampous said:

Hi cerradus@gmail.com,

Thanks for posting in our forum. Your conditions for checking the distance are wrong. If you want to check the distance between entry price and current price, you should use something like the below

(Symbol.Bid - posi.EntryPrice) / Symbol.PipSize > Distance

Best Regards,

Panagiotis

 

 

Very thankful Mr. Panagiotis!

 

If not inconvenient, could you explain the meaning of "PipSize"?
I do not understand the values he represents.

 

Cordial greetings,

Allan

 

 

 

 

Dear Panagiotis, although his correction has greatly improved the performance of the robot, orders are still made with less than desired distance. 

Would the error be in the code that finds the last position? "var posi = Positions.Find (label, Symbol);"

The code looks like this:

protected override void OnBar()
        {
            var posi = Positions.Find(label, Symbol);

            
            if (posi == null || posi.TradeType == TradeType.Buy && (Symbol.Bid - posi.EntryPrice) / Symbol.PipSize < step)

               
                {
                    PlaceLimitOrder(TradeType.Buy, Symbol, Volume, Symbol.Bid, label);
                }



            if (posi == null || posi.TradeType == TradeType.Sell && (Symbol.Bid - posi.EntryPrice) / Symbol.PipSize > step)

             
                {
                    PlaceLimitOrder(TradeType.Sell, Symbol, Volume, Symbol.Bid, label);

                }
        }

Thanks in advance


@cerradus@gmail.com

cerradus@gmail.com
10 Sep 2018, 16:07

RE:

Panagiotis Charalampous said:

Hi cerradus@gmail.com,

Thanks for posting in our forum. Your conditions for checking the distance are wrong. If you want to check the distance between entry price and current price, you should use something like the below

(Symbol.Bid - posi.EntryPrice) / Symbol.PipSize > Distance

Best Regards,

Panagiotis

 

 

Very thankful Mr. Panagiotis!

 

If not inconvenient, could you explain the meaning of "PipSize"?
I do not understand the values he represents.

 

Cordial greetings,

Allan

 


@cerradus@gmail.com