Account.MarginLevel
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
Replies
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
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
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
... Deleted by UFO ...
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