For some reason even though 'maximum risk' is set to 4%, my EA still initiates a 0.01 lot position. Can anyone help me with this? Thanks very much.
Inserted Code
#include <stdlib.mqh>
extern int myMagic = 1;
extern double TakeProfit =2000;
extern bool PartialClosing = false;
extern int TPDivide = 1;
extern double StopLoss =100;
extern int TrailingStop =0;
extern double BreakEvenStop =120;
extern int slippage=20;
extern int shift=0;
extern double Lots = 0.01;
extern double MaximumRisk = 4.0; //
extern bool FixedLot = false;
extern int TimeStart1=0000;
extern int TimeStop1=2359;
extern int TimeStart2=0000;
extern int TimeStop2=0000;
extern int TimeStart3=0000;
extern int TimeStop3=0000;
extern int TimeStart4=0000;
extern int TimeStop4=0000;
extern int OrderTriesNumber=25;
bool buysig,sellsig,closebuy,closesell; int lastsig,tries;
double LotsRisk(int StopLoss) {
double lot=Lots;
//---- select lot size
if (!FixedLot && (StopLoss > 0))
lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk*0.001/StopLoss,2);
else
lot=Lots;
//---- return lot size
if(lot<0.01) lot=0.01;
return(lot);
}
//+------------------------------------------------------------------+
//| Calculate open positions |
//+------------------------------------------------------------------+
int CalculateCurrentOrders(string symbol)
{
int buys=0,sells=0;
//----
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) continue;
if(OrderSymbol()==Symbol() && OrderMagicNumber()==myMagic)
{
if(OrderType()==OP_BUY) buys++;
if(OrderType()==OP_SELL) sells++;
}
}
//---- return orders volume
if(buys>0) return(buys);
else return(-sells);
}
//+------------------------------------------------------------------+
//| Check for open order conditions |
//+------------------------------------------------------------------+
void CheckForSignals(datetime tradeopenbar) {
double iMA30=iMA(NULL,0,30,0,MODE_EMA,PRICE_CLOSE,0);
double iMA35=iMA(NULL,0,35,0,MODE_EMA,PRICE_CLOSE,0);
double iMA40=iMA(NULL,0,40,0,MODE_EMA,PRICE_CLOSE,0);
double iMA45=iMA(NULL,0,45,0,MODE_EMA,PRICE_CLOSE,0);
double iMA50=iMA(NULL,0,50,0,MODE_EMA,PRICE_CLOSE,0);
double iMA55=iMA(NULL,0,55,0,MODE_EMA,PRICE_CLOSE,0);
double iMA60=iMA(NULL,0,60,0,MODE_EMA,PRICE_CLOSE,0);
double iMA13current=iMA(NULL,0,13,0,MODE_EMA,PRICE_CLOSE,0);
double iMA13previous=iMA(NULL,0,13,0,MODE_EMA,PRICE_CLOSE,+1);
double CCICurrent=iCCI(NULL,0,3,PRICE_CLOSE,0);
double CCIPrevious=iCCI(NULL,0,3,PRICE_CLOSE,+1);
double CycleCurrent2=iStochastic(NULL,0,3,2,2,MODE_SMA,0,MODE_MAIN,0); //MODE_SIGNAL is longer look back period
double CyclePrevious2=iStochastic(NULL,0,3,2,2,MODE_SMA,0,MODE_MAIN,+1); //MODE_MAIN is shorter look back period
double SAR0009previous=iSAR(NULL,0,0.0009,0.009,+1);
double SAR0009current=iSAR(NULL,0,0.0009,0.009,0);
double SAR0015previous=iSAR(NULL,0,0.0015,0.015,+1);
double SAR0015current=iSAR(NULL,0,0.0015,0.015,0);
double SAR0025previous=iSAR(NULL,0,0.0025,0.025,+1);
double SAR0025current=iSAR(NULL,0,0.0025,0.025,0);
double SAR0040previous=iSAR(NULL,0,0.0040,0.04,+1);
double SAR0040current=iSAR(NULL,0,0.0040,0.04,0);
double SAR0066previous=iSAR(NULL,0,0.0066,0.066,+1);
double SAR0066current=iSAR(NULL,0,0.0066,0.066,0);
double SAR02previous=iSAR(NULL,0,0.02,0.2,+1);
double SAR02current=iSAR(NULL,0,0.02,0.2,0);
double iZeroSMAUp=iCustom(NULL,0,"iZeroSMA13",13,0.4,6,0,0,0);
double iZeroSMADown=iCustom(NULL,0,"iZeroSMA13",13,0.4,6,0,1,0);
iMA30=NormalizeDouble(iMA30,Digits);
iMA35=NormalizeDouble(iMA35,Digits);
iMA40=NormalizeDouble(iMA40,Digits);
iMA45=NormalizeDouble(iMA45,Digits);
iMA50=NormalizeDouble(iMA50,Digits);
iMA55=NormalizeDouble(iMA55,Digits);
iMA60=NormalizeDouble(iMA60,Digits);
iMA13current=NormalizeDouble(iMA13current,Digits);
iMA13previous=NormalizeDouble(iMA13previous,Digits);
CCICurrent=NormalizeDouble(CCICurrent,Digits);
CCIPrevious=NormalizeDouble(CCIPrevious,Digits);
CycleCurrent2=NormalizeDouble(CycleCurrent2,Digits);
CyclePrevious2=NormalizeDouble(CyclePrevious2,Digits);
SAR0009previous=NormalizeDouble(SAR0009previous,Digits);
SAR0009current=NormalizeDouble(SAR0009current,Digits);
SAR0015previous=NormalizeDouble(SAR0015previous,Digits);
SAR0015current=NormalizeDouble(SAR0015current,Digits);
SAR0025previous=NormalizeDouble(SAR0025previous,Digits);
SAR0025current=NormalizeDouble(SAR0025current,Digits);
SAR0040previous=NormalizeDouble(SAR0040previous,Digits);
SAR0040current=NormalizeDouble(SAR0040current,Digits);
SAR0066previous=NormalizeDouble(SAR0066previous,Digits);
SAR0066current=NormalizeDouble(SAR0066current,Digits);
SAR02previous=NormalizeDouble(SAR02previous,Digits);
SAR02current=NormalizeDouble(SAR02current,Digits);
iZeroSMAUp=NormalizeDouble(iZeroSMAUp,Digits);
iZeroSMADown=NormalizeDouble(iZeroSMADown,Digits);
sellsig=false;
buysig=false;
if(tradeopenbar<Time[0]){
if
(Close[0]-Open[0]>=70*Point && Open[0]>iMA60)
{
buysig=true;
}
if
(Open[0]-Close[0]>=70*Point && Open[0]<iMA60)
{
sellsig=true;
}
}
closebuy=false;
closesell=false;
if (High[0]<Low[0]) {
closebuy=true;
}
if (High[0]<Low[0]) {
closesell=true;
}
}
datetime CheckForOpen(datetime orderopenbar) {
int res,ct;
datetime result = orderopenbar;
ct=Hour()*100+Minute();
if
( !((ct>=TimeStart1 && ct<TimeStop1) || (ct>=TimeStart2 && ct<TimeStop2) ||
(ct>=TimeStart3 && ct<TimeStop3) || (ct>=TimeStart4 && ct<TimeStop4)) ) return(result);
double takeProfit;
double SetLots=0;
string EAComment='';
//---- sell conditions
if(sellsig && lastsig!=-1) {
res=0;
tries=0;
while (res<=0 && tries<OrderTriesNumber) {
while(!IsTradeAllowed()) Sleep(5000);
RefreshRates();
if(TakeProfit > MarketInfo(Symbol(), MODE_STOPLEVEL))
takeProfit = Bid-TakeProfit*Point;
else
takeProfit = 0;
SetLots=LotsRisk(StopLoss);
EAComment="TimeWave EURUSD M15";
res=OrderSend(Symbol(),OP_SELL,SetLots,Bid,slippage,Bid+StopLoss*Point,
takeProfit,EAComment,myMagic,0,DeepPink);
if (res<0)
Print("Error opening SELL order : ",ErrorDescription(GetLastError()));
else
result = Time[0]; // return this bar open time.
tries++;
}
lastsig= 0;
return(result);
}
//---- buy conditions
if(buysig && lastsig!=1) {
res=0;
tries=0;
while (res<=0 && tries<OrderTriesNumber) {
while(!IsTradeAllowed()) Sleep(5000);
RefreshRates();
if(TakeProfit > MarketInfo(Symbol(), MODE_STOPLEVEL))
takeProfit = Ask+TakeProfit*Point;
else
takeProfit = 0;
SetLots=LotsRisk(StopLoss);
EAComment="TimeWave EURUSD M15";
res=OrderSend(Symbol(),OP_BUY,LotsRisk(StopLoss),Ask,slippage,Ask-StopLoss*Point,
takeProfit,EAComment,myMagic,0,Yellow);
if (res<0)
Print("Error opening BUY order : ",ErrorDescription(GetLastError()));
else
result = Time[0]; // return this bar open Time
tries++;
}
lastsig = 0;
return(result);
}
return(result);
}
//+------------------------------------------------------------------+
//| Check for close order conditions |
//+------------------------------------------------------------------+
void CheckForClose() {
bool bres; int tr;
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderMagicNumber()!=myMagic || OrderSymbol()!=Symbol()) continue;
//---- check order type
if(OrderType()==OP_BUY)
{
if (closebuy) {
bres=false;
tries=0;
while (!bres && tries<OrderTriesNumber) {
while(tr<7 && !IsTradeAllowed()) { tr++; Sleep(5000); }
RefreshRates();
bres=OrderClose(OrderTicket(),OrderLots(),Bid,slippage,White);
Sleep(3000);
if (!bres) Print("Error closing order : ",ErrorDescription(GetLastError()));
tries++;
}
}
else
if(PartialClosing) PartialClosingExe();
break;
}
if(OrderType()==OP_SELL)
{
if (closesell) {
bres=false;
tries=0;
while (!bres && tries<OrderTriesNumber) {
while(tr<7 && !IsTradeAllowed()) { tr++; Sleep(5000); }
RefreshRates();
bres=OrderClose(OrderTicket(),OrderLots(),Ask,slippage,White);
Sleep(3000);
if (!bres) Print("Error closing order : ",ErrorDescription(GetLastError()));
tries++;
}
}
else
if(PartialClosing) PartialClosingExe();
break;
}
}
}
void BreakEvenStop() {
bool bres;
double StopLoss;
if ( BreakEvenStop > 2 ) {
for (int i = 0; i < OrdersTotal(); i++) {
if ( OrderSelect (i, SELECT_BY_POS) == false ) continue;
if ( OrderSymbol() != Symbol() || OrderMagicNumber() != myMagic ) continue;
if ( OrderType() == OP_BUY ) {
if ( (Bid < OrderOpenPrice()+BreakEvenStop*Point) ) return;
StopLoss = OrderOpenPrice();
if ( StopLoss > OrderStopLoss() ) {
bres=OrderModify (OrderTicket(), OrderOpenPrice(), StopLoss, OrderTakeProfit(), 0, White);
Sleep(3000);
if (!bres) Print("Error Modifying BUY order : ",ErrorDescription(GetLastError()));
}
}
if ( OrderType() == OP_SELL ) {
if ( (Ask > OrderOpenPrice()-BreakEvenStop*Point) ) return;
StopLoss = OrderOpenPrice();
if ( StopLoss < OrderStopLoss() ) {
bres=OrderModify (OrderTicket(), OrderOpenPrice(), StopLoss, OrderTakeProfit(), 0, White);
Sleep(3000);
if (!bres) Print("Error Modifying SELL order : ",ErrorDescription(GetLastError()));
}
}
}
}
return;
}
void TrailingStop() {
bool bres;
double StopLoss;
if ( TrailingStop > MarketInfo(Symbol(), MODE_STOPLEVEL)) {
for (int i = 0; i < OrdersTotal(); i++) {
if ( OrderSelect (i, SELECT_BY_POS) == false ) continue;
if ( OrderSymbol() != Symbol() || OrderMagicNumber() != myMagic ) continue;
if ( OrderType() == OP_BUY ) {
if ( (Bid < OrderStopLoss()+TrailingStop*Point) ) return;
StopLoss = Bid-TrailingStop*Point;
if ( StopLoss > OrderStopLoss() ) {
bres=OrderModify (OrderTicket(), OrderOpenPrice(), StopLoss, OrderTakeProfit(), 0, White);
Sleep(3000);
if (!bres) Print("Error Modifying BUY order : ",ErrorDescription(GetLastError()));
}
}
if ( OrderType() == OP_SELL ) {
if ( (Ask > OrderStopLoss()-TrailingStop*Point) ) return;
StopLoss = Ask+TrailingStop*Point;
if ( StopLoss < OrderStopLoss() ) {
bres=OrderModify (OrderTicket(), OrderOpenPrice(), StopLoss, OrderTakeProfit(), 0, White);
Sleep(3000);
if (!bres) Print("Error Modifying SELL order : ",ErrorDescription(GetLastError()));
}
}
}
}
return;
}
double RoundLots(string S, double P)
{
double value=P;
double MinimalLot=0;
MinimalLot=MarketInfo(S,MODE_MINLOT);
if(MinimalLot<=0)
{
RefreshRates();
return(-1);
}
value=MathFloor(P/MinimalLot)*MinimalLot;
return(value);
}
//+------------------------------------------------------------------+
//| Linear closing of position |
//+------------------------------------------------------------------+
void PartialClosingExe()
{
int Profit=0;
if(OrderType()==OP_BUY) Profit=MathRound((Bid-OrderOpenPrice())/Point);
if(OrderType()==OP_SELL) Profit=MathRound((OrderOpenPrice()-Ask)/Point);
if(Profit<=0) return;
int PipStep=MathFloor(TakeProfit/TPDivide);
if(PipStep<1) PipStep=1;
if(PipStep>TakeProfit) PipStep=TakeProfit;
double LotPercent=1.00/TPDivide;
if(LotPercent>1.00) LotPercent=1.00;
if(TPDivide>TakeProfit && TakeProfit>0) LotPercent=1.00/TakeProfit;
double FirstPosition=LotsRisk(StopLoss);
double CutLots=FirstPosition*LotPercent*MathFloor(Profit/PipStep);
CutLots=RoundLots(Symbol(),CutLots);
if(CutLots<=0) return;
CutLots=CutLots-(FirstPosition-OrderLots());
if(CutLots<=0) return;
if(OrderLots()<CutLots) CutLots=OrderLots();
if(OrderType()==OP_BUY)
OrderClose(OrderTicket(),CutLots,Bid,slippage,White);
if(OrderType()==OP_SELL)
OrderClose(OrderTicket(),CutLots,Ask,slippage,White);
return;
}
void start() {
static datetime TradeOpenBar;
//---- check for history and trading
if(Bars<100 || IsTradeAllowed()==false) return;
//---- check for signals
CheckForSignals(TradeOpenBar);
//---- calculate open orders by current symbol
if (CalculateCurrentOrders(Symbol())==0)
TradeOpenBar = CheckForOpen(TradeOpenBar);
else
CheckForClose();
BreakEvenStop();
TrailingStop();
}
//+------------------------------------------------------------------+ change the future, not the past