decrease factor's algorithm

univetsity

Trader
Mar 14, 2011
27
0
22
china
dear gurus:I am an newbie in MQL4,just find an method of cut positions when lossing, the code as follows:
extern double DecreaseFactor = 10;
int losses=0;
if(DecreaseFactor>0)
{
for(int i=orders-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) { Print("Error in history!"); break; }
if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL) continue;
//----
if(OrderProfit()>0) break;
if(OrderProfit()<0) losses++;
}
if(losses>1) lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1);
}
however,I use it to backtrack,the results disorient me.
the follow are some trade log:
type order no size price profit balance
buy 1 2.00 1.3345 0.00 50000.00
close 1 2.00 1.3272 -1555.74 48444.26
sell 2 1.90 1.3297 0.00 48444.26
close 2 1.90 1.3361 -1292.00 47152.26
buy 3 1.50 1.3354 0.00 47152.26
close 3 1.50 1.3315 -656.81 46495.45
sell 4 1.30 1.3306 0.00 46495.45
close 4 1.30 1.2956 4436.60 50932.05

the question is :
2 is the default size in my EA,but what the "lot" stands for in the equation, 2 or the latest size?
why the order 4's size becomes 1.30?

PLs gurus show arithmetic process,TKS!:)
 

Enivid

Administrator
Staff member
Nov 30, 2008
18,622
1,367
144
Odesa
www.earnforex.com
If that's the same EA that you've listed here, then your Lot size depends not only on this equation with the decrease factor but also on the amount of free margin available:
lot=NormalizeDouble(AccountFreeMargin()*MaximumRis k/100.0,1);

But it still doesn't make much sense... Care to attach this EA .mq4 file with all the input parameters you are using?
 

univetsity

Trader
Mar 14, 2011
27
0
22
china
If that's the same EA that you've listed here, then your Lot size depends not only on this equation with the decrease factor but also on the amount of free margin available:
lot=NormalizeDouble(AccountFreeMargin()*MaximumRis k/100.0,1);

But it still doesn't make much sense... Care to attach this EA .mq4 file with all the input parameters you are using?

it's the same equation,that just because I wanna when ordeprofit>0,the next order's positon should largen,when lossing the position should reduction.
however,some guys say the "losses" in the equation
"if (OrderProfit()<0) losses++;"
stands lossing amount of loss,others say it is the lossing order's N.O.
this is why I ask losses stands what,and in my log why the order 4's position is 1.3 pls gurus help me.:(
 

univetsity

Trader
Mar 14, 2011
27
0
22
china
In the file attached there, DecreaseFactor = 3, not 10. So, what is it? And the starting balance is 5000, right?

the trade log is produced by DecreaseFactor=10,and the starting balance is 50,000. lot=NormalizeDouble(AccountFreeMargin()*MaximumRis k/500.0,1);

My question I have listed in 3# tks!
 

Enivid

Administrator
Staff member
Nov 30, 2008
18,622
1,367
144
Odesa
www.earnforex.com
The it's all correct. The cycle inside LotsOptimized() calculates the number of losing orders met in a row. Initially we have 50,000 free margin and DecreaseFactor = 10:

1. lot = 50000 * 0.02 / 500 = 2
since losses <= 1, we don't do anything else.
2. lot = 48444.26 * 0.02 / 500 = 1.93 = 1.9 after normalizing
since losses <= 1, we don't do anything else.
3. lot = 47152.26 * 0.02 / 500 = 1.88 = 1.9 after normalizing
losses > 1, lot = 1.9 - 1.9 * 2 / 10 = 1.52 = 1.5 after normalizing
4. lot = 46495.45 * 0.02 / 500 = 1,85 = 1,9 after normalizing
losses > 1, lot = 1.9 - 1.9 * 3 / 10 = 1,33 = 1,3 after normalizing
 

univetsity

Trader
Mar 14, 2011
27
0
22
china
thank you for your help to show the cycle correctly,but now when I backtracking EURUSD on 01.01.2011-03.26.2011 M5 the starting balance is 50,000 by add.mq4,it produce some results amazing me again.
I modified the decrease factor=3,and
if(losses>=1) lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1);

my question is why the third order's lot is 2?
I think it should count like this,
lot=50180*0.02/500=2,
since losses=1,lot=2-2*1/3=1.34=1.3 after normalizing
confused:
 

Attachments

  • add.mq4
    5.5 KB · Views: 38
  • add.htm
    49.4 KB · Views: 30

univetsity

Trader
Mar 14, 2011
27
0
22
china
unconsciously, I found just the model will affect order's lots.
using the add.mq4,all other factors are same,(eurusd, m5 ,01.01.2011-26.03.2011)but under each tick model, the log's 77-78 line will get as the attachment.(lot is 2.1)
gurus can contrast the openprice model 's report I attached in the 9#(77-78line the order's lot is 1.0),I am appeciating for explain this.add another question:when living trading,it will follow which model?each tick or open price and other?
I have this equation code
//---- go trading only for first tiks of new bar
if(Volume[0]>1) return;
I think it would limit only openprice model taking effect.
but why the demo backtracketing test have differernt result under differernt model?
I am an newbie,so don't laugh at me.confused:
 

Attachments

  • eachtickmodel.jpg
    eachtickmodel.jpg
    5.4 KB · Views: 13
Last edited:

Enivid

Administrator
Staff member
Nov 30, 2008
18,622
1,367
144
Odesa
www.earnforex.com
thank you for your help to show the cycle correctly,but now when I backtracking EURUSD on 01.01.2011-03.26.2011 M5 the starting balance is 50,000 by add.mq4,it produce some results amazing me again.
I modified the decrease factor=3,and
if(losses>=1) lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1);

my question is why the third order's lot is 2?
I think it should count like this,
lot=50180*0.02/500=2,
since losses=1,lot=2-2*1/3=1.34=1.3 after normalizing
confused:
Are you sure you were running the test on a newer version of add.mq4 (with if(losses>=1))? Because I've got 1.3 lots on the 3rd order and results were the same for open prices and for every tick models.
 

univetsity

Trader
Mar 14, 2011
27
0
22
china
Are you sure you were running the test on a newer version of add.mq4 (with if(losses>=1))? Because I've got 1.3 lots on the 3rd order and results were the same for open prices and for every tick models.
I am sure running the newer version.sickeningly,I even got different result by different Version of MT4,but all of them are wrong.
Enviod,I am appreciating for show your MT4's downloading address.
 

univetsity

Trader
Mar 14, 2011
27
0
22
china
It's MetaTrader 4 from Alpari.
TKS!
another problem:I wanna add Trailing stop for my code as follow:
void TS(double ts=550)
{
bool res = false;
int total = OrdersTotal();

for(int i = 0;i<total;i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) ==false) continue;
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)
{
if( OrderType()==OP_BUY && Bid-OrderOpenPrice()>Point*ts && OrderStopLoss()<Bid-Point*ts)//OrderOpenPrice() - OffsetPips * Point > OrderStopLoss())
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*ts,OrderTakeProfit(),0,Blue);
if( OrderType()==OP_SELL && OrderOpenPrice()-Ask>OrderStopLoss()&& OrderStopLoss()<Point*ts-Ask) //OrderOpenPrice() + OffsetPips * Point < OrderStopLoss()
OrderModify(OrderTicket(),OrderOpenPrice(),Point*ts-Ask,OrderTakeProfit(),0,Yellow);
}
}
}

and I insert TS() into my open order condtion code,but I found it couldn't make effect.The whole frame is the same as the add.MQ4 ,thd digits is 5.
 

Enivid

Administrator
Staff member
Nov 30, 2008
18,622
1,367
144
Odesa
www.earnforex.com
What do you mean by "couldn't make effect"? Did you see any errors in the Journal or Experts tab of MT4?

BTW, it's always better to use brackets around separate conditions inside one "if". E.g.:
if ((OrderType()==OP_BUY) && (Bid-OrderOpenPrice()>Point*ts) && (OrderStopLoss()<Bid-Point*ts))
 

univetsity

Trader
Mar 14, 2011
27
0
22
china
What do you mean by "couldn't make effect"? Did you see any errors in the Journal or Experts tab of MT4?

BTW, it's always better to use brackets around separate conditions inside one "if". E.g.:
if ((OrderType()==OP_BUY) && (Bid-OrderOpenPrice()>Point*ts) && (OrderStopLoss()<Bid-Point*ts))
no error,no warning.
It seems this is connected with the code
if(CalculateCurrentOrders(Symbol())<1)
when it is modified with“<2”,TS() could takes effect(sorry,not make effect ).
however,under "<2" ,I found the the first open condition as follow:
if (m1<m3-0.0003)
{
ticket1=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,5,Ask+105*Point,0,"SELL#1",MAGICMA,0,Red); return;
}
often would open 2 orders,but the second open condition as follow:
if (m1<m4-0.0005)
{
ticket2=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,5,Ask+105*Point,0,"SELL#2",MAGICMA,0,Green); return;
}
often could't open any order.
I wanna the code on the basis of the first conditon could open 1 order, then, if meet the second condition would add another order.
I have tried to nest them,but failed. could you help me again?:)
 

univetsity

Trader
Mar 14, 2011
27
0
22
china
is modify like this?
if(CalculateCurrentOrders(Symbol())<1&&(m1<m3-0.0003))
{ticket1=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,5,Ask+105*Point,0,"SELL#1",MAGICMA,0,Red); return; }
if(CalculateCurrentOrders(Symbol())<2&&(m1<m4-0.0005))
{ticket2=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,5,Ask+105*Point,0,"SELL#2",MAGICMA,0,Green); return; }
now the second condtion will open 2 orders,but the first will none.
My strategy is follow trend mode.Although there are 2 open condition,when meet the second contion,the first order should be profit. but I donn't know how to nest them to when the 1 order profit meet some points,the 2 will open.but if the 1's profit (even loss)couldn't meet this point,the 2 should not be opened.This makes an add opening strategy.
 
Last edited: