Calculated Position Volume doesn't work

Created at 04 Jun 2016, 13:51
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!
JE

jeremy.vostik

Joined 13.05.2016

Calculated Position Volume doesn't work
04 Jun 2016, 13:51


Hi,

 

I've searched and copied a Little code from here and there and cam up with the following:

 

 
        [Parameter("Risk Percent", DefaultValue = 0.5, MinValue = 0.1, MaxValue = 5.0)]
        public double RiskPercent { get; set; }

        [Parameter("Max Volume", DefaultValue = 5000000)]
        public int MaxVolume { get; set; }

        private int _volume;

 

 

void OnBar()

 

            {
                CalculateVolume();
            }

 

 

 

if ( longPosition.Length < MaxLongTrades)
            {

                {

                 ExecuteMarketOrder(TradeType.Buy, Symbol, _volume, label, StopLoss, TakeProfit);
                 }


            }

 

 

 

//AT THE END OF MY SCRIPT

private void CalculateVolume()
        {
            double openingBalance = Account.Balance;

            double riskPerTrade = (openingBalance * RiskPercent) / 100;

            double exactVolume = Math.Round(riskPerTrade / (Symbol.PipValue * 20), 2);
            _volume = (((int)exactVolume) / 100000) * 100000;

            if (_volume > MaxVolume)
            {
                _volume = MaxVolume;
            }

            
        }

 

//END

 

 

 

As I see it the volume should be calculated correctly. It seems like the ExecuteOrder has a Problem when it has "_volume" instead of the usual "Volume" in it.

 

It does compile, but no trades open.

Anybody knows why that is?

Obviously I am trying to get a dynamic Volume size based on a set risk percentage and account balance dependent.

Should be a simple task...


@jeremy.vostik
Replies

whis.gg
04 Jun 2016, 14:27

Hi, you might want to try my method. It doesn't include commissions though.

private long Volume(int riskPercent, int stopLossPips)
{
    double risked = Account.Balance * riskPercent / 100;
    double volume = risked / stopLossPips / Symbol.PipValue;
    return Symbol.NormalizeVolume(volume, RoundingMode.ToNearest);
}

 


@whis.gg

whis.gg
04 Jun 2016, 14:27

RE:

tmc. said:

Hi, you might want to try my method. It doesn't include commissions though.

private long Volume(int riskPercent, int stopLossPips)
{
    double risked = Account.Balance * riskPercent / 100;
    double volume = risked / stopLossPips / Symbol.PipValue;
    return Symbol.NormalizeVolume(volume, RoundingMode.ToNearest);
}

 

 


@whis.gg

jeremy.vostik
04 Jun 2016, 16:51

RE: RE:

tmc. said:

tmc. said:

Hi, you might want to try my method. It doesn't include commissions though.

private long Volume(int riskPercent, int stopLossPips)
{
    double risked = Account.Balance * riskPercent / 100;
    double volume = risked / stopLossPips / Symbol.PipValue;
    return Symbol.NormalizeVolume(volume, RoundingMode.ToNearest);
}

Can you please tem me where I paste this code? OnStart? OnBar?

Writing it anywhere gibes me 23 Errors.

 

 


@jeremy.vostik

jeremy.vostik
04 Jun 2016, 16:55

Just tried posting it into the private void CalculateVolume() section.

Only 2 Errors now.

} expected

and

Type or Nnamespace Definition, or end-of-file expected.


@jeremy.vostik

whis.gg
04 Jun 2016, 17:16

RE:

It's method. You put it anywhere in the robot class and call it only when needed. Sample:

ExecuteMarketOrder(TradeType.Buy, Symbol, Volume(2, 100), "Risk 2%, S/L 100pips", 100, 200);

 


@whis.gg

jeremy.vostik
04 Jun 2016, 19:23

Okay, but how do I call it exactly?

Sorry I am stupid.

 

This is how I have it.

ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, label, StopLoss, TakeProfit);

How do I implement the new "Returned" volume in that?

 

 


@jeremy.vostik