Questions from Beginners MQL5 MT5 MetaTrader 5 - page 1415
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Whereby you can of course change the 0.9 to (1 - maxRisk) and implement a "Maximum Risk in %" property that can be set by the user.
P.S. Do you use the first name or the last name here in the forum?Thanks for the information. In The checks a trading robot must pass a method "CheckMoneyForTrade" is mentioned, which checks whether the margin to be used for the trade is greater than the free margin (ACCOUNT_FREE_MARGIN). If I have understood you correctly, then you should also check whether the funds used also exceed a certain percentage of the account balance, i.e. something like:
Whereby you can of course change the 0.9 to (1 - maxRisk) and implement a "Maximum Risk in %" property that can be set by the user.
P.S. Are we on first-name terms or first-name terms here in the forum?Everyone stumbles over the lot size and there is a lot of code and comments here!
I have now puzzled together what William Roeder wrote for MT5, that would be the untested(!!) function:
(https://www.mql5.com/en/forum/133792/page3#comment_3405179 & https://www.mql5.com/en/forum/390088#comment_28092477)
Please let me know if you find a mistake!
Since most people here are somehow anonymous with their nicknames or first names and that used to be even more the case, the "Du" has become naturalised.
Thanks for the code. The only thing missing was the adaptation to a valid intermediate lot size:
I have another question: If I calculate the loss of a stopped out trade with TICK_VALUE or with deltaValue as above, the result is always slightly different from the MQL5 internal calculation:
Is this because the loss/gain per tick is different for a EUR account and for market price 1.0 and market price 1.2? Because if I enter, for example, "price + 0.1" instead of price and "sl + 0.1" instead of sl in the code below, then the tick distance (diff) is the same, but the respective results output by OrderCalcProfit are different. And when I look at the profits/losses I get from the manual trades, the latter seems to be confirmed.
If I am right, then the following code would be more accurate:
P.S. Thanks again for the basic idea: tying the risk factor not to the balance but to the free margin clearly makes sense.The following code was created according to mql 5's own expert parabolic sar. Can I reverse the operation of the buy signal and the sell signal here? That is, when the buy signal comes, I want it to open sales instead of buying. can anyone help?
#property copyright "Copyright 2022, MetaQuotes Ltd."
#property link "https://www.mql5.com"
#property version "1.00"
//+------------------------------------------------------------------+
//| Include |
//+------------------------------------------------------------------+
#include <Expert\Expert.mqh>
//--- available signals
#include <Expert\Signal\SignalSAR.mqh>
//--- available trailing
#include <Expert\Trailing\TrailingNone.mqh>
//--- available money management
#include <Expert\Money\Money\MoneyFixedLot.mqh>
//+------------------------------------------------------------------+
//| Inputs |
//+------------------------------------------------------------------+
//--- inputs for expert
input string Expert_Title ="sar"; // Document name
ulong Expert_MagicNumber =13831; //
bool Expert_EveryTick =false; //
//--- inputs for main signal
input int Signal_ThresholdOpen =10; // Signal threshold value to open [0...100]
input int Signal_ThresholdClose=10; // Signal threshold value to close [0...100]
input double Signal_PriceLevel =0.0; // Price level to execute a deal
input int Signal_Expiration =4; // Expiration of pending orders (in bars)
input double Signal_SAR_Step =0.02; // Parabolic SAR(0.02,0.2) Speed increment
input double Signal_SAR_Maximum =0.2; // Parabolic SAR(0.02,0.2) Maximum rate
input double Signal_SAR_Weight =1.0; // Parabolic SAR(0.02,0.2) Weight [0....1.0]
//--- inputs for money
input double Money_FixLot_Percent =10.0; // Percent
input double Money_FixLot_Lots =0.1; // Fixed volume
//+------------------------------------------------------------------+
//| Global expert object |
//+------------------------------------------------------------------+
CExpert ExtExpert;
//+------------------------------------------------------------------+
//| Initialisation function of the expert |
//+------------------------------------------------------------------+
int OnInit()
{
//--- Initialising expert
if(!ExtExpert.Init(Symbol(),Period(),Expert_EveryTick,Expert_MagicNumber))
{
//----- failed
printf(__FUNCTION__+": error initialising expert");
ExtExpert.Deinit();
return(INIT_FAILED);
}
//--- Creating signal
CExpertSignal *signal=new CExpertSignal;
if(signal==NULL)
{
//----- failed
printf(__FUNCTION__+": error creating signal");
ExtExpert.Deinit();
return(INIT_FAILED);
}
//---
ExtExpert.InitSignal(signal);
signal.ThresholdOpen(Signal_ThresholdOpen);
signal.ThresholdClose(Signal_ThresholdClose);
signal.PriceLevel(Signal_PriceLevel);
signal.Expiration(Signal_Expiration);
//--- Creating filter CSignalSAR
CSignalSAR *filter0=new CSignalSAR;
if(filter0==NULL)
{
//----- failed
printf(__FUNCTION__+": error creating filter0");
ExtExpert.Deinit();
return(INIT_FAILED);
}
signal.AddFilter(filter0);
//--- Set filter parameters
filter0.Step(Signal_SAR_Step);
filter0.Maximum(Signal_SAR_Maximum);
filter0.Weight(Signal_SAR_Weight);
//--- Creation of trailing object
CTrailingNone *trailing=new CTrailingNone;
if(trailing==NULL)
{
//----- failed
printf(__FUNCTION__+": error creating trailing");
ExtExpert.Deinit();
return(INIT_FAILED);
}
//--- Add trailing to expert (will be deleted automatically))
if(!ExtExpert.InitTrailing(trailing))
{
//----- failed
printf(__FUNCTION__+": error initialising trailing");
ExtExpert.Deinit();
return(INIT_FAILED);
}
//--- Set trailing parameters
//--- Creation of money object
CMoneyFixedLot *money=new CMoneyFixedLot;
if(money==NULL)
{
//----- failed
printf(__FUNCTION__+": error creating money");
ExtExpert.Deinit();
return(INIT_FAILED);
}
//--- Add money to expert (will be deleted automatically))
if(!ExtExpert.InitMoney(money))
{
//----- failed
printf(__FUNCTION__+": error initialising money");
ExtExpert.Deinit();
return(INIT_FAILED);
}
//--- Set money parameters
money.Percent(Money_FixLot_Percent);
money.Lots(Money_FixLot_Lots);
//--- Check all trading objects parameters
if(!ExtExpert.ValidationSettings())
{
//----- failed
ExtExpert.Deinit();
return(INIT_FAILED);
}
//--- Tuning of all necessary indicators
if(!ExtExpert.InitIndicators())
{
//----- failed
printf(__FUNCTION__+": error initialising indicators");
ExtExpert.Deinit();
return(INIT_FAILED);
}
//--- ok
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Deinitialisation function of the expert |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
ExtExpert.Deinit();
}
//+------------------------------------------------------------------+
//| "Tick" event handler function |
//+------------------------------------------------------------------+
void OnTick()
{
ExtExpert.OnTick();
}
//+------------------------------------------------------------------+
//| "Trade" event handler function |
//+------------------------------------------------------------------+
void OnTrade()
{
ExtExpert.OnTrade();
}
//+------------------------------------------------------------------+
//| "Timer" event handler function |
//+------------------------------------------------------------------+
void OnTimer()
{
ExtExpert.OnTimer();
}
//+------------------------------------------------------------------+
I have now had time to work out your solution. It is more accurate (while the other is more cautious as commission is taken into account?) and it is apparently less error-prone, as can be seen with gold and silver on the demo account at MQ - with Roboforex it is, as expected, somewhat more cautious than your solution:
On the MQ account, the lot sizes would be far too high.
Here is my script for comparing the two methods:
I derived the direction from in > sl (buy) or in < sl (sell). Then there would be no problems with the pending orders
Thank you for your tests. Yes, if you compare the results with each other, you can see on the one hand the consistency of OrderCalcProfit and on the other hand, at least for EURUSD and RoboForex, that the results differ by approximately "Lot size * CommissionPerLot". This could be added:
If I used the forum search correctly, there is no way to query the "CommissionPerLot" value directly using MQL5, is there?
If I have used the forum search correctly, then there is no way to query the "CommissionPerLot" value directly using MQL5, is there?Yes, not directly in the terminal, but request it from the broker or in the trading history: DEAL_COMMISSION.
Dear members~.
I am trying to count candles. please help~~
int Barcount;
if(Barcount != Bars)
Print("pt=",pt) ;
int OT,SHi;
if(OrderSelect(ticket,SELECT_BY_TICKET)==true)
{ }
OT = OrderOpenTime();
}
SHi = iBarShift(Symbol(),0,OT);
Print("SHi=",SHi);