Coding help - page 238

 

Is there a chance to add options to choose?

For example,

Separately, purchase, and sale separately

Modification: BE all buy 0, or +1, or +2 pips or sell all BE 0, 1 or 2 pips?

Is it possible?

be.mq4

Files:
be.mq4  1 kb
 

Hello Mladen ,

Can anyone please tell me what im doing wrong, i mostly write my ea using FX Gen with snippets and codes using examples from mq4 compiler and online forums.Im trying to code my strategy for easier trading, ive been able to select the last order successfully using this code.

void OpenNewTrade()

{ datetime lastTime = 0;

int lastTicket = -1; // None open.

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

if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))

{

if (OrderSymbol() == Symbol() && OrderMagicNumber() == OrderId && OrderOpenTime() >= lastTime && OrderTicket() > lastTicket )

{

lastTime = OrderOpenTime();

lastTicket = OrderTicket();

if (OrderType() == OP_BUY || OrderType() == OP_SELL

&& Ask - OrderOpenPrice() >= Grid*PipValue*Point

{

{

BuyOrder();

}

}

if (OrderType() == OP_BUY || OrderType() == OP_SELL

&& OrderOpenPrice() - Bid >= Grid*PipValue*Point

{

{

SellOrder();

}

}

}

return(lastTicket);

}

else

Print("OrderSelect() error - ", ErrorDescription(GetLastError()));

}

Can you take a look at this code am trying to delete the oldest order first using datetime and order ticket, e.g if opened order >2 delete oldest order.

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

// Etasoft Inc. Forex EA and Script Generator version 4.1 EA

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

// Keywords: MT4, Forex EA builder, create EA, expert advisor developer

#property copyright "Copyright © 2011, Etasoft Inc. Forex EA Generator v4.1"

#property link

#include

#include

// exported variables

extern int OrderId = 1;

extern int Slippage = 2;

extern double Lots = 0.01;

extern int MaxOrdersAllowed = 2;// op buy and op sell allowed

extern int Grid = 10; // total distance in pips to open orders

extern string EAComment = "564 testing";

// local variables

double PipValue=1; // this variable is here to support 5-digit brokers

bool Terminated = false;

string LF = "\n"; // use this in custom or utility blocks where you need line feeds

int NDigits = 4; // used mostly for NormalizeDouble in Flex type blocks

int ObjCount = 0; // count of all objects created on the chart, allows creation of objects with unique names

int current = 0;

int init()

{

NDigits = Digits;

if (false) ObjectsDeleteAll(); // clear the chart

Comment(""); // clear the chart

}

// Expert start

int start()

{

if (Bars < 10)

{

Comment("Not enough bars");

return (0);

}

if (Terminated == true)

{

Comment("EA Terminated.");

return (0);

}

OnEveryTick1();

}

void OnEveryTick1()

{

if (true == false && false) PipValue = 10;

if (true && (NDigits == 3 || NDigits == 5)) PipValue = 10;

IfOrderDoesNotExist2();

IfOrderDoesNotExist4();

MaximumOrder();

}

void IfOrderDoesNotExist2()

{

bool exists = false;

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

if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))

{

if (OrderType() == OP_BUYSTOP && OrderSymbol() == Symbol() && OrderMagicNumber() == 1)

{

exists = true;

}

}

else

{

Print("OrderSelect() error - ", ErrorDescription(GetLastError()));

}

if (exists == false)

{

BuyPendingOrder();

}

}

void IfOrderDoesNotExist4()

{

bool exists = false;

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

if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))

{

if (OrderType() == OP_SELLSTOP && OrderSymbol() == Symbol() && OrderMagicNumber() == 1)

{

exists = true;

}

}

else

{

Print("OrderSelect() error - ", ErrorDescription(GetLastError()));

}

if (exists == false)

{

SellPendingOrder();

}

}

void BuyPendingOrder()

{

int expire = TimeCurrent() + 60 * 0;

double price = NormalizeDouble((Ask-((Ask-Bid)/2)), NDigits) + Grid*PipValue*Point;

double SL = price - 0*PipValue*Point;

if (0 == 0) SL = 0;

double TP = price + 0*PipValue*Point;

if (0 == 0) TP = 0;

if (0 == 0) expire = 0;

int ticket = OrderSend(Symbol(), OP_BUYSTOP, Lots, price, Slippage, SL, TP, EAComment, OrderId, expire, Blue);

if (ticket == -1)

{

Print("OrderSend() error - ", ErrorDescription(GetLastError()));

}

}

void SellPendingOrder()

{

int expire = TimeCurrent() + 60 * 0;

double price = NormalizeDouble((Bid+((Ask-Bid)/2)), NDigits) - Grid*PipValue*Point;

double SL = price + 0*PipValue*Point;

if (0 == 0) SL = 0;

double TP = price - 0*PipValue*Point;

if (0 == 0) TP = 0;

if (0 == 0) expire = 0;

int ticket = OrderSend(Symbol(), OP_SELLSTOP, Lots, price, Slippage, SL, TP, EAComment, OrderId, expire, Red);

if (ticket == -1)

{

Print("OrderSend() error - ", ErrorDescription(GetLastError()));

}

}

void MaximumOrder()

{

if (MaxOrdersAllowed> 0)

{

OrderCount();

}

}

void OrderCount()

{

int count = 0;

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

if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))

{

if (OrderSymbol() == Symbol())

if (OrderMagicNumber() == OrderId)

if (OrderType() == OP_BUY || OrderType() == OP_SELL)

{

count++;

}

}

else

{

Print("OrderSend() error - ", ErrorDescription(GetLastError()));

}

if (count > MaxOrdersAllowed )

{

DeleteOldestOrder();

}

}

void DeleteOldestOrder()

{

datetime lastTime = 0;

int lastTicket = -1; // None open.

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

if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))

{

if (OrderSymbol() == Symbol() && OrderMagicNumber() == OrderId && OrderOpenTime() < lastTime && OrderTicket() < lastTicket )

{

lastTime = OrderOpenTime();

lastTicket = OrderTicket();

if (OrderType() == OP_BUY || OrderType() == OP_SELL )

{

{

OrderClose(OrderTicket(),OrderLots(), OrderClosePrice(),Slippage,Red);

}

}

}

return(lastTicket);

}

else

Print("OrderSelect() error - ", ErrorDescription(GetLastError()));

}

int deinit()

{

if (false) ObjectsDeleteAll();

}

 
sulaimoney:
Hello Mladen ,

Can anyone please tell me what im doing wrong, i mostly write my ea using FX Gen with snippets and codes using examples from mq4 compiler and online forums.Im trying to code my strategy for easier trading, ive been able to select the last order successfully using this code.

void OpenNewTrade()

{ datetime lastTime = 0;

int lastTicket = -1; // None open.

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

if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))

{

if (OrderSymbol() == Symbol() && OrderMagicNumber() == OrderId && OrderOpenTime() >= lastTime && OrderTicket() > lastTicket )

{

lastTime = OrderOpenTime();

lastTicket = OrderTicket();

if (OrderType() == OP_BUY || OrderType() == OP_SELL

&& Ask - OrderOpenPrice() >= Grid*PipValue*Point

{

{

BuyOrder();

}

}

if (OrderType() == OP_BUY || OrderType() == OP_SELL

&& OrderOpenPrice() - Bid >= Grid*PipValue*Point

{

{

SellOrder();

}

}

}

return(lastTicket);

}

else

Print("OrderSelect() error - ", ErrorDescription(GetLastError()));

}

Can you take a look at this code am trying to delete the oldest order first using datetime and order ticket, e.g if opened order >2 delete oldest order.

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

// Etasoft Inc. Forex EA and Script Generator version 4.1 EA

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

// Keywords: MT4, Forex EA builder, create EA, expert advisor developer

#property copyright "Copyright © 2011, Etasoft Inc. Forex EA Generator v4.1"

#property link

#include

#include

// exported variables

extern int OrderId = 1;

extern int Slippage = 2;

extern double Lots = 0.01;

extern int MaxOrdersAllowed = 2;// op buy and op sell allowed

extern int Grid = 10; // total distance in pips to open orders

extern string EAComment = "564 testing";

// local variables

double PipValue=1; // this variable is here to support 5-digit brokers

bool Terminated = false;

string LF = "\n"; // use this in custom or utility blocks where you need line feeds

int NDigits = 4; // used mostly for NormalizeDouble in Flex type blocks

int ObjCount = 0; // count of all objects created on the chart, allows creation of objects with unique names

int current = 0;

int init()

{

NDigits = Digits;

if (false) ObjectsDeleteAll(); // clear the chart

Comment(""); // clear the chart

}

// Expert start

int start()

{

if (Bars < 10)

{

Comment("Not enough bars");

return (0);

}

if (Terminated == true)

{

Comment("EA Terminated.");

return (0);

}

OnEveryTick1();

}

void OnEveryTick1()

{

if (true == false && false) PipValue = 10;

if (true && (NDigits == 3 || NDigits == 5)) PipValue = 10;

IfOrderDoesNotExist2();

IfOrderDoesNotExist4();

MaximumOrder();

}

void IfOrderDoesNotExist2()

{

bool exists = false;

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

if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))

{

if (OrderType() == OP_BUYSTOP && OrderSymbol() == Symbol() && OrderMagicNumber() == 1)

{

exists = true;

}

}

else

{

Print("OrderSelect() error - ", ErrorDescription(GetLastError()));

}

if (exists == false)

{

BuyPendingOrder();

}

}

void IfOrderDoesNotExist4()

{

bool exists = false;

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

if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))

{

if (OrderType() == OP_SELLSTOP && OrderSymbol() == Symbol() && OrderMagicNumber() == 1)

{

exists = true;

}

}

else

{

Print("OrderSelect() error - ", ErrorDescription(GetLastError()));

}

if (exists == false)

{

SellPendingOrder();

}

}

void BuyPendingOrder()

{

int expire = TimeCurrent() + 60 * 0;

double price = NormalizeDouble((Ask-((Ask-Bid)/2)), NDigits) + Grid*PipValue*Point;

double SL = price - 0*PipValue*Point;

if (0 == 0) SL = 0;

double TP = price + 0*PipValue*Point;

if (0 == 0) TP = 0;

if (0 == 0) expire = 0;

int ticket = OrderSend(Symbol(), OP_BUYSTOP, Lots, price, Slippage, SL, TP, EAComment, OrderId, expire, Blue);

if (ticket == -1)

{

Print("OrderSend() error - ", ErrorDescription(GetLastError()));

}

}

void SellPendingOrder()

{

int expire = TimeCurrent() + 60 * 0;

double price = NormalizeDouble((Bid+((Ask-Bid)/2)), NDigits) - Grid*PipValue*Point;

double SL = price + 0*PipValue*Point;

if (0 == 0) SL = 0;

double TP = price - 0*PipValue*Point;

if (0 == 0) TP = 0;

if (0 == 0) expire = 0;

int ticket = OrderSend(Symbol(), OP_SELLSTOP, Lots, price, Slippage, SL, TP, EAComment, OrderId, expire, Red);

if (ticket == -1)

{

Print("OrderSend() error - ", ErrorDescription(GetLastError()));

}

}

void MaximumOrder()

{

if (MaxOrdersAllowed> 0)

{

OrderCount();

}

}

void OrderCount()

{

int count = 0;

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

if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))

{

if (OrderSymbol() == Symbol())

if (OrderMagicNumber() == OrderId)

if (OrderType() == OP_BUY || OrderType() == OP_SELL)

{

count++;

}

}

else

{

Print("OrderSend() error - ", ErrorDescription(GetLastError()));

}

if (count > MaxOrdersAllowed )

{

DeleteOldestOrder();

}

}

void DeleteOldestOrder()

{

datetime lastTime = 0;

int lastTicket = -1; // None open.

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

if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))

{

if (OrderSymbol() == Symbol() && OrderMagicNumber() == OrderId && OrderOpenTime() < lastTime && OrderTicket() < lastTicket )

{

lastTime = OrderOpenTime();

lastTicket = OrderTicket();

if (OrderType() == OP_BUY || OrderType() == OP_SELL )

{

{

OrderClose(OrderTicket(),OrderLots(), OrderClosePrice(),Slippage,Red);

}

}

}

return(lastTicket);

}

else

Print("OrderSelect() error - ", ErrorDescription(GetLastError()));

}

int deinit()

{

if (false) ObjectsDeleteAll();

}

Try using a function like this :

void DeleteOldestOrder()

{

datetime lastTime = Time[0]+Period()*60;

int lastTicket = -1; // None open.

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

{

if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;

if ((OrderType() == OP_BUY || OrderType() == OP_SELL) && OrderSymbol() == Symbol() && OrderMagicNumber() == OrderId && OrderOpenTime() <= lastTime)

{

lastTime = OrderOpenTime();

lastTicket = OrderTicket();

}

}

if (lastTicket>-1)

if (OrderSelect(lastTicket,SELECT_BY_TICKET,MODE_TRADES))

OrderClose(OrderTicket(),OrderLots(), OrderClosePrice(),Slippage,Red);

}
 

Thanks Mladen

it worked flawlessly, btw i apologise for posting such a long piece of code on the forum. Im still getting used to the interface. Thank You.

 

Indicator doesn´t automatically refresh.

Here I have an indicator that is very well suited for my trading.

Unfortunately, he does not update by itself. I must always manually refresh that he is renewed.

Is there a way to solve this problem?

It would be very helpful !!

Thanks in advance.

P.S.: Attached all I have.

Files:
 

He wants the notification sms. How looks the code ?

 
popej30:
He wants the notification sms. How looks the code ?

popej30

Do like this :

SendNotification("notification message that you want to send");

 

And where you insert your phone number ? My number 0039 555-23-45. Where to enter ?

Example:

#property indicator_separate_window

#property indicator_minimum -1.1

#property indicator_maximum 1.1

#property indicator_buffers 2

#property indicator_color1 Aqua

#property indicator_color2 Lime

extern int period = 25;

extern int limit = 5000;

double g_ibuf_84[];

double g_ibuf_88[];

double g_ibuf_92[];

int init() {

IndicatorBuffers(3);

SetIndexStyle(0, DRAW_ARROW);

SetIndexStyle(1, DRAW_ARROW);

SetIndexStyle(2, DRAW_NONE);

SetIndexEmptyValue(0, 0.0);

SetIndexEmptyValue(1, 0.0);

SetIndexArrow(0, 233);

SetIndexArrow(1, 234);

SetIndexBuffer(0, g_ibuf_88);

SetIndexBuffer(1, g_ibuf_92);

SetIndexBuffer(2, g_ibuf_84);

IndicatorShortName("BR16");

return (0);

}

int start() {

double ld_72;

double ld_24 = 0;

double ld_32 = 0;

double ld_unused_40 = 0;

double ld_unused_48 = 0;

double ld_56 = 0;

double ld_unused_64 = 0;

double l_low_80 = 0;

double l_high_88 = 0;

for (int li_96 = 0; li_96 <= limit; li_96++) {

g_ibuf_88[li_96] = 0;

g_ibuf_92[li_96] = 0;

}

for (li_96 = 0; li_96 <= limit; li_96++) {

l_high_88 = High;

l_low_80 = Low;

ld_72 = (High[li_96] + Low[li_96]) / 2.0;

if (l_high_88 != l_low_80) ld_24 = 0.66 * ((ld_72 - l_low_80) / (l_high_88 - l_low_80) - 0.5) + 0.67 * ld_32;

else ld_24 = 0.0;

ld_24 = MathMin(MathMax(ld_24, -0.999), 0.999);

g_ibuf_84[li_96] = MathLog((ld_24 + 1.0) / (1 - ld_24)) / 2.0 + ld_56 / 2.0;

ld_32 = ld_24;

ld_56 = g_ibuf_84[li_96];

}

for (li_96 = 0; li_96 <= limit; li_96++) {

if (g_ibuf_84[li_96] >= 0.0 && g_ibuf_84[li_96 + 1] < 0.0) g_ibuf_88[li_96] = -1;

if (g_ibuf_84[li_96] 0.0) g_ibuf_92[li_96] = 1;

}

return (0);

}
 
popej30:
And where you insert your phone number ? My number 0039 555-23-45. Where to enter ?

Example:

#property indicator_separate_window

#property indicator_minimum -1.1

#property indicator_maximum 1.1

#property indicator_buffers 2

#property indicator_color1 Aqua

#property indicator_color2 Lime

extern int period = 25;

extern int limit = 5000;

double g_ibuf_84[];

double g_ibuf_88[];

double g_ibuf_92[];

int init() {

IndicatorBuffers(3);

SetIndexStyle(0, DRAW_ARROW);

SetIndexStyle(1, DRAW_ARROW);

SetIndexStyle(2, DRAW_NONE);

SetIndexEmptyValue(0, 0.0);

SetIndexEmptyValue(1, 0.0);

SetIndexArrow(0, 233);

SetIndexArrow(1, 234);

SetIndexBuffer(0, g_ibuf_88);

SetIndexBuffer(1, g_ibuf_92);

SetIndexBuffer(2, g_ibuf_84);

IndicatorShortName("BR16");

return (0);

}

int start() {

double ld_72;

double ld_24 = 0;

double ld_32 = 0;

double ld_unused_40 = 0;

double ld_unused_48 = 0;

double ld_56 = 0;

double ld_unused_64 = 0;

double l_low_80 = 0;

double l_high_88 = 0;

for (int li_96 = 0; li_96 <= limit; li_96++) {

g_ibuf_88[li_96] = 0;

g_ibuf_92[li_96] = 0;

}

for (li_96 = 0; li_96 <= limit; li_96++) {

l_high_88 = High;

l_low_80 = Low;

ld_72 = (High[li_96] + Low[li_96]) / 2.0;

if (l_high_88 != l_low_80) ld_24 = 0.66 * ((ld_72 - l_low_80) / (l_high_88 - l_low_80) - 0.5) + 0.67 * ld_32;

else ld_24 = 0.0;

ld_24 = MathMin(MathMax(ld_24, -0.999), 0.999);

g_ibuf_84[li_96] = MathLog((ld_24 + 1.0) / (1 - ld_24)) / 2.0 + ld_56 / 2.0;

ld_32 = ld_24;

ld_56 = g_ibuf_84[li_96];

}

for (li_96 = 0; li_96 <= limit; li_96++) {

if (g_ibuf_84[li_96] >= 0.0 && g_ibuf_84[li_96 + 1] < 0.0) g_ibuf_88[li_96] = -1;

if (g_ibuf_84[li_96] 0.0) g_ibuf_92[li_96] = 1;

}

return (0);

}

Here is a good explanation how you can do that :

 
Jim Clark:
Indicator doesn´t automatically refresh.

Here I have an indicator that is very well suited for my trading.

Unfortunately, he does not update by itself. I must always manually refresh that he is renewed.

Is there a way to solve this problem?

It would be very helpful !!

Thanks in advance.

P.S.: Attached all I have.

Jim

Instead of rewriting the whole thing added a limit to calculate bars and then all those bars are recalculated. That should solve the refreshing problem (juts keep the BarsTocalculate at some reasonable - not too big not too small value. I used 1000 as a default and it seems to be OK like that). Try it out

Files: