总新手到Mql5,并需要一些帮助,这个int OpenOrders()代码来自Mql4 EA,它找到打开的位置-(如果有的话),并选择最初的符号,和魔术号,而且专家不会打开任何其他位置,只要它在这个符号上打开的交易还没有关闭。我希望EA识别它已经打开的票证、相关的符号和将在ONTICK中使用的POSITION_TYPE,并且在关闭该头寸之前不会在同一图表上打开任何其他交易,但可以继续在任何其他图表上进行交易。
input double LotSize =0.3;
input double Incriment =0.01;
input int StopLoss =50;
input int TakeProfit =100;
input int Trend =21;
input int Momentum =21;
input int Strength =13;
int adxlevel =34;
int buylevel =62;
int selllevel =36;
//---------------------
double pips;
#include <Trade\Trade.mqh>
CTrade Execute;
ulong passport, StopLevel;
double ask, bid;
double takeout=0,stopout=0;
int moving,rsi,adx,Spread;
//+------------------------------------------------------------------+
int OnInit()
{
//---
double ticksize = SymbolInfoDouble(_Symbol,SYMBOL_POINT);
if(_Digits==3||_Digits==4)
pips=ticksize*1000;
else
pips =ticksize;
//---
moving=iMA(_Symbol,PERIOD_CURRENT,Trend,0,MODE_SMA,PRICE_CLOSE);
rsi=iRSI(_Symbol,PERIOD_CURRENT,Momentum,PRICE_MEDIAN);
adx=iADX(_Symbol,PERIOD_CURRENT,Strength);
//---
return(INIT_SUCCEEDED);
}
***int OpenOrders()
{
int BUYS=0,SELLS=0;
for(int i=0; i<PositionsTotal(); i++)
{
if(PositionSelectByTicket(passport)==false) break;
int dealtype=(int)PositionGetInteger(POSITION_TYPE); // buy or sell
// string position_symbol=PositionGetString(POSITION_SYMBOL); // chart symbol
if(Symbol()==PositionGetSymbol(i) && passport==PositionGetTicket(POSITION_TICKET))
{
if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY) BUYS++;
if(PositionGetInteger(POSITION_TYPE)==ORDER_TYPE_SELL) SELLS++;
}
}
//---
if(BUYS>0) return(BUYS);
else return(SELLS);
}***
//+------------------------------------------------------------------+
void OnTick()
{
//---
MqlRates rates[3];
double movingarray[],rsiarray[],adxarray[];
CopyRates(_Symbol,PERIOD_CURRENT,1,3,rates);
CopyBuffer(rsi,0,1,3,rsiarray);
CopyBuffer(adx,0,1,3,adxarray);
ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
Spread=int(SymbolInfoInteger(_Symbol,SYMBOL_SPREAD));
StopLevel=SymbolInfoInteger(Symbol(),SYMBOL_TRADE_STOPS_LEVEL);
//---
if(OpenOrders()==0)
{
if(rates[0].open > moving )// CONDITION
if(rsiarray[0] > buylevel && rsiarray[1] < buylevel )//SIGNAL
{
if(TakeProfit>0) takeout=ask+TakeProfit*pips;
if(StopLoss>0) stopout=ask-StopLoss*pips;
Execute.Buy(LotSize,NULL,ask,stopout,takeout,NULL);
passport=Execute.ResultOrder();
Print("BUY Opened");
}
if(rates[0].open < moving )//CONTITION
if(rsiarray[0] < selllevel &&rsiarray[1] > selllevel )//SIGNAL
{
if(TakeProfit>0) takeout=bid+TakeProfit*pips;
if(StopLoss>0) stopout=bid-StopLoss*pips;
Execute.Sell(LotSize,NULL,bid,stopout,takeout,NULL);
passport=Execute.ResultOrder();
Print("SELL Opened");
}
}
//---
if(OpenOrders()>0)
{
int dealtype=(int)PositionGetInteger(POSITION_TYPE);
if(dealtype==POSITION_TYPE_BUY)
if(rsiarray[0] < buylevel )
{
Execute.PositionClose(passport);
passport=0;
}
else if(dealtype==POSITION_TYPE_SELL)
if(rsiarray[0] > selllevel )
{
Execute.PositionClose(passport);
passport=0;
}
}
}
发布于 2021-02-04 04:28:09
我想你真正想要的是:
input double LotSize =0.3;
input double Incriment =0.01;
input int StopLoss =50;
input int TakeProfit =100;
input int Trend =21;
input int Momentum =21;
input int Strength =13;
int adxlevel =34;
int buylevel =62;
int selllevel =36;
//---------------------
double pips;
#include <Trade\Trade.mqh>
CTrade Execute;
ulong StopLevel;
double ask, bid;
double takeout=0,stopout=0;
int moving,rsi,adx,Spread;
//+------------------------------------------------------------------+
int OnInit()
{
//---
double ticksize = SymbolInfoDouble(_Symbol,SYMBOL_POINT);
if(_Digits==3||_Digits==4)
pips=ticksize*1000;
else
pips =ticksize;
//---
moving=iMA(_Symbol,PERIOD_CURRENT,Trend,0,MODE_SMA,PRICE_CLOSE);
rsi=iRSI(_Symbol,PERIOD_CURRENT,Momentum,PRICE_MEDIAN);
adx=iADX(_Symbol,PERIOD_CURRENT,Strength);
//---
Execute.SetExpertMagicNumber(0xCAFE); // INCLUDED
Execute.SetAsyncMode(false); // CHANGED
return(INIT_SUCCEEDED);
}
int OpenPositions(ulong &passport)
{
int BUYS=0,SELLS=0;
for(int i=0; i<PositionsTotal(); i++)
{
ulong ticket=PositionGetTicket(i); // changed
if(PositionSelectByTicket(ticket)==false) break;
int dealtype=(int)PositionGetInteger(POSITION_TYPE); // buy or sell
// string position_symbol=PositionGetString(POSITION_SYMBOL); // chart symbol
if(Symbol()==PositionGetSymbol(i) && 0xCAFE==PositionGetInteger(POSITION_MAGIC)) // CHANGED
{
passport = ticket; // CHANGED
if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY) BUYS++;
if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL) SELLS++; // CHANGED
}
}
//---
if(BUYS>0) return(BUYS);
else return(SELLS);
}
//+------------------------------------------------------------------+
void OnTick()
{
//---
MqlRates rates[3];
double movingarray[],rsiarray[],adxarray[];
CopyRates(_Symbol,PERIOD_CURRENT,1,3,rates);
CopyBuffer(rsi,0,1,3,rsiarray);
CopyBuffer(adx,0,1,3,adxarray);
ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
Spread=int(SymbolInfoInteger(_Symbol,SYMBOL_SPREAD));
StopLevel=SymbolInfoInteger(Symbol(),SYMBOL_TRADE_STOPS_LEVEL);
//---
ulong passport = 0; // CHANGED
int positions = OpenPositions(passport); // CHANGED
if(positions==0) // CHANGED
{
if(rates[0].open > moving )// CONDITION
if(rsiarray[0] > buylevel && rsiarray[1] < buylevel )//SIGNAL
{
if(TakeProfit>0) takeout=ask+TakeProfit*pips;
if(StopLoss>0) stopout=ask-StopLoss*pips;
Execute.Buy(LotSize,NULL,ask,stopout,takeout,NULL);
Print("BUY Opened");
}
if(rates[0].open < moving )//CONTITION
if(rsiarray[0] < selllevel &&rsiarray[1] > selllevel )//SIGNAL
{
if(TakeProfit>0) takeout=bid+TakeProfit*pips;
if(StopLoss>0) stopout=bid-StopLoss*pips;
Execute.Sell(LotSize,NULL,bid,stopout,takeout,NULL);
passport=Execute.ResultDeal(); // CHANGED
Print("SELL Opened");
}
}
else if(positions>0) // CHANGED to prevent on the same tick do both
{
int dealtype=(int)PositionGetInteger(POSITION_TYPE);
if(dealtype==POSITION_TYPE_BUY)
if(rsiarray[0] < buylevel )
{
Execute.PositionClose(passport);
}
else if(dealtype==POSITION_TYPE_SELL)
if(rsiarray[0] > selllevel )
{
Execute.PositionClose(passport);
}
}
}
我没有测试这些变化。大部分时间都应该很好。只有在延迟执行购买/销售的东西时,才会有问题。
https://stackoverflow.com/questions/65949362
复制相似问题