Replies

protradefx
20 Sep 2024, 19:41

Hi,

it seems used ee margin is not released after the position is closed. See example below after I have closed 1 positions.  I have got 0 open positions but still I got used margin.

Is this a potential bug?


@protradefx

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

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

protradefx
29 Mar 2018, 23:27 ( Updated at: 21 Dec 2023, 09:20 )

hello, 

it seems there is another issue with modify position in backtesting. Look at this case: I have change the position size to 1K and then closed the postion...look at final result. (I would have expected some partial close btw)

Regards


@protradefx

protradefx
29 Oct 2017, 22:38

Hi Paul,

thank you vey much! ..the 3rd point really fixed my issue.

Regards


@protradefx