Distance of the last executed order

06 Sep 2018, 17:29Distance of the last executed order#1
cerradus@gmail.composts: 7since: 20 Dec 2017

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.

 

 

07 Sep 2018, 10:18#2
Panagiotis Charalampousposts: 1360since: 13 Jan 2017

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


Head of Community Management at cTrader
10 Sep 2018, 16:07RE:#3
cerradus@gmail.composts: 7since: 20 Dec 2017

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

 

10 Sep 2018, 16:12#4
Panagiotis Charalampousposts: 1360since: 13 Jan 2017

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


Head of Community Management at cTrader
13 Sep 2018, 19:35RE: RE:#5
cerradus@gmail.composts: 7since: 20 Dec 2017

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

17 Sep 2018, 12:43#6
Panagiotis Charalampousposts: 1360since: 13 Jan 2017

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


Head of Community Management at cTrader
17 Sep 2018, 23:45RE:#7
cerradus@gmail.composts: 7since: 20 Dec 2017

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

 

18 Sep 2018, 09:32#8
Panagiotis Charalampousposts: 1360since: 13 Jan 2017

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


Head of Community Management at cTrader
18 Sep 2018, 13:31RE:#9
cerradus@gmail.composts: 7since: 20 Dec 2017

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

18 Sep 2018, 14:12#10
Panagiotis Charalampousposts: 1360since: 13 Jan 2017

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


Head of Community Management at cTrader