Metatrader 5 coding questions / issues - page 12

 
mladen:
Doc From the code it should not be possible (since your code would open buy only if there is no open position for that symbol) but have to check more

I know! It's something crazy...when closing there is only trReq.volume=POSITION_VOLUME; coded

 

Good morning Mladen,

I tried again without success, I thought it was a bug of mt5 instead probably is only my falls...please take a look at the code with indicator used.

Thanks as usual

doc

Files:
x_tsd_2.mq5  16 kb
 

ea should open a buy when bid price is = or above the upper band

and close it only if bid price is = or below the lower band or if it take profit (viceversa for sell)

if((curBidVal>=upenvelope[0])&&(spread<=maxspread)&& timecond && getLastOrderType(_Symbol)!=1) OpenBuySignal = 1;

if(((curBidVal<=dnenvelope[0])&&(spread=MinPips)&&(spread<=maxspread))) CloseBuySignal = 1;

and why it starts with 1 lot than close it with 3 lots???

As you can see here, nothing has rispected...but why?

Files:
11_1.png  46 kb
 
dr.house7:
ea should open a buy when bid price is = or above the upper band

and close it only if bid price is = or below the lower band or if it take profit (viceversa for sell)

if((curBidVal>=upenvelope[0])&&(spread<=maxspread)&& timecond && getLastOrderType(_Symbol)!=1) OpenBuySignal = 1;

if(((curBidVal<=dnenvelope[0])&&(spread=MinPips)&&(spread<=maxspread))) CloseBuySignal = 1;

and why it starts with 1 lot than close it with 3 lots???

As you can see here, nothing has rispected...but why?

Doc

Will test it and will try to find a reason why it does it

 
dr.house7:
ea should open a buy when bid price is = or above the upper band

and close it only if bid price is = or below the lower band or if it take profit (viceversa for sell)

if((curBidVal>=upenvelope[0])&&(spread<=maxspread)&& timecond && getLastOrderType(_Symbol)!=1) OpenBuySignal = 1;

if(((curBidVal<=dnenvelope[0])&&(spread=MinPips)&&(spread<=maxspread))) CloseBuySignal = 1;

and why it starts with 1 lot than close it with 3 lots???

As you can see here, nothing has rispected...but why?

Doc

Something is wrong with that kind of lots calculation (lots can not be calculated only from balance and percentage) Some other way of calculating lots must be used

 

A function similar to this could be used to calculate lot size but for this kind of calculation you have to have a known stop loss (not just the percentage that should be used) :

//----------------------------------------------------------------------------------------

//

//----------------------------------------------------------------------------------------

//

//

//

//

//

//

//

double getLots(double BaseLot, double risk, double stopLossDistance)

{

double TickValue = SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_VALUE);

double MinLots = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MIN) ,2);

double MaxLots = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MAX) ,2);

double LotStep = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_STEP),2);

double lots = BaseLot;

int LotDigit;

if (LotStep==1) LotDigit=0;

if (LotStep==0.1) LotDigit=1;

if (LotStep==0.01) LotDigit=2;

if (risk>0 && stopLossDistance>0)

{

if (AccountInfoDouble(ACCOUNT_BALANCE)>AccountInfoDouble(ACCOUNT_FREEMARGIN))

lots = NormalizeDouble(AccountInfoDouble(ACCOUNT_FREEMARGIN)*(risk/100.0)/(stopLossDistance*TickValue/_Point),LotDigit);

else lots = NormalizeDouble(AccountInfoDouble(ACCOUNT_BALANCE) *(risk/100.0)/(stopLossDistance*TickValue/_Point),LotDigit);

}

return(MathMax(MathMin(lots,MaxLots),MinLots));

}
 

PS: attaching this EA (I think it was already posted, but am not sure) that is coded the simplest way possible for metatrader 5 with some additional features that might help or can be used as a frame for coding EAs using metatrader 5 MQL5

Files:
 
mladen:
PS: attaching this EA (I think it was already posted, but am not sure) that is coded the simplest way possible for metatrader 5 with some additional features that might help or can be used as a frame for coding EAs in using metatrader 5

Thanks Mladen,

but my signal is ontick mode so I cannot use those void and I don't have a SL fixed

 
dr.house7:
Thanks Mladen, but my signal is ontick mode so I cannot use those void and I don't have a SL fixed

Doc

It works on every tick (it is not limited by that - see the OnTick() procedure - all that has to be changed is the way how checkEntry() works and to call it from ManageOpened() too - then you would have a simple way of managing already opened orders/positions from ManageOpened() and opening new orders/positions from checkForOpen())

As of lack of stop loss : not difficult to add something like this in the ManageOpened()

trade.PositionClose(_Symbol);

when the signal type changes and then it would work even without stop loss (but with a fixed lot size)

 
mladen:
Doc

It works on every tick (it is not limited by that - see the OnTick() procedure - all that has to be changed is the way how checkEntry() works and to call it from ManageOpened() too - then you would have a simple way of managing already opened orders/positions from ManageOpened() and opening new orders/positions from checkForOpen())

As of lack of stop loss : not difficult to add something like this in the ManageOpened()

trade.PositionClose(_Symbol);

when the signal type changes and then it would work even without stop loss (but with a fixed lot size)

I need to try a bit...

by the way, I did delete all the StopCycle filter from the first code, that is probably the main problem, but I thought it was useless with OpenBuySignal condition...

A normal mind, find the C + + language really stupid, now I know why it has been replaced by many other languages ​​leaner and more dynamic

Reason: