Account.MarginLevel

Created at 30 Jun 2023, 04:55
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!
TH

thecaffeinatedtrader

Joined 22.07.2020

Account.MarginLevel
30 Jun 2023, 04:55


Hi, 

I am just wondering if there is a way to set a restriction that stops the bot from entering new trades if the 'Account.MarginLevel' is below a set amount. 

Thank you


@thecaffeinatedtrader
Replies

PanagiotisChar
30 Jun 2023, 09:00

Hi there,

You can just write an if statement before you place the trades and check the margin level

Aieden Technologies

Need help? Join us on Telegram

 


@PanagiotisChar

thecaffeinatedtrader
30 Jun 2023, 12:44

RE:

PanagiotisChar said:

Hi there,

You can just write an if statement before you place the trades and check the margin level

Aieden Technologies

Need help? Join us on Telegram

 

See, I thought so too, but the way I tried to write it was … 

if (xxx && Account.MarginLevel > 250) 

but now the whole Bot fails to take any trades.

i also tried to use a parameter and optimize for a number between 250 - 500 and write,

if (xxx && Account.MarginLevel > MarginRequirement) 

Please help me understand why that doesn’t work. Thank you!


@thecaffeinatedtrader

firemyst
30 Jun 2023, 17:24

//Try this
if (Account.MarginLevel.HasValue && Account.MarginLevel >= MarginRequirement)
  //execute your order
}
else
{
    if (!Account.MarginLevel.HasValue)
        Print("Margin Level has no value.");
    else
       //print out your margin levels so you can actually see what it is
       Print("Margin Level is {0}", Account.MarginLevel);
}

 


@firemyst

thecaffeinatedtrader
30 Jun 2023, 17:42

RE:

firemyst said:

//Try this
if (Account.MarginLevel.HasValue && Account.MarginLevel >= MarginRequirement)
  //execute your order
}
else
{
    if (!Account.MarginLevel.HasValue)
        Print("Margin Level has no value.");
    else
       //print out your margin levels so you can actually see what it is
       Print("Margin Level is {0}", Account.MarginLevel);
}

 

Thanks for your reply, but it's still not entering trades. Do I need to write something so it recognizes percentages rather than integers?

        [Parameter("Margin Level", DefaultValue = 250, MinValue = 250, Group = "Risk Management")]
        public double MarginRequirement { get; set; }

        private void Entry(int index1)
        {            
              {
                    var currentHours = Server.Time.TimeOfDay.TotalHours;
                    bool istimecorrect = currentHours > StartTrading && currentHours < StopTrading;
                    if (!istimecorrect)
                    return;
              
                    {
                        // Buy Only
                        if (Account.MarginLevel.HasValue && Account.MarginLevel > MarginRequirement)
                        {
                              if (xxx)
                                  {
                                  ExecuteMarketOrder(TradeType.Buy, SymbolName, VolumeInUnits, InstanceName, StopLoss, TakeProfit);
                                  }

 


@thecaffeinatedtrader

firemyst
30 Jun 2023, 17:46

How do you even know it's getting past this to enter trades?

 

if (!istimecorrect)
                    return;

 

????

 

You need to put Print statements to see if it even gets past that return statement.


@firemyst

ncel01
30 Jun 2023, 18:54

Hello,

The most likely reason why no trades are being placed:

When there are no open positions, Account.MarginLevel == null, which has a logic value eqivalent to !Account.MarginLevel.HasValue, as it assumes an infinite (NaN) value, therefore, both the conditionals Account.MarginLevel >= MarginRequirement and Account.MarginLevel.HasValue are false.

Try one of the following options, they must be equivalent:

if (Account.MarginLevel >= MarginRequirement || Account.MarginLevel == null)
  // place a trade
}
else
{
  // do not place a trade
}

Alternative:

if (Account.MarginLevel >= MarginRequirement || !Account.MarginLevel.HasValue)
  // place a trade
}
else
{
  // do not place a trade
}

 


@ncel01

thecaffeinatedtrader
30 Jun 2023, 19:21

RE:

ncel01 said:

Hello,

The most likely reason why no trades are being placed:

When there are no open positions, Account.MarginLevel == null, which has a logic value eqivalent to !Account.MarginLevel.HasValue, as it assumes an infinite (NaN) value, therefore, both the conditionals Account.MarginLevel >= MarginRequirement and Account.MarginLevel.HasValue are false.

Try one of the following options, they must be equivalent:

if (Account.MarginLevel >= MarginRequirement || Account.MarginLevel == null)
  // place a trade
}
else
{
  // do not place a trade
}

Alternative:

if (Account.MarginLevel >= MarginRequirement || !Account.MarginLevel.HasValue)
  // place a trade
}
else
{
  // do not place a trade
}

 

That worked to help enter the trade. But now instead of holding the current position and not entering new trades when below the set 'marginrequirement', it exits the already open position.


@thecaffeinatedtrader

ncel01
30 Jun 2023, 19:34

Hi,

That should be related to the instructions you're providing inside the conditionals.

It is hard to help without looking at the remaining code.


@ncel01

... Deleted by UFO ...