I have an incomplete script which will close all orders with a certain magic number at a certain profit target. I can not get the script to work and seems to be missing some basic formatting/function calls. Can anyone take a look, I'm sure it just needs a few things added (int init() etc etc).

Thanks

...

extern bool AutoClosePositions = true;

...

double MaxTargetProfit;

MaxTargetProfit = TotalOrders()*10; //This is up to your profit target or formula!

...

...

if(AutoClosePositions && TotalOrders()!= 0) {

if(Profit()>=MaxTargetProfit){

int total = OrdersTotal();

for(int i=total-1;i>=0;i--){

OrderSelect(i, SELECT_BY_POS);

int type = OrderType();

bool result = false;

if(OrderMagicNumber()==MAGIC){

switch(type){

case OP_BUY : result = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),5,Red);

break;

case OP_SELL : result = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),5, Blue);

} //switch

if(result == false){

Print("Order ", OrderTicket(), " failed to close. Error:", GetLastError() );

}//if(result == false)

}//if(OrderMagicNumber()==MAGIC){

}//for(int i= ...

}//if(AccountProfit()>=MaxTargetProfit)

}//if(AutoClose...

int TotalOrders() {

int cnt=0;

for (int i=0; i<OrdersTotal(); i++) {

if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {

if (OrderMagicNumber()==MAGIC && (OrderType()==OP_BUY || OrderType()==OP_SELL)) {

cnt++;

}

}

}

return(cnt);

}

double Profit(){

double Prof=0;

for (int i=0; i<OrdersTotal(); i++) {

if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {

if (OrderMagicNumber()==MAGIC) {

Prof = Prof + OrderProfit();

}

}

}

return(Prof);

}

Thanks

...

extern bool AutoClosePositions = true;

...

double MaxTargetProfit;

MaxTargetProfit = TotalOrders()*10; //This is up to your profit target or formula!

...

...

if(AutoClosePositions && TotalOrders()!= 0) {

if(Profit()>=MaxTargetProfit){

int total = OrdersTotal();

for(int i=total-1;i>=0;i--){

OrderSelect(i, SELECT_BY_POS);

int type = OrderType();

bool result = false;

if(OrderMagicNumber()==MAGIC){

switch(type){

case OP_BUY : result = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),5,Red);

break;

case OP_SELL : result = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),5, Blue);

} //switch

if(result == false){

Print("Order ", OrderTicket(), " failed to close. Error:", GetLastError() );

}//if(result == false)

}//if(OrderMagicNumber()==MAGIC){

}//for(int i= ...

}//if(AccountProfit()>=MaxTargetProfit)

}//if(AutoClose...

int TotalOrders() {

int cnt=0;

for (int i=0; i<OrdersTotal(); i++) {

if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {

if (OrderMagicNumber()==MAGIC && (OrderType()==OP_BUY || OrderType()==OP_SELL)) {

cnt++;

}

}

}

return(cnt);

}

double Profit(){

double Prof=0;

for (int i=0; i<OrdersTotal(); i++) {

if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {

if (OrderMagicNumber()==MAGIC) {

Prof = Prof + OrderProfit();

}

}

}

return(Prof);

}