ok,
void enterAskPosition(double lots)
{
if(0 > OrderSend(Symbol(),OP_SELL,lots,Bid,slippoint,stoploss,takeprofit,"My Order #kay",13603,0,Red))
{
writeLog("sell open failure");
}
}
//---write log to another file for convienience to debug
void writeLog(string s)
{
int handle = FileOpen("log.txt",FILE_READ | FILE_WRITE,';');
if( handle == -1)
{
Print("------------ XXXXXXXXXXXXXXXXX ----------------");
}
else
{
FileSeek(handle, 0, SEEK_END);
FileWrite(handle,s);
FileClose(handle);
}
}
//---close all the bid position include limit or stop order
void closeAllBidPosition()
{
writeLog("---- enter function close all bid position");
for(int i = OrdersTotal()-1;i >= 0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderType() == OP_BUY)
{
OrderClose(OrderTicket(),OrderLots(),Bid,0,White);
}
else if(OrderType() == OP_BUYLIMIT || OrderType() == OP_BUYSTOP)
{
OrderDelete(OrderTicket());
}
}
}
}
//---close all the ask position include limit or stop order
void closeAllAskPosition()
{
writeLog("---- enter function close all ask position");
for(int i = OrdersTotal()-1;i >= 0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderType() == OP_SELL)
{
OrderClose(OrderTicket(),OrderLots(),Ask,0,White);
}
else if(OrderType() == OP_SELLLIMIT || OrderType() == OP_SELLSTOP)
{
OrderDelete(OrderTicket());
}
}
}
}
void enterBidPosition(double lots)
{
if(0 > OrderSend(Symbol(),OP_BUY,lots,Ask,slippoint,stoploss,takeprofit,"My Order #kay",13602,0,Blue))
{
writeLog("buy open failure");
}
}
//---calculate the number of all the bid ticket
double calculate_totalCount_bid()
{
double tb = 0;
for(int i = OrdersTotal() - 1;i >= 0;i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() == Symbol())
{
if(OrderType() == OP_BUY)
{
tb++;
}
}
}
}
totalCount_bid = tb;
return (tb);
}
//---calculate the number of all the ask ticket
double calculate_totalCount_ask()
{
double ta = 0;
for(int i = OrdersTotal() - 1;i >= 0;i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() == Symbol())
{
if(OrderType() == OP_SELL)
{
ta++;
}
}
}
}
totalCount_ask = ta;
return (ta);
}
//---calculate the number of all position
double calculate_totalCount_pos()
{
double tp = 0;
for(int i = OrdersTotal() - 1;i >= 0;i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() == Symbol())
{
if(OrderType() == OP_SELL || OrderType() == OP_BUY)
{
tp++;
}
}
}
}
totalCount_pos = tp;
return (tp);
}
//---calculate all the cost price besides bid and ask
void calculate_cost_price()
{
if(!ifChangedOfTotalCount_pos())
{
return;
}
for(int i = OrdersTotal() - 1;i >= 0;i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() == Symbol())
{
if(OrderType() == OP_BUY)
{
Print("-------- hi hi");
totalLots_bid += OrderLots();
totalValue_bid += OrderLots() * OrderOpenPrice();
Print("-----------totalValue_bid = ",totalValue_bid);
}
else if(OrderType() == OP_SELL)
{
totalLots_ask += OrderLots();
totalValue_ask += OrderLots() * OrderOpenPrice();
Print("----------totalValue_ask = ",totalValue_ask);
}
}
}
}
if(totalLots_bid > 0)
{
costPrice_bid = totalValue_bid / totalLots_bid;
}
if(totalLots_ask > 0)
{
costPrice_ask = totalValue_ask / totalLots_ask;
}
if(costPrice_bid > 0 && costPrice_ask > 0)
{
Print("------------ costPrice_bid = ",costPrice_bid);
Print("------------ costPrice_ask = ",costPrice_ask);
}
}
//---check for whether there is a ask position
bool ifExistAskPosition()
{
bool flag = false;
for(int i = OrdersTotal();i > 0;i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() == Symbol())
{
if(OrderType() == OP_SELL)
{
flag = true;
}
}
}
}
return (flag);
}
//---check for whether there is a bid position
bool ifExistBidPosition()
{
bool flag = false;
for(int i = OrdersTotal();i > 0;i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() == Symbol())
{
if(OrderType() == OP_BUY)
{
flag = true;
}
}
}
}
return (flag);
}
//---calculate all the profit in the current symbol
double totalProfit()
{
double totalProfit = 0;
for(int i = OrdersTotal();i > 0;i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() == Symbol())
{
if(OrderType() == OP_BUY || OrderType() == OP_SELL)
{
totalProfit += OrderProfit();
}
}
}
}
return (totalProfit);
}
//---calculate all the bid position profit in the current Symbol
double totalBidProfit()
{
double totalBidProfit = 0;
for(int i = OrdersTotal();i > 0;i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() == Symbol())
{
if(OrderType() == OP_BUY)
{
totalBidProfit += OrderProfit();
}
}
}
}
return (totalBidProfit);
}
//---calculate all the ask position profit in the current symbol
double totalAskProfit()
{
double totalAskProfit = 0;
for(int i = OrdersTotal();i > 0;i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() == Symbol())
{
if(OrderType() == OP_SELL)
{
totalAskProfit += OrderProfit();
}
}
}
}
return (totalAskProfit);
}