- Parameters and arguments
- Trading Principles - Trade - MetaTrader 5 for iPhone
- Trading Principles - Trade - MetaTrader 5 for Android
Do not double post!
Your duplicate topic has been deleted.
I am having trouble with my EA and cant find anyone to help me with it. I have optimized an EA to yeild around 4k in profit however this number stays the same regardless of volume of trade. whether I trade 0.1 lots or 10 lots profit is exactly the same. is this normal? is there something Im doing wrong?
Thanks Arthur. Here is my code so far:
include<Trade/Trade.mqh> CTrade trade; input int Stop_Loss=10; //Stop Loss input int SMA_Period=200; //SMA input int BB_Period=20; //Bollinger Band input int EA_Magic=12345; //EA Magic Number input double SMA_Threshold=0.001; //SMA Threshold input double Lots=0.1; //Lots to Trade int STP; //To be used for stop loss level string Buy_Tradable = 0; string Sell_Tradable = 0; void OnTick() { MqlTick latest_price; MqlTradeRequest mrequest; MqlTradeResult mresult; MqlRates mrates[]; ZeroMemory(mrequest); double Ask = NormalizeDouble( SymbolInfoDouble(_Symbol, SYMBOL_ASK),_Digits); double Bid = NormalizeDouble( SymbolInfoDouble(_Symbol, SYMBOL_BID),_Digits); MqlRates PriceInfo[]; ArraySetAsSeries(PriceInfo, true); int PriceData = CopyRates(_Symbol,_Period,0,3, PriceInfo); double current_price = SymbolInfoDouble(_Symbol, SYMBOL_LAST); double currentlow = NormalizeDouble(PriceInfo[0]. low,_Digits); double currenthigh = NormalizeDouble(PriceInfo[0]. high,_Digits); double currentclose = NormalizeDouble(PriceInfo[0]. close,_Digits); double lastlow = PriceInfo[1].low; double lasthigh = PriceInfo[1].high; double lastclose = PriceInfo[1].close; double tradablehigh = PriceInfo[2].high; double tradablelow = PriceInfo[2].low; double UpperBandArray[]; double LowerBandArray[]; ArraySetAsSeries( UpperBandArray,true); ArraySetAsSeries( LowerBandArray,true); int BollingerBandsDefinition = iBands(_Symbol,_Period,BB_ Period,0,2,PRICE_CLOSE); CopyBuffer( BollingerBandsDefinition,1,0, 7,UpperBandArray); CopyBuffer( BollingerBandsDefinition,2,0, 7,LowerBandArray); double UpperBandValue=UpperBandArray[ 0]; double LowerBandValue=LowerBandArray[ 0]; double LastUpperBandValue= UpperBandArray[1]; double LastLowerBandValue= LowerBandArray[1]; double BBtradableupper = UpperBandArray[2]; double BBtradablelower = LowerBandArray[2]; double UpperBand=NormalizeDouble( UpperBandValue,_Digits); double LowerBand=NormalizeDouble( LowerBandValue,_Digits); double BBcenterArray[]; int BBcenterDefinition = iMA (_Symbol,_Period, 20,0,MODE_SMA,PRICE_CLOSE); ArraySetAsSeries( BBcenterArray,true); CopyBuffer( BBcenterDefinition,0,0,3, BBcenterArray); double BBcentervalue = BBcenterArray[0]; double movingaverageArray[]; int MovingAverageDefinition = iMA (_Symbol,_Period, SMA_Period,0,MODE_SMA,PRICE_ CLOSE); ArraySetAsSeries( movingaverageArray,true); CopyBuffer( MovingAverageDefinition,0,0,3, movingaverageArray); double movingaveragevalue = movingaverageArray[0]; double High_Threshold = movingaveragevalue+SMA_ Threshold; double Low_Threshold = movingaveragevalue-SMA_ Threshold; double MAhighprint = NormalizeDouble(High_ Threshold,_Digits); double MAlowprint = NormalizeDouble(Low_Threshold, _Digits); STP = Stop_Loss; if(_Digits==5 || _Digits==3) { STP = STP*10; } //--- Get the last price quote using the MQL5 MqlTick Structure if(!SymbolInfoTick(_Symbol, latest_price)) { Alert("Error getting the latest price quote - error:",GetLastError(),"!!"); return; } double maxlot = SymbolInfoDouble(Symbol(), SYMBOL_VOLUME_MAX); bool Buy_opened=false; // variable to hold the result of Buy opened position bool Sell_opened=false; // variable to hold the result of Sell opened position if (PositionSelect(_Symbol) ==true) // we have an opened position { if (PositionGetInteger(POSITION_ TYPE) == POSITION_TYPE_BUY) { Buy_opened = true; //It is a Buy } else if(PositionGetInteger( POSITION_TYPE) == POSITION_TYPE_SELL) { Sell_opened = true; // It is a Sell } } //____________________________ ______________________________ ____________________ //If buy trade opened, not tradable is close crosses upper band retradable, close also needs to be above MA if(current_price> UpperBandValue) { Buy_Tradable =1; } if(current_price< LowerBandValue) { Sell_Tradable =1; } if(current_price < movingaveragevalue) { Buy_Tradable =1; } if(current_price > movingaveragevalue) { Sell_Tradable =1; } //Enter_Buys_&_Sells__________ ______________________________ ______ bool Long_Condition1 = PriceInfo[0].close <= LowerBandValue; bool Long_Condition2 = PriceInfo[1].low >= LastLowerBandValue; bool Long_Condition3 = LowerBandValue >= High_Threshold; bool Long_Condition4 = Buy_Tradable > 0; bool Short_Condition1 = PriceInfo[0].close >= UpperBandValue; bool Short_Condition2 = PriceInfo[1].high <= LastUpperBandValue; bool Short_Condition3 = UpperBandValue <= Low_Threshold; bool Short_Condition4 = Sell_Tradable > 0; if(Long_Condition1 && Long_Condition2) { if(Long_Condition3 && Long_Condition4) { if(PositionsTotal()<1) { mrequest.action = TRADE_ACTION_DEAL; mrequest.price = NormalizeDouble(latest_price. ask,_Digits); mrequest.sl = NormalizeDouble(latest_price. ask - STP*_Point,_Digits); mrequest.tp = UpperBand; mrequest.symbol = _Symbol; mrequest.volume = Lots; mrequest.magic = EA_Magic; mrequest.type = ORDER_TYPE_BUY; mrequest.type_filling = ORDER_FILLING_FOK; mrequest.deviation = 100; OrderSend(mrequest,mresult); // get the result code if(mresult.retcode==10009 || mresult.retcode==10008) //Request is completed or order placed { Alert("A Buy order has been successfully placed with Ticket#:",mresult.order,"!!"); } else { Alert("The Buy order request could not be completed -error:",GetLastError()); ResetLastError(); return; } } } } if(Short_Condition1 && Short_Condition2) { if(Short_Condition3 && Short_Condition4) { if(PositionsTotal()<1) { mrequest.action = TRADE_ACTION_DEAL; mrequest.price = NormalizeDouble(latest_price. bid,_Digits); mrequest.sl = NormalizeDouble(latest_price. bid + STP*_Point,_Digits); mrequest.tp = LowerBand; mrequest.symbol = _Symbol; mrequest.volume = Lots; mrequest.magic = EA_Magic; mrequest.type = ORDER_TYPE_SELL; mrequest.type_filling = ORDER_FILLING_FOK; mrequest.deviation = 100; OrderSend(mrequest,mresult); if(mresult.retcode==10009 || mresult.retcode==10008) //Request is completed or order placed { Alert("A Sell order has been successfully placed with Ticket#:",mresult.order,"!!"); } else { Alert("The Sell order request could not be completed -error:",GetLastError()); ResetLastError(); return; } } } } if(Long_Condition1 && Long_Condition3) { Buy_Tradable =-1; } if(Short_Condition1 && Short_Condition3) { Sell_Tradable =-1; } //Modify Orders and Exits______________________ bool Long_Exit_Condition1 = current_price > movingaveragevalue; bool Long_Exit_Condition2 = current_price >= UpperBand; bool Short_Exit_Condition1 = current_price < movingaveragevalue; bool Short_Exit_Condition2 = current_price <= LowerBand; bool Long_SL_Update1 = current_price >= BBcentervalue; bool Short_SL_Update1 = current_price <= BBcentervalue; /*Update Take Profit for(int i=PositionsTotal()-1; i>=0; i--) { string symbol=PositionGetSymbol(i); if(_Symbol==symbol) { ulong PositionTicket = PositionGetInteger(POSITION_ TICKET); double CurrentStopLoss = PositionGetDouble(POSITION_SL) ; if(Short_Exit_Condition1) { trade.PositionModify( PositionTicket, CurrentStopLoss,LowerBand); } if(Long_Exit_Condition1) { trade.PositionModify( PositionTicket, CurrentStopLoss,UpperBand); } } } Update Stop Loss for(int i=PositionsTotal()-1; i>=0; i--) { string symbol=PositionGetSymbol(i); if(_Symbol==symbol) { int PositionTicket = PositionGetInteger(POSITION_ TICKET); int PositionDirection = PositionGetInteger(POSITION_ TYPE); if(PositionDirection== POSITION_TYPE_SELL) { if(Short_SL_Update1) { trade.PositionModify( PositionTicket,mrequest.price, LowerBand); } } if(PositionDirection== POSITION_TYPE_BUY) { if(Long_SL_Update1) { trade.PositionModify( PositionTicket,mrequest.price, UpperBand); } } } } */ //Exit Position for(int i=PositionsTotal()-1; i>=0; i--) { string symbol=PositionGetSymbol(i); if(_Symbol==symbol) { int PositionTicket = PositionGetInteger(POSITION_ TICKET); int PositionDirection = PositionGetInteger(POSITION_ TYPE); if(PositionDirection== POSITION_TYPE_SELL) { if(Short_Exit_Condition1 && Short_Exit_Condition2) { trade.PositionClose( PositionTicket); } } if(PositionDirection== POSITION_TYPE_BUY) { if(Long_Exit_Condition1 && Long_Exit_Condition2) { trade.PositionClose( PositionTicket); } } } } Comment("Buyable=",Buy_ Tradable," Sellable=",Sell_Tradable," L1=",Long_Condition1," L2=",Long_Condition2," L3=",Long_Condition3," L4=",Long_Condition4," | S1=",Short_Condition1," S2=",Short_Condition2," S3=",Short_Condition3," S4=",Short_Condition4," | LE1 = ",Long_Exit_Condition1," LE2=",Long_Exit_Condition2," SE1=",Short_Exit_Condition1," SE2=",Short_Exit_Condition2); //," HighThreshold=",MAhighprint," LowThreshold=",MAlowprint," BBLow=",LowerBand," BBHigh=",UpperBand," Close3Bars= ",PriceInfo[2].close); //Comment("Buyable=",Buy_ Tradable," Sellable=",Sell_Tradable," MA=",movingaveragevalue); }
abbottc06:
...
Forum on trading, automated trading systems and testing trading strategies
When you post code please use the CODE button (Alt-S)!
I am having trouble with my EA and cant find anyone to help me with it. I have optimized an EA to yeild around 4k in profit however this number stays the same regardless of volume of trade. whether I trade 0.1 lots or 10 lots profit is exactly the same. is this normal? is there something Im doing wrong?
From looking at your code I'd say it's not normal. Lots is directly assigned to the order volume and it's constant so you'd definitely see a difference in the outcome. Maybe you're testing the wrong EA. Did you compile it, did you move or rename it recently? Add a line Print("My Lots: ",Lots); and check the output.
There's also a few things that belong into OnInit() instead of OnTick() like indicator handles and one time calculations. And did you notice you assign 0,1 and -1 to a string variable?
Sorry now I know
From looking at your code I'd say it's not normal. Lots is directly assigned to the order volume and it's constant so you'd definitely see a difference in the outcome. Maybe you're testing the wrong EA. Did you compile it, did you move or rename it recently? Add a line Print("My Lots: ",Lots); and check the output.
There's also a few things that belong into OnInit() instead of OnTick() like indicator handles and one time calculations. And did you notice you assign 0,1 and -1 to a string variable?
Thank you Lippmaje.
I still havent been able to get volume to work properly but in regards to the 1,-1 string variable. What Im trying to accomplish is something of an on/off switch for my EA wherein if the current close reaches the upper bolinger band and a trade is entered, I only want the EA to be eligible to enter another sell trade if the price crosses the lower band or turn it back on. Do you know of a more effective way to write this?
Thank you Lippmaje.
I still havent been able to get volume to work properly but in regards to the 1,-1 string variable. What Im trying to accomplish is something of an on/off switch for my EA wherein if the current close reaches the upper bolinger band and a trade is entered, I only want the EA to be eligible to enter another sell trade if the price crosses the lower band or turn it back on. Do you know of a more effective way to write this?

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use