|
|
现有一网格EA,是按着振荡市业设计的。但遇到单边市,很容易爆仓。程序在设计时,有一点做的很好,就是当当前净值比余额增长1%时,会平掉全部仓位,并重新布单。
想改变EA中的一处设计,可以用在单边及振荡市。
当有持仓的多单被止赢或止损后,程序会在原价位挂SELL STOP单;当有持仓的空单被止赢或止损后,程序会在原价位挂BUY STOP单。也就是被止赢或止损的单,程序会在原价位反向挂单。
想把此处修改成:当有持仓的多单被止赢或止损后,当前价格在原价位50点以下时,程序在原价位挂BUY STOP单;当持仓的空单被止赢或止损后,当前价格在原价位50点以上时,程序在原价位挂SELL STOP单。也就是改成为正向挂单。其它的不变,谢谢
以下是EA源码:
//+------------------------------------------------------------------+
//| hedge.mq4 |
//+------------------------------------------------------------------+
extern double Lots=0.10;
extern bool Use_TrailingStop=false;
extern int TrailingStop=15;
extern bool Use_BreakEven=false;
extern int BreakEvenPoint=40;
extern int BreakEven_Move_Stop_To=0;
extern bool Use_StopLoss=false;
extern int SL=50;
extern bool Use_TakeProfit=true;
extern int TP=50;
extern int BuyStep=50;
extern int SellStep=50;
extern int BuyOrderNumber=4;
extern int SellOrderNumber=4;
extern int BuyBegin=0;
extern int SellBegin=50;
extern int MinDistance=4;
extern bool houseclean=true;
extern double Equity_Percent=1;
extern int Magic_Number=3456;
extern int Slippage=3;
string com="hedge";
int init(){return(0);}
int deinit(){return(0);}
int start()
{
Comment(" BuyStop orders: "+TotalOrdersMode(Magic_Number,OP_BUYSTOP)+"\n SellStop orders: "+TotalOrdersMode(Magic_Number,OP_SELLSTOP)+"\n Sell orders: "+TotalOrdersMode(Magic_Number,OP_SELL)+"\n Buy orders: "+TotalOrdersMode(Magic_Number,OP_BUY));
double s=0;
double SL1=0;double TP1=0;
int i=0,j=0,r=-1;
bool g=false;
int k=0;
if (TotalOrders(Magic_Number)==0){
s=next(OP_BUYSTOP,Ask,BuyBegin,MinDistance,BuyStep,Digits);
for (j=0;j<BuyOrderNumber;j++){
r=-1;
SL1=0;TP1=0;
if (Use_StopLoss) SL1=NormalizeDouble(s+BuyStep*Point*j-SL*Point,Digits);
if (Use_TakeProfit) TP1=NormalizeDouble(s+BuyStep*Point*j+TP*Point,Digits);
k=0;
while (r==-1 && k<10){
r=OrderSend(Symbol(),OP_BUYSTOP,Lots,NormalizeDouble(s+BuyStep*Point*j,Digits),Slippage,SL1,TP1,com,Magic_Number,0,Blue);
i=GetLastError();
if (i!=1 && i!=0) Print("Start Buystop Error :"+error(i)+" at "+Symbol()+" "+Period());
k++;
}
}
s=next(OP_SELLSTOP,Bid,SellBegin,MinDistance,SellStep,Digits);
for (j=0;j<SellOrderNumber;j++){
r=-1;
SL1=0;TP1=0;
if (Use_StopLoss) SL1=NormalizeDouble(s-SellStep*Point*j+SL*Point,Digits);
if (Use_TakeProfit) TP1=NormalizeDouble(s-SellStep*Point*j-TP*Point,Digits);
k=0;
while (r==-1 && k<10){
r=OrderSend(Symbol(),OP_SELLSTOP,Lots,NormalizeDouble(s-SellStep*Point*j,Digits),Slippage,SL1,TP1,com,Magic_Number,0,Red);
i=GetLastError();
if (i!=1 && i!=0) Print("Start SellStop Error :"+error(i)+" at "+Symbol()+" "+Period());
k++;
}
}
}
if (TotalOrders(Magic_Number)>0){
check(Magic_Number);
check1(Magic_Number);
if (Use_TrailingStop) tr(Magic_Number,TrailingStop);
if (Use_BreakEven) be(Magic_Number,BreakEvenPoint,BreakEven_Move_Stop_To);
if (houseclean && AccountEquity()>=AccountBalance()+((AccountBalance()*Equity_Percent)/100)){
Close_All();
}
}
return(0);
}
void Close_All(){
bool g1=false;
int i=0;
while (!g1){
g1=true;
for (int j=0;j<OrdersTotal();j++){
OrderSelect(j,SELECT_BY_POS);
if (OrderType()==OP_BUY){
RefreshRates();
if (!OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(MarketInfo(OrderSymbol(),MODE_BID),Digits),Slippage,Pink)){
i=GetLastError();
if (i!=1 && i!=0) Print("Close_all Buy Error :"+error(i)+" at "+Symbol()+" "+Period());
}else{
g1=false;
break;
}
}
if (OrderType()==OP_SELL){
RefreshRates();
if (!OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(MarketInfo(OrderSymbol(),MODE_ASK),Digits),Slippage,Pink)){
i=GetLastError();
if (i!=1 && i!=0) Print("Close_all Sell Error :"+error(i)+" at "+Symbol()+" "+Period());
}else{
g1=false;
break;
}
}
if (OrderType()==OP_BUYSTOP || OrderType()==OP_SELLSTOP){
if (!OrderDelete(OrderTicket())){
i=GetLastError();
if (i!=1 && i!=0) Print("Close_all orders Error :"+error(i)+" at "+Symbol()+" "+Period());
}else{
g1=false;
break;
}
}
}
}
}
void check(int m){
int j=0;
int r=-1;
int i=0;
int k=0;
double SL1=0;double TP1=0;
double s=next(OP_BUYSTOP,Ask,BuyBegin,MinDistance,BuyStep,Digits);
for (j=0;j<MathRound(BuyOrderNumber/2);j++){
if (!ex(m,s+BuyStep*Point*j)){
r=-1;
SL1=0;TP1=0;
if (Use_StopLoss) SL1=NormalizeDouble(s+BuyStep*Point*j-SL*Point,Digits);
if (Use_TakeProfit) TP1=NormalizeDouble(s+BuyStep*Point*j+TP*Point,Digits);
k=0;
while (r==-1 && k<10){
r=OrderSend(Symbol(),OP_BUYSTOP,Lots,NormalizeDouble(s+BuyStep*Point*j,Digits),Slippage,SL1,TP1,com,Magic_Number,0,LightBlue);
i=GetLastError();
if (i!=1 && i!=0) Print("check Buystop Error :"+error(i)+" at "+Symbol()+" "+Period());
k++;
}
}
}
s=next(OP_SELLSTOP,Bid,SellBegin,MinDistance,SellStep,Digits);
for (j=0;j<MathRound(SellOrderNumber/2);j++){
if (!ex(m,s-SellStep*Point*j)){
r=-1;
SL1=0;TP1=0;
if (Use_StopLoss) SL1=NormalizeDouble(s-SellStep*Point*j+SL*Point,Digits);
if (Use_TakeProfit) TP1=NormalizeDouble(s-SellStep*Point*j-TP*Point,Digits);
k=0;
while (r==-1 && k<10){
r=OrderSend(Symbol(),OP_SELLSTOP,Lots,NormalizeDouble(s-SellStep*Point*j,Digits),Slippage,SL1,TP1,com,Magic_Number,0,Orange);
i=GetLastError();
if (i!=1 && i!=0) Print("Start SellStop Error :"+error(i)+" at "+Symbol()+" "+Period());
k++;
}
}
}
}
void check1(int m){
int i=0,j=0,r=-1;
double TP1=0,SL1=0;
double s=0;
int k=0;
if (TotalOrdersMode(m,OP_BUYSTOP)<BuyOrderNumber+TotalOrdersMode(m,OP_BUY)){
OrderSelect(lh_order(m,1),SELECT_BY_TICKET);
s=OrderOpenPrice();
for(j=1;j<BuyOrderNumber+TotalOrdersMode(m,OP_BUY)-TotalOrdersMode(m,OP_BUYSTOP);j++){
r=-1;
SL1=0;TP1=0;
if (Use_StopLoss) SL1=NormalizeDouble(s+BuyStep*Point*j-SL*Point,Digits);
if (Use_TakeProfit) TP1=NormalizeDouble(s+BuyStep*Point*j+TP*Point,Digits);
k=0;
while (r==-1 && k<10){
r=OrderSend(Symbol(),OP_BUYSTOP,Lots,NormalizeDouble(s+BuyStep*Point*j,Digits),Slippage,SL1,TP1,com,Magic_Number,0,DarkBlue);
i=GetLastError();
if (i!=1 && i!=0) Print("check 1 Buystop Error :"+error(i)+" at "+Symbol()+" "+Period());
k++;
}
}
}
if (TotalOrdersMode(m,OP_SELLSTOP)<SellOrderNumber+TotalOrdersMode(m,OP_SELL)){
OrderSelect(lh_order(m,0),SELECT_BY_TICKET);
s=OrderOpenPrice();
for(j=1;j<BuyOrderNumber+TotalOrdersMode(m,OP_SELL)-TotalOrdersMode(m,OP_SELLSTOP);j++){
r=-1;
SL1=0;TP1=0;
if (Use_StopLoss) SL1=NormalizeDouble(s-SellStep*Point*j+SL*Point,Digits);
if (Use_TakeProfit) TP1=NormalizeDouble(s-SellStep*Point*j-TP*Point,Digits);
k=0;
while (r==-1 && k<10){
Alert(s-SellStep*Point*j);
r=OrderSend(Symbol(),OP_SELLSTOP,Lots,NormalizeDouble(s-SellStep*Point*j,Digits),Slippage,SL1,TP1,com,Magic_Number,0,Maroon);
i=GetLastError();
if (i!=1 && i!=0) Print("check 1 SellStop Error :"+SL1+" "+error(i)+" at "+Symbol()+" "+Period());
k++;
}
}
}
}
void tr(int magic,int triling_i){
for (int k=0;k<OrdersTotal();k++){
OrderSelect(k,SELECT_BY_POS);
if (OrderMagicNumber()==magic){
if (OrderType()==OP_BUY && NormalizeDouble(Ask-OrderOpenPrice(),Digits)>=triling_i*Point && OrderStopLoss()<NormalizeDouble(Ask-triling_i*Point,Digits)) {
if (!OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Ask-triling_i*Point,Digits),OrderTakeProfit(),0,Blue)){
Print("Trailing stop error Buy :"+error(GetLastError()));
}
}
if (OrderType()==OP_SELL && NormalizeDouble(Bid+triling_i*Point,Digits)<=OrderOpenPrice() && (OrderStopLoss()>NormalizeDouble(Bid+triling_i*Point,Digits) || OrderStopLoss()==0)) {
if (!OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Bid+triling_i*Point,Digits),OrderTakeProfit(),0,Red)){
Print("Trailing stop error Sell :"+error(GetLastError()));
}
}
}
}
}
void be(int magic,int b,int mv){
for (int k=0;k<OrdersTotal();k++){
OrderSelect(k,SELECT_BY_POS);
if (OrderMagicNumber()==magic){
if (OrderType()==OP_BUY && NormalizeDouble(Bid-OrderOpenPrice(),Digits)>=b*Point && OrderStopLoss()<NormalizeDouble(OrderOpenPrice()+mv*Point,Digits)) {
if (!OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(OrderOpenPrice()+mv*Point,Digits),OrderTakeProfit(),0,Blue)){
Print("Breakeven stop error Buy :"+error(GetLastError()));
}
}
if (OrderType()==OP_SELL && NormalizeDouble(Ask+b*Point,Digits)<=OrderOpenPrice() && (OrderStopLoss()>NormalizeDouble(OrderOpenPrice()-mv*Point,Digits) || OrderStopLoss()==0)) {
if (!OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(OrderOpenPrice()-mv*Point,Digits),OrderTakeProfit(),0,Red)){
Print("Breakeven stop error Sell :"+error(GetLastError()));
}
}
}
}
}
double next(int mode,double cp,int start,int min,int step,int d){
double r=0;
if (mode==OP_BUYSTOP){
r=((base(cp,d)+100*Point+start*Point)-(step*Point*(MathFloor((100+start-l2(cp,d))/step))));
if (MathAbs(r-cp)<min*Point) {return(r+step*Point);}else {return (r);}
}
if (mode==OP_SELLSTOP){
r=((base(cp,d)-100*Point+start*Point)+(step*Point*(MathFloor((100-start+l2(cp,d))/step))));
if (MathAbs(cp-r)<min*Point) {return(r-step*Point);}else {return (r);}
}
}
double base(double n,int d){
if (d<4) return (MathFloor(n));
double rez=0;
rez=MathFloor((n-MathFloor(n))*100)/100;
return (rez+MathFloor(n));
}
int l2(double n,int d){
if (d<4) return ((n-MathFloor(n))*100);
return (NormalizeDouble((n*100-MathFloor(n*100))*100,0));
}
int TotalOrdersMode(int magic,int mode){
int rez=0;
for (int i=0;i<OrdersTotal();i++){
OrderSelect(i,SELECT_BY_POS);
if (OrderMagicNumber()==magic && OrderType()==mode){
rez++;
}
}
return(rez);
}
int lh_order(int m,int mode){
int rl=0,rh=0;
double l=500000,h=0;
for (int k=0;k<OrdersTotal();k++){
OrderSelect(k,SELECT_BY_POS);
if (OrderMagicNumber()==m && OrderOpenPrice()<l) {
l=OrderOpenPrice();
rl=OrderTicket();
}
if (OrderMagicNumber()==m && OrderOpenPrice()>h) {
h=OrderOpenPrice();
rh=OrderTicket();
}
}
if (mode==0) return (rl);
return (rh);
}
bool ex(int m,double p){
for (int i=0;i<OrdersTotal();i++){
OrderSelect(i,SELECT_BY_POS);
if (OrderMagicNumber()==m && MathAbs(OrderOpenPrice()-NormalizeDouble(p,Digits))<=Slippage*Point+(Ask-Bid)){
return (true);
}
}
return (false);
}
int TotalOrders(int magic){
int rez=0;
for (int i=0;i<OrdersTotal();i++){
OrderSelect(i,SELECT_BY_POS);
if (OrderMagicNumber()==magic){
rez++;
}
}
return(rez);
}
string error(int err){
switch (err){
case 0 :return("No error returned.");
case 1 :return("No error returned, but the result is unknown. ");
case 2 :return("Common error. ");
case 3 :return("Invalid trade parameters. ");
case 4 :return("Trade server is busy. ");
case 5 :return("Old version of the client terminal. ");
case 6 :return("No connection with trade server. ");
case 7 :return("Not enough rights. ");
case 8 :return("Too frequent requests.");
case 9 :return("Malfunctional trade operation. ");
case 64 :return("Account disabled. ");
case 65 :return("Invalid account. ");
case 128 :return("Trade timeout. ");
case 129 :return("Invalid price. ");
case 130 :return("Invalid stops. ");
case 131 :return("Invalid trade volume. ");
case 132 :return("Market is closed. ");
case 133 :return("Trade is disabled. ");
case 134 :return("Not enough money. ");
case 135 :return("Price changed. ");
case 136 :return("Off quotes. ");
case 137 :return("Broker is busy.");
case 138 :return("Requote. ");
case 139 :return("Order is locked. ");
case 140 :return("Long positions only allowed. ");
case 141 :return("Too many requests. ");
case 145 :return("Modification denied because order too close to market. ");
case 146 :return("Trade context is busy. ");
case 147 :return("Expirations are denied by broker. ");
case 148 :return("The amount of open and pending orders has reached the limit set by the broker. ");
case 4000 :return("No error. ");
case 4001 :return("Wrong function pointer. ");
case 4002 :return("Array index is out of range. ");
case 4003 :return("No memory for function call stack. ");
case 4004 :return("Recursive stack overflow. ");
case 4005 :return("Not enough stack for parameter. ");
case 4006 :return("No memory for parameter string. ");
case 4007 :return("No memory for temp string. ");
case 4008 :return("Not initialized string. ");
case 4009 :return("Not initialized string in array. ");
case 4010 :return("No memory for array string. ");
case 4011 :return("Too long string. ");
case 4012 :return("Remainder from zero divide. ");
case 4013 :return("Zero divide. ");
case 4014 :return("Unknown command. ");
case 4015 :return("Wrong jump (never generated error). ");
case 4016 :return("Not initialized array. ");
case 4017 :return("DLL calls are not allowed. ");
case 4018 :return("Cannot load library. ");
case 4019 :return("Cannot call function. ");
case 4020 :return("Expert function calls are not allowed. ");
case 4021 :return("Not enough memory for temp string returned from function. ");
case 4022 :return("System is busy (never generated error). ");
case 4050 :return("Invalid function parameters count. ");
case 4051 :return("Invalid function parameter value. ");
case 4052 :return("String function internal error. ");
case 4053 :return("Some array error. ");
case 4054 :return("Incorrect series array using. ");
case 4055 :return("Custom indicator error. ");
case 4056 :return("Arrays are incompatible. ");
case 4057 :return("Global variables processing error. ");
case 4058 :return("Global variable not found. ");
case 4059 :return("Function is not allowed in testing mode. ");
case 4060 :return("Function is not confirmed. ");
case 4061 :return("Send mail error. ");
case 4062 :return("String parameter expected. ");
case 4063 :return("Integer parameter expected. ");
case 4064 :return("Double parameter expected. ");
case 4065 :return("Array as parameter expected. ");
case 4066 :return("Requested history data in updating state. ");
case 4067 :return("Some error in trading function. ");
case 4099 :return("End of file. ");
case 4100 :return("Some file error.");
case 4101 :return("Wrong file name. ");
case 4102 :return("Too many opened files. ");
case 4103 :return("Cannot open file. ");
case 4104 :return("Incompatible access to a file. ");
case 4105 :return("No order selected. ");
case 4106 :return("Unknown symbol. ");
case 4107 :return("Invalid price. ");
case 4108 :return("Invalid ticket. ");
case 4109 :return("Trade is not allowed. ");
case 4110 :return("Longs are not allowed. ");
case 4111 :return("Shorts are not allowed. ");
case 4200 :return("Object exists already. ");
case 4201 :return("Unknown object property. ");
case 4202 :return("Object does not exist. ");
case 4203 :return("Unknown object type. ");
case 4204 :return("No object name. ");
case 4205 :return("Object coordinates error. ");
case 4206 :return("No specified subwindow. ");
case 4207 :return("Some error in object function. ");
default: return("Unknown error:"+err);
}
}
[ 本帖最后由 zhuyabin 于 2008-11-4 09:00 编辑 ] |
|