작업 종료됨
명시
Hello,
i use JFD MT5. I have an EA that opens a position on normal CFD (stock) but it fails to open the position on physical stock too.
Your job is to download MT5 from JFD here: https://download.mql5.com/cdn/web/jfd.group.ltd/mt5/jfd5setup.exe
Create a demo account and find out why the following ea don't open a position on physical stock.
//+------------------------------------------------------------------+
//|
Moving Averages.mq5 |
//|
Copyright 2009-2017, MetaQuotes Software Corp. |
//|
http://www.mql5.com |
//+------------------------------------------------------------------+
#property
copyright "Copyright 2009-2017, MetaQuotes Software Corp."
#property link
"http://www.mql5.com"
#property version "1.00"
#include <Trade\Trade.mqh>
input int MovingPeriod = 2; //
Moving Average period
input double ClosePerc = 0.1;
input int MovingFilter = 10;
// Moving Average Filter
//---
int ExtHandle=0;
bool ExtHedging=false;
CTrade ExtTrade;
#define MA_MAGIC 1234501
//+------------------------------------------------------------------+
//| Calculate optimal lot
size
|
//+------------------------------------------------------------------+
double TradeSizeOptimized(void)
{
double price=0.0;
double margin=0.0;
//--- select lot size
if(!SymbolInfoDouble(_Symbol,SYMBOL_ASK,price))
return(0.0);
if(!OrderCalcMargin(ORDER_TYPE_BUY,_Symbol,1.0,price,margin))
return(0.0);
if(margin<=0.0)
return(0.0);
double lot=1;
//NormalizeDouble(AccountInfoDouble(ACCOUNT_MARGIN_FREE)*MaximumRisk/margin,2);
//---
calculate number of losses orders without a break
//--- normalize and check limits
double stepvol=SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_STEP);
lot=stepvol*NormalizeDouble(lot/stepvol,0);
double
minvol=SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MIN);
if(lot<minvol)
lot=minvol;
double
maxvol=SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MAX);
if(lot>maxvol)
lot=maxvol;
//--- return trading volume
return(lot);
}
//+------------------------------------------------------------------+
//| Check for open position
conditions
|
//+------------------------------------------------------------------+
void CheckForOpen(void)
{
MqlRates rt[2];
//--- go trading only for first ticks of new bar
if(CopyRates(_Symbol,_Period,0,2,rt)!=2)
{
Print("CopyRates of ",_Symbol," failed, no history");
return;
}
//--- get current Moving Average
double ma[1];
if(CopyBuffer(ExtHandle,0,0,1,ma)!=1)
{
Print("CopyBuffer from iMA failed, no data");
return;
}
//--- check signals
ENUM_ORDER_TYPE signal=WRONG_VALUE;
double
filterma=iMA(_Symbol,_Period,MovingFilter,0,MODE_SMA,PRICE_CLOSE);
// buy
double perc=
ClosePerc;
double add=perc*ma[0]/100;
double maup=ma[0]+add;
double mamid=ma[0];
double madown=ma[0]-add;
//Print(maup+"
"+mamid+" "+madown);
Comment(
DoubleToString(maup,2)
+"\n"+
DoubleToString(mamid,2)
+"\n"+ DoubleToString(madown,2)
);
// trading for first tick of bar
//
if(rt[1].tick_volume>1) return;
//open Position due to condition
//if( rt[0].close>
maup && rt[0].close>filterma )
signal=ORDER_TYPE_BUY; // buy conditions
//--- additional checking
if(signal!=WRONG_VALUE)
{
if(TerminalInfoInteger(TERMINAL_TRADE_ALLOWED)
&& Bars(_Symbol,_Period)>100){
ExtTrade.PositionOpen(_Symbol,signal,TradeSizeOptimized(), SymbolInfoDouble(_Symbol,signal==ORDER_TYPE_SELL ?
SYMBOL_BID:SYMBOL_ASK), 0,0);
Print("should Buy Now");}
}
//---
}
//+------------------------------------------------------------------+
//| Check for close position
conditions
|
//+------------------------------------------------------------------+
void CheckForClose(void)
{
MqlRates rt[2];
//--- go trading only for first ticks of new bar
if(CopyRates(_Symbol,_Period,0,2,rt)!=2)
{
Print("CopyRates of ",_Symbol," failed, no history");
return;
}
if(rt[1].tick_volume>1)
return;
//--- get current Moving Average
double ma[1];
if(CopyBuffer(ExtHandle,0,0,1,ma)!=1)
{
Print("CopyBuffer from iMA failed, no data");
return;
}
//--- positions already selected before
bool signal=false;
long
type=PositionGetInteger(POSITION_TYPE);
// Close
double perc= ClosePerc;
double add=perc*ma[0]/100;
double maup=ma[0]+add;
double mamid=ma[0];
double madown=ma[0]-add;
if(type==(long)POSITION_TYPE_BUY &&
rt[0].close<madown)
signal=true;
//--- additional
checking
if(signal)
{
if(TerminalInfoInteger(TERMINAL_TRADE_ALLOWED) && Bars(_Symbol,_Period)>100)
ExtTrade.PositionClose(_Symbol,3);
}
//---
}
//+------------------------------------------------------------------+
//| Position select
depending on netting or
hedging |
//+------------------------------------------------------------------+
bool SelectPosition()
{
bool res=false;
//--- check position in Hedging mode
if(ExtHedging)
{
uint total=PositionsTotal();
for(uint i=0; i<total; i++)
{
string position_symbol=PositionGetSymbol(i);
if(_Symbol==position_symbol &&
MA_MAGIC==PositionGetInteger(POSITION_MAGIC))
{
res=true;
break;
}
}
}
//--- check position in Netting mode
else
{
if(!PositionSelect(_Symbol))
return(false);
else
return(PositionGetInteger(POSITION_MAGIC)==MA_MAGIC); //---check
Magic number
}
//--- result for Hedging mode
return(res);
}
//+------------------------------------------------------------------+
//| Expert initialization
function
|
//+------------------------------------------------------------------+
int OnInit(void)
{
//--- prepare trade class to control positions if hedging mode is active
ExtHedging=((ENUM_ACCOUNT_MARGIN_MODE)AccountInfoInteger(ACCOUNT_MARGIN_MODE)==ACCOUNT_MARGIN_MODE_RETAIL_HEDGING);
ExtTrade.SetExpertMagicNumber(MA_MAGIC);
ExtTrade.SetMarginMode();
ExtTrade.SetTypeFillingBySymbol(Symbol());
//--- Moving Average indicator
ExtHandle=iMA(_Symbol,_Period,MovingPeriod,0,MODE_SMA,PRICE_CLOSE);
if(ExtHandle==INVALID_HANDLE)
{
printf("Error creating MA indicator");
return(INIT_FAILED);
}
if(SelectPosition())
CheckForClose();
else
CheckForOpen();
//--- ok
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert tick
function
|
//+------------------------------------------------------------------+
void OnTick(void)
{
//---
if(SelectPosition())
CheckForClose();
else
CheckForOpen();
//---
}
//+------------------------------------------------------------------+
//| Expert deinitialization
function
|
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
}
//+------------------------------------------------------------------+