Community of Expertise - page 4

 
Проверьте, пожалуйста, не в этом ли ошибка с попытками повторных установок стопа по той же цене?

3 people were watching =)) Renat came and just pointed his finger at the error =)))
I'll check it now, of course, but that's probably the problem... I haven't normalized "bid - TrailingStop * point", and this very construction is involved in order modification...
We are not attentive, gentlemen ;)


So several people have offered you this variant of the problem. Even I wrote that bid may have 5 decimal places. I thought at first it wasn't possible, but then I remembered that the quotes are made by an automaton that probably takes quotes from several datafeeds and averages them using some kind of algorithm.
 
Even I wrote that bid can have 5 decimal places. At first I thought that was impossible

Bid/Ask have unambiguously standard symbol accuracy. Another thing is that instead of 1.6666 you may see 1.6665999 (due to floating point error).

I want to note: problems comparing these numbers(type double) exist in _all_ languages and are a consequence of the basic limited precision of this type of arithmetic. Consequently, one should not blame anyone, but be aware of the problem and write protected code.
 
Даже я писал , что bid может иметь 5 знаков после запятой. Я сначала думал, что такое невозможно

Bid/Ask are clearly of standard character accuracy. Another thing is that instead of 1.6666 one may see 1.6665999 (due to the peculiarities of floating-point error).

I want to note: problems comparing these numbers (type double) exist in _all_ languages and are a consequence of the basic limited precision of this type of arithmetic. Correspondingly, you should not blame anyone, but be aware of the problem and write secure code.


So I'm not accusing anyone. I didn't ask komposter for nothing - on which currency trailing didn't work correctly. I remember for sure that in MT3 you can see 3 decimal places in Yen. At least, I have seen it more than once.
 
3 people were watching =) Renat came and just pointed his finger at the error =)))<br/ translate="no"> now I will check, of course, but most likely this is the case... I didn't normalize "bid - TrailingStop * point", and this very construction takes part in order modification...
we are not attentive, gentlemen ;)

normalization didn't help =(
more precisely - trailing errors (euro - buy position):
03:41
12:07
12:11
14:31
14:33
14:36
14:39
14:44
14:46
14:47
14:48
(server time)
 
Sorry, I meant the standard NormalizeDouble.

Renat, how should I proceed?
At the moment there are 3 options:
1. if ( orderstoploss < ( bid - TrailingStop * point ) ) replaced by if ( TrailingStop < ( bid -orderstoploss) / point )
2. Compare int, not double, using Begun functions
3. Change stop advance ( not every point, but after n spreads)
and their combinations, of course.

Since the software is quite responsible, I would like to get (! not a guarantee!) recommendations for writing...
 
<br / translate="no"> normalisation didn't help =(


Try instead:
if (orderstoploss < ( bid - TrailingStop * point ))


write:

double newSL = NormalizeDouble(bid - TrailingStop * Point);
if (orderstoploss + Point < newSL)
   orderstoploss  = newSL;//OrderModify(... newSL...)



Will there be errors as well?

 
well, if " + Point" I don't know... but actually I have the same thing now...
It's clear that you can make a backlash, but it's not serious.... And if I have to make a 10-20 pips backlash, "for reliability", yes, on M30, just a fairy tale =)
 
Renat, how to proceed anyway?

Please recheck your code again, simplify it, insert debug messages.
 
Please recheck your code again, simplify it, insert debug messages.


To be honest, I don't know how to simplify it...
But you can check it, of course. Here is the code as it is used now (in parts):

check incoming parameters and select required order. If an error occurs, we simply exit, i.e. the order will not be modified...
int _TrailingStop ( int _OrderTicket, int TrailingStop, color _Color = CLR_NONE )
{
//	Print( "_TrailingStop ( ", _OrderTicket, ", ", TrailingStop, " ) - loaded..." );
	if ( TrailingStop <= 0 )
	{
//		Print( "_TrailingStop ( ", _OrderTicket, ", ", TrailingStop, " ) - return(0);" );
		return(0);
	}
	
	if ( OrderSelect( _OrderTicket, SELECT_BY_TICKET ) == false )
	{
//		Print( "_TrailingStop ( ", _OrderTicket, ", ", TrailingStop, " ) - return(-2);" );
		return(-2);
	}



Save all order data into variables and normalise them:

	string _Symbol = OrderSymbol();
	int _OrderType = OrderType();
	double point = MarketInfo ( _Symbol, MODE_POINT );
	int digits = MarketInfo( _Symbol, MODE_DIGITS );
	double bid = NormalizeDouble( MarketInfo ( _Symbol, MODE_BID ), digits );
	double ask = NormalizeDouble( MarketInfo ( _Symbol, MODE_ASK ), digits );
	double orderopenprice = NormalizeDouble ( OrderOpenPrice(), digits );
	double orderstoploss = NormalizeDouble ( OrderStopLoss(), digits );
	double ordertakeprofit = NormalizeDouble ( OrderTakeProfit(), digits );
	datetime orderexpiration = OrderExpiration();
	double NewStopLoss;




this is the colour and "niceness" of the log...

	string _OrderType_str = _OrderType_str ( _OrderType );
	if ( _Color <= 0 )
	{
		_Color = ordersellcolor;
		if ( _OrderType == OP_BUY || _OrderType == OP_BUYLIMIT || _OrderType == OP_BUYSTOP )
		{ _Color = orderbuycolor; }
	}



now, for a long position, define a new SL level, normalize it and compare it with the old one (only if the position is profitable)

	if ( _OrderType == OP_BUY )
	{
		NewStopLoss = NormalizeDouble( bid - TrailingStop * point, digits );
		if ( ( bid - orderopenprice ) > 0 )
  		{
			if ( orderstoploss <= 0 || orderstoploss < NewStopLoss )//TrailingStop < ( bid - orderstoploss ) / point )
			{


dump it into the log

				_info1 ( "Trailing Stop (" + TrailingStop + " points) для позиции № " + _OrderTicket + ", " + _OrderType_str + "..." );
				_info2 ( "New Stop Loss = " + DoubleToStr( NewStopLoss, digits ) );
				_info3 ( "" );
				_info4 ( "" );



a triple attempt to modify the order after a 10 second pause (the order is highlighted each time)

				for ( int x = 0; x < 3; x ++ )
				{
					while ( LocalTime() - GlobalVariableGet ( "LastTradeTime" ) < 10 )
					{ Sleep(1000); }

					if ( OrderSelect( _OrderTicket, SELECT_BY_TICKET ) == false )
					{
//						Print( "_TrailingStop ( ", _OrderTicket, ", ", TrailingStop, " ) - return(-2);" );
						return(-2);
					}

					int ordermodify = OrderModify( _OrderTicket, orderopenprice, NewStopLoss, ordertakeprofit, orderexpiration, _Color );


and only if ordermodify <= 0 an error code is sent

					if ( ordermodify > 0 )
					{
						_info3 ( "Успешно..." );
						GlobalVariableSet ( "LastTradeTime", LocalTime() );
//						Print( "_TrailingStop ( ", _OrderTicket, ", ", TrailingStop, " ) - return(1);" );
						return(1);
					}
					int error_code = GetLastError();
					_info1 ( "Ошибка Trailing Stop!!!", 1 );
					Processing_Error ( error_code );
					Sleep(10000);
				}


if after 3 tries the order is not modified, we exit (-1)

//				Print( "_TrailingStop ( ", _OrderTicket, ", ", TrailingStop, " ) - return(-1);" );
				return(-1);
			}
  		}
  	}


then the same for sell positions

 	if ( _OrderType == OP_SELL )
	{
	}


and finally, if there is no need to modify the stop, exit(0)

//Print( "_TrailingStop ( ", _OrderTicket, ", ", TrailingStop, " ) - return(0);" );
return(0);
}





don't know....

 
<br / translate="no"> understandably, it is possible to make a backlash, but it is not serious.... And if we have to make a backlash of 10-20 pips, "for reliability", yes on M30, just a fairy tale =)


What does this have to do with it? "+Point" solves the problem of rounding the last significant digit of price. We are not talking about 2, 3, and all the more about 10-20 pips.
Reason: