double longpos,shortpos;
//#property show_confirm
void Wait()
{
while( IsTradeContextBusy() )
{
Sleep(5);
}
}
//+------------------------------------------------------------------+
//| script "close first market order if it is first in the list" |
//+------------------------------------------------------------------+
double NetPos()
{
int total = OrdersTotal();
static double current_Pos,BuyPos,SelPos;
current_Pos=0; BuyPos=0; SelPos=0;
for(int cnt=0; cnt<total; cnt++)
{
OrderSelect(cnt, SELECT_BY_POS);
if(OrderSymbol() == Symbol()&&OrderType()==OP_BUY)
BuyPos=BuyPos+OrderLots();
if(OrderSymbol() == Symbol()&&OrderType()==OP_SELL)
SelPos=SelPos+OrderLots();
}
current_Pos=BuyPos-SelPos;
longpos=BuyPos;
shortpos=SelPos;
return (current_Pos);
}
int start()
{
bool result;
double price;
int BuyTick,SellTick,cnt1,cnt2,total;
//----
double net = NetPos();
if( net > 0)
{
while(shortpos>0)
{
total = OrdersTotal();
for (cnt1=0;cnt1<total;cnt1++)
{ OrderSelect(cnt1, SELECT_BY_POS);
if (OrderSymbol()==Symbol()&&OrderType()==OP_BUY)
{
BuyTick=OrderTicket();
for (cnt2=0;cnt2<total;cnt2++)
{ OrderSelect(cnt2, SELECT_BY_POS);
if (OrderSymbol()==Symbol()&&OrderType()==OP_SELL)
{
SellTick=OrderTicket();
Wait();
RefreshRates();
OrderCloseBy(BuyTick,SellTick);
}
}
}
}
net = NetPos();
}
} else
if( net < 0 )
{
while(longpos>0)
{
total = OrdersTotal();
for (cnt1=0;cnt1<total;cnt1++)
{ OrderSelect(cnt1, SELECT_BY_POS);
if (OrderSymbol()==Symbol()&&OrderType()==OP_SELL)
{
SellTick=OrderTicket();
for (cnt2=0;cnt2<total;cnt2++)
{ OrderSelect(cnt2, SELECT_BY_POS);
if (OrderSymbol()==Symbol()&&OrderType()==OP_BUY)
{
BuyTick=OrderTicket();
Wait();
RefreshRates();
OrderCloseBy(SellTick,BuyTick);
}
}
}
}
net = NetPos();
}
} else
return(0);
}
從這個範例,你所要認知的是:
1、多單 10 口
2、建立 空單2口
3、此時 淨多單8口
4、採用 OrderCloseBy(SellTick,BuyTick); 去沖銷
反之,淨空單 8口
採用 OrderCloseBy(BuyTick,SellTick); |