如何将一个数字分成几个不相等但递增的数字 [用于发送 PlaceOrder( OP_BUY, lots ) 合约 XTO ]
How to divide a number into several, unequal, yet increasing numbers [ for sending a PlaceOrder( OP_BUY, lots ) contract XTO ]
我尝试创建一个 MQL4 脚本(一种几乎与 C++ 相关的语言,MQL4),我想在其中 将 double
值划分为 9 部分,其中分数不相等,但增加
我当前的代码试图这样做(伪代码):
Lots1 = 0.1;
Lots2 = (Lots1 / 100) * 120;//120% of Lot1
Lots3 = (Lots2 / 100) * 130;//130% of Lot2
Lots4 = (Lots3 / 100) * 140;//140% of Lot3
Lots5 = (Lots4 / 100) * 140;//140% of Lot4
Lots6 = (Lots5 / 100) * 160;//160% of Lot5
Lots7 = (Lots6 / 100) * 170;//170% of Lot6
Lots8 = (Lots7 / 100) * 180;//180% of Lot7
Lots9 = (Lots8 / 100) * 190;//190% of Lot8
...
或更好:
double Lots = 0.1; // a Lot Size
double lot = Lots;
...
/* Here is the array with percentages of lots' increments
in order */
int AllZoneLots[8] = { 120, 130, 140, 140, 160, 170, 180, 190 }; // 120%, 130%,...
/* Here, the lot sizes are used by looping the array
and increasing the lot size by the count */
for( int i = 0; i < ArraySize( AllZoneLots ); i++ ) {
lots = AllZoneLots[i] * ( lots / 100 ) *;
// PlaceOrder( OP_BUY, lots );
}
但是,我想要的只是将固定值 6.7 分成 9 个部分,就像这些代码所做的那样,但要增加值,而不是相同...
例如,6.7 拆分为:
double lots = { 0.10, 0.12, 0.16, 0.22, 0.31, 0.50, 0.85, 1.53, 2.91 };
/* This is just an example
of how to divide a value of 6.7 into 9, growing parts
不确定我是否理解正确,但可能您需要在所有批次之间共享 3.5 个。
而且我只能看到 8 批次,不包括最初的批次。
totalPercentage = 0;
for(int i = 0; i < ArraySize(AllZoneLots); i++) {
totalPercentage += AllZoneLots[i];
}
double totalValue = 3.5;
// total value is total percentage, Lots1 - 100%, so:
Lots1 = totalValue / totalPercentage * 100.00;
然后你继续你的代码。
如果要包括 Lots1,只需将总数加 100,然后执行相同的操作。
这样做可以使值相等。如果有9步,将数值除以45得到第一个数值,等步x
。为什么?因为1..9的和是45.
x = 6.7 / 45
即 0.148889
第一项是x
,第二项是2 * x
,第三项是3 * x
等等。它们加起来是45 * x
也就是6.7,但是最好除以last。所以第二项,比如说,将是 6.7 * 2 / 45;
这里的代码展示了如何在 C 中完成,因为 MQL4 使用 C 语法:
#include <stdio.h>
int main(void) {
double val = 6.7;
double term;
double sum = 0;
for(int i = 1; i <= 9; i++) {
term = val * i / 45;
printf("%.3f ", term);
sum += term;
}
printf("\nsum = %.3f\n", sum);
}
程序输出:
0.149 0.298 0.447 0.596 0.744 0.893 1.042 1.191 1.340
sum = 6.700
Q : How to divide a number into several, unequal, yet increasing numbers [ for sending a PlaceOrder( OP_BUY, lots )
contract XTO ]?
A : 这个问题并不像第一眼看上去那么简单 :
在metatrader终端生态系统中,问题的制定还必须服从外部决定的因素(对于任何希望不被拒绝的 XTO 来说都是强制性的,因为主要与 XTO 条款和条件不兼容设置并填充 ~ "placed" 在市场 )
可通过致电报告这些因素:
MarketInfo( <_a_SymbolToReportSTRING>, MODE_MINLOT ); // a minimum permitted size
MarketInfo( <_a_SymbolToReportSTRING>, MODE_LOTSTEP ); // a mandatory size-stepping
MarketInfo( <_a_SymbolToReportSTRING>, MODE_MAXLOT ); // a maximum permitted size
此外,对于给定的小数位数,任何此类手数都必须先于提交 XTO "normalised",以便交易服务器在经纪商方面成功放置/接受。如果不这样做会导致远程拒绝 XTO-s(这显然会带来显着的阻塞/巨大的代码执行延迟惩罚,人们总是希望防止在实际交易中发生这种情况)
最后但并非最不重要的一点是,任何此类 XTO 规模都必须由一定数量的杠杆资产覆盖(在出于上述原因发送任何此类 XTO 之前,首先检查可用保证金的可用性)。
代码:
虽然上面的初始伪代码进行了渐进式(类似 Martingale 的)手数缩放:
>>> aListOfFACTORs = [ 100, 120, 130, 140, 140, 160, 170, 180, 190 ]
>>> for endPoint in range( len( aListOfFACTORs ) ):
... product = 1.
... for item in aListOfFACTORs[:1+endPoint]:
... product *= item / 100.
... print( "Lots{0:} ~ ought be about {1:} times the amount of Lots1".format( 1 + endPoint, product ) )
...
Lots1 ~ ought be about 1.0 times the amount of Lots1
Lots2 ~ ought be about 1.2 times the amount of Lots1
Lots3 ~ ought be about 1.56 times the amount of Lots1
Lots4 ~ ought be about 2.184 times the amount of Lots1
Lots5 ~ ought be about 3.0576 times the amount of Lots1
Lots6 ~ ought be about 4.89216 times the amount of Lots1
Lots7 ~ ought be about 8.316672 times the amount of Lots1
Lots8 ~ ought be about 14.9700096 times the amount of Lots1
Lots9 ~ ought be about 28.44301824 times the amount of Lots1
_MINLOT
、_LOTSTEP
和_MAXLOT
让游戏焕然一新。
任何成功的策略都不是可以自由选择尺寸的。给定所述 9
-steps 和固定数量的 total-amount ~ 6.7
lots,该过程可以服从步进和总计 plus,它必须遵守 MarketInfo()
-报告的大小代数
鉴于 9 步 是强制性的,
每个都必须至少 _MINLOT
-sized:
double total_amount_to_split = aSizeToSPLIT;
total_amount_to_split = Min( aSizeToSPLIT, // a wished-to-have-sizing
FreeMargin/LotInBaseCurr*sFty // a FreeMargin-covered size
);
int next = 0;
while ( total_amount_to_split >= _MINLOT )
{ total_amount_to_split -= _MINLOT;
lot_size[next++] = _MINLOT;
}
/*
###################################################################################
------------------------------------------------- HERE, WE HAVE 0:next lot_sizes
next NEED NOT == 9
If there is anything yet to split:
there is an integer amount of _LOTSTEP-s to distribute among 'em
HERE, and ONLY here, you have a freedom to decide about split/mapping
of the integer amount of _LOTSTEP-sized
additions to the _MINLOT "pre"-sets
in lot_size[]-s
YET, still no more than _MAXLOT is permissible for the above explained reasons
------------------------------------------------- CODE has to obey this, if XTO-s
are to
get a chance
###################################################################################
*/
我尝试创建一个 MQL4 脚本(一种几乎与 C++ 相关的语言,MQL4),我想在其中 将 double
值划分为 9 部分,其中分数不相等,但增加
我当前的代码试图这样做(伪代码):
Lots1 = 0.1;
Lots2 = (Lots1 / 100) * 120;//120% of Lot1
Lots3 = (Lots2 / 100) * 130;//130% of Lot2
Lots4 = (Lots3 / 100) * 140;//140% of Lot3
Lots5 = (Lots4 / 100) * 140;//140% of Lot4
Lots6 = (Lots5 / 100) * 160;//160% of Lot5
Lots7 = (Lots6 / 100) * 170;//170% of Lot6
Lots8 = (Lots7 / 100) * 180;//180% of Lot7
Lots9 = (Lots8 / 100) * 190;//190% of Lot8
...
或更好:
double Lots = 0.1; // a Lot Size
double lot = Lots;
...
/* Here is the array with percentages of lots' increments
in order */
int AllZoneLots[8] = { 120, 130, 140, 140, 160, 170, 180, 190 }; // 120%, 130%,...
/* Here, the lot sizes are used by looping the array
and increasing the lot size by the count */
for( int i = 0; i < ArraySize( AllZoneLots ); i++ ) {
lots = AllZoneLots[i] * ( lots / 100 ) *;
// PlaceOrder( OP_BUY, lots );
}
但是,我想要的只是将固定值 6.7 分成 9 个部分,就像这些代码所做的那样,但要增加值,而不是相同... 例如,6.7 拆分为:
double lots = { 0.10, 0.12, 0.16, 0.22, 0.31, 0.50, 0.85, 1.53, 2.91 };
/* This is just an example
of how to divide a value of 6.7 into 9, growing parts
不确定我是否理解正确,但可能您需要在所有批次之间共享 3.5 个。
而且我只能看到 8 批次,不包括最初的批次。
totalPercentage = 0;
for(int i = 0; i < ArraySize(AllZoneLots); i++) {
totalPercentage += AllZoneLots[i];
}
double totalValue = 3.5;
// total value is total percentage, Lots1 - 100%, so:
Lots1 = totalValue / totalPercentage * 100.00;
然后你继续你的代码。
如果要包括 Lots1,只需将总数加 100,然后执行相同的操作。
这样做可以使值相等。如果有9步,将数值除以45得到第一个数值,等步x
。为什么?因为1..9的和是45.
x = 6.7 / 45
即 0.148889
第一项是x
,第二项是2 * x
,第三项是3 * x
等等。它们加起来是45 * x
也就是6.7,但是最好除以last。所以第二项,比如说,将是 6.7 * 2 / 45;
这里的代码展示了如何在 C 中完成,因为 MQL4 使用 C 语法:
#include <stdio.h>
int main(void) {
double val = 6.7;
double term;
double sum = 0;
for(int i = 1; i <= 9; i++) {
term = val * i / 45;
printf("%.3f ", term);
sum += term;
}
printf("\nsum = %.3f\n", sum);
}
程序输出:
0.149 0.298 0.447 0.596 0.744 0.893 1.042 1.191 1.340
sum = 6.700
Q : How to divide a number into several, unequal, yet increasing numbers [ for sending a
PlaceOrder( OP_BUY, lots )
contract XTO ]?
A : 这个问题并不像第一眼看上去那么简单 :
在metatrader终端生态系统中,问题的制定还必须服从外部决定的因素(对于任何希望不被拒绝的 XTO 来说都是强制性的,因为主要与 XTO 条款和条件不兼容设置并填充 ~ "placed" 在市场 )
可通过致电报告这些因素:
MarketInfo( <_a_SymbolToReportSTRING>, MODE_MINLOT ); // a minimum permitted size
MarketInfo( <_a_SymbolToReportSTRING>, MODE_LOTSTEP ); // a mandatory size-stepping
MarketInfo( <_a_SymbolToReportSTRING>, MODE_MAXLOT ); // a maximum permitted size
此外,对于给定的小数位数,任何此类手数都必须先于提交 XTO "normalised",以便交易服务器在经纪商方面成功放置/接受。如果不这样做会导致远程拒绝 XTO-s(这显然会带来显着的阻塞/巨大的代码执行延迟惩罚,人们总是希望防止在实际交易中发生这种情况)
最后但并非最不重要的一点是,任何此类 XTO 规模都必须由一定数量的杠杆资产覆盖(在出于上述原因发送任何此类 XTO 之前,首先检查可用保证金的可用性)。
代码:
虽然上面的初始伪代码进行了渐进式(类似 Martingale 的)手数缩放:
>>> aListOfFACTORs = [ 100, 120, 130, 140, 140, 160, 170, 180, 190 ]
>>> for endPoint in range( len( aListOfFACTORs ) ):
... product = 1.
... for item in aListOfFACTORs[:1+endPoint]:
... product *= item / 100.
... print( "Lots{0:} ~ ought be about {1:} times the amount of Lots1".format( 1 + endPoint, product ) )
...
Lots1 ~ ought be about 1.0 times the amount of Lots1
Lots2 ~ ought be about 1.2 times the amount of Lots1
Lots3 ~ ought be about 1.56 times the amount of Lots1
Lots4 ~ ought be about 2.184 times the amount of Lots1
Lots5 ~ ought be about 3.0576 times the amount of Lots1
Lots6 ~ ought be about 4.89216 times the amount of Lots1
Lots7 ~ ought be about 8.316672 times the amount of Lots1
Lots8 ~ ought be about 14.9700096 times the amount of Lots1
Lots9 ~ ought be about 28.44301824 times the amount of Lots1
_MINLOT
、_LOTSTEP
和_MAXLOT
让游戏焕然一新。
任何成功的策略都不是可以自由选择尺寸的。给定所述 9
-steps 和固定数量的 total-amount ~ 6.7
lots,该过程可以服从步进和总计 plus,它必须遵守 MarketInfo()
-报告的大小代数
鉴于 9 步 是强制性的,
每个都必须至少 _MINLOT
-sized:
double total_amount_to_split = aSizeToSPLIT;
total_amount_to_split = Min( aSizeToSPLIT, // a wished-to-have-sizing
FreeMargin/LotInBaseCurr*sFty // a FreeMargin-covered size
);
int next = 0;
while ( total_amount_to_split >= _MINLOT )
{ total_amount_to_split -= _MINLOT;
lot_size[next++] = _MINLOT;
}
/*
###################################################################################
------------------------------------------------- HERE, WE HAVE 0:next lot_sizes
next NEED NOT == 9
If there is anything yet to split:
there is an integer amount of _LOTSTEP-s to distribute among 'em
HERE, and ONLY here, you have a freedom to decide about split/mapping
of the integer amount of _LOTSTEP-sized
additions to the _MINLOT "pre"-sets
in lot_size[]-s
YET, still no more than _MAXLOT is permissible for the above explained reasons
------------------------------------------------- CODE has to obey this, if XTO-s
are to
get a chance
###################################################################################
*/