问吧! - 页 17

 

嘿,编码员们

Aaragorn和我最近一直在研究这个EA。

extern int MagicNumber = 0;

extern bool SignalMail = False;

extern bool EachTickMode = False;

extern double Lots = 0.1;

extern int Slippage = 3;

extern bool StopLossMode = False;

extern int StopLoss = 5;

extern bool TakeProfitMode = True;

extern int TakeProfit = 50;

extern bool TrailingStopMode = False;

extern int TrailingStop = 5;

extern int MaxOpenTrade = 1;

extern int Shift = 2;

extern double Slope = 2;

extern int EnterEMA = 38;

extern int ExitEMA = 1500;

#define SIGNAL_NONE 0

#define SIGNAL_BUY 1

#define SIGNAL_SELL 2

#define SIGNAL_CLOSEBUY 3

#define SIGNAL_CLOSESELL 4

int BarCount;

int Current;

bool TickCheck = False;

//+------------------------------------------------------------------+

//| expert initialization function |

//+------------------------------------------------------------------+

int init() {

BarCount = Bars;

if (EachTickMode) Current = 0; else Current = 1;

return(0);

}

//+------------------------------------------------------------------+

//| expert deinitialization function |

//+------------------------------------------------------------------+

int deinit() {

return(0);

}

//+------------------------------------------------------------------+

//| expert start function |

//+------------------------------------------------------------------+

int start() {

int Order = SIGNAL_NONE;

int Total, Ticket;

double StopLossLevel, TakeProfitLevel;

if (EachTickMode && Bars != BarCount) TickCheck = False;

Total = OrdersTotal();

Order = SIGNAL_NONE;

//+------------------------------------------------------------------+

//| Variable Begin |

//+------------------------------------------------------------------+

double Buy1_1 = iMA(NULL, 0, EnterEMA, 0, MODE_EMA, PRICE_CLOSE, Current + 0);

double Buy1_2 = iClose(NULL, 0, Current + 0);

double Buy2_1 = iMA(NULL, 0, EnterEMA, 0, MODE_EMA, PRICE_CLOSE, Current + Shift);

double Buy2_2 = iClose(NULL, 0, Current + Shift);

double Sell1_1 = iMA(NULL, 0, EnterEMA, 0, MODE_EMA, PRICE_CLOSE, Current + 0);

double Sell1_2 = iClose(NULL, 0, Current + 0);

double Sell2_1 = iMA(NULL, 0, EnterEMA, 0, MODE_EMA, PRICE_CLOSE, Current + Shift);

double Sell2_2 = iClose(NULL, 0, Current + Shift);

double CloseBuy1_1 = iClose(NULL, 0, Current + 0);

double CloseBuy1_2 = iMA(NULL, 0, ExitEMA, 0, MODE_EMA, PRICE_CLOSE, Current + 0);

double CloseSell1_1 = iClose(NULL, 0, Current + 0);

double CloseSell1_2 = iMA(NULL, 0, ExitEMA, 0, MODE_EMA, PRICE_CLOSE, Current + 0);

//+------------------------------------------------------------------+

//| Variable End |

//+------------------------------------------------------------------+

bool IsTrade = False;

for (int i = 0; i < Total; i ++) {

OrderSelect(i, SELECT_BY_POS, MODE_TRADES);

if(OrderType() <= OP_SELL && OrderSymbol() == Symbol()) {

IsTrade = True;

if(OrderType() == OP_BUY) {

//+------------------------------------------------------------------+

//| Signal Begin(Exit Buy) |

//+------------------------------------------------------------------+

if (Sell1_1 < CloseBuy1_2) Order = SIGNAL_CLOSEBUY;

//+------------------------------------------------------------------+

//| Signal End(Exit Buy) |

//+------------------------------------------------------------------+

if (Order == SIGNAL_CLOSEBUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {

OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, MediumSeaGreen);

if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Close Buy");

if (!EachTickMode) BarCount = Bars;

IsTrade = False;

continue;

}

if(TrailingStopMode && TrailingStop > 0) {

if(Bid - OrderOpenPrice() > Point * TrailingStop) {

if(OrderStopLoss() < Bid - Point * TrailingStop) {

OrderModify(OrderTicket(), OrderOpenPrice(), Bid - Point * TrailingStop, OrderTakeProfit(), 0, MediumSeaGreen);

if (!EachTickMode) BarCount = Bars;

continue;

}

}

}

} else {

//+------------------------------------------------------------------+

//| Signal Begin(Exit Sell) |

//+------------------------------------------------------------------+

if (Sell1_1 > CloseBuy1_2) Order = SIGNAL_CLOSESELL;

//+------------------------------------------------------------------+

//| Signal End(Exit Sell) |

//+------------------------------------------------------------------+

if (Order == SIGNAL_CLOSESELL && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {

OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, DarkOrange);

if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + " Close Sell");

if (!EachTickMode) BarCount = Bars;

IsTrade = False;

continue;

}

if(TrailingStopMode && TrailingStop > 0) {

if((OrderOpenPrice() - Ask) > (Point * TrailingStop)) {

if((OrderStopLoss() > (Ask + Point * TrailingStop)) || (OrderStopLoss() == 0)) {

OrderModify(OrderTicket(), OrderOpenPrice(), Ask + Point * TrailingStop, OrderTakeProfit(), 0, DarkOrange);

if (!EachTickMode) BarCount = Bars;

continue;

}

}

}

}

}

}

//+------------------------------------------------------------------+

//| Signal Begin(Entry logics) |

//+------------------------------------------------------------------+

if (Buy1_1 < Buy1_2 && Buy2_1 Buy2_1*/) Order = SIGNAL_BUY;

if (Sell1_1 > Sell1_2 && Sell2_1 > Sell2_2/* && Sell1_1 + Slope*Point < Sell2_1*/) Order = SIGNAL_SELL;

//+------------------------------------------------------------------+

//| Signal End |

//+------------------------------------------------------------------+

if (Order == SIGNAL_BUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {

IsTrade = False;//---allows multiple orders to open

if(!IsTrade) {

if (AccountFreeMargin() < (1000 * Lots)) {

Print("We have no money. Free Margin = ", AccountFreeMargin());

return(0);

}

if (StopLossMode) StopLossLevel = Ask - StopLoss * Point; else StopLossLevel = 0.0;

if (TakeProfitMode) TakeProfitLevel = Ask + TakeProfit * Point; else TakeProfitLevel = 0.0;

if(OrdersTotal() < MaxOpenTrade)

Ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, StopLossLevel, TakeProfitLevel, "Buy(#" + MagicNumber + ")", MagicNumber, 0, DodgerBlue);

if(Ticket > 0) {

if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) {

Print("BUY order opened : ", OrderOpenPrice());

if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + " Open Buy");

} else {

Print("Error opening BUY order : ", GetLastError());

}

}

if (EachTickMode) TickCheck = True;

if (!EachTickMode) BarCount = Bars;

return(0);

}

}

if (Order == SIGNAL_SELL && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {

IsTrade = False;//---allows multiple orders to open

if(!IsTrade) {

if (AccountFreeMargin() < (1000 * Lots)) {

Print("We have no money. Free Margin = ", AccountFreeMargin());

return(0);

}

if (StopLossMode) StopLossLevel = Bid + StopLoss * Point; else StopLossLevel = 0.0;

if (TakeProfitMode) TakeProfitLevel = Bid - TakeProfit * Point; else TakeProfitLevel = 0.0;

if(OrdersTotal() < MaxOpenTrade)

Ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, StopLossLevel, TakeProfitLevel, "Sell(#" + MagicNumber + ")", MagicNumber, 0, DeepPink);

if(Ticket > 0) {

if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) {

Print("SELL order opened : ", OrderOpenPrice());

if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Open Sell");

} else {

Print("Error opening SELL order : ", GetLastError());

}

}

if (EachTickMode) TickCheck = True;

if (!EachTickMode) BarCount = Bars;

return(0);

}

}

if (!EachTickMode) BarCount = Bars;

return(0);

}

//+------------------------------------------------------------------+

"Shift "是指你往后看多少期,"Slope "是指 "EnterEMA "在 "Shift "中上升了多少个点。我们还在完善这个策略,我想知道你是否能帮我解决一些问题。

 

这是为了在我缩小到我有问题的代码之前提供一些背景......

#property copyright "Aaragorn"

#property link "http://sufx.core.t3-ism.net/ExpertAdvisorBuilder/"

extern int MagicNumber = 0;

extern bool SignalMail = False;

extern bool EachTickMode = False;

extern double Lots = 0.35;

extern int Slippage = 3;

extern bool StopLossMode = False;

extern int StopLoss = 5;

extern bool TakeProfitMode = True;

extern int TakeProfit = 42;

extern bool TrailingStopMode = False;

extern int TrailingStop = 10;

extern int MaxOpenTrade = 1;

extern int Shift = 3;

//extern double Slope = 2;

extern int EntLongEMA = 46;

extern int EntShortEMA = 1;

extern int ExitEMA = 52;

extern int TrendEMA = 150;

//+-----------close based on not triggering trailing stop in allotted time----------------+

extern int MonitorInMinutes = 60; // minutes after open to check state of trade

extern int ThresholdMove = 1; // if after that time we don't have +'x' pips we will exit

extern int MinsMultiplier = 90; // multiplies the MonitorInMinutes to make minutes (if 'x'=60) into hours

#define SIGNAL_NONE 0

#define SIGNAL_BUY 1

#define SIGNAL_SELL 2

#define SIGNAL_CLOSEBUY 3

#define SIGNAL_CLOSESELL 4

int BarCount;

int Current;

bool TickCheck = False;

//+------------------------------------------------------------------+

//| expert initialization function |

//+------------------------------------------------------------------+

int init() {

BarCount = Bars;

if (EachTickMode) Current = 0; else Current = 1;

return(0);

}

//+------------------------------------------------------------------+

//| expert deinitialization function |

//+------------------------------------------------------------------+

int deinit() {

return(0);

}

//+------------------------------------------------------------------+

//| expert start function |

//+------------------------------------------------------------------+

int start() {

int Order = SIGNAL_NONE;

int Total, Ticket;

double StopLossLevel, TakeProfitLevel;

if (EachTickMode && Bars != BarCount) TickCheck = False;

Total = OrdersTotal();

Order = SIGNAL_NONE;

//+------------------------------------------------------------------+

//| Variable Begin |

//+------------------------------------------------------------------+

double Buy1_1 = iMA(NULL, 0, EntLongEMA, 0, MODE_EMA, PRICE_CLOSE, Current + 0);

double Buy1_2 = iMA(NULL, 0, EntShortEMA, 0, MODE_EMA, PRICE_CLOSE, Current + 0);

double Buy2_1 = iMA(NULL, 0, EntLongEMA, 0, MODE_EMA, PRICE_CLOSE, Current + Shift);

double Buy2_2 = iMA(NULL, 0, EntShortEMA, 0, MODE_EMA, PRICE_CLOSE, Current + Shift);

double Sell1_1 = iMA(NULL, 0, EntLongEMA, 0, MODE_EMA, PRICE_CLOSE, Current + 0);

double Sell1_2 = iMA(NULL, 0, EntShortEMA, 0, MODE_EMA, PRICE_CLOSE, Current + 0);

double Sell2_1 = iMA(NULL, 0, EntLongEMA, 0, MODE_EMA, PRICE_CLOSE, Current + Shift);

double Sell2_2 = iMA(NULL, 0, EntShortEMA, 0, MODE_EMA, PRICE_CLOSE, Current + Shift);

double CloseBuy1_1 = iMA(NULL, 0, EntShortEMA, 0, MODE_EMA, PRICE_CLOSE, Current + 0);

double CloseBuy1_2 = iMA(NULL, 0, ExitEMA, 0, MODE_EMA, PRICE_CLOSE, Current + 0);

double CloseSell1_1 = iMA(NULL, 0, EntShortEMA, 0, MODE_EMA, PRICE_CLOSE, Current + 0);

double CloseSell1_2 = iMA(NULL, 0, ExitEMA, 0, MODE_EMA, PRICE_CLOSE, Current + 0);

double C_trendsetter = iMA(NULL, 0, TrendEMA, 0, MODE_EMA, PRICE_CLOSE, Current + 0);

double S_trendsetter = iMA(NULL, 0, TrendEMA, 0, MODE_EMA, PRICE_CLOSE, Current + Shift);

//+------------------------------------------------------------------+

//| Variable End |

//+------------------------------------------------------------------+

//Check position

bool IsTrade = False;

for (int i = 0; i < Total; i ++) {

OrderSelect(i, SELECT_BY_POS, MODE_TRADES);

if(OrderType() <= OP_SELL && OrderSymbol() == Symbol()) {

IsTrade = True;

if(OrderType() == OP_BUY) {

//Close

//+------------------------------------------------------------------+

//| Signal Begin(Exit Buy)Closing logic for long positions |

//+------------------------------------------------------------------+

// if (Buy1_1 <= C_trendsetter) Order = SIGNAL_CLOSEBUY;

if (Buy1_1 <= C_trendsetter) StopLossMode = True;

if (Buy1_1 <= CloseBuy1_2) CloseOrder();

if (Buy1_1 <= CloseBuy1_2) TrailingStopMode = True;

//+------------------------------------------------------------------+

//| Signal End(Exit Buy) |

//+------------------------------------------------------------------+

if (Order == SIGNAL_CLOSEBUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {

OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, MediumSeaGreen);

if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Close Buy");

if (!EachTickMode) BarCount = Bars;

IsTrade = False;

continue;

}

//Trailing stop

if(TrailingStopMode && TrailingStop > 0) {

if(Bid - OrderOpenPrice() > Point * TrailingStop) {

if(OrderStopLoss() < Bid - Point * TrailingStop) {

OrderModify(OrderTicket(), OrderOpenPrice(), Bid - Point * TrailingStop, OrderTakeProfit(), 0, MediumSeaGreen);

if (!EachTickMode) BarCount = Bars;

TrailingStopMode = False; //resets mode after each order

StopLossMode = False; //resets mode after each order

continue;

}

}

}

} else {

//Close

//+------------------------------------------------------------------+

//| Signal Begin(Exit Sell)Closing logic for short positions |

//+------------------------------------------------------------------+

// if (Buy1_1 >= C_trendsetter) Order = SIGNAL_CLOSEBUY;

if (Buy1_1 >= C_trendsetter) StopLossMode = True;

if (Sell1_1 >= CloseSell1_2) CloseOrder();

if (Sell1_1 >= CloseSell1_2) TrailingStopMode = True;

//+------------------------------------------------------------------+

//| Signal End(Exit Sell) |

//+------------------------------------------------------------------+

if (Order == SIGNAL_CLOSESELL && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {

OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, DarkOrange);

if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + " Close Sell");

if (!EachTickMode) BarCount = Bars;

IsTrade = False;

continue;

}

//Trailing stop

if(TrailingStopMode && TrailingStop > 0) {

if((OrderOpenPrice() - Ask) > (Point * TrailingStop)) {

if((OrderStopLoss() > (Ask + Point * TrailingStop)) || (OrderStopLoss() == 0)) {

OrderModify(OrderTicket(), OrderOpenPrice(), Ask + Point * TrailingStop, OrderTakeProfit(), 0, DarkOrange);

if (!EachTickMode) BarCount = Bars;

TrailingStopMode = False; //resets mode after each order

StopLossMode = False; //resets mode after each order

continue;

}

}

}

}

}

}[/PHP]

I am working on the exit logics... This is the buy.closing...

//+------------------------------------------------------------------+

//| Signal Begin(Exit Buy)Closing logic for long positions |

//+------------------------------------------------------------------+

if (Buy1_1 <= C_trendsetter) Order = SIGNAL_CLOSEBUY;

// if (Buy1_1 <= C_trendsetter) StopLossMode = True;

if (Buy1_1 <= CloseBuy1_2) CloseOrder();

if (Buy1_1 <= CloseBuy1_2) TrailingStopMode = True;

When I turn on the close buy condition it does in fact close the trade.

see attached.

[PHP]//+------------------------------------------------------------------+

//| Signal Begin(Exit Buy)Closing logic for long positions |

//+------------------------------------------------------------------+

// if (Buy1_1 <= C_trendsetter) Order = SIGNAL_CLOSEBUY;

if (Buy1_1 <= C_trendsetter) StopLossMode = True;

if (Buy1_1 <= CloseBuy1_2) CloseOrder();

if (Buy1_1 <= CloseBuy1_2) TrailingStopMode = True;

如果止损 被触发,它应该很快就会收盘,但却没有,但追踪止损显然被触发了,见附件。

附加的文件:
 

还是不按我说的做...。

它对第一笔交易进行了平仓......但对第二笔交易没有......见附件。

附加的文件:
 

if (Buy1_1 + StopLoss*Point <= C_trendsetter) Order = SIGNAL_CLOSEBUY;

这似乎可以工作。

发现了这个错误...

这应该是

if (Buy1_1 + StopLoss*Point <= C_trendsetter) Order = SIGNAL_CLOSESELL;

对于短线...

我复制和粘贴了很多,所以我失去了对订单信号的跟踪...哎。

 

if (Buy1_1 + StopLoss*Point <= C_trendsetter) Order = SIGNAL_CLOSEBUY;

应该是

if (Buy1_1 + StopLoss*Point <= C_trendsetter) Order = SIGNAL_CLOSESELL;

 

颜色问题

如何使移动平均线成为我在策略 测试器图表中打开时指定的颜色?

如何使箭头在我打开策略测试器图表时显示得更大?

 

你认为是否有可能让移动平均线的包络线 以直线方式投射到未来?

 
kidhudi:
你认为是否有可能让移动平均线的包络线以直线方式投射到未来?

移动 平均线是由过去 "x "个时期的平均值决定的。

例如,如果你将移动平均线应用于收盘价,一个时期的收盘价为1.3417,下一个时期的收盘价为5点,再下一个时期的收盘价为5点,等等,移动平均线将 "平均 "这些数值以得出当前数值。

因为价格总是在变化,只有那些回看超过100点左右的移动平均线才会在大多数时候看起来接近于直线。

在回答你的问题时,如果有可能,请告诉我。(笑)

 

你好,我正在开发一个EA,我想用它来做一个价格交叉。

我怎么知道它是否已经交叉了?

如果我这样做,如果Ask>iCustom(),那么它在任何时候都会在上面下单。

如果我做if ask=iCustom(),那么它就会错过买入,因为它每分钟做一次。

在dealbook上,我会做if ask[-1] =iCustom,但我不能在这里找出它,因为Ask和Bid没有历史记录。

 

补充图表

你好。

这可能与本主题无关,但我想知道如何为metatrader 4添加额外的图表......比如白银、澳元、新西兰元、铂金等。有人曾经给过我这个附件,让我在metatrader文件夹中复制粘贴以获得这些图表,但我好像把它弄丢了,所以谁能帮我一下。

谢谢

启航