int AdjustSlippage(string Currency,int Slippage_pips )//スリッページ調整
{
int Calculated_Slippage=0;
int Symbol_Digits=(int)MarketInfo(Currency,MODE_DIGITS);
if (Symbol_Digits==2 || Symbol_Digits==3)
{
Calculated_Slippage=Slippage_pips;
}
else if (Symbol_Digits==4 || Symbol_Digits==5)
{
Calculated_Slippage=Slippage_pips*10;
}
return(Calculated_Slippage);
}

int LongPosition()//ロングポジション数を取得
{
int buys=0;
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true && OrderSymbol()==Symbol() && OrderMagicNumber()==MAGIC)
{
if(OrderType()==OP_BUY) buys++;
}
}
return(buys);
}

int ShortPosition()//ショートポジション数を取得
{
int sells=0;
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true && OrderSymbol()==Symbol() && OrderMagicNumber()==MAGIC)
{
if(OrderType()==OP_SELL) sells++;
}
}
return(sells);
}

//+------------------------------------------------------------------+
// エントリ関連関数
//+------------------------------------------------------------------+

//ポジションエントリ関数
void OpenOrder(int EntryPosition)
{
int res;
bool Modified;
double SL;
double TP;
int SLP=AdjustSlippage(Symbol(),Slippage );

//ロットサイズ調整
Lots=BaseLots;//固定ロット

if( EntryPosition == 1 ) //買いの場合のエントリ条件
{
res=OrderSend(Symbol(),OP_BUY,Lots ,Ask,SLP,0,0,"sell trail",MAGIC,0,Red);
if (OrderSelect(res,SELECT_BY_TICKET)==true)
{
if (stoploss!=0) SL=OrderOpenPrice()-stoploss*AdjustPoint(Symbol());
if (takeprofit!=0) TP=OrderOpenPrice()+takeprofit*AdjustPoint(Symbol());
}
if(SL!=0 || TP!=0) Modified=OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP,0,Red);
}
else if(EntryPosition == -1 ) //---- 売りエントリ
{
res=OrderSend(Symbol(),OP_SELL,Lots,Bid,SLP,0,0,"sell trail",MAGIC,0,White);
if(OrderSelect(res,SELECT_BY_TICKET)==true)
{
if(stoploss!=0) SL=OrderOpenPrice()+stoploss*AdjustPoint(Symbol());
if(takeprofit!=0) TP=OrderOpenPrice()-takeprofit*AdjustPoint(Symbol());
}
if(SL!=0 || TP!=0) Modified=OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP,0,White);
}
return;
}