如何编码? - 页 287

 

...

请看这个帖子:https://www.mql5.com/en/forum/174385/page12

这是关于类似的问题,你可以使用同样的countOpenedOnACurrentBar

函数来限制每个交叉点只开一个交易。

dipu:
嗨,朋友们,我是一个新手。我有一个简单的策略。我只是在EMA交叉的时候进行剥头皮。我只是在每个交叉点上只做一笔交易。我需要一些代码,可以在每次EMA交叉时只进行一次 交易。请......
 

简单的EMA交叉代码需要........

非常感谢你,MLADEN...

我刚刚检查了你的功能。它看起来不错。我也需要一些帮助。我需要将RSI(50)值与EMA交叉....,我怎么能...?

货币对。 英镑/美元和欧元/美元

时间框架。 15分钟,1小时及以上。

-------------------------------

买入。EMA5与EMA12交叉向上,RSI大于50

卖出:EMA5与EMA12交叉向下,RSI小于50

(每个EMA交叉点只有一次交易)

我在等待答复。请.......

 

你可以通过几种方式来做到这一点。

一种方法(直接在EA中)可以是这样的。

double macdc = iMA(NULL,0,EmaFast,0,MODE_SMA,EmaPrice,0)-iMA(NULL,0,EmaSlow,0,MODE_SMA,EmaPrice,0);

double macdp = iMA(NULL,0,EmaFast,0,MODE_SMA,EmaPrice,1)-iMA(NULL,0,EmaSlow,0,MODE_SMA,EmaPrice,1);

double rsi = iRSI(NULL,0,RsiPeriod,RsiPrice,0);

if (macdc*macdp<0) // change of sign, signal for a new order

{

if (macdc>0 && rsi>50) ... code for buy

if (macdc<0 && rsi<50) ... code for sell

}

[/PHP]

The good thing about it is that it is small (the code) and does not take any significant process time. The bad thing when using conditions like that in an EA is that you have to imagine (and test, test, test ...) what is it going to perform like. I prefer making a "binary" indicator with which the it is very easy to visually inspect if the condition has any logic in it and if it is going to be profitable or not and then call that indicator (using iCustom()) from the EA.

Here is how would the indicator with the above conditions look like in "binary" form :

你有所有需要的信息:灰线是 "趋势"。绿线和红线可以作为进入和再进入点。你可以在瞬间对其进行视觉上的 "优化",然后在EA中使用这些参数(比起痛苦的 "改变参数,运行它,看看你得到了什么,然后一遍又一遍地做同样的事情",视觉上的 "优化 "要快很多。而且,从EA中调用它是相当容易的。它将会是这样的。

[PHP] double trendc = iCustom(NULL,0, "EMA + RSI binary",EmaFast,EmaSlow,EmaPrice,RsiPeriod,RsiPrice,2,0) 。

double trendp = iCustom(NULL,0, "ema + rsi binary",EmaFast,EmaSlow,EmaPrice,RsiPeriod,RsiPrice,2,1)。

如果(trendc!=trendp)

{

如果(trendc==1) ...... 买入信号

如果(trendc==-1) ...... 卖出信号

}

因此,总而言之,我总是建议使用一个来自Ea的指标用法。你甚至可以通过改变和完善你的指标代码来改变Ea的逻辑,在这种情况下,EA "只是 "一个订单和资金管理的框架(因为信号来自指标),在我看来,这是使用EA的最有效方式。同时附上指标,这样你就可以决定你想在你的EA中使用哪种方式(直接或 "通过指标 "的方式)。

PS:在上面的例子中,测试是在当前(仍然打开)的酒吧上进行的。如果你想测试关闭的条形图,在调用iCustom()、iMa()和iRSI()时,将0和1改为1和to。

dipu:
非常感谢你,MLADEN...

我刚刚检查了你的函数。它看起来不错。我也需要一些帮助。我需要将RSI(50)值与EMA交叉....,我怎么能...?

货币对。 英镑/美元和欧元/美元

时间框架。 15分钟,1小时及以上。

-------------------------------

买入。EMA5与EMA12交叉向上,RSI大于50

卖出: EMA5与EMA12交叉向下,RSI小于50

(每个EMA交叉点只有一次交易)

我正在等待答复。请.......
附加的文件:
 

每月1日的余额?

今天我一直坐在这里,想知道是否有办法提取每月1日的起始余额,以便在屏幕上显示。

我想做的是获得1号的余额和当前余额之间的百分比差异。

我有一个总体的百分比差。

(余额-存款)/存款*100

我想增加一个月差。

(余额-第一笔)/第一笔*100

任何提示或实际的代码都是很好的。

谢谢

贝诺

 

再次感谢MALADEN...

谢谢你的指标和建议。我只是试着按照你的指示去做。但由于我对mq4不熟练,我没能做到................。

这里是我的..............

------------------------------------

-----------------------------------

#属性 indicator_separate_window

#属性 indicator_buffers 3

#属性 indicator_color1 石灰绿

#属性 indicator_color2 PaleVioletRed

#属性 indicator_color3 深灰色

#属性 indicator_width1 2

#属性指示器_宽度2 2

#属性 indicator_minimum -1.1

#属性 indicator_maximum +1.1

Extern int EmaFast = 5;

extern int EmaSlow = 12;

Extern int EmaPrice = PRICE_CLOSE;

extern int RsiPeriod = 14;

外置 int RsiPrice = PRICE_CLOSE;

double signup[];

double signdn[];

double trend[];

extern double TakeProfit=15.0;

extern double Lots=0.1;

//------------------------------------------------------------------

//

//------------------------------------------------------------------

int init()

{

返回(0)。

}

//------------------------------------------------------------------

//

//------------------------------------------------------------------

int start()

{

int count,counted_bars=IndicatorCounted()。

if(counted_bars < 0) return(-1);

如果(counted_bars>0) counted_bars--。

int limit = MathMin(Bars-counted_bars,Bars-1);

int cnt, ticket, total;

double trendc = iCustom(NULL,0, "ema + rsi binary",EmaFast,EmaSlow,EmaPrice,RsiPeriod,RsiPrice,2,0);

double trendp = iCustom(NULL,0, "ema + rsi binary",EmaFast,EmaSlow,EmaPrice,RsiPeriod,RsiPrice,2,1);

如果(trendc!=trendp)

total = OrdersTotal();

如果(total < 1)

{

如果(trendc==1) //...买入信号

{

ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point, "My EA",12345,0,Green) 。

如果(ticket>0)

{

如果(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))Print("BUY order opened : ",OrderOpenPrice());

}

否则 Print("打开BUY订单错误。",GetLastError())。

return(0);

}

如果(trendc==-1) //...卖出的信号

{

ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point, "My EA",12345,0,Red) 。

如果(ticket>0)

{

如果(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))Print("Sell order opened : ",OrderOpenPrice());

}

否则 Print("打开卖出订单错误。",GetLastError())。

return(0);

}

}

return(0);

}

---------------------------------------

---------------------------------------

我需要你的帮助....

我向你道歉,如果我在你摔倒时对你有更多的打扰。但我真的很无助...

我在等待答复。请.......

 

...

试试用这样的方法。

double balanceAtStartOfMonth(int forMonth)

{

double current = AccountBalance();

datetime startTime = iTime(NULL,PERIOD_MN1,forMonth);

//

//

//

//

//

for (int k=OrdersHistoryTotal()-1; k>=0; k--)

{

if (!OrderSelect(k,SELECT_BY_POS,MODE_HISTORY)) break;

if (OrderCloseTime()<startTime) continue;

current -= (OrderProfit()+OrderSwap());

}

return(current);

}

forMonth是你希望看到余额的月份,0是当前月份。然后计算出所需月份的第一天的余额。这个函数 也会考虑到存款和取款(OrderType() == 6),所以它应该可以工作。

Beno:
今天,我一直坐在这里,试图找出是否有办法提取每月1日的起始余额,以便在屏幕上显示。

我想做的是获得1号的余额和当前余额之间的百分比差异。

我有一个总体的百分比差。

(余额-存款)/存款*100

我想增加一个月差。

(余额-第一笔)/第一笔*100

任何提示或实际的代码都是很好的。

欢呼声

贝诺
 

...

你不能从指标上管理订单。你只能从脚本或EA中管理订单(这是你正在寻找的情况)。你帖子中的代码是一个指标。

我发布的例子是在EA中使用的(据我所知,你已经有一个EA,你想在其中限制订单数量,并希望测试ema和rsi条件的入口)。

dipu:
再次感谢,MALADEN...

谢谢你的指标和建议。我只是试着按照您的指示去做。但由于我对mq4不熟练,我没能做到................。

这里是我的..............

------------------------------------

-----------------------------------

#属性 indicator_separate_window

#属性 indicator_buffers 3

#属性 indicator_color1 石灰绿

#属性 indicator_color2 PaleVioletRed

#属性 indicator_color3 深灰色

#属性 indicator_width1 2

#属性指示器_宽度2 2

#属性 indicator_minimum -1.1

#属性 indicator_maximum +1.1

外部int EmaFast = 5;

extern int EmaSlow = 12;

Extern int EmaPrice = PRICE_CLOSE;

extern int RsiPeriod = 14;

外置 int RsiPrice = PRICE_CLOSE;

double signup[];

double signdn[];

double trend[];

extern double TakeProfit=15.0;

extern double Lots=0.1;

//------------------------------------------------------------------

//

//------------------------------------------------------------------

int init()

{

返回(0)。

}

//------------------------------------------------------------------

//

//------------------------------------------------------------------

int start()

{

int count,counted_bars=IndicatorCounted()。

if(counted_bars < 0) return(-1);

如果(counted_bars>0) counted_bars--。

int limit = MathMin(Bars-counted_bars,Bars-1);

int cnt, ticket, total;

double trendc = iCustom(NULL,0, "ema + rsi binary",EmaFast,EmaSlow,EmaPrice,RsiPeriod,RsiPrice,2,0);

double trendp = iCustom(NULL,0, "ema + rsi binary",EmaFast,EmaSlow,EmaPrice,RsiPeriod,RsiPrice,2,1);

如果(trendc!=trendp)

total = OrdersTotal();

如果(total < 1)

{

如果(trendc==1) //...买入信号

{

ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point, "My EA",12345,0,Green) 。

如果(ticket>0)

{

如果(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))Print("BUY order opened : ",OrderOpenPrice());

}

否则 Print("打开BUY订单错误。",GetLastError())。

return(0);

}

如果(trendc==-1) //...卖出的信号

{

ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point, "My EA",12345,0,Red) 。

如果(ticket>0)

{

如果(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))Print("Sell order opened : ",OrderOpenPrice());

}

否则 Print("打开卖出订单错误。",GetLastError())。

return(0);

}

}

return(0);

}

---------------------------------------

---------------------------------------

我需要你的帮助....

我向你道歉,如果我在你摔倒时对你有更多的打扰。但我真的很无助...

我正在等待答复。请.......
 

谢谢你的回答,MLADEN

对不起,我想你是让我把这些代码放到一个新的EA中。

我只是没有理解你的答复....

是的。你是对的....

我已经有了EA并想进行修改。用一些新的条件.......

我的条件是:

------------------------------------------------------

买入。EMA5与EMA12交叉向上,RSI大于50

卖出: EMA5与EMA12交叉向下,RSI小于50

退出:小幅获利。

(每个EMA交叉点只有一次交易)

这里是我的EA.....

---------------------

---------------------

外置双倍数Lots=0.1;

extern int TakeProfit=5;

extern int StopLoss=5;

extern int RSIPos=50;

extern int RSINeg=50;

Extern int Slippage=2;

extern int abandon=101;

datetime bartime=0;

int bartick=0。

双倍p。

int cnt=0;

int OrdersPerSymbol=0;

double bullMA3=0;

double bearMA7=0;

双重RSI=0。

bool RSIPOS=0;

bool RSINEG=0;

双重TP。

双重SL。

int init()

{

if (Symbol()=="AUDUSD") {TakeProfit=60; StopLoss=23; abandon=103;}.

if (Symbol()=="EURAUD") {TakeProfit=95; StopLoss=141; abandon=33;}.

if (Symbol()=="EURCHF") {TakeProfit=81; StopLoss=77; abandon=97;}.

if (Symbol()=="EURGBP") {TakeProfit=11; StopLoss=77; abandon=108;}.

如果(Symbol()=="EURJPY") {TakeProfit= 38; StopLoss= 75; abandon=183;}.

如果(Symbol()=="EURUSD") {TakeProfit=35; StopLoss= 35; abandon=5;}

如果(Symbol()=="GBPCHF") {TakeProfit=79; StopLoss=98; abandon=113;}.

如果(Symbol()=="GBPJPY") {TakeProfit=13; StopLoss=98; abandon=117;}。

如果(Symbol()=="GBPUSD") {TakeProfit=55; StopLoss=100; abandon=69;}

if (Symbol()=="USDCAD") {TakeProfit=66; StopLoss=76; abandon=106;}.

如果(Symbol()=="USDCHF") {TakeProfit=117; StopLoss=78; abandon=111;}.

如果(Symbol()=="USDJPY") {TakeProfit=53; StopLoss=74; abandon=110;}.

}

int deinit()

{

}

int start()

{

p=Point;

// 错误检查和 条形计数

if(AccountFreeMargin()<(200*Lots)) {Print("-----NO MONEY"); return(0); }

如果(Bars<100) {Print("-----NO BARS"); return(0);}

如果(bartime!=Time[0]) {bartime=Time[0]; bartick++; }

bullMA3=iMA(Symbol(),0,3,0,MODE_EMA,PRICE_CLOSE,1)。

bearMA7=iMA(Symbol(),0,7,0,MODE_EMA,PRICE_CLOSE,1);

RSI=iRSI(Symbol(),0,2,PRICE_CLOSE,2)。

if(RSI>RSIPos) {RSIPOS=true; RSINEG=false;}.

如果(RSI<RSINeg) {RSIPOS=false; RSINEG=true;}

OrdersPerSymbol=0。

for(cnt=OrdersTotal();cnt>=0;cnt--)

{

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES)。

if( OrderSymbol()==Symbol() )

{

OrdersPerSymbol++;

}

}

如果(OrdersPerSymbol==0)

{

如果(bullMA3>(bearMA7+p) && RSINEG)

{

SL=Ask-(StopLoss*p);

TP=Ask+(TakeProfit*p)。

OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,SL,TP, "BUY "+CurTime(),0,0,White) 。

bartick=0。

}

如果(bullMA3<(bearMA7-p) && RSIPOS)

{

SL=Bid+(StopLoss*p);

TP=Bid-(TakeProfit*p);

OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,SL,TP, "SELL "+CurTime(),0,0,Red)。

bartick=0。

}

}

如果(OrdersPerSymbol==1 && bartick==abandon)

{

如果(OrderType()==OP_BUY)

{

OrderClose(OrderTicket(),Lots,Bid,Slippage,White);

SL=Bid+(StopLoss*p)。

TP=Bid-(TakeProfit*p)。

OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,SL,TP, "SELL "+CurTime(),0,0,Red)。

bartick++;

}

如果(OrderType()==OP_SELL)

{

OrderClose(OrderTicket(),Lots,Ask,Slippage,Red)。

SL=Ask-(StopLoss*p)。

TP=Ask+(TakeProfit*p)。

OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,SL,TP, "BUY "+CurTime(),0,0,White) 。

bartick++;

}

}

for(cnt=0;cnt<OrdersTotal();cnt++)

{

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES)。

if( OrderSymbol()==Symbol() )

{

如果(OrderType()==OP_BUY)

{

如果(OrderMagicNumber()==0)

{

如果( Bid-OrderOpenPrice()>3*Point )

{

OrderClose(OrderTicket(),Lots,Bid,0,White)。

返回(0)。

}

}

}

如果(OrderType()==OP_SELL)

{

如果(OrderMagicNumber()==0)

{

如果( OrderOpenPrice()-Ask > (3*Point) )

{

OrderClose(OrderTicket(),Lots,Ask,0,Red)。

返回(0)。

}

}

}

}

}

return(0);

}

-------------------

---------------------

我怎样才能修改它....? 等待答复。请.......

 

dipu

就进场而言,这个进场是可以的。

现在注意退出,因为你写的方式是过早地关闭订单,而且5位数的经纪人现在也得到了照顾,进项也在正常工作。rsi的默认周期被设置为14(我不知道你想使用哪一个,在代码中你使用周期 2,但这可能太短)。Ema周期默认为5和12。另外,它现在正在测试一个封闭的条形图(它一直等到条形图关闭,如果存在买入或卖出的信号,它就下单)。

附加的文件:
dipu.mq4  5 kb
 

谢谢你的回答,MLADEN

对不起,信息不清楚.......

买入。EMA5与EMA12交叉向上,RSI[14]大于50

卖出: EMA 5与EMA 12交叉下行,RSI[14]小于50

出场:用小的TP/Sl....(5pip)

(每个EMA交叉点只有一次交易)

我只是尝试了一下,并将告知你结果....。

它可能也会被修改......或者不会。

再次感谢您的帮助.....