# 交易者的黄金法则

11 四月 2016, 10:24
0
4 228

### 简介

1. 进入市场时了解所面临的风险（即初始止损值）；
2. 迅速止盈，并使利润奔跑起来（不要在系统要求之前平仓）；
3. 了解你系统的数学预期——定期对其测试和调整。

### 仓位跟踪程序代码

```//+------------------------------------------------------------------+
void LevelFibo()
{
double Fb1,Fb2,Fb3,Fb4,Fb5,Fb6,Fb7,Fb8,Fb9,Fb10,Fb11,Fb12,Fb13;
// "Vegas" channel
double Ma144_1 = iMA(NULL,0,144,0,MODE_EMA,PRICE_CLOSE,1);
double Ma169_1 = iMA(NULL,0,169,0,MODE_EMA,PRICE_CLOSE,1);
// "Vegas" channel median
double MedVegas=NormalizeDouble((Ma144_1+Ma169_1)/2,Digits);
// calculate Fibo levels values using "Vegas" method
Fb1=MedVegas-377*Point;     Fb12=MedVegas+377*Point;
Fb2=MedVegas-233*Point;     Fb11=MedVegas+233*Point;
Fb3=MedVegas-144*Point;     Fb10=MedVegas+144*Point;
Fb4=MedVegas-89*Point;      Fb9=MedVegas+89*Point;
Fb5=MedVegas-55*Point;      Fb8=MedVegas+55*Point;
Fb6=MedVegas-34*Point;      Fb7=MedVegas+34*Point;
}
//+------------------------------------------------------------------+```

```//+---------------------------------------------------------------------+
//| Function (table) for specifying Stop Loss values for BUY position   |
//| by Fibo levels according to the lowest value of the first candle    |
//+---------------------------------------------------------------------+
void StopLevelFiboBuy()
{
if(Low[1]>Fb12)                                newSL_B=Fb12-100*Point;
if(Low[1]<=Fb12 && Low[1]>(Fb12+Fb11)/2)       newSL_B=(Fb12+Fb11)/2;
if(Low[1]<=(Fb12+Fb11)/2 && Low[1]>Fb11)       newSL_B=Fb11;
if(Low[1]<=Fb11 && Low[1]>(Fb11+Fb10)/2)       newSL_B=(Fb11+Fb10)/2;
if(Low[1]<=(Fb10+Fb11)/2 && Low[1]>Fb10)       newSL_B=Fb10;
if(Low[1]<=Fb10 && Low[1]>(Fb10+Fb9)/2)        newSL_B=Fb9;
if(Low[1]<=(Fb10+Fb9)/2 && Low[1]>Fb9)         newSL_B=Fb8;
if(Low[1]<=Fb9  && Low[1]>Fb8)                 newSL_B=Fb7;
if(Low[1]<=Fb8  && Low[1]>Fb7)                 newSL_B=(Fb7+MedVegas)/2;
if(Low[1]<=Fb7  && Low[1]>MedVegas)            newSL_B=Fb6;
if(Low[1]<=MedVegas && Low[1]>(MedVegas+Fb6)/2)newSL_B=Fb6;
if(Low[1]<=(MedVegas+Fb6)/2 && Low[1]>Fb6)     newSL_B=Fb5;
if(Low[1]<=Fb6  && Low[1]>Fb5)                 newSL_B=Fb4;
if(Low[1]<=Fb5  && Low[1]>Fb4)                 newSL_B=(Fb3+Fb4)/2;
if(Low[1]<=Fb4  && Low[1]>Fb3)                 newSL_B=Fb3;
if(Low[1]<=Fb3  && Low[1]>(Fb3+Fb2)/2)         newSL_B=(Fb3+Fb2)/2;
if(Low[1]<=(Fb3+Fb2)/2  && Low[1]>Fb2)         newSL_B=Fb2;
if(Low[1]<=Fb2  && Low[1]>(Fb2+Fb1)/2)         newSL_B=(Fb1+Fb2)/2;
if(Low[1]<=(Fb2+Fb1)/2 && Low[1]>Fb1)          newSL_B=Fb1;
if(Low[1]<=Fb1)                                newSL_B=Fb1-100*Point;
}
//+------------------------------------------------------------------+```

```//+----------------------------------------------------------------------+
//| Function (table) for specifying Stop Loss values for SELL position   |
//| by Fibo levels according to the highest value of the first candle    |
//+----------------------------------------------------------------------+
void StopLevelFiboSell()
{
if(High[1]<=Fb12 && High[1]>(Fb12+Fb11)/2)        newSL_S=Fb12+100*Point;
if(High[1]<=Fb12 && High[1]>Fb11)                 newSL_S=Fb12;
if(High[1]<=Fb11 && High[1]>Fb11+Fb10)            newSL_S=Fb11;
if(High[1]<=Fb10 && High[1]>(Fb10+Fb9)/2)         newSL_S=(Fb11+Fb10)/2;
if(High[1]<=Fb9  && High[1]>Fb8)                  newSL_S=(Fb10+Fb9)/2;
if(High[1]<=Fb8  && High[1]>Fb7)                  newSL_S=Fb9;
if(High[1]<=Fb7  && High[1]>MedVegas)             newSL_S=Fb8;
if(High[1]<=MedVegas && High[1]>MedVegas)         newSL_S=Fb7;
if(High[1]<=(MedVegas+Fb6)/2 && High[1]>Fb6)      newSL_S=MedVegas;
if(High[1]<=Fb6  && High[1]>Fb5)                  newSL_S=MedVegas;
if(High[1]<=Fb5  && High[1]>Fb4)                  newSL_S=Fb6;
if(High[1]<=Fb4  && High[1]>Fb3)                  newSL_S=Fb5;
if(High[1]<=Fb3  && High[1]>Fb2)                  newSL_S=Fb4;
if(High[1]<=Fb2  && High[1]>(Fb2+Fb1)/2)          newSL_S=(Fb2+Fb3)/2;
if(High[1]<(Fb2+Fb1)/2   && High[1]>Fb1)          newSL_S=Fb2;
if(High[1]<Fb1)                                   newSL_S=(Fb2+Fb1)/2;
}
//+------------------------------------------------------------------+```

```//+----------------------------------------------------------------------+
//| Function (table) for specifying Stop Loss values for BUY position    |
//| by Fibo levels according to the lowest value of the first candle     |
//+----------------------------------------------------------------------+
void StoplevelFiboBuy()
{
double Fb1,Fb2,Fb3,Fb4,Fb5,Fb6,Fb7,Fb8,Fb9,Fb10,Fb11,Fb12,Fb13;
double Ma144_1 = iMA(NULL,0,144,0,MODE_EMA,PRICE_CLOSE,1);
double Ma169_1 = iMA(NULL,0,169,0,MODE_EMA,PRICE_CLOSE,1);
double MedVegas=NormalizeDouble((Ma144_1+Ma169_1)/2,Digits);
Fb1=MedVegas-377*Point;     Fb12=MedVegas+377*Point;
Fb2=MedVegas-233*Point;     Fb11=MedVegas+233*Point;
Fb3=MedVegas-144*Point;     Fb10=MedVegas+144*Point;
Fb4=MedVegas-89*Point;      Fb9=MedVegas+89*Point;
Fb5=MedVegas-55*Point;      Fb8=MedVegas+55*Point;
Fb6=MedVegas-34*Point;      Fb7=MedVegas+34*Point;
if(Low[1]>Fb12)                                newSL_B=Fb12-100*Point;
if(Low[1]<=Fb12 && Low[1]>(Fb12+Fb11)/2)       newSL_B=(Fb12+Fb11)/2;
if(Low[1]<=(Fb12+Fb11)/2 && Low[1]>Fb11)       newSL_B=Fb11;
if(Low[1]<=Fb11 && Low[1]>(Fb11+Fb10)/2)       newSL_B=(Fb11+Fb10)/2;
if(Low[1]<=(Fb10+Fb11)/2 && Low[1]>Fb10)       newSL_B=Fb10;
if(Low[1]<=Fb10 && Low[1]>(Fb10+Fb9)/2)        newSL_B=Fb9;
if(Low[1]<=(Fb10+Fb9)/2 && Low[1]>Fb9)         newSL_B=Fb8;
if(Low[1]<=Fb9  && Low[1]>Fb8)                 newSL_B=Fb7;
if(Low[1]<=Fb8  && Low[1]>Fb7)                 newSL_B=(Fb7+MedVegas)/2;
if(Low[1]<=Fb7  && Low[1]>MedVegas)            newSL_B=Fb6;
if(Low[1]<=MedVegas && Low[1]>(MedVegas+Fb6)/2)newSL_B=Fb6;
if(Low[1]<=(MedVegas+Fb6)/2 && Low[1]>Fb6)     newSL_B=Fb5;
if(Low[1]<=Fb6  && Low[1]>Fb5)                 newSL_B=Fb4;
if(Low[1]<=Fb5  && Low[1]>Fb4)                 newSL_B=(Fb3+Fb4)/2;
if(Low[1]<=Fb4  && Low[1]>Fb3)                 newSL_B=Fb3;
if(Low[1]<=Fb3  && Low[1]>(Fb3+Fb2)/2)         newSL_B=(Fb3+Fb2)/2;
if(Low[1]<=(Fb3+Fb2)/2  && Low[1]>Fb2)         newSL_B=Fb2;
if(Low[1]<=Fb2  && Low[1]>(Fb2+Fb1)/2)         newSL_B=(Fb1+Fb2)/2;
if(Low[1]<=(Fb2+Fb1)/2 && Low[1]>Fb1)          newSL_B=Fb1;
if(Low[1]<=Fb1)                                newSL_B=Fb1-100*Point;
}
// ----------------------------------------------------------------------+```

```//+------------------------------------------------------------------+
//| TRAILING OPEN POSITIONS                                          |
//+------------------------------------------------------------------+
for(int i=OrdersTotal()-1; i>=0; i--)
{
if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{Print("Order selection error = ",GetLastError());}
if(OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUY)
{
if(OrderMagicNumber()==Magic)
{
// ----------------------------------------------------------------------+
if((High[1]-OrderOpenPrice())>=SL_B*Point && OrderLots()==0.2)Close_B_lot();
// ----------------------------------------------------------------------+
if((High[1]-OrderOpenPrice())/Point>=100 && OrderLots()==0.1 && OrderStopLoss()<OrderOpenPrice())
{
Print(" 1 - StopLoss shift");
if(!OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+2*Point,OrderTakeProfit(),0,Aqua))
{
Print(" at Modif.ord.# ",OrderTicket()," Error # ",GetLastError());
}
return;
}
// ----------------------------------------------------------------------+
if((High[1]-OrderOpenPrice())>=120*Point && OrderOpenPrice()>Ma144_1-144*Point)
{
StoplevelFiboBuy();
newSL_B=newSL_B+21*Point;
if((Bid-newSL_B)/Point<StopLevel)newSL_B=Bid-StopLevel*Point;
if(newSL_B>OrderStopLoss() && (Bid-newSL_B)/Point>StopLevel)
{
Print("2nd shift of StopLoss ");
Modyf_B_lot();
}
}
// ----------------------------------------------------------------------+
if((High[1]-OrderOpenPrice())>=200*Point && (High[1]-OrderOpenPrice())<=250*Point)
{
StoplevelFiboBuy();
if((Bid-newSL_B)/Point<StopLevel)newSL_B=Bid-StopLevel*Point;
if(newSL_B>OrderStopLoss() && (Bid-newSL_B)/Point>StopLevel)
{
Print(" 3rd shift by level order # ",OrderTicket());
Modyf_B_lot();
}
}
// ----------------------------------------------------------------------+
if((High[1]-OrderOpenPrice())>=250*Point && OrderOpenPrice()>Ma144_1-144*Point)
{
StoplevelFiboBuy();
newSL_B=newSL_B+10*Point;
if((Bid-newSL_B)/Point<StopLevel) newSL_B=Bid-StopLevel*Point;
if(newSL_B>OrderStopLoss() && (Bid-newSL_B)/Point>StopLevel)
{
Print(" 4th shift by level order # ",OrderTicket());
Modyf_B_lot();
}
}
// ----------------------------------------------------------------------+
if((High[1]-OrderOpenPrice())>=300*Point && (High[1]-OrderOpenPrice())<=350*Point)
{
StoplevelFiboBuy();
newSL_B=newSL_B+20*Point;
if((Bid-newSL_B)/Point<StopLevel) newSL_B=Bid-StopLevel*Point;
if(newSL_B>OrderStopLoss() && (Bid-newSL_B)/Point>StopLevel)
{
Print(" 5th shift by level order # ",OrderTicket());
Modyf_B_lot();
}
}
// ----------------------------------------------------------------------+
...
}
}
}
}
```

“Vegas”指标应该用于查看斐波那契线，如同在光亮的截屏上一样。 可以在 MQL4 网站进行查看。 https://www.mql5.com/en/code/7148

### 总结

• LevelFiboKgd.mq4
• StopLevelFiboBuy.mq4
• StopLevelFiboSell.mq4
• 演示文件：
• Demo_trail_Buy_v1.mq4 – Demo file for Buy
• Demo_trail_Sell_v1.mq4 - Demo file for Sell

LevelFiboKgd.mq4 (1.96 KB)
Expert Advisor 参数的测试(优化)技术和一些选择条件