2012年1月28日 星期六

MarketPosition

The MarketPosition function in MC is very useful.
And I want to realize this function in MT4.

In MC, the method of using MarketPosition is
MP=marketposition;
CS=currentcontracts;
TotalMP=MP*CS;
TotalMP[1]=MP[1]*CS[1];

The results are ..-2,-1,0,1,2..
And notice that MP[1] is usually used.

In MT4, the situation is much complicate.
There are 7 situation in the market:
1.Buy Order
2.Sell Order
3.BuyLimit Order
4.SellLimit Order
5.BuyStop Order
6.SellStop Order
7.no Order

I want to simplify the situation to prevent my strategy hard to play.
Then they are defined as
aMP_Current=1,2,7
aMP_Limit=3,4,7
aMP_Stop=5,6,7


The Classic Code of Marketposition in MT4 is
void MarketPosition()
{
_OrderBuy=0;
_OrderSell=0;
_OrderCount=0;
_OrdersTotal=0;
_OrderTicket=0;
_OrderOpenPrice=0;
_OrderLots=0;
_OrderStopLoss=0;
_OrderTakeProfit=0;
for (i=0;i<=OrdersTotal();i++)
{
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if (OrderMagicNumber()==iMagicNumber)
{
//_OrdersTotal=_OrdersTotal+1;
if (OrderType()==OP_BUY) _OrderBuy=_OrderBuy+1;
if (OrderType()==OP_SELL) _OrderSell=OrderSell+1;
_OrderTicket=OrderTicket();
_OrderOpenPrice=OrderOpenPrice();
_OrderLots=OrderLots();
_OrderStopLoss=OrderStopLoss();
_OrderTakeProfit=OrderTakeProfit();
}
}
}

This will record the info of the latest order.
But I think the loading of this function is also heavy.
Actually the detail info of order is not always used.
So there are three function created.


double aMP_Current()
{
double aMP_Orders=0
for (i=0;i&lt;=OrdersTotal();i++)
{
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if (OrderMagicNumber()==iMagicNumber)
{
if (OrderType()==OP_BUY) aMP_Orders=aMP_Orders+OrderLots();
if (OrderType()==OP_SELL) aMP_Orders=aMP_Orders-OrderLots();
}
}
return(aMP_orders);
}

double aMPL_Current()
{
double aMP_Orders=0
for (i=0;i&lt;=OrdersTotal();i++)
{
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if (OrderMagicNumber()==iMagicNumber)
{
if (OrderType()==OP_BUYLIMIT) aMPL_Orders=aMPL_Orders+OrderLots();
if (OrderType()==OP_SELLLIMIT) aMPL_Orders=aMPL_Orders-OrderLots();
}
}
return(aMPL_orders);
}

double aMPS_Current()
{
double aMPS_Orders=0
for (i=0;i&lt;=OrdersTotal();i++)
{
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if (OrderMagicNumber()==iMagicNumber)
{
if (OrderType()==OP_BUYSTOP) aMPS_Orders=aMPS_Orders+OrderLots();
if (OrderType()==OP_SELLSTOP) aMPS_Orders=aMPS_Orders-OrderLots();
}
}
return(aMPS_orders);
}


Actually I'm not sure if these function are easily used.
The using experience may be post in the future.

But for a easily strategy,
the most important thing is not to put too many order on market.
This will make your think unclear and make CPU heavy load.