Distance of the last executed order

Created at 06 Sep 2018, 17:29
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
CE

cerradus@gmail.com

Joined 20.12.2017

Distance of the last executed order
06 Sep 2018, 17:29


Dear,

I would like the code below to only place an order if it is a certain distance from the last order executed in the same direction (buy or sell):

 

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

           if (posi == null || posi.TradeType == TradeType.Buy && posi.Pips > 10 * Symbol.Bid)

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


            if (posi == null || posi.TradeType == TradeType.Sell && posi.Pips < 10 * Symbol.Bid)
                
                {
                    PlaceLimitOrder(TradeType.Sell, Symbol, Volume, Symbol.Bid, label);

                }
             }
         }

However, he continues to make new orders very close to the old ones. 

I already changed "Positions.Find(label, Symbol);" for "Hisory.FindLast (label, Symbol);"

I also tested "posi.EntryPrice > 10 * Symbol.Bid", "posi.Pips > 10 * Symbol.PipSize" and "posi.EntryPrice > 10 * Symbol.PipSize".

Even so, I did not succeed. Please kindly help me with this section. Thanks in advance.

 

 


@cerradus@gmail.com
Replies

PanagiotisCharalampous
07 Sep 2018, 10:18

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


@PanagiotisCharalampous

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

PanagiotisCharalampous
10 Sep 2018, 16:12

Hi Allan,

Symbol.PipSize represents the absolute size of each pip. For example, for EURUSD it is 0.0001. If the prize moves up by five pips then it has moved by 0.0005.

Best Regards,

Panagiotis


@PanagiotisCharalampous

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

PanagiotisCharalampous
17 Sep 2018, 12:43

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


@PanagiotisCharalampous

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

PanagiotisCharalampous
18 Sep 2018, 09:32

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


@PanagiotisCharalampous

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

PanagiotisCharalampous
18 Sep 2018, 14:12

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


@PanagiotisCharalampous

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

PanagiotisCharalampous
19 Sep 2018, 10:46

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


@PanagiotisCharalampous

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

PanagiotisCharalampous
19 Sep 2018, 14:31

Hi Allan,

See below

Positions.Count(x => x.TradeType == TradeType.Buy);

Best Regards,

Panagiotis


@PanagiotisCharalampous