How to close transactions before StopOut

 

Hello,

I have EA that makes a lot of transactions and it's profitable, I just need to close it just before account stopout or at proper moment.

The charts of EA results look always the same:

Method I use at this moment (with average results, because trades get closed sometimes too fast and no more trades are being done):

int start(){

if(AccountMargin()>600 && AccountMargin()<highestequity-equitydrawdownamount) {

stop=1;

while(OrdersTotal()>0)

{

OrderSelect(0,SELECT_BY_POS);

if(OrderType()==OP_BUY) OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),999,Blue);

if(OrderType()==OP_SELL) OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),999,Red);

if(OrderType()==OP_BUYSTOP||OrderType()==OP_SELLSTOP||OrderType()==OP_BUYLIMIT||OrderType()==OP_SELLLIMIT) OrderDelete(OrderTicket());

}}

if (AccountMargin() > highestequity)

{highestequity = AccountMargin();}

...[/CODE]

AccountMargin() > 600 because I want to allow it to make any trades

equitydrawdownamount = amount of equity that lowers since previous tick, I've tried from 100 to 800 and it catches some transactions properly, some not.

Just before StopOut MT4 Strategy Tester journal reports this:

Tester: not enough money for sell 0.1 EURUSD at 1.13286

2015.02.16 20:37 Tester: PrevBalance: 1359.08, PrevPL: 121.68, PrevEquity 1480.76, PrevMargin: 1468.74, NewMargin: 1509, FreeMargin: -28.32

Tester: not enough money for sell 0.1 EURUSD at 1.13303

2015.02.16 20:41 Tester: PrevBalance: 1359.08, PrevPL: 4.50, PrevEquity 1363.58, PrevMargin: 1509.52, NewMargin: 1550, FreeMargin: -186.51

Tester: not enough money for sell 0.1 EURUSD at 1.13338

2015.02.16 20:44 Tester: PrevBalance: 1359.08, PrevPL: -228.60, PrevEquity 1130.48, PrevMargin: 1509.52, NewMargin: 1551, FreeMargin: -420.09

So I think that ErrorCode 134 (not enough money in account) should trigger closing all open trades.

So I've tried this code:

[CODE]

int start(){

if(AccountBalance()>equitystop && GetLastError()==134) {

stop=1;

while(OrdersTotal()>0)

{

OrderSelect(0,SELECT_BY_POS);

if(OrderType()==OP_BUY) OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),999,Blue);

if(OrderType()==OP_SELL) OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),999,Red);

if(OrderType()==OP_BUYSTOP||OrderType()==OP_SELLSTOP||OrderType()==OP_BUYLIMIT||OrderType()==OP_SELLLIMIT) OrderDelete(OrderTicket());

}}

Does anybody have any idea to close trades faster (all at once) or just before StopOut?

Thanks.

Files:
action1.jpg  77 kb
action2.jpg  76 kb
action3.jpg  79 kb
action4.jpg  79 kb
action5.jpg  75 kb
 
Maine:
Hello,

I have EA that makes a lot of transactions and it's profitable, I just need to close it just before account stopout or at proper moment.

The charts of EA results look always the same:

Method I use at this moment (with average results, because trades get closed sometimes too fast and no more trades are being done):

int start(){

if(AccountMargin()>600 && AccountMargin()<highestequity-equitydrawdownamount) {

stop=1;

while(OrdersTotal()>0)

{

OrderSelect(0,SELECT_BY_POS);

if(OrderType()==OP_BUY) OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),999,Blue);

if(OrderType()==OP_SELL) OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),999,Red);

if(OrderType()==OP_BUYSTOP||OrderType()==OP_SELLSTOP||OrderType()==OP_BUYLIMIT||OrderType()==OP_SELLLIMIT) OrderDelete(OrderTicket());

}}

if (AccountMargin() > highestequity)

{highestequity = AccountMargin();}

...[/CODE]

AccountMargin() > 600 because I want to allow it to make any trades

equitydrawdownamount = amount of equity that lowers since previous tick, I've tried from 100 to 800 and it catches some transactions properly, some not.

Just before StopOut MT4 Strategy Tester journal reports this:

Tester: not enough money for sell 0.1 EURUSD at 1.13286

2015.02.16 20:37 Tester: PrevBalance: 1359.08, PrevPL: 121.68, PrevEquity 1480.76, PrevMargin: 1468.74, NewMargin: 1509, FreeMargin: -28.32

Tester: not enough money for sell 0.1 EURUSD at 1.13303

2015.02.16 20:41 Tester: PrevBalance: 1359.08, PrevPL: 4.50, PrevEquity 1363.58, PrevMargin: 1509.52, NewMargin: 1550, FreeMargin: -186.51

Tester: not enough money for sell 0.1 EURUSD at 1.13338

2015.02.16 20:44 Tester: PrevBalance: 1359.08, PrevPL: -228.60, PrevEquity 1130.48, PrevMargin: 1509.52, NewMargin: 1551, FreeMargin: -420.09

So I think that ErrorCode 134 (not enough money in account) should trigger closing all open trades.

So I've tried this code:

[CODE]

int start(){

if(AccountBalance()>equitystop && GetLastError()==134) {

stop=1;

while(OrdersTotal()>0)

{

OrderSelect(0,SELECT_BY_POS);

if(OrderType()==OP_BUY) OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),999,Blue);

if(OrderType()==OP_SELL) OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),999,Red);

if(OrderType()==OP_BUYSTOP||OrderType()==OP_SELLSTOP||OrderType()==OP_BUYLIMIT||OrderType()==OP_SELLLIMIT) OrderDelete(OrderTicket());

}}

Does anybody have any idea to close trades faster (all at once) or just before StopOut?

Thanks.

Use AccountInfoDouble(ACCOUNT_MARGIN_SO_SO); to get the stop out level ( amount of money ), and when your equity is getting close to it, close all opened positions

 
Maine:
Hello,

I have EA that makes a lot of transactions and it's profitable, I just need to close it just before account stopout or at proper moment.

The charts of EA results look always the same:

Method I use at this moment (with average results, because trades get closed sometimes too fast and no more trades are being done):

int start(){

if(AccountMargin()>600 && AccountMargin()<highestequity-equitydrawdownamount) {

stop=1;

while(OrdersTotal()>0)

{

OrderSelect(0,SELECT_BY_POS);

if(OrderType()==OP_BUY) OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),999,Blue);

if(OrderType()==OP_SELL) OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),999,Red);

if(OrderType()==OP_BUYSTOP||OrderType()==OP_SELLSTOP||OrderType()==OP_BUYLIMIT||OrderType()==OP_SELLLIMIT) OrderDelete(OrderTicket());

}}

if (AccountMargin() > highestequity)

{highestequity = AccountMargin();}

...[/CODE]

AccountMargin() > 600 because I want to allow it to make any trades

equitydrawdownamount = amount of equity that lowers since previous tick, I've tried from 100 to 800 and it catches some transactions properly, some not.

Just before StopOut MT4 Strategy Tester journal reports this:

Tester: not enough money for sell 0.1 EURUSD at 1.13286

2015.02.16 20:37 Tester: PrevBalance: 1359.08, PrevPL: 121.68, PrevEquity 1480.76, PrevMargin: 1468.74, NewMargin: 1509, FreeMargin: -28.32

Tester: not enough money for sell 0.1 EURUSD at 1.13303

2015.02.16 20:41 Tester: PrevBalance: 1359.08, PrevPL: 4.50, PrevEquity 1363.58, PrevMargin: 1509.52, NewMargin: 1550, FreeMargin: -186.51

Tester: not enough money for sell 0.1 EURUSD at 1.13338

2015.02.16 20:44 Tester: PrevBalance: 1359.08, PrevPL: -228.60, PrevEquity 1130.48, PrevMargin: 1509.52, NewMargin: 1551, FreeMargin: -420.09

So I think that ErrorCode 134 (not enough money in account) should trigger closing all open trades.

So I've tried this code:

[CODE]

int start(){

if(AccountBalance()>equitystop && GetLastError()==134) {

stop=1;

while(OrdersTotal()>0)

{

OrderSelect(0,SELECT_BY_POS);

if(OrderType()==OP_BUY) OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),999,Blue);

if(OrderType()==OP_SELL) OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),999,Red);

if(OrderType()==OP_BUYSTOP||OrderType()==OP_SELLSTOP||OrderType()==OP_BUYLIMIT||OrderType()==OP_SELLLIMIT) OrderDelete(OrderTicket());

}}

Does anybody have any idea to close trades faster (all at once) or just before StopOut?

Thanks.

Are you martingaling?

Reason: