Spread

Created at 24 Jan 2018, 00: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!
DR

Drummond360

Joined 22.12.2017

Spread
24 Jan 2018, 00:29


Hi All,

I using the code below to open and clsoe positions upon spread size. As you can see the MaxSpread parameter is set to 1 so it should open positions when the spread is < 1 and close them when the spread is > 1 but it isn't respecting these levels.

The screen shot shows the trade log with all the spread is regularly above 1 and I can confirm that it also prints the spread ot be regularly below 1...

Can anyone spot the fault in my code, it seems too simple to get wrong!

 

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class Hedging2positionsonly : Robot
    {

        [Parameter("Max Spread", DefaultValue = 0.0, MinValue = -5.0)]
        public double MaxSpread { get; set; }

        string label = "Spread Test";

        protected override void OnStart()
        {

        }
        protected override void OnTick()
        {
            var longPosition = Positions.Find(label, Symbol, TradeType.Buy);
            var shortPosition = Positions.Find(label, Symbol, TradeType.Sell);
            //var spread = Symbol.Spread / Symbol.PipSize;
            var spread = Symbol.Ask - Symbol.Bid;
            //var spread = Math.Round(Symbol.Spread / Symbol.PipSize, 1);
            Print("Spread is " + spread + " Symbol.Spread Value = " + Symbol.Spread);
            {

                if (shortPosition == null && spread <= MaxSpread)
                {
                    ExecuteMarketOrder(TradeType.Sell, Symbol, 1000, label);

                    Print(" enterered short, var spread is " + spread + " MaxSpread = " + MaxSpread);


                }
                if (longPosition == null && spread <= MaxSpread)
                {
                    ExecuteMarketOrder(TradeType.Buy, Symbol, 1000, label);

                    Print(" entered long, var spread is " + spread + " MaxSpread = " + MaxSpread);
                }
            }


            var symbolSearch = Positions.FindAll(label, Symbol);

            if (spread >= MaxSpread)
            {
                foreach (Position position in symbolSearch)
                {
                    ClosePosition(position);
                    Print("Exit because Spread @ " + spread + " closing positions");
                }

            }
        }
    }
}

 

Many thanks in advance, Drummond...


@Drummond360
Replies

PanagiotisCharalampous
24 Jan 2018, 09:28

Hi Drummond,

What does Spread > 1 means? Bigger than one pip? Because I don't think you mean it in absolute terms...

Best Regards,

Panagiotis


@PanagiotisCharalampous

jani
09 Nov 2019, 20:29 ( Updated at: 21 Dec 2023, 09:21 )

RE:

Drummond360 said:

Hi All,

I using the code below to open and clsoe positions upon spread size. As you can see the MaxSpread parameter is set to 1 so it should open positions when the spread is < 1 and close them when the spread is > 1 but it isn't respecting these levels.

The screen shot shows the trade log with all the spread is regularly above 1 and I can confirm that it also prints the spread ot be regularly below 1...

Can anyone spot the fault in my code, it seems too simple to get wrong!

 

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class Hedging2positionsonly : Robot
    {

        [Parameter("Max Spread", DefaultValue = 0.0, MinValue = -5.0)]
        public double MaxSpread { get; set; }

        string label = "Spread Test";

        protected override void OnStart()
        {

        }
        protected override void OnTick()
        {
            var longPosition = Positions.Find(label, Symbol, TradeType.Buy);
            var shortPosition = Positions.Find(label, Symbol, TradeType.Sell);
            //var spread = Symbol.Spread / Symbol.PipSize;
            var spread = Symbol.Ask - Symbol.Bid;
            //var spread = Math.Round(Symbol.Spread / Symbol.PipSize, 1);
            Print("Spread is " + spread + " Symbol.Spread Value = " + Symbol.Spread);
            {

                if (shortPosition == null && spread <= MaxSpread)
                {
                    ExecuteMarketOrder(TradeType.Sell, Symbol, 1000, label);

                    Print(" enterered short, var spread is " + spread + " MaxSpread = " + MaxSpread);


                }
                if (longPosition == null && spread <= MaxSpread)
                {
                    ExecuteMarketOrder(TradeType.Buy, Symbol, 1000, label);

                    Print(" entered long, var spread is " + spread + " MaxSpread = " + MaxSpread);
                }
            }


            var symbolSearch = Positions.FindAll(label, Symbol);

            if (spread >= MaxSpread)
            {
                foreach (Position position in symbolSearch)
                {
                    ClosePosition(position);
                    Print("Exit because Spread @ " + spread + " closing positions");
                }

            }
        }
    }
}

 

Many thanks in advance, Drummond...

I think  you just need to add:

 if (shortPosition == null && spread <= MaxSpread*Symbol.PipSize)

 


@jani