Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Nowhere without you - 6. - page 1063

 

Hello Guru.

Can you tell me if I'm on the right track?

Objective: to introduce a variable lot size depending on the distance from the MA.

In the input parameters:

extern double Lot1 = 0.01; // first variant of lot
extern double Lot2 = 0.03; // second variant of lot
extern int Distan = 20; // distance from SlowMA

Ma has been defined in the Expert Advisor body:

double SlowMA = iMA(NULL,60,periodSlowMA,0,MODE_EMA,PRICE_MEDIAN,0);

Further we need a condition: if the current price (Bid or Ask depending on direction) is up to the Distant size, we use the first lot size, if it is more, we use the second one.

The first thing that came to mind:

if (Ask-SlowMA<Distan) Lot == Lot1;
if (SlowMA-Bid<Distan) Lot == Lot1;
if (Ask-SlowMA>Distan) Lot == Lot2;
if (SlowMA-Bid>Distan) Lot == Lot2;

But it doesn't work. The error is either in Lot connections or in general logic.

Thanks in advance.

 
delf699:

Hello Guru.

Can you tell me if I'm on the right track?

Objective: to introduce a variable lot size depending on the distance from the MA.

In the input parameters:

extern double Lot1 = 0.01; // first lot size
extern double Lot2 = 0.03; // second lot variant.
extern int Distan = 20; // distance from the SlowMA

In the body of the Expert Advisor defined Ma:

double SlowMA = iMA(NULL,60,periodSlowMA,0,MODE_EMA,PRICE_MEDIAN,0);

Then we need a condition: if the current price (Bid or Ask depending on direction) is up to the Distant size, we use the first lot size, if it is larger, we use the second.

The first thing that came to mind:

if (Ask-SlowMA<Distan) Lot == Lot1;
if (SlowMA-Bid<Distan) Lot == Lot1;
if (Ask-SlowMA>Distan) Lot == Lot2;
if (SlowMA-Bid>Distan) Lot == Lot2;

But it doesn't work. It's either an error in conjunction with Lot, or in general logic.

Thank you in advance.

What does Ask, Bid and SlowMA matter when Ask-SlowMA can be greater than Distan???
 

Bid and Ask are the last known bid and ask prices, i.e. the current price.

SlowMA is my renamed Ma

(double SlowMA = iMA(NULL,60,periodSlowMA,0,MODE_EMA,PRICE_MEDIAN,0);)

The deviation of the current price (Bid or Ask) from the Ma (SlowMA) can be more or less than Distans. According to this you should set either the first or the second lot size.

I've found variants of changing the lot based on the deposit or the number of open orders, but I can't get any response from such a linear term.

Something tells me we need a function that returns an absolute value. I.e. if we apply it to a negative number, the result will be positive.

 
delf699:

Bid and Ask are the last known bid and ask prices, i.e. the current price.

SlowMA is my renamed Ma

(double SlowMA = iMA(NULL,60,periodSlowMA,0,MODE_EMA,PRICE_MEDIAN,0);)

The deviation of the current price (Bid or Ask) from the Ma (SlowMA) can be more or less than Distans. According to this you should set either the first or the second lot size.

I've found variants of changing the lot based on the deposit or the number of open orders, but I can't get any response from such a linear term.

Something tells me we need a function that returns an absolute value. I.e. if we apply it to a negative number, the result will be positive.


The absolute value of the difference of Bid and MA may be needed a bit later; now, since you haven't got the hint, let's calculate together

Bid = 1.12730;

MA = 1.12530;

Distans = 20;

Question:

When and whether 1.1273 to 1.1253 will be more than 20???

For some reason my calculator shows 0.002 only. But that's a long way from 20...

 

Thank you, I see. Or you could do it like this:

dist=Distan*Point;

if (Ask-SlowMA<dist) Lot == Lot1;
if (SlowMA-Bid<dist) Lot == Lot1;
if (Ask-SlowMA>dist) Lot == Lot2;
if (SlowMA-Bid>dist) Lot == Lot2;

So how do I do this properly?

 
delf699:

Thank you, I see. Or you could do it like this:

dist=Distan*Point;

if (Ask-SlowMA<dist) Lot == Lot1;
if (SlowMA-Bid<dist) Lot == Lot1;
if (Ask-SlowMA>dist) Lot == Lot2;
if (SlowMA-Bid>dist) Lot == Lot2;

So how do I do this properly?

And now we can use MathAbs() in order not to overthink our head by keeping track of what is being subtracted from what. And there is another interesting conditional operator

Lot = MathAbs(Ask-SlowMA) < dist ? Lot1 : Lot2;


Which means: The Lot variable will be assigned the value of Lot1 if Ask- SlowMA is less than dist, otherwise, the value of Lot2 will be assigned.

One more recommendation: Forget the Point variable. Use _Point or the Point() function

 
Thank you, a beautiful solution.
 
I've already broken my eyes, I can't find where brackets are missing, I get errors like '(' - unbalanced left parenthesis test.mq4 31 8 (starting from the line where TrendDetection() is called)

I need help, I am still just learning...


#define BULL 1;
#define BEAR 2;

//--------------------------------------------------------------------
extern int SL = 200;
extern int TP = 200;
extern double Lots = 0.01;
extern double SarStep = 0.02;
extern double SarMaximum = 0.2;
extern int slippage = 5;
extern int Magic = 1;
extern int MaxOrder = 1;
string OrderComments = "Bargain 1.0;
int Cnt=0;
datetime PreviousBar;

int start()
{
Cnt=OrdersTotal();
{
if (Cnt<MaxOrder)
{
if(NewBar() == true)
{
if(TrendDetection() == BULL){
OrderSend(Symbol(),OP_BUY,Lots,NormalizeDouble(Ask,Digits),slippage,NormalizeDouble(Ask,Digits)-(SL*Point),NormalizeDouble(Ask,Digits)+(TP*Point),OrderComments,Magic,0,CLR_NONE);}
if(TrendDetection() == BEAR){
OrderSend(Symbol(),OP_SELL,Lots,NormalizeDouble(Bid,Digits),slippage,NormalizeDouble(Bid,Digits)+(SL*Point),NormalizeDouble(Bid,Digits)-(TP*Point),OrderComments,Magic,0,CLR_NONE);}
}
}
else
{return(0);}
}
return(0);
}

int TrendDetection()
{
double Sar = iSAR(NULL,0,SarStep,SarMaximum,1);
if(Close[1] > Sar) {return(BULL);}
if(Close[1] < Sar) {return(BEAR);}
return(0);}
bool NewBar()
{
if(PreviousBar<Time[0])
{
PreviousBar = Time[0];
{ return(true);
}
else
{
return(false);
}
return(false);
}
 
AlexeyVik:

And now you can apply MathAbs() so that you don't have to keep track of what to subtract from what. And another interesting conditional operator


Which means: The Lot variable will be assigned the value of Lot1 in case Ask- SlowMA is less than dist, otherwise, the value of Lot2 will be assigned.

One more recommendation: Forget the Point variable. Use _Point or the Point() function

What about Point, everything seems to be working?
 
abeiks:
What about Point, does it seem to be working?

The Volga GAZ 21 also works. And even the president drives one, but for some reason no one buys it for use. They only buy it as an antique.