Recall result of last trade

 

is it possible to recall if the last trade hit TP or SL and make a trade according to that? thanks.

 

Also, you may make use of the practical but undocumented "fact" (well, my belief, really) that the orders history list is sorted in close-time order. That is, scan from high index towards low (0), and break the loop as soon as discovery is made. This makes some difference to performance when there are many closed trades.

As an aside, I think it's good habit to always scan the orders lists from high index to low, since it's typically in search of a most recent something, or within a recent history, or that some trades should be closed (which then avoids changing the remaining list, in a high-to-low scan sequence).

 
ralph.ronnquist:
Also, you may make use of the practical but undocumented "fact" (well, my belief, really) that the orders history list is sorted in close-time order. That is, scan from high index towards low (0), and break the loop as soon as discovery is made. This makes some difference to performance when there are many closed trades.

This is true on BT, but in live, one may sort the list by any column of the History tab, so the last index doesn't index anymore the last closed order.

 

ive changed the code according to the other thread i made, and all is well so thank you for that - btw very clean code i like it ive compiled and no problems, however, no trades are made either heres my code

//+----------------------------START---------------------------------+

extern int TakeProfit = 90;

extern int StopLoss = 30;

int start()

{

double ma1, stop, profit, slippage;

ma1 = iMA(NULL,0,21,0,MODE_EMA,PRICE_CLOSE,0);

slippage = 1;

stop = StopLoss*Point;

profit = TakeProfit*Point;

if(OrdersTotal()<1) //max number of orders at a time

{

/*BUY*/ if(Close[1]<bbL && Open[1]<ma1 && Close[1]<ma1)

OrderSend(Symbol(),OP_BUY,lots(),Ask,slippage,Ask-stop,Ask+profit,0);

/*SELL*/if(Close[1]>bbU && Open[1]>ma1 && Close[1]>ma1)

OrderSend(Symbol(),OP_SELL,lots(),Bid,slippage,Bid+stop,Bid-profit,0);

}

}

int lots()

{

int i, lots,HstTotal=OrdersHistoryTotal();

datetime LastClose = 0;

int Magic = 2005;

for(i = HstTotal-1; i >= 0; i --)

{

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

if(OrderSymbol() != Symbol()) continue;

if(OrderMagicNumber() != Magic) continue;

if(OrderCloseTime() <= LastClose) continue;

LastClose = OrderCloseTime();

lots = 0.1;

if(OrderClosePrice() == OrderStopLoss()) lots = 0.2;

if(OrderClosePrice() == OrderTakeProfit()) lots = 0.1;

}

return(lots);

}

//+-----------------------------END----------------------------------+

 
trevman:
is it possible to recall if the last trade hit TP or SL and make a trade according to that? thanks.

You can try something like this (maybe it doesn't work with slippage > 0):

int CheckLastClosed()

{

int i, Res, HstTotal=OrdersHistoryTotal();

datetime LastClose = 0;

for(i = HstTotal-1; i >= 0; i --)

{

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

if(OrderSymbol() != Symbol()) continue;

if(OrderMagicNumber() != Magic) continue;

if(OrderCloseTime() <= LastClose) continue;

LastClose = OrderCloseTime();

Res = 0;

if(OrderClosePrice() == OrderStopLoss()) Res = 1;

if(OrderClosePrice() == OrderTakeProfit()) Res = 2;

}

return(Res);

}

Another way is to look for "SL" or "TP" in OrderComment(); but this doesn't seem more reliable.

 
trevman:
ive changed the code according to the other thread i made, and all is well so thank you for that - btw very clean code i like it ive compiled and no problems, however, no trades are made either heres my code

//+----------------------------START---------------------------------+

extern int TakeProfit = 90;

extern int StopLoss = 30;

int start()

{

double ma1, stop, profit, slippage;

ma1 = iMA(NULL,0,21,0,MODE_EMA,PRICE_CLOSE,0);

slippage = 1;

stop = StopLoss*Point;

profit = TakeProfit*Point;

if(OrdersTotal()<1) //max number of orders at a time

{

/*BUY*/ if(Close[1]<bbL && Open[1]<ma1 && Close[1]<ma1)

OrderSend(Symbol(),OP_BUY,lots(),Ask,slippage,Ask-stop,Ask+profit,0);

/*SELL*/if(Close[1]>bbU && Open[1]>ma1 && Close[1]>ma1)

OrderSend(Symbol(),OP_SELL,lots(),Bid,slippage,Bid+stop,Bid-profit,0);

}

}

int lots()

{

int i, lots,HstTotal=OrdersHistoryTotal();

datetime LastClose = 0;

int Magic = 2005;

for(i = HstTotal-1; i >= 0; i --)

{

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

if(OrderSymbol() != Symbol()) continue;

if(OrderMagicNumber() != Magic) continue;

if(OrderCloseTime() <= LastClose) continue;

LastClose = OrderCloseTime();

lots = 0.1;

if(OrderClosePrice() == OrderStopLoss()) lots = 0.2;

if(OrderClosePrice() == OrderTakeProfit()) lots = 0.1;

}

return(lots);

}

//+-----------------------------END----------------------------------+

Problems I see :

- if there is no one order in the History, the func Lots() returns 0 as order size.

- OrderSend() has more arguments, how did you compile that ? more precisely, the magic number must be set as a parameter, ortherwhize the Lots() func cannot recognize those orders.

- Where are you computing bbL and bbU ? if this is done in the init() func, it's the wrong place because init() is called only once, not at every new bar.

 

my mistake bbL and bbU shouldn't be there, should be

/*BUY*/ if(Open[1]<ma1 && Close[1]<ma1)

OrderSend(Symbol(),OP_BUY,lots(),Ask,slippage,Ask-stop,Ask+profit,0);

/*SELL*/if(Open[1]>ma1 && Close[1]>ma1)

OrderSend(Symbol(),OP_SELL,lots(),Bid,slippage,Bid+stop,Bid-profit,0);

ive never dealt much with magic number, should lots be set at start of the lots() function? so it will be 0.1 until a order is made and then it will be based on the result of that trade

 

ok ive made them changes but i dont get any trades still, though no errors either which is good i guess, heres my new code

//+----------------------------START---------------------------------+

extern int TakeProfit = 90;

extern int StopLoss = 30;

int start()

{

double ma1, stop, profit, slippage;

ma1 = iMA(NULL,0,21,0,MODE_EMA,PRICE_CLOSE,0);

slippage = 1;

stop = StopLoss*Point;

profit = TakeProfit*Point;

if(OrdersTotal()<1) //max number of orders at a time

{

/*BUY*/ if(Close[1]<bbL && Open[1]<ma1 && Close[1]<ma1)

OrderSend(Symbol(),OP_BUY,lots(),Ask,slippage,Ask-stop,Ask+profit,"My EA has open this order",0,Blue);

/*SELL*/if(Close[1]>bbU && Open[1]>ma1 && Close[1]>ma1)

OrderSend(Symbol(),OP_SELL,lots(),Bid,slippage,Bid+stop,Bid-profit,"My EA has open this order",0,Blue);

}

}

int lots()

{

int i, HstTotal=OrdersHistoryTotal();

int lots = 0.1;

datetime LastClose = 0;

int Magic = 2005;

for(i = HstTotal-1; i >= 0; i --)

{

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

if(OrderSymbol() != Symbol()) continue;

if(OrderMagicNumber() != Magic) continue;

if(OrderCloseTime() <= LastClose) continue;

LastClose = OrderCloseTime();

if(OrderClosePrice() == OrderStopLoss()) lots = 0.2;

if(OrderClosePrice() == OrderTakeProfit()) lots = 0.1;

}

return(lots);

}

//+-----------------------------END----------------------------------+

 
trevman:
ok ive made them changes but i dont get any trades still, though no errors either which is good i guess, heres my new code

//+----------------------------START---------------------------------+

extern int TakeProfit = 90;

extern int StopLoss = 30;

int start()

{

double ma1, stop, profit, slippage;

ma1 = iMA(NULL,0,21,0,MODE_EMA,PRICE_CLOSE,0);

slippage = 1;

stop = StopLoss*Point;

profit = TakeProfit*Point;

if(OrdersTotal()<1) //max number of orders at a time

{

/*BUY*/ if(Close[1]<bbL && Open[1]<ma1 && Close[1]<ma1)

OrderSend(Symbol(),OP_BUY,lots(),Ask,slippage,Ask-stop,Ask+profit,"My EA has open this order",0,Blue);

/*SELL*/if(Close[1]>bbU && Open[1]>ma1 && Close[1]>ma1)

OrderSend(Symbol(),OP_SELL,lots(),Bid,slippage,Bid+stop,Bid-profit,"My EA has open this order",0,Blue);

}

}

int lots()

{

int i, HstTotal=OrdersHistoryTotal();

int lots = 0.1;

datetime LastClose = 0;

int Magic = 2005;

for(i = HstTotal-1; i >= 0; i --)

{

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

if(OrderSymbol() != Symbol()) continue;

if(OrderMagicNumber() != Magic) continue;

if(OrderCloseTime() <= LastClose) continue;

LastClose = OrderCloseTime();

if(OrderClosePrice() == OrderStopLoss()) lots = 0.2;

if(OrderClosePrice() == OrderTakeProfit()) lots = 0.1;

}

return(lots);

}

//+-----------------------------END----------------------------------+

Please, look carefully at my previous answer : you have forget to put the Magic in the OrderSend() func, AND you have to put the statement "int Magic = 2005;" at the global scope because it's used by the start() func AND the Lots() func. Global scope means outside of any function, so the value remains accessible by any function.

Another mistake is to declare the return value of the lots() func as int.

First, OrderSend() needs a double value, and second, if you want to return 0.1, it's clearly a double value...

Now, "My EA has open this order" as comment was a sample, if you do not use it, just put "" as comment argument for the OrderSend().

 
trevman:
my mistake bbL and bbU shouldn't be there, should be

/*BUY*/ if(Open[1]<ma1 && Close[1]<ma1)

OrderSend(Symbol(),OP_BUY,lots(),Ask,slippage,Ask-stop,Ask+profit,0);

/*SELL*/if(Open[1]>ma1 && Close[1]>ma1)

OrderSend(Symbol(),OP_SELL,lots(),Bid,slippage,Bid+stop,Bid-profit,0);

ive never dealt much with magic number, should lots be set at start of the lots() function? so it will be 0.1 until a order is made and then it will be based on the result of that trade

The magic number is just a way that the EA can use to sign its own orders : so you can mix multiple EAs on the same pair, and each one works only with its own orders.

The use of OrderSend() I know is :

int OrderSend( string symbol, int cmd, double volume, double price, int slippage, double stoploss, double takeprofit, string comment=NULL, int magic=0, datetime expiration=0, color arrow_color=CLR_NONE)

So, put "int Magic = 2005;" at the global level of the EA(not at the Lots() level()) and use OrderSend() like this :

OrderSend(Symbol(),OP_BUY,lots(),Ask,slippage,Ask-stop,Ask+profit,"My EA has open this order",Magic,0,Blue);

About the lots size, just initialize Lots = 0.1 at the beginning of the Lots() func, not inside the loop, because if no a single loop is executed, Lots will not be initialized.

 

thank you very much!.... my apologies for my newbie mistakes haha but its working now

Reason: