Code: Select all
void SellPendingOrderWithRiskFixed2()
{
double lotsize = MarketInfo(Symbol(),MODE_LOTSIZE) / AccountLeverage();
double pipsize = 1 * 10;
double maxlots = AccountFreeMargin() / 100 * BalanceRiskPercent2 / lotsize * pipsize;
if (SellStoploss2 == 0) Print("OrderSend() error - stoploss can not be zero");
double lots = maxlots / SellStoploss2 * 10;
// calculate lot size based on current risk
double lotvalue = 0.001;
double minilot = MarketInfo(Symbol(), MODE_MINLOT);
int powerscount = 0;
while (minilot < 1)
{
minilot = minilot * MathPow(10, powerscount);
powerscount++;
}
lotvalue = NormalizeDouble(lots, powerscount - 1);
if (lotvalue < MarketInfo(Symbol(), MODE_MINLOT)) // make sure lot is not smaller than allowed value
{
lotvalue = MarketInfo(Symbol(), MODE_MINLOT);
}
if (lotvalue > MarketInfo(Symbol(), MODE_MAXLOT)) // make sure lot is not greater than allowed value
{
lotvalue = MarketInfo(Symbol(), MODE_MAXLOT);
}
int expire = TimeCurrent() + 60 * 60;
double price = NormalizeDouble(Bid, NDigits) - PriceOffset2*PipValue*Point;
double SL = price + SellStoploss2*PipValue*Point;
if (SellStoploss2 == 0) SL = 0;
double TP = price - SellTakeprofit2*PipValue*Point;
if (SellTakeprofit2 == 0) TP = 0;
if (60 == 0) expire = 0;
int ticket = OrderSend(Symbol(), OP_SELLSTOP, lotvalue, price, 4, SL, TP, "EA Sell 3", 3, expire, Red);
if (ticket == -1)
{
Print("OrderSend() error - ", ErrorDescription(GetLastError()));
}
}