Need help with coding using history values - page 2

[Deleted]  
cloudbreaker:

Was just checking that I understood. In that case, the code I gave you is what you need. To isolate the orders in history which relate to the symbol of the current chart just check OrderSymbol() against Symbol().

I tried changing it to "GBPUSD" == Symbol() and OrderSymbol() == " GBPUSD",tried different variations and still no luck,what I get then is it shows some losses I don't even have,from other currencies,maybe the code is wrong or ?


what would be the right way to check OrderSymbol() == Symbol () in your opinion,the way it should work?

[Deleted]  
atomi50:

I tried changing it to "GBPUSD" == Symbol() and OrderSymbol() == " GBPUSD",tried different variations and still no luck,what I get then is it shows some losses I don't even have,from other currencies,maybe the code is wrong or ?


what would be the right way to check OrderSymbol() == Symbol () in your opinion,the way it should work?



Why are you hard-coding GPBUSD into your EA when you told me you wanted an EA that could be attached to any chart??

Have you looked at the docs to understand what the Symbol() and OrderSymbol() functions return, and therefore try to understand the code I've given you?

Try adding these print statements:


Print("Symbol = ",Symbol());

Print("OrderSymbol = ",OrderSymbol());

if (OrderSymbol() == Symbol())

 {

  do stuff;

 }

[Deleted]  
cloudbreaker:

Why are you hard-coding GPBUSD into your EA when you told me you wanted an EA that could be attached to any chart??

Have you looked at the docs to understand what the Symbol() and OrderSymbol() functions return, and therefore try to understand the code I've given you?

Try adding these print statements:


Print("Symbol = ",Symbol());

Print("OrderSymbol = ",OrderSymbol());

if (OrderSymbol() == Symbol())

{

do stuff;

}

I have checked the book and tried different stuff but no luck,this is a little advanced for me at this moment as I am just starting,the following code is the code of the EA I'm using.


The code relevant is at the beginning of the init start function,maybe you can see any mistakes I made cuz I'm looking at it every day and cant see anything.


#define BUYING 1
#define SELLING 2


//---- input parameters
extern string Desc_1 = "---- Global Settings ----";
extern int FastMA = 3;
extern int SlowMA = 50;

extern string Desc_2 = "---- Money Management ----";
extern double Lots = 0.01;
extern int StopLoss = 150;
extern int TakeProfit = 187;
extern int TrailingStop = 0;

extern string Desc_3 = "---- EA Settings ----";
extern int StartHour = -1;
extern int EndHour = 24;
extern int MagicNumber = 5623;
extern string EA_Name = "Atomi MA Cross";


int status;
int StopLevel;
int BarTraded = 0;

bool NewBar()
{
static datetime lastbar;
datetime curbar = Time[0];
if(lastbar!=curbar)
{
BarTraded = 0;
lastbar=curbar;
return (true);
}
else
{
return(false);
}
}

//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
StopLevel = MarketInfo(Symbol(), MODE_STOPLEVEL) + MarketInfo(Symbol(), MODE_SPREAD);

return(0);
}

//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
return(0);
}

//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
int i, total,i2,ticket,hstTotal=OrdersHistoryTotal();

// retrieving info from trade history

for(i2=0;i2<hstTotal;i2++)
{

if(OrderSelect(i2,SELECT_BY_POS,MODE_HISTORY)){
if (OrderSymbol() == Symbol()){
if ((TimeDayOfYear(OrderOpenTime()) == DayOfYear()) && (TimeYear(OrderOpenTime()) == Year())){
if (OrderProfit() <= 0)
{
Print(" Loss ");
}
else{
Print (" Profit " );
}
}
}
}
}


NewBar();
GetStatus();

if(TotalOpenTrades() == 0){
if(!TradesAllowed())
return(0);

//Check if we start with a long position
if(status == BUYING && !BarTraded){
Buy_Order();
BarTraded = 1;
Alert( " Buying " );
}

//Check if we start with a short position
if(status == SELLING && !BarTraded){
Sell_Order();
BarTraded = 1;
Alert( "Selling " );
}
}
else{
//Check open trades to potentially close them
total = OrdersTotal();
for(i=0;i<total;i++){
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)){
if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber){
if(OrderType() == OP_BUY){
if(status == SELLING){
ticket = OrderClose(OrderTicket(), OrderLots(), Bid, 3, Violet);

if(ticket == -1)
Print("OrderSend failed with error #", GetLastError());
else{
if(TradesAllowed() && !BarTraded){
Sell_Order();
BarTraded = 1;
}
}
}
else if(TrailingStop > 0 && OrderStopLoss() < Bid-TrailingStop*Point){
ticket = OrderModify(OrderTicket(), OrderOpenPrice(), Bid-TrailingStop*Point, OrderTakeProfit(), 0, Blue);

if(ticket == -1)
Print("OrderModify failed with error #", GetLastError());
}
}
else if(OrderType() == OP_SELL){
if(status == BUYING){
ticket = OrderClose(OrderTicket(), OrderLots(), Ask, 3, Violet);

if(ticket == -1)
Print("OrderSend failed with error #", GetLastError());
else{
if(TradesAllowed() && !BarTraded){
Buy_Order();
BarTraded = 1;
}
}
}
else if(TrailingStop > 0 && OrderStopLoss() > Ask+TrailingStop*Point){
ticket = OrderModify(OrderTicket(), OrderOpenPrice(), Ask+TrailingStop*Point, OrderTakeProfit(), 0, Blue);

if(ticket == -1)
Print("OrderModify failed with error #", GetLastError());
}
}
}
}
}
}

return(0);
}

int TotalOpenTrades()
{
int i;
int trades = 0;
int total = OrdersTotal()+ 1;

for(i=0;i<total;i++)
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
if(OrderType() <= OP_SELL && OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
trades++;

return(trades);
}

int Buy_Order()
{
double SL,TP;
int ticket;

if (StopLoss != 0 && StopLoss < StopLevel) StopLoss = StopLevel;
if (TakeProfit != 0 && TakeProfit < StopLevel) TakeProfit = StopLevel;

if(StopLoss != 0) SL = Ask-StopLoss*Point;
else SL = 00;
if(TakeProfit != 0) TP = Ask+TakeProfit*Point;
else TP = 0.0;

ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, 3, SL, TP, EA_Name, MagicNumber, 0, Green);

if(ticket == -1)
Print("OrderSend failed with error #", GetLastError());

return(ticket);
}

int Sell_Order()
{
double SL,TP;
int ticket;

if (StopLoss != 0 && StopLoss < StopLevel) StopLoss = StopLevel;
if (TakeProfit != 0 && TakeProfit < StopLevel) TakeProfit = StopLevel;

if(StopLoss != 0) SL = Bid+StopLoss*Point;
else SL = 0.0;
if(TakeProfit != 0) TP = Bid-TakeProfit*Point;
else TP = 0.0;

ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, 3, SL, TP, EA_Name, MagicNumber, 0, Red);

if(ticket == -1)
Print("OrderSend failed with error #", GetLastError());

return(ticket);
}

bool TradesAllowed()
{
if(Hour() >= StartHour && Hour() < EndHour)
return(true);

return(false);
}

void GetStatus()
{
double Fast_MA_Now = iMA(NULL, 0, FastMA, 0, MODE_EMA, PRICE_CLOSE, 1); //1 for last candle
double Slow_MA_Now = iMA(NULL, 0, SlowMA, 0, MODE_SMMA, PRICE_CLOSE, 1);
double Fast_MA_Prev = iMA(NULL, 0, FastMA, 0, MODE_EMA, PRICE_CLOSE, 2);
double Slow_MA_Prev = iMA(NULL, 0, SlowMA, 0, MODE_SMMA, PRICE_CLOSE, 2);

if(Fast_MA_Now > Slow_MA_Now && Fast_MA_Prev < Slow_MA_Prev /*&& Fast_MA_Now - Slow_MA_Now >= 0.00015*/)
status = BUYING;
else if(Fast_MA_Now < Slow_MA_Now && Fast_MA_Prev > Slow_MA_Prev /*&& Slow_MA_Now - Fast_MA_Now >= 0.00015*/)
status = SELLING;
else
status = 0;
}

[Deleted]  
atomi50:

I have checked the book and tried different stuff but no luck,this is a little advanced for me at this moment as I am just starting,the following code is the code of the EA I'm using.


The code relevant is at the beginning of the init start function,maybe you can see any mistakes I made cuz I'm looking at it every day and cant see anything.


#define BUYING 1
#define SELLING 2


//---- input parameters
extern string Desc_1 = "---- Global Settings ----";
extern int FastMA = 3;
extern int SlowMA = 50;

extern string Desc_2 = "---- Money Management ----";
extern double Lots = 0.01;
extern int StopLoss = 150;
extern int TakeProfit = 187;
extern int TrailingStop = 0;

extern string Desc_3 = "---- EA Settings ----";
extern int StartHour = -1;
extern int EndHour = 24;
extern int MagicNumber = 5623;
extern string EA_Name = "Atomi MA Cross";


int status;
int StopLevel;
int BarTraded = 0;

bool NewBar()
{
static datetime lastbar;
datetime curbar = Time[0];
if(lastbar!=curbar)
{
BarTraded = 0;
lastbar=curbar;
return (true);
}
else
{
return(false);
}
}

//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
StopLevel = MarketInfo(Symbol(), MODE_STOPLEVEL) + MarketInfo(Symbol(), MODE_SPREAD);

return(0);
}

//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
return(0);
}

//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
int i, total,i2,ticket,hstTotal=OrdersHistoryTotal();

// retrieving info from trade history

for(i2=0;i2<hstTotal;i2++)
{

if(OrderSelect(i2,SELECT_BY_POS,MODE_HISTORY)){
if (OrderSymbol() == Symbol()){
if ((TimeDayOfYear(OrderOpenTime()) == DayOfYear()) && (TimeYear(OrderOpenTime()) == Year())){
if (OrderProfit() <= 0)
{
Print(" Loss ");
}
else{
Print (" Profit " );
}
}
}
}
}


NewBar();
GetStatus();

if(TotalOpenTrades() == 0){
if(!TradesAllowed())
return(0);

//Check if we start with a long position
if(status == BUYING && !BarTraded){
Buy_Order();
BarTraded = 1;
Alert( " Buying " );
}

//Check if we start with a short position
if(status == SELLING && !BarTraded){
Sell_Order();
BarTraded = 1;
Alert( "Selling " );
}
}
else{
//Check open trades to potentially close them
total = OrdersTotal();
for(i=0;i<total;i++){
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)){
if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber){
if(OrderType() == OP_BUY){
if(status == SELLING){
ticket = OrderClose(OrderTicket(), OrderLots(), Bid, 3, Violet);

if(ticket == -1)
Print("OrderSend failed with error #", GetLastError());
else{
if(TradesAllowed() && !BarTraded){
Sell_Order();
BarTraded = 1;
}
}
}
else if(TrailingStop > 0 && OrderStopLoss() < Bid-TrailingStop*Point){
ticket = OrderModify(OrderTicket(), OrderOpenPrice(), Bid-TrailingStop*Point, OrderTakeProfit(), 0, Blue);

if(ticket == -1)
Print("OrderModify failed with error #", GetLastError());
}
}
else if(OrderType() == OP_SELL){
if(status == BUYING){
ticket = OrderClose(OrderTicket(), OrderLots(), Ask, 3, Violet);

if(ticket == -1)
Print("OrderSend failed with error #", GetLastError());
else{
if(TradesAllowed() && !BarTraded){
Buy_Order();
BarTraded = 1;
}
}
}
else if(TrailingStop > 0 && OrderStopLoss() > Ask+TrailingStop*Point){
ticket = OrderModify(OrderTicket(), OrderOpenPrice(), Ask+TrailingStop*Point, OrderTakeProfit(), 0, Blue);

if(ticket == -1)
Print("OrderModify failed with error #", GetLastError());
}
}
}
}
}
}

return(0);
}

int TotalOpenTrades()
{
int i;
int trades = 0;
int total = OrdersTotal()+ 1;

for(i=0;i<total;i++)
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
if(OrderType() <= OP_SELL && OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
trades++;

return(trades);
}

int Buy_Order()
{
double SL,TP;
int ticket;

if (StopLoss != 0 && StopLoss < StopLevel) StopLoss = StopLevel;
if (TakeProfit != 0 && TakeProfit < StopLevel) TakeProfit = StopLevel;

if(StopLoss != 0) SL = Ask-StopLoss*Point;
else SL = 00;
if(TakeProfit != 0) TP = Ask+TakeProfit*Point;
else TP = 0.0;

ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, 3, SL, TP, EA_Name, MagicNumber, 0, Green);

if(ticket == -1)
Print("OrderSend failed with error #", GetLastError());

return(ticket);
}

int Sell_Order()
{
double SL,TP;
int ticket;

if (StopLoss != 0 && StopLoss < StopLevel) StopLoss = StopLevel;
if (TakeProfit != 0 && TakeProfit < StopLevel) TakeProfit = StopLevel;

if(StopLoss != 0) SL = Bid+StopLoss*Point;
else SL = 0.0;
if(TakeProfit != 0) TP = Bid-TakeProfit*Point;
else TP = 0.0;

ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, 3, SL, TP, EA_Name, MagicNumber, 0, Red);

if(ticket == -1)
Print("OrderSend failed with error #", GetLastError());

return(ticket);
}

bool TradesAllowed()
{
if(Hour() >= StartHour && Hour() < EndHour)
return(true);

return(false);
}

void GetStatus()
{
double Fast_MA_Now = iMA(NULL, 0, FastMA, 0, MODE_EMA, PRICE_CLOSE, 1); //1 for last candle
double Slow_MA_Now = iMA(NULL, 0, SlowMA, 0, MODE_SMMA, PRICE_CLOSE, 1);
double Fast_MA_Prev = iMA(NULL, 0, FastMA, 0, MODE_EMA, PRICE_CLOSE, 2);
double Slow_MA_Prev = iMA(NULL, 0, SlowMA, 0, MODE_SMMA, PRICE_CLOSE, 2);

if(Fast_MA_Now > Slow_MA_Now && Fast_MA_Prev < Slow_MA_Prev /*&& Fast_MA_Now - Slow_MA_Now >= 0.00015*/)
status = BUYING;
else if(Fast_MA_Now < Slow_MA_Now && Fast_MA_Prev > Slow_MA_Prev /*&& Slow_MA_Now - Fast_MA_Now >= 0.00015*/)
status = SELLING;
else
status = 0;
}

I'll look at it this evening.

Meanwhile, put the Print() statement I suggested directly before the if(OrderSymbol... statement

And - are you sure that there is an order in your history which was opened in the current day?

And I'm assuming you are running this against a demo account? If you're just running in the tester, please try it against a demo account.

[Deleted]  
cloudbreaker:

I'll look at it this evening.

Meanwhile, put the Print() statement I suggested directly before the if(OrderSymbol... statement

And - are you sure that there is an order in your history which was opened in the current day?

And I'm assuming you are running this against a demo account? If you're just running in the tester, please try it against a demo account.

K man I added the print statement


not sure if I understood right I wrote it like this:


if(OrderSelect(i2,SELECT_BY_POS,MODE_HISTORY)){
Print(" This is " + Symbol());

if (OrderSymbol() == Symbol())


and I get statement like:


GBPUSD,M5: this is GBPUSD ...


and like that for every currency

Is that what you meant or?


-yes I am sure there is an order,otherwise I don't get statements in expert journal,it only Prints if I have today's closed orders in history,like it's suppose to do.

-yep I used it in demo and live account

[Deleted]  
atomi50:

K man I added the print statement


not sure if I understood right I wrote it like this:


if(OrderSelect(i2,SELECT_BY_POS,MODE_HISTORY)){
Print(" This is " + Symbol());

if (OrderSymbol() == Symbol())


and I get statement like:


GBPUSD,M5: this is GBPUSD ...


and like that for every currency

Is that what you meant or?


-yes I am sure there is an order,otherwise I don't get statements in expert journal,it only Prints if I have today's closed orders in history,like it's suppose to do.

-yep I used it in demo and live account

You stated that the if statement which compares the current symbol with the symbol of the order in order history didn't seem to be working.

So I asked you to put the following TWO print statements in your code.

Print("Symbol = ",Symbol());

Print("OrderSymbol = ",OrderSymbol());


Do you get what I'm trying to walk you through here? It seems not!

if a = b then do xyz.

xyz is not happening.

so if a and b aren't equal, let's have a look and see what each of a and b actually are.

[Deleted]  
cloudbreaker:

You stated that the if statement which compares the current symbol with the symbol of the order in order history didn't seem to be working.

So I asked you to put the following TWO print statements in your code.

Print("Symbol = ",Symbol());

Print("OrderSymbol = ",OrderSymbol());


Do you get what I'm trying to walk you through here? It seems not!

if a = b then do xyz.

xyz is not happening.

so if a and b aren't equal, let's have a look and see what each of a and b actually are.

I perfectly understand what you are trying to explain CB and the logic,or at least I think I understand . But I am confused because of the statements I am getting when EA is attached to 4 charts at once,that is what I am trying to explain the whole time.


when EA is attached to just one chart ( EUR/USD ),it's fine,EA checks OrderSymbol()==Symbol() start the next block,returns history values ( Profits and losses ),and prints the statements in experts tab.


When attached to 2 or more charts then it returns the history values of those currencies too,it all works great no errors there and I never said there are any errors I just don't know how to write/specify EA to check only one currency,like EA that is trading EURUSD to checks only losses and profits for EURUSD without interfering with other orders in history. Can I write it in a way to make 4 different EA's?


EA1 to check only EUR/USD and returns history,then if (order <=0 ) **do stuff** else **do stuff**

EA2 -||- only GBP/USD -||-

..... and so on



Like I said I am just starting to understand programming since I never did it in my life but I think ( please correct me if I'm wrong ) that in order to do that on 1 EA I need to specify currency in OrderSymbol and Symbol to tell the EA which history I want, because if I do not specify it the EA return values for every chart where attached.

[Deleted]  

Hey I added the following code:


if (OrderSymbol() == "GBPUSD" && Symbol() == "GBPUSD")


now I changed the name of the curreices for each of the 4 EA's, EA's have the same code I just change this part.


Can I use it this way?

[Deleted]  
atomi50:

Hey I added the following code:


if (OrderSymbol() == "GBPUSD" && Symbol() == "GBPUSD")


now I changed the name of the curreices for each of the 4 EA's, EA's have the same code I just change this part.


Can I use it this way?

Seems you are resorting to guess work now.


An EA can only be attached to one chart. It cant be attached to more than one chart.

An instance of an EA can be attached to a chart. And another instance of the same EA can be attached to another chart.

In this case you need to code this 'multi-purpose' EA to not care about what currency pair its dealing with.

To do this you use Symbol() rather than hard coding the pair name.


Your history file spans all trades made within a single account.

Because a number of charts may be open, each with an EA attached, and because you may also be opening orders manually, there will possibly be orders in the one history file for multiple currency pairs.


In order to check (within this EA that doesn't care what chart its attached to) whether a particular order (which you have selected with OrderSelect() is relevant to the chart that the EA has been attached to you will use the following conditional statement:

if (OrderSymbol() == Symbol())


That's it.

[Deleted]  
cloudbreaker:

Seems you are resorting to guess work now.


An EA can only be attached to one chart. It cant be attached to more than one chart.

An instance of an EA can be attached to a chart. And another instance of the same EA can be attached to another chart.

In this case you need to code this 'multi-purpose' EA to not care about what currency pair its dealing with.

To do this you use Symbol() rather than hard coding the pair name.


Your history file spans all trades made within a single account.

Because a number of charts may be open, each with an EA attached, and because you may also be opening orders manually, there will possibly be orders in the one history file for multiple currency pairs.


In order to check (within this EA that doesn't care what chart its attached to) whether a particular order (which you have selected with OrderSelect() is relevant to the chart that the EA has been attached to you will use the following conditional statement:

if (OrderSymbol() == Symbol())


That's it.

no guess work, but learning. Again,I am not a programmer,but one day I will get better at it



Can you tell me how would I count the losses in history for a selected currency pair?

if I have 3 losses for one pair and I want to sum them,how would I do that?