//+------------------------------------------------------------------+
//| スクリプトプログラム開始関数 |
//+------------------------------------------------------------------+
void OnStart()
{
//--- すべての口座ポジションのループ
int total=PositionsTotal();
for(int i=0; i<total; i++)
{
//--- 自動的にポジションを選択して、そのプロパティにアクセスし、次のポジションのチケットを取得する
ulong ticket=PositionGetTicket(i);
if(ticket==0)
continue;
//--- ポジションタイプを取得し、ポジションの実数プロパティ一覧のヘッダーを表示する
string type=(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE ? "Buy" : "Sell");
PrintFormat("Double properties of an open position %s #%I64u:", type, ticket);
//--- 選択した取引のすべての実数プロパティをヘッダーの下に表示する
PositionPropertiesDoublePrint(15);
}
/*
結果:
Double properties of an open position Buy #2807075208:
Volume: 1.00
Price open: 1.10516
StopLoss: 0.00000
TakeProfit: 0.00000
Price current: 1.10518
Swap: 0.00
Profit: 2.00 USD
*/
}
//+------------------------------------------------------------------+
//| 操作ログに選択したポジションの実数プロパティを表示する |
//+------------------------------------------------------------------+
void PositionPropertiesDoublePrint(const uint header_width=0)
{
uint w=0;
string header="";
double value=0;
//--- 口座の通貨、ポジションの銘柄、およびその銘柄の小数点以下の桁数を取得する
string currency=AccountInfoString(ACCOUNT_CURRENCY);
string symbol =PositionGetString(POSITION_SYMBOL);
int digits =(int)SymbolInfoInteger(symbol, SYMBOL_DIGITS);
//--- ヘッダーのテキストとヘッダーフィールドの幅を定義する
//--- ヘッダーの幅が関数に渡され、ゼロと等しい場合、幅はヘッダー行のサイズ+1になる
header="Volume:";
w=(header_width==0 ? header.Length()+1 : header_width);
//--- ポジションボリュームを取得し、指定されたヘッダー幅で操作ログに表示する
if(!PositionGetDouble(POSITION_VOLUME, value))
return;
PrintFormat("%-*s%-.2f", w, header, value);
//--- 操作ログにポジション価格を表示する
header="Price open:";
w=(header_width==0 ? header.Length()+1 : header_width);
if(!PositionGetDouble(POSITION_PRICE_OPEN, value))
return;
PrintFormat("%-*s%-.*f", w, header, digits, value);
//--- 操作ログにストップロス値を表示する
header="StopLoss:";
w=(header_width==0 ? header.Length()+1 : header_width);
if(!PositionGetDouble(POSITION_SL, value))
return;
PrintFormat("%-*s%-.*f", w, header, digits, value);
//--- 操作ログにテイクプロフィット値を表示する
header="TakeProfit:";
w=(header_width==0 ? header.Length()+1 : header_width);
if(!PositionGetDouble(POSITION_TP, value))
return;
PrintFormat("%-*s%-.*f", w, header, digits, value);
//--- 操作ログに現行価格を表示する
header="Price current:";
w=(header_width==0 ? header.Length()+1 : header_width);
if(!PositionGetDouble(POSITION_PRICE_CURRENT, value))
return;
PrintFormat("%-*s%-.*f", w, header, digits, value);
//--- 蓄積されたスワップ値を操作ログに表示する
header="Swap:";
w=(header_width==0 ? header.Length()+1 : header_width);
if(!PositionGetDouble(POSITION_SWAP, value))
return;
PrintFormat("%-*s%-.2f", w, header, value);
//--- 操作ログに現在の利益値を表示する
header="Profit:";
w=(header_width==0 ? header.Length()+1 : header_width);
if(!PositionGetDouble(POSITION_PROFIT, value))
return;
PrintFormat("%-*s%-.2f %s", w, header, value, currency);
}
|