| / | Forum |
|
chaffinsjc
2008.10.30 00:42
Below is a simple test script that uses an allegedly correct routine to make sure the orderLotSize is evenly divisibly by 2. I think the routine does, indeed, always provide an even number that is divisible by 2. The problem, as shown in this example, is that an input value of .50 is returned as .48 after the routine (the value is not wrong, but not .50 which _is_ evenly divisible by 2). Is there a better routine to round numbers? P.S. In a real trading situation, the initial proposed lot size might be 2% of margin resulting in 1.67. The routine does, indeed, change this to 1.66. // aaaTEST rounding.mq4 int start() Alert("orderLotSize=", orderLotSize); return(0); |
|
Everybody could apply for participation in the Automated Trading Championship 2007 within 3 months. 2011 people have been registered. 731 Expert Advisors have already been checked and are ready to start the contest. |
|
Ais
2008.10.31 01:57
Hello,
Even lot size code sample:
int start ()
{
double OrderLotSize = 0.51 ;
double OrderLotStep = MarketInfo ( Symbol () , MODE_LOTSTEP ) ;
double StepsInput = OrderLotSize / OrderLotStep ;
double StepsOutput = MathFloor ( StepsInput / 2 ) * 2 ;
double EvenLotSize = OrderLotStep * StepsOutput ;
Alert ( " " ) ;
Alert ( "EvenLotSize = " , EvenLotSize ) ;
Alert ( "Output: " ) ;
Alert ( " " ) ;
Alert ( "StepsOutput = " , StepsOutput ) ;
Alert ( "StepsInput = " , StepsInput ) ;
Alert ( "Processing: " ) ;
Alert ( " " ) ;
Alert ( "OrderLotSize = " , OrderLotSize ) ;
Alert ( "OrderLotStep = " , OrderLotStep ) ;
Alert ( "Input: " ) ;
}
Best regards, Ais. |
|
wabbit
2008.10.31 02:14
If you are looking to have a value divisible by two which is also a multiple of LOTSTEP, then: Force the original value to a multiple of LOTSTEP Halve the result value, Normailize the result, Double the result; ??? orderLotSize = orderLotSize - MathMod( orderLotSize, MarketInfo( Symbol(), MODE_LOTSTEP ) ); orderLotSize = 2 * NormalizeDouble( orderLotSize / 2, 2 ); ??? Hope this helps. |
|
chaffinsjc
2008.10.31 21:11
First, thanks guys for your reply. Unfortunately, there are inconsistent answers with each routine. Using the test script attached below, I ran both algorithms for some sample lot sizes. The results are often OK, and at times not acceptable when (1) the final lot size is increased, and (2) a perfectly even lot size is reduced by .02. [I'm sorry for the info below: spaces and tabs don't seem to keep the columns separate.] // Here are the test results: // 0.78 0.78 0.78 |
|
phy
2008.10.31 21:16
Why so worried about divide by 2?
|
|
chaffinsjc
2008.10.31 21:27
A lot size which is divisible by 2 means 1/2 the profit can be taken early in the trade, with the remaining 1/2 left in the trade. For example, start with 1.76 lot size, take the profit on .88 lot size, and later take the money for .88 lot size. |
|
Ais
2008.11.01 02:02
Hello,
Lot size adjusting code sample:
//< afd.AdjustLotSize >
//< head>
double afd.AdjustLotSize ( // input 2 / code 7 / output 1
//
double aad.InputLotSize , // input 1 / 2
int aai.Fractions // input 2 / 2
//
) //
//</head>
{//<body>
double ald.OrderLotStep = MarketInfo ( Symbol () , MODE_LOTSTEP ) ; // 1
double ald.StepsInput = aad.InputLotSize / ald.OrderLotStep ; // 2
int ali.StepsRounded = MathRound ( ald.StepsInput ) ; // 3
int ali.StepsOutput = MathFloor ( ali.StepsRounded / aai.Fractions ) * aai.Fractions ; // 4
double ald.AdjustedLotSize = ali.StepsOutput * ald.OrderLotStep ; // 5
// 6
Alert ( " " ) ;
Alert ( "ald.AdjustedLotSize = " , ald.AdjustedLotSize ) ;
Alert ( "Output: " ) ;
Alert ( " " ) ;
Alert ( "ali.StepsOutput = " , ali.StepsOutput ) ;
Alert ( "ali.StepsRounded = " , ali.StepsRounded ) ;
Alert ( "ald.StepsInput = " , ald.StepsInput ) ;
Alert ( "Processing: " ) ;
Alert ( " " ) ;
Alert ( "ald.OrderLotStep = " , ald.OrderLotStep ) ;
Alert ( "aai.Fractions = " , aai.Fractions ) ;
Alert ( "aad.InputLotSize = " , aad.InputLotSize ) ;
Alert ( "Input: " ) ;
return ( ald.AdjustedLotSize ) ; // 7
}//</body>
//</afd.AdjustLotSize >
//< init >
int init ()
{
double avd.InputLotSize = 0.58 ;
int avi.Fractions = 2 ;
double avd.AdjustedLotSize = afd.AdjustLotSize ( avd.InputLotSize , avi.Fractions ) ;
}
//</init >
Best regards, Ais. |
|
phy
2008.11.01 08:36
Ok. Calculate intial lot size that is 1/2 of what you want, and double it. Later, you can close half. |