Coding help - page 157

 
mladen:
Christoff

That way Decimals variable should be OK

In any case, check what error code are you getting in a case of unsuccessful order (partial) close

Mladen,

Thank you for the hint.

The problem is that there is no error message. When it does not work, it seems as if PartialTP function has not even been called. Other times - usually at the first orders after the EA restarted - it works well.

I thought maybe one of the variables does not get back to zero value, or something like that, but I still cannot find the bug.

 
chrisstoff:
Mladen,

Thank you for the hint.

The problem is that there is no error message. When it does not work, it seems as if PartialTP function has not even been called. Other times - usually at the first orders after the EA restarted - it works well.

I thought maybe one of the variables does not get back to zero value, or something like that, but I still cannot find the bug.

chrisstoff

Sorry, but with a partial code I can not help more

What you are describing means that there is a need to debug the code - and that can be done only by executing it while using some sort of control points

 
mladen:
chrisstoff

Sorry, but with a partial code I can not help more

What you are describing means that there is a need to debug the code - and that can be done only by executing it while using some sort of control points

Thank you, Mladen.

It is reassuring that you did not see problems in the code.

I think all the relevant parts of the code was posted, so I have to find the bug elsewhere.

 
mladen:
hock87

Please read the post above your post for a solution of that problem

The complete code for that is the following :

int TotalOrders = 0;

for (int i=0; i <= OrdersTotal(); i++)

{

if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))

if (OrderSymbol() == Symbol())

TotalOrders++;

}

if (TotalOrders<1)

{

ticket=OrderSend(Symbol(), OP_BUY, lots, Ask, 3, 0, 0);

stop=(Ask-stopsize*Point);

prof=(Ask+profsize*Point);

OrderModify( ticket, OrderOpenPrice(), stop, prof, 0, Blue);

}

Malden,

If I want to open lot sizes at a percentage of my account balance, how to code it?

Example:

My account balance $500,

i want to open a lot sizes with 5%, then it auto count and open the $0.25 lots, TP=10 SL=20.

How to code it?

Thanks.

 
hock87:
Malden,

If I want to open lot sizes at a percentage of my account balance, how to code it?

Example:

My account balance $500,

i want to open a lot sizes with 5%, then it auto count and open the $0.25 lots, TP=10 SL=20.

How to code it?

Thanks.

You can use a function like this for that :

double getLots(double stopLoss, double risk)

{

RefreshRates();

double pPoint = MarketInfo(Symbol(),MODE_POINT);

double step = MarketInfo(Symbol(),MODE_LOTSTEP);

double minLot = MarketInfo(Symbol()l,MODE_MINLOT);

double maxLot = MarketInfo(Symbol(),MODE_MAXLOT);

double lots = minLot;

if (risk>0 && stopLoss>0)

{

lots = AccountFreeMargin()*(risk/100.0)/(stopLoss*MarketInfo(s_symbol,MODE_TICKVALUE)/pPoint);

}

}

return(MathMax(MathMin(lots,maxLot),minLot));

}

PS: stopLoss has to be passed to the function in pips/points already (not in integer values)

 
mladen:
You can use a function like this for that :
double getLots(double stopLoss, double risk)

{

RefreshRates();

double pPoint = MarketInfo(Symbol(),MODE_POINT);

double step = MarketInfo(Symbol(),MODE_LOTSTEP);

double minLot = MarketInfo(Symbol()l,MODE_MINLOT);

double maxLot = MarketInfo(Symbol(),MODE_MAXLOT);

double lots = minLot;

if (risk>0 && stopLoss>0)

{

lots = AccountFreeMargin()*(risk/100.0)/(stopLoss*MarketInfo(s_symbol,MODE_TICKVALUE)/pPoint);

}

}

return(MathMax(MathMin(lots,maxLot),minLot));

}

PS: stopLoss has to be passed to the function in pips/points already (not in integer values)

Thanks, Mladen.

But I failure to compound the code to buy order code.

It appear many errors and warning.

How to compound it?

Thanks.

extern double lots = 0.1;extern double stopsize = 20;

extern double profsize = 10;

int err;

int ticket;

double stop;

double prof;

int start()

{

int TotalOrders = 0;

for (int i=0; i <= OrdersTotal(); i++)

{

if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))

if (OrderSymbol() == Symbol())

TotalOrders++;

}

if (TotalOrders<1)

{

ticket=OrderSend(Symbol(), OP_BUY, lots, Ask, 3, 0, 0, NULL,LimeGreen);

stop=(Ask-stopsize*Point);

prof=(Ask+profsize*Point);

OrderModify( ticket, OrderOpenPrice(), stop, prof, 0, Blue);

}

err=GetLastError();

// Comment("This is a test ", err, " ", stop, " ", prof);

Comment(" ");

}
 
hock87:
Thanks, Mladen.

But I failure to compound the code to buy order code.

It appear many errors and warning.

How to compound it?

Thanks.

extern double lots = 0.1;extern double stopsize = 20;

extern double profsize = 10;

int err;

int ticket;

double stop;

double prof;

int start()

{

int TotalOrders = 0;

for (int i=0; i <= OrdersTotal(); i++)

{

if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))

if (OrderSymbol() == Symbol())

TotalOrders++;

}

if (TotalOrders<1)

{

ticket=OrderSend(Symbol(), OP_BUY, lots, Ask, 3, 0, 0, NULL,LimeGreen);

stop=(Ask-stopsize*Point);

prof=(Ask+profsize*Point);

OrderModify( ticket, OrderOpenPrice(), stop, prof, 0, Blue);

}

err=GetLastError();

// Comment("This is a test ", err, " ", stop, " ", prof);

Comment(" ");

}

Try it like this (I tested it now and it works) :

extern double lots = 0.1;extern double stopsize = 20;

extern double profsize = 10;

int err;

int ticket;

double stop;

double prof;

int init() { return(0); }

int deinit() { return(0); }

int start()

{

int TotalOrders = 0;

for (int i=0; i <= OrdersTotal(); i++)

{

if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))

if (OrderSymbol() == Symbol())

TotalOrders++;

}

if (TotalOrders<1)

{

ticket=OrderSend(Symbol(), OP_BUY, lots, Ask, 3, 0, 0, NULL,LimeGreen);

stop=(Ask-stopsize*Point*MathPow(10,Digits%2));

prof=(Ask+profsize*Point*MathPow(10,Digits%2));

OrderModify( ticket, OrderOpenPrice(), stop, prof, 0, Blue);

}

err=GetLastError();

// Comment("This is a test ", err, " ", stop, " ", prof);

Comment(" ");

}
Files:
_test_1.mq4  1 kb
test_1.gif  78 kb
 

dear mladen and mr. tools pls,

is it possible to create an ea that gives signal based on certain conditions like the signals of 3 or 4 indicators meeting predefined conditions.

e.g

i want the ea to signal a buy hwen:

ema 8 cross ema 21

qqe above 0

non lag ma is green

and candle is above the ichimoku cloud

the EA should not open trades but just give the signal when the stated conditions are met

 

Thank you, Mladen.

I have try it.

but it still open the lots with $0.1

I want toopen lot sizes at a percentage of my account balance.

EX:account balance have $500.

open a lot sizes with 5%, then it auto count and open the $0.25 lots, TP=10 SL=20.

LotSizeSlot1 = AccountBalance() * (RiskSlot1 / 100)

LotSizeSlot1 = $500*(5%/100)

LotSizeSlot1 = $ 0.25

Then it auto open $0.25 lots and TP=10 SL=20.

How to create it?

Thanks.

 
hock87:
Thank you, Mladen.

I have try it.

but it still open the lots with $0.1

I want toopen lot sizes at a percentage of my account balance.

EX:account balance have $500.

open a lot sizes with 5%, then it auto count and open the $0.25 lots, TP=10 SL=20.

LotSizeSlot1 = AccountBalance() * (RiskSlot1 / 100)

LotSizeSlot1 = $500*(5%/100)

LotSizeSlot1 = $ 0.25

Then it auto open $0.25 lots and TP=10 SL=20.

How to create it?

Thanks.

Like in the one attached

Files:
_test_2.mq4  2 kb