if (prevshortcurrentlong && currentshort>currentlong>Trendsetter && Ask > resistance - upperproximity*Point){ //---conditions to open long positions change as desired
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES )) Print("BUY order opened : ",OrderOpenPrice());
}
else Print("Error opening BUY order : ",GetLastError());
return(0);
}
//+---------enter short positions----------+
if (prevshort>prevlong && currentshort<currentlong && currentshort<currentlong<Trendsetter && Ask < support + lowerproximity*Point){ //---conditions to open short positions change as desired
对不起,我没有时间检查整个代码,但让我们只检查这个片段。
if(OrderSymbol()==Symbol() && OrderType()==OP_SELL && Bid+Profit>OrderOpenPrice()
让我们说,OrderOpenPrice = 1.2100
而利润是0.0010
所以,如果买入价小于或等于1.2100-0.0010=1.2090,你想关闭空头交易。
让我们假设有一个向下的缺口,价格跳过了1.2090,现在的买入价格是1.2088。根据你的公式。
Bid + Profit = 1.2088 + 0.0010 = 1.2098。即使您的系统已经超过了利润阈值,它也不比OrderOpenPrice大。因此,订单将不会被关闭。在我看来,平仓条件的逻辑需要被重新评估和重新编写。另外,在处理空头交易时,你应该处理问价,因为你只能在问价时平仓。
还有一点建议。
for (int cnt = 0 ; cnt = 0; cnt--) 的计数循环或类似的东西。
好运。我很感谢学习这个,谢谢Maji!反正这不是我想要的平仓逻辑......我想要的逻辑是,如果它没有触发追踪止损,就平仓。由于我不知道究竟如何检测是否触发,所以在我学会如何编程以实现我的意图之前,我不能使用这种按时间收盘的收盘策略。
新的收盘标准......请帮我把代码放对......。
我需要一些帮助,在其中加入另一个平仓标准,即长EMA低于minortrendsetter(如果是多头),如果是空头则相反。我不确定如何用这个新的标准来隔离多头和空头的仓位,来关闭它们。
//+--------- settings may vary use at your own risk-----------------+
//+--------------user inputs--------------------+
extern double Trendsetter = 250;
extern double Minortrendsetter = 150;
extern double LongEMA = 20;
extern double ShortEMA = 5;
extern double TrailingStop = 15;
extern double TrailingStopTrigger = 1;
extern double StopLoss = 186;
extern double TakeProfit = 250;
extern double Lots = 0.1;
extern double EquityStop = 9;
//---- Custom "Channel-1" Indicator and Filter Parameters
extern int Hours=36;
extern color col=SkyBlue;
extern double TF = 60; //--which bar period for the custom indicator to use
extern double upperproximity = 30; //---disallows long orders within this proximity to resistance line
extern double lowerproximity = 30; //---disallows short orders within this proximity to the support line
//+-----------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 = 11; // if after that time we don't have +'x' pips we will exit
extern int MinsMultiplier = 600; // multiplies the MonitorInMinutes to make minutes (if 'x'=60) into hours
//+----------------------end of allotted time user inputs-----------------------------+
//+-----------------------------end of user inputs----------------------------------+
//+------------------------------------------------------------------+
//| expert start function
//+------------------------------------------------------------------+
int start(){
CloseOrder();
int cnt, ticket;
if(Bars<100){
Print("bars less than 100");
return(0);
}
//+----------------------Get Moving Average(s) Data----------------------------------------+
double currentlong=iMA(NULL,0,LongEMA,0,MODE_EMA,PRICE_CLOSE,0);//--current period longEMA
double currentshort=iMA(NULL,0,ShortEMA,0,MODE_EMA,PRICE_CLOSE,0);//--current period shortEMA
double trendsetter=iMA(NULL,0,Trendsetter,0,MODE_EMA,PRICE_CLOSE,0);//--current period TrendsetterEMA
double minorts=iMA(NULL,0,Minortrendsetter,0,MODE_EMA,PRICE_CLOSE,0);//--current period MinortrendsetterEMA
double prevlong=iMA(NULL,0,LongEMA,0,MODE_EMA,PRICE_CLOSE,1);//--previous period longEMA
double prevshort=iMA(NULL,0,ShortEMA,0,MODE_EMA,PRICE_CLOSE,1);//--previous period shortEMA
double prevtrendsetter=iMA(NULL,0,Trendsetter,0,MODE_EMA,PRICE_CLOSE,1);//--previous period TrendsetterEMA
double prevminorts=iMA(NULL,0,Minortrendsetter,0,MODE_EMA,PRICE_CLOSE,1);//--previous period MinortrendsetterEMA
//+----------------------------end of Get Moving Average(s) Data-----------------------------+
//+--------------------channel filter---------------------------+
double resistance = iCustom(NULL,TF,"Channel-1",Hours,col,0,0);
double support = iCustom(NULL,TF,"Channel-1",Hours,col,2,0);
//+------------------- end channel filter------------------------+
//+---------Obnoxious money management code needs revision----------------+
int total=OrdersTotal();
if(total<1){
if(AccountFreeMargin()<(1000*Lots)){
Print("We have no money. Free Margin = ", AccountFreeMargin());
return(0);
}
//+---------end of Obnoxious money management code-----------------+
//+---------------------------------------Order Entry--------------------------------------------+
//+---------enter long positions----------+
if (prevshortcurrentlong && currentshort>currentlong>Trendsetter && Ask > resistance - upperproximity*Point){ //---conditions to open long positions change as desired
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point, NULL,16384,0,Green);
if(ticket>0){
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES )) Print("BUY order opened : ",OrderOpenPrice());
}
else Print("Error opening BUY order : ",GetLastError());
return(0);
}
//+---------enter short positions----------+
if (prevshort>prevlong && currentshort<currentlong && currentshort<currentlong<Trendsetter && Ask < support + lowerproximity*Point){ //---conditions to open short positions change as desired
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+StopLoss*Point,Bid-TakeProfit*Point, NULL,16384,0,Red);
if(ticket>0) {
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES )) Print("SELL order opened : ",OrderOpenPrice());
}
else Print("Error opening SELL order : ",GetLastError());
return(0);
}
}
//+---------end of order entry-------------------------+
//+-------------------------Trailing Stop Code------------------------------------+
for(cnt=0;cnt<total;cnt++) {
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL && OrderSymbol()==Symbol()) {
if(OrderType()==OP_BUY){
if(TrailingStop>0) {if(Bid-OrderOpenPrice()>Point*TrailingStopTrigger) {
if(OrderStopLoss()<Bid-Point*TrailingStop) {
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);
return(0);
}
}
}
}else{
if(TrailingStop>0) {if((OrderOpenPrice()-Ask)>(Point*TrailingStopTrigger)) {
if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0)) {
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);
return(0);
}
}
}
}
//+-------------------------End of Trailing Stop Code----------------------------+
//+---------------------Equity Stop Code---------------------------+
if((AccountEquity()+ EquityStop)<AccountBalance()) {
{
int ttotal = OrdersTotal();
for(int i=ttotal-1;i>=0;i--)
{
OrderSelect(i, SELECT_BY_POS);
int type = OrderType();
bool result = false;
switch(type)
{
//Close opened long positions
case OP_BUY : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
break;
//Close opened short positions
case OP_SELL : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );
}
if(result == false)
{
Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
Sleep(3000);
}
}
return(0);
}
}
}
}
}
//+---------------------End of Equity Stop Code---------------------------+
//|
//+---------------------Close Based on Time-------------------------------+
//+--------------needs revision, not working as desired---------------------+
//+------------I want it to close IF and ONLY IF trailing stop is NOT triggered-------------+
void CloseOrder()
{
double Profit=ThresholdMove*Point;
int total = OrdersTotal();
for (int cnt = 0 ; cnt < total ; cnt++)
{
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
if ((CurTime()-OrderOpenTime())>MonitorInMinutes*60*MinsMultiplier)
{
if(OrderSymbol()==Symbol() && OrderType()==OP_BUY && Bid-Profit<OrderOpenPrice() )
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet);
}
if(OrderSymbol()==Symbol() && OrderType()==OP_SELL && Bid+Profit>OrderOpenPrice())
{
OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet);
}
}
}
}
//+---------------------------end of close on time code---------------+好的,我现在需要理解这部分代码....
第一行计数
第二行选择已计数的订单
第三行......这让我很困惑......我认为它想知道所选的订单 是否是卖出......但 "0 "是怎么回事?我的意思是这里有五个 "如果 "行......。
我猜如果其中任何一个为真,那么它将修改订单,否则......我猜它会转到......'其他'和......好吧,这必须设法处理多头和空头,但我还不明白。
因为我想根据多头EMA与最小趋势setterEMA的交叉来增加平仓多头或空头的标准......我不知道该在哪里做这件事。
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL && OrderSymbol()==Symbol()) {
if(OrderType()==OP_BUY){
if(TrailingStop>0) {if(Bid-OrderOpenPrice()>Point*TrailingStopTrigger) {
if(OrderStopLoss()<Bid-Point*TrailingStop) {
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);
return(0);
}
}
}
}else{
if(TrailingStop>0) {if((OrderOpenPrice()-Ask)>(Point*TrailingStopTrigger)) {
if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0)) {
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);
return(0);
}
}
}
}好吧,我知道所有的if和其他都会导致这几行。
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green)。
OrderModify(OrderTicket(),OrderOpenPrice(), Ask+Point*TrailingStop,OrderTakeProfit(),0,Red)。
我需要更多地了解这些订单修改线是如何构建的。这些是关闭以及改变订单的吗?还是它们只是改变了订单,而其他东西关闭了订单?
看看MetaEditor的帮助
OP_BUY 0 Buying position.
OP_SELL 1 Selling position.
OP_BUYLIMIT 2 Buy limit pending position.
OP_SELLLIMIT 3 Sell limit pending position.
OP_BUYSTOP 4 Buy stop pending position.
OP_SELLSTOP 5 Sell stop pending position.
[/PHP]
so, <= OP_SELL is OP_BUY or OP_SELL. Just instead of using
[PHP]if((OrderType()==OP_SELL) || (OrderType()==OP_BUY) ...只要少打点
在这里你可以
void CloseOrders(int op)
{
int tik[30], t = 0;
for(int i =0;i<OrdersTotal();i++){
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
if(OrderSymbol()==Symbol() && MagicNum==OrderMagicNumber() && OrderType() == op){
tik[t] = OrderTicket(); t++;
}
}
}
for (i = 0; i<t; i++)
{
if(OrderSelect(tik,SELECT_BY_TICKET)){
double prc = Bid;
if (op == OP_SELL) prc = Ask;
CloseOrder(tik, OrderLots(), prc);
}
}
}调用:CloseOrder(OP_BUY); // 关闭所有买入订单
或
CloseOrder(OP_SELL); // 关闭所有卖单
我把所有的订单 保存在一个数组中,然后删除,是因为当我关闭1号位置的订单时,下一个订单又变成了1,我们就有问题了。
我忘了这个
{
int CloseCnt, err;
// try to close 3 Times
CloseCnt = 0;
color clr = Violet;
if (OrderType() == OP_SELL)
clr = Orange;
while (CloseCnt < 3)
{
if (OrderClose(ticket,numLots,close_price,Slippage,clr))
{
CloseCnt = 3;
}
else
{
err=GetLastError();
Print(CloseCnt," Error closing order : (", err , ") " + ErrorDescription(err));
if (err > 0) CloseCnt++;
}
}
}
[/PHP]
and dont forget to add this line after #property link
[PHP]#property link "http://www.elihayun.com"
#include
我还是很迷惑...
请理解,这段代码最初不是我创造的。我正在努力理解别人的做法,并对其进行修改。我把我的名字放在财产线上,只是现在我已经修改了很多,以至于它和原来的东西已经没有什么相似之处。我现在对它的修改比我没动过的要多。但仍有一些方面我不明白,因此无法修改。
我所寻找的是关闭订单的代码......似乎所有这些都只是为了更新追踪止损而进行的修改。
这不止是一种关闭方式。
现在,这个EA有能力用止损关闭。
以拖曳止损收盘。
以获利 目标收盘。
或者在开盘后的某个规定时间内关闭。
我想让它具备的是,如果长EMA回到minortrendsetterEMA上,它就可以关闭。它可以做到向上穿越以关闭空头 头寸,或者向下穿越以关闭多头 头寸。我怎样才能让它这样做呢?我的意思是,在所有这些其他的平仓选项中,我应该把新的代码放在哪里,以超越所有这些其他的平仓选项?
在我的expirementation中,当我不想使用这个参数的某些方面时,我只是简单地使该参数的标准变得如此极端,以至于它永远不会触发它,这就像把该标准基本上关闭。这样做允许其他标准的发挥,以显示它们会返回什么。
我忘了这个
{
int CloseCnt, err;
// try to close 3 Times
CloseCnt = 0;
color clr = Violet;
if (OrderType() == OP_SELL)
clr = Orange;
while (CloseCnt < 3)
{
if (OrderClose(ticket,numLots,close_price,Slippage,clr))
{
CloseCnt = 3;
}
else
{
err=GetLastError();
Print(CloseCnt," Error closing order : (", err , ") " + ErrorDescription(err));
if (err > 0) CloseCnt++;
}
}
}
[/PHP]
and dont forget to add this line after #property link
[PHP]#property link "http://www.elihayun.com"
#include
好的,谢谢你!
所以这基本上只是一个平仓代码?
这是否会同时关闭多头和空头头寸?
如果longema在minortrendsetterema上向后交叉,我将如何使用它来关闭? 如果它开了空头,就向上交叉,如果它开了多头,就向下交叉?
p.s. 我今天下午晚些时候才会回到电脑前。我到时再来看看。
还有一个问题...
交换 "是什么意思?
每当你决定开立一个长线头寸的时候,就调用:CloseOrders(OP_SELL)。
反之亦然。
看看你的代码,在那里你正在打开订单。
你不需要检查 是否有一个订单要关闭。该程序将为你做这件事。
BTW,我发布了一个代码,将2006.07.02有点单元格转换为excel中的日期。请看你的帖子