# 请教，如何根据上单刚刚平仓单子的盈亏情况来下单？

209
2011.03.23 19:08

//aa函数对吗？我的意思是，先下多单，等平仓后返回这单的盈亏。

int aa(double bb)
{int d=0;
if(d==0)
{
OrderSelect(d,SELECT_BY_POS,MODE_HISTORY);
if(OrderSymbol()==Symbol() && OrderComment()=="duodan" && OrderType()==OP_BUY ) bb=OrderProfit();
}
return;
}

int start()
{
double t;
int ticket2,ticket3, total;
if(Bars<100)
{
Print("bars less than 100");
return(0);
}
total=OrdersTotal();
if(total<1)
{
aa(t);
if(t>0)
{
if(ticket2>0)
{
}
else Print("Error opening BUY order : ",GetLastError());

}else
{ticket3=OrderSend(Symbol(),OP_SELL,Lots2,Bid,3,Bid+StopLoss2*Point,Bid-TakeProfit2*Point,"kongdan",16888,0,Red);
}
}

//----
return(0);
}

166
2011.03.24 12:52
```switch(lastOrder())
{
case 11:  //最后一单是亏损空单
{
//.............
}
break;

case 12:  //最后一单是赢利空单
{
//............
}
break;

case 21:  //最后一单是亏损多单
{
//............
}
break;

case 22:  //最后一单是赢利多单
{
//...........
}
break;
}

//-----------------------------------------------------+
int lastOrder()
{
double takeProfit = 0;
int    orderType  = 0;
int    _result    = 0;

int lastNum = OrdersHistoryTotal() - 1;

OrderSelect(lastNum, SELECT_BY_POS, MODE_HISTORY);
orderType = OrderType();
takeProfit = OrderTakeProfit();

if(orderType == OP_SELL)
{
if(takeProfit > 0)
_result = 12;
if(takeProfit < 0)
_result = 11;
}

{
if(takeProfit > 0)
_result = 22;
if(takeProfit < 0)
_result = 21;
}

return(_result);
}```
209
2011.03.24 17:50
dmgy:

？？？？？？？？？？

166
2011.03.25 08:41

```switch(lastOrder())
{
case 0:   //前面没有平仓单
{
//.............
}
break;

case 11:  //最后一单是亏损空单
{
//.............
}
break;

case 12:  //最后一单是赢利空单
{
//............
}
break;

case 21:  //最后一单是亏损多单
{
//............
}
break;

case 22:  //最后一单是赢利多单
{
//...........
}
break;
}```

```int lastOrder()
{
double takeProfit = 0;
int    orderType  = 0;
int    _result    = 0;

datetime time1 = 0;
datetime time2 = 0;

int lastNum = OrdersHistoryTotal() - 1;

for(int i = lastNum; i >= 0; i--)
{
OrderSelect(i, SELECT_BY_POS, MODE_HISTORY);
time2 = OrderCloseTime();
if(time2 > time1)
{
time1 = time2;
orderType = OrderType();
takeProfit = OrderTakeProfit();
}
}

if(orderType == OP_SELL)
{
if(takeProfit > 0)
_result = 12;
if(takeProfit < 0)
_result = 11;
}

{
if(takeProfit > 0)
_result = 22;
if(takeProfit < 0)
_result = 21;
}

return(_result);
}```

209
2011.03.25 11:36
dmgy:

1

/