ModifyPosition API 3.0 Backtesting

Created at 10 Jul 2018, 21:46
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!
PR

protradefx

Joined 26.10.2017

ModifyPosition API 3.0 Backtesting
10 Jul 2018, 21:46


Hi,

I have some question related to the ModifyPosition API 3.0 coming from some backtests done so far.

  1. how is it possible to figure out upfront the (gross/net) amount impact on the balance due to a decrease of the total position volume?
  2. How is possible to calculate the VWAP of a postion which has been changed in volume?

Furtheremore please have a look at the following test case. I open a sell position and I take some profit over the time (I decrease the volume). It seems though there is one problem with the net/gross amount. Apparently in History the close price is correct but the entry price is like that of the position 1.17821 (but it is wrong as I have modified the position volume at different price level). I guess the net/gross amount is calculated using the difference in ticks from entry and close prices giving incorrect results.

Regards


@protradefx
Replies

PanagiotisCharalampous
16 Jul 2018, 14:48

Hi Daniele,

The entry price of a position does not change if you close a part of it. If for example you open a EURUSD position of €10k at 1.10, when you close €5k it doesn't change the fact that the remaining €5k have been entered at 1.10.

Best Regards,

Panagiotis


@PanagiotisCharalampous

protradefx
26 Jul 2018, 21:36 ( Updated at: 21 Dec 2023, 09:20 )

ModifyPosition API 3.01 Backtesting

Hi,

I have moved to 3.01 and still i guess there are some issues with change in position volume (ModifyPosition API). Here is my understanding so far after some tests, please confirm to better understand the logic behind.

  1. EntryPrice on a positionis is actually the vwap of the position. EntryPrice is updated every time there is a change in volume (positive/negative). 
  2. If I decrease the volume of an open position when the position NetProfit > 0 I should expect a positive pnl (ex for buy position is pnl = (Symbol.BidPrice - EntryPrice) * Volume) and negatie otherwise

Please see below, there are many (Sell) position  closed with a negaive pnl even though the delta pips is negative (therefore I shoud expect a positive pnl). Buy Position are ok instead.


@protradefx

PanagiotisCharalampous
27 Jul 2018, 09:51

Hi Daniele.

  1. Entry price is increased only when you increase the size of the positions, thus adding new new deals to the position at different entry prices. When you close deals, entry price is not affected.
  2. True.
  3. From a first sight at the screenshot, there seems to be an issue. Can you please send me a cBot and backtesting parameters so that I can reproduce and investigate?

Best Regards,

Panagiotis


@PanagiotisCharalampous

protradefx
30 Jul 2018, 23:11

Hi,

probably the issue is now under investigation (https://ctrader.com/forum/whats-new/13492?page=1#8) by the way here is the bug I was referring too.

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

namespace cAlgo
{

    //this is an example on how margin is calculated, Precondition for testing is open at leas one position
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class Test_PosiionSize : Robot
    {
        protected override void OnStart()
        {

        }


        protected override void OnBar()
        {
            var pos = this.Positions.Find("root");

            //take profit
            if (pos != null && pos.Pips > 5)
            {
                double vol_released = this.Symbol.NormalizeVolumeInUnits(pos.VolumeInUnits * 0.1);
                if (vol_released > this.Symbol.VolumeInUnitsMin)
                {
                    TradeResult tr = pos.ModifyVolume(pos.VolumeInUnits - vol_released);
                    var last = this.History[this.History.Count - 1];
                    if (last.NetProfit < 0)
                    {
                        Print("##error: last closed position should have a positive pnl: {0}", last.NetProfit);
                        this.Stop();
                    }
                }
            }


            if (this.Positions.Count == 0)
            {
                this.ExecuteMarketOrder(TradeType.Sell, this.Symbol, this.Symbol.VolumeInUnitsMin, "root");
            }
            else
            {
                if (pos.Pips < 0)
                {
                    double vol_delta = this.Symbol.NormalizeVolumeInUnits(this.Account.FreeMargin * 0.01 * this.Account.PreciseLeverage);
                    if (vol_delta > this.Symbol.VolumeInUnitsMin)
                    {
                        TradeResult tr = pos.ModifyVolume(pos.VolumeInUnits + vol_delta);
                    }

                }

            }

        }

    }

}

 


@protradefx

PanagiotisCharalampous
23 Aug 2018, 15:03

Hi Daniele,

We have released an update today that should have fixed this issue.

Best Regards,

Panagiotis


@PanagiotisCharalampous