布林带入场开盘价不正确
Bollinger band entry opening price is incorrect
建立基于布林带和随机指标的实践策略。
应该发生的是,如果 5 分钟柱的 Close
高于波段上限且随机指标已超过 80,则应进入 sell
订单,相反,波段下限 20 .
然而,当我 运行 在 MT4
进行测试时,我注意到交易严重偏离入场点(例如,在布林带内开盘,而不是在布林带外开盘)。它们不是在柱线的 Close
开盘,而是太高或太低的几个点。
我做错了什么?
这是我的代码:
extern int MagicNumber = 1;
extern double Lots = 1;
extern double StopLoss = 5;
extern double TakeProfit = 15;
extern int TrailingStop = 0;
int ThisBarTrade = 0;
int start() {
double MyPoint = Point;
if ( Digits == 3 || Digits == 5 ) MyPoint = Point * 10;
double TheStopLoss = 0;
double TheTakeProfit = 0;
if ( TotalOrdersCount() == 0 ) {
if ( Bars != ThisBarTrade ) {
ThisBarTrade = Bars;
int result = 0;
if ( Open[0] < iBands( NULL, 0, 20, 2, 0, PRICE_CLOSE, MODE_LOWER, 0 )
&& 20 > iStochastic( NULL, 0, 12, 1, 3, MODE_SMA, 1, MODE_MAIN, 0 )
&& 0 == OrdersTotal()
) // Here is your open [Buy] rule -------------------
{
result = OrderSend( Symbol(), OP_BUY, Lots, Ask, 0, 0, 0, "", MagicNumber, 0, Blue );
if ( result > 0 )
{
TheStopLoss = 0;
TheTakeProfit = 0;
if ( TakeProfit > 0 ) TheTakeProfit = Ask + MyPoint * TakeProfit;
if ( StopLoss > 0 ) TheStopLoss = Ask - MyPoint * StopLoss;
OrderSelect( result, SELECT_BY_TICKET );
OrderModify( OrderTicket(), OrderOpenPrice(), NormalizeDouble( TheStopLoss, Digits ), NormalizeDouble( TheTakeProfit, Digits ), 0, Green );
}
return( 0 ); // JIT/RET --> --> --> --> --> --> --> --> -->
}
if ( Open[0] > iBands( NULL, 0, 20, 2, 0, PRICE_CLOSE, MODE_UPPER, 0 )
&& 80 < iStochastic( NULL, 0, 12, 1, 3, MODE_SMA, 1, MODE_MAIN, 0 )
&& 0 == OrdersTotal()
) // Here is your open [Sell] rule -----------------------
{
result = OrderSend( Symbol(), OP_SELL, Lots, Bid, 0, 0, 0, "", MagicNumber, 0, Red );
if ( result > 0 )
{
TheStopLoss = 0;
TheTakeProfit = 0;
if ( TakeProfit > 0 ) TheTakeProfit = Bid - MyPoint * TakeProfit;
if ( StopLoss > 0 ) TheStopLoss = Bid + MyPoint * StopLoss;
OrderSelect( result, SELECT_BY_TICKET );
OrderModify( OrderTicket(), OrderOpenPrice(), NormalizeDouble( TheStopLoss, Digits ), NormalizeDouble( TheTakeProfit, Digits ), 0, Green );
}
return( 0 ); // JIT/RET --> --> --> --> --> --> --> --> -->
}
}
for ( int cnt = 0; cnt < OrdersTotal(); cnt++ ) {
OrderSelect( cnt, SELECT_BY_POS, MODE_TRADES );
if ( OrderType() <= OP_SELL // !PENDING ORDER
&& OrderSymbol() == Symbol() // !ANOTHER MARKET
&& OrderMagicNumber() == MagicNumber // !ANOTHER EA/MMI TRADE
)
{
if ( OrderType() == OP_BUY )
{
if ( TrailingStop > 0 )
{
if ( Bid - OrderOpenPrice() > MyPoint * TrailingStop )
{
if ( OrderStopLoss() < Bid - MyPoint * TrailingStop )
{
OrderModify( OrderTicket(), OrderOpenPrice(), Bid - TrailingStop * MyPoint, OrderTakeProfit(), 0, Green );
return( 0 ); // JIT/RET --> --> --> --> --> --> --> --> -->
}
}
}
}
else
{
if ( TrailingStop > 0 )
{
if ( OrderOpenPrice() - Ask > MyPoint * TrailingStop )
{
if ( OrderStopLoss() > ( Ask + MyPoint * TrailingStop )
|| OrderStopLoss() == 0
)
{
OrderModify( OrderTicket(), OrderOpenPrice(), Ask + MyPoint * TrailingStop, OrderTakeProfit(), 0, Red );
return( 0 ); // JIT/RET --> --> --> --> --> --> --> --> -->
}
}
}
}
}
}
}
return( 0 );
}
int TotalOrdersCount()
{
int result = 0;
for ( int i = 0; i < OrdersTotal(); i++ )
{
OrderSelect( i, SELECT_BY_POS, MODE_TRADES );
if ( OrderMagicNumber() == MagicNumber ) result++;
}
return( result );
}
在使用 iBand()
和 iStochastic()
时,您使用的是 SHIFT 0(最后一个参数)。这意味着您正在提取 CURRENT(市场边缘)蜡烛的价值。但是,由于当前蜡烛尚未结束,其价值会根据新的传入价格不断变化。这就是为什么您的交易是以看似随机的价格点进行的。
您可能想要参考上一个(已结束)蜡烛,这意味着 SHIFT 应该是 1 而不是 0。
建立基于布林带和随机指标的实践策略。
应该发生的是,如果 5 分钟柱的 Close
高于波段上限且随机指标已超过 80,则应进入 sell
订单,相反,波段下限 20 .
然而,当我 运行 在 MT4
进行测试时,我注意到交易严重偏离入场点(例如,在布林带内开盘,而不是在布林带外开盘)。它们不是在柱线的 Close
开盘,而是太高或太低的几个点。
我做错了什么?
这是我的代码:
extern int MagicNumber = 1;
extern double Lots = 1;
extern double StopLoss = 5;
extern double TakeProfit = 15;
extern int TrailingStop = 0;
int ThisBarTrade = 0;
int start() {
double MyPoint = Point;
if ( Digits == 3 || Digits == 5 ) MyPoint = Point * 10;
double TheStopLoss = 0;
double TheTakeProfit = 0;
if ( TotalOrdersCount() == 0 ) {
if ( Bars != ThisBarTrade ) {
ThisBarTrade = Bars;
int result = 0;
if ( Open[0] < iBands( NULL, 0, 20, 2, 0, PRICE_CLOSE, MODE_LOWER, 0 )
&& 20 > iStochastic( NULL, 0, 12, 1, 3, MODE_SMA, 1, MODE_MAIN, 0 )
&& 0 == OrdersTotal()
) // Here is your open [Buy] rule -------------------
{
result = OrderSend( Symbol(), OP_BUY, Lots, Ask, 0, 0, 0, "", MagicNumber, 0, Blue );
if ( result > 0 )
{
TheStopLoss = 0;
TheTakeProfit = 0;
if ( TakeProfit > 0 ) TheTakeProfit = Ask + MyPoint * TakeProfit;
if ( StopLoss > 0 ) TheStopLoss = Ask - MyPoint * StopLoss;
OrderSelect( result, SELECT_BY_TICKET );
OrderModify( OrderTicket(), OrderOpenPrice(), NormalizeDouble( TheStopLoss, Digits ), NormalizeDouble( TheTakeProfit, Digits ), 0, Green );
}
return( 0 ); // JIT/RET --> --> --> --> --> --> --> --> -->
}
if ( Open[0] > iBands( NULL, 0, 20, 2, 0, PRICE_CLOSE, MODE_UPPER, 0 )
&& 80 < iStochastic( NULL, 0, 12, 1, 3, MODE_SMA, 1, MODE_MAIN, 0 )
&& 0 == OrdersTotal()
) // Here is your open [Sell] rule -----------------------
{
result = OrderSend( Symbol(), OP_SELL, Lots, Bid, 0, 0, 0, "", MagicNumber, 0, Red );
if ( result > 0 )
{
TheStopLoss = 0;
TheTakeProfit = 0;
if ( TakeProfit > 0 ) TheTakeProfit = Bid - MyPoint * TakeProfit;
if ( StopLoss > 0 ) TheStopLoss = Bid + MyPoint * StopLoss;
OrderSelect( result, SELECT_BY_TICKET );
OrderModify( OrderTicket(), OrderOpenPrice(), NormalizeDouble( TheStopLoss, Digits ), NormalizeDouble( TheTakeProfit, Digits ), 0, Green );
}
return( 0 ); // JIT/RET --> --> --> --> --> --> --> --> -->
}
}
for ( int cnt = 0; cnt < OrdersTotal(); cnt++ ) {
OrderSelect( cnt, SELECT_BY_POS, MODE_TRADES );
if ( OrderType() <= OP_SELL // !PENDING ORDER
&& OrderSymbol() == Symbol() // !ANOTHER MARKET
&& OrderMagicNumber() == MagicNumber // !ANOTHER EA/MMI TRADE
)
{
if ( OrderType() == OP_BUY )
{
if ( TrailingStop > 0 )
{
if ( Bid - OrderOpenPrice() > MyPoint * TrailingStop )
{
if ( OrderStopLoss() < Bid - MyPoint * TrailingStop )
{
OrderModify( OrderTicket(), OrderOpenPrice(), Bid - TrailingStop * MyPoint, OrderTakeProfit(), 0, Green );
return( 0 ); // JIT/RET --> --> --> --> --> --> --> --> -->
}
}
}
}
else
{
if ( TrailingStop > 0 )
{
if ( OrderOpenPrice() - Ask > MyPoint * TrailingStop )
{
if ( OrderStopLoss() > ( Ask + MyPoint * TrailingStop )
|| OrderStopLoss() == 0
)
{
OrderModify( OrderTicket(), OrderOpenPrice(), Ask + MyPoint * TrailingStop, OrderTakeProfit(), 0, Red );
return( 0 ); // JIT/RET --> --> --> --> --> --> --> --> -->
}
}
}
}
}
}
}
return( 0 );
}
int TotalOrdersCount()
{
int result = 0;
for ( int i = 0; i < OrdersTotal(); i++ )
{
OrderSelect( i, SELECT_BY_POS, MODE_TRADES );
if ( OrderMagicNumber() == MagicNumber ) result++;
}
return( result );
}
在使用 iBand()
和 iStochastic()
时,您使用的是 SHIFT 0(最后一个参数)。这意味着您正在提取 CURRENT(市场边缘)蜡烛的价值。但是,由于当前蜡烛尚未结束,其价值会根据新的传入价格不断变化。这就是为什么您的交易是以看似随机的价格点进行的。
您可能想要参考上一个(已结束)蜡烛,这意味着 SHIFT 应该是 1 而不是 0。