Coding help - page 483

pking25
242
pking25  

Oh man you really are a star. I love to trade, but it would be impossible without your help. Thanks again

Mladen Rakic
183075
Mladen Rakic  
xtractalpha:
Hi,

Could someone please help me with my LotsizeCalc() function.

I would like to multiply my lotsize with the 'MartingaleAmount' if my last trade was a loser. If my last trade was a winner I would like to use the 'normal' lotsize. Unfortunately, when I backtest the EA I get error 4051 from the journal.

It has something to do with the lotsize, because the EA works when I try to run the EA without the "LotsizeCalc()" function.

Does anyone know why my lotsize function won't work?

Thanks in advance,

Thierry

extern int MaximumTrades = 10; extern double StopLoss=25; extern double TakeProfit=50; extern double MartingaleAmount = 1.5; input double LotSize=0.1; int magic; double pips; int tradeNumber; string ScreenComment; double martinlot; double martinsize;

[/PHP]

double LotsizeCalc() { if(OrderSelect(lastTradeTicket(),MODE_HISTORY)) if(OrderProfit()<0 && tradeNumber 0){ if(OrderSelect(ticket,SELECT_BY_TICKET)){ sl = OrderOpenPrice()+(StopLoss*pips); if(StopLoss==0)sl=0; tp = OrderOpenPrice()-(TakeProfit*pips); if(OrderType()==OP_BUY){ sl = OrderOpenPrice()-(StopLoss*pips); if(StopLoss==0)sl=0; tp = OrderOpenPrice()+(TakeProfit*pips); } if(!OrderModify(ticket,price,sl,tp,0,Magenta)) { err = GetLastError(); Print("Encountered an error during modification!"+(string)err+" "+ErrorDescription(err) ); } } else{//in case it fails to select the order for some reason Print("Failed to Select Order ",ticket); err = GetLastError(); Print("Encountered an error while seleting order "+(string)ticket+" error number "+(string)err+" "+ErrorDescription(err) ); } } else{//in case it fails to place the order and send us back a ticket number. err = GetLastError(); Print("Encountered an error during order placement!"+(string)err+" "+ErrorDescription(err) ); if(err==ERR_TRADE_NOT_ALLOWED)MessageBox("You can not place a trade because \"Allow Live Trading\" is not checked in your options. Please check the \"Allow Live Trading\" Box!","Check Your Settings!"); } }

Thierry

You can use this function to find out if the last closed order was closed with profit or loss :

[PHP]double lastOrderProfit(int magicNumber=0)

{

datetime lastTime = 0;

double lastProfit = 0;

for(int i=OrdersHistoryTotal()-1; i>=0; i--)

{

if (OrderSelect(i,SELECT_BY_POS, MODE_HISTORY)==false) break;

if (magicNumber!=0)

if (OrderMagicNumber() != magicNumber) continue;

if (OrderSymbol() != Symbol()) continue;

if (OrderCloseTime() <= lastTime) continue;

lastTime = OrderCloseTime();

lastProfit = OrderProfit()+OrderSwap()+OrderCommission();

}

return(lastProfit);

}

Just check if the result of lastOrderProfit() function, and if it is < 0, then apply your martingale rule

Jason2005
160
Jason2005  

Hi Mladen! Me again...

How would it be possible to use the lines from this indicator in an EA using iCustom since there doesn't seem to be any buffers set for these lines?

Thanks

Files:
Mladen Rakic
183075
Mladen Rakic  
Jason2005:
Hi Mladen! Me again...

How would it be possible to use the lines from this indicator in an EA using iCustom since there doesn't seem to be any buffers set for these lines?

Thanks

Those are objects

You can access objects by name (you must know the exact name of the object, but the indicator must be active on the chart and those objects must be drawn on chart).

Jason2005
160
Jason2005  
mladen:
Those are objects You can access objects by name (you must know the exact name of the object, but the indicator must be active on the chart and those objects must be drawn on chart).

Great. So how would I call the indicator from the EA? With something other than iCustom or have I got it all wrong... Thanks mladen

Mladen Rakic
183075
Mladen Rakic  
Jason2005:
Great. So how would I call the indicator from the EA? With something other than iCustom or have I got it all wrong... Thanks mladen

That indicator simply draws levels at 0,20,50,80 pips of each 100 pips segment. The simplest way is to check if those levels are crossed from your EA.

If you try using object names (and their prices, you will not be able to know the exact names of the lines since they depend on highest high and lowest low - they are a subject of change. Use the mode described above

Jason2005
160
Jason2005  
mladen:
That indicator simply draws levels at 0,20,50,80 pips of each 100 pips segment. The simplest way is to check if those levels are crossed from your EA. If you try using object names (and their prices, you will not be able to know the exact names of the lines since they depend on highest high and lowest low - they are a subject of change. Use the mode described above

Would you have an example on how to do that... I'm sorry for asking so much, I just am starting out with coding and haven't got the hang of how to check if those levels are crossed in the condition to buy or sell.

Mladen Rakic
183075
Mladen Rakic  
Jason2005:
Would you have an example on how to do that... I'm sorry for asking so much, I just am starting out with coding and haven't got the hang of how to check if those levels are crossed in the condition to buy or sell.

Jason2005

You have a code how it is done in the indicator you attached

Jason2005
160
Jason2005  
mladen:
Jason2005 You have a code how it is done in the indicator you attached

Would it be this then?

MathMod(i, 5) == 0.0
Thierry F.
123
Thierry F.  
mladen:
Thierry

You can use this function to find out if the last closed order was closed with profit or loss :

double lastOrderProfit(int magicNumber=0)

{

datetime lastTime = 0;

double lastProfit = 0;

for(int i=OrdersHistoryTotal()-1; i>=0; i--)

{

if (OrderSelect(i,SELECT_BY_POS, MODE_HISTORY)==false) break;

if (magicNumber!=0)

if (OrderMagicNumber() != magicNumber) continue;

if (OrderSymbol() != Symbol()) continue;

if (OrderCloseTime() <= lastTime) continue;

lastTime = OrderCloseTime();

lastProfit = OrderProfit()+OrderSwap()+OrderCommission();

}

return(lastProfit);

}

Just check if the result of lastOrderProfit() function, and if it is < 0, then apply your martingale rule


Hi Mladen,

Thanks for your response.

The lastorderprofit function isn't needed because my lastTradeTicket() function does the same job.

Could you please give a look at my EA?

I'm not asking you to edit the code, but I would be very thankful if you could tell me what I coded wrong.

Thanks in advance,

Thierry

53_martingale.mq4

Files: