//--- define
#define BALANCE_PROFIT_WITHDRAWAL 5 // テスターの口座から資金が引き出される残高利益の値
//--- 入力パラメータ
input double InpLots = 0.1; // ロット
input uint InpStopLoss = 50; // ポイント単位のストップロス
input uint InpTakeProfit = 150; // ポイント単位のテイクプロフィット
sinput ulong InpMagic = 123; // マジックナンバー
sinput ulong InpDeviation = 5; // 偏差
//--- グローバル変数
CTrade trade; // 取引クラスのインスタンス
CSymbolInfo symb; // 銘柄クラスのインスタンス
CAccountInfo account; // 取引口座クラスのインスタンス
...
double balance_op_sum; // 残高操作の合計額
uint balance_op_total; // 残高操作の数
//+------------------------------------------------------------------+
//| エキスパート初期化関数 |
//+------------------------------------------------------------------+
int OnInit()
{
...
//--- 初期バランス値を保存する
balance_prev=account.Balance();
balance_op_sum=0;
balance_op_total=0;
//--- 正常な初期化
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| エキスパートティック関数 |
//+------------------------------------------------------------------+
void OnTick()
{
//--- 現在の相場を更新する
if(!symb.RefreshRates())
return;
...
//--- 残高利益が現在の残高をBALANCE_PROFIT_WITHDRAWALマクロ置換で指定された値だけ超える場合、
//--- これらの資金を口座から引き出す必要があるTesterWithdrawal() 関数を呼び出します
//--- BALANCE_PROFIT_WITHDRAWALを超える残高利益を確認する
if(balance_prev!=account.Balance())
{
if(account.Balance()>balance_prev+BALANCE_PROFIT_WITHDRAWAL)
{
double profit=account.Balance()-balance_prev;
PrintFormat("The account balance has been increased by %.2f %s. Need to withdraw these funds from the account.",profit,account.Currency());
if(TesterWithdrawal(profit))
{
balance_op_total++;
balance_op_summ+=profit;
balance_prev=account.Balance();
PrintFormat("Funds have been withdrawn from the account. Account balance: %.2f %s.",account.Balance(),account.Currency());
PrintFormat("Total withdrawals: %lu. Amount of withdrawals: %.2f %s.",balance_op_total,balance_op_summ,account.Currency());
}
/*
結果:
The account balance has been increased by 21.00 USD. Need to withdraw these funds from the account.
deal #13 balance -21.00 [withdrawal] done
Funds have been withdrawn from the account. Account balance: 10000.00 USD.
Total withdrawals: 1. Amount of withdrawals: 21.00 USD.
*/
}
}
}
//+------------------------------------------------------------------+
//| テスタ関数 |
//+------------------------------------------------------------------+
double OnTester()
{
//--- 最大残高ドローダウンを金額換算で出力ハンドラー値として設定する
double ret=TesterStatistics(STAT_BALANCE_DD);
//--- ドローダウン、出金回数、およびその合計額に関するメッセージをログに表示する
PrintFormat("%s: Maximum balance drawdown in money: %.2f %s. Total withdrawals: %lu. Amount of withdrawals: %.2f %s.",__FUNCTION__,ret,account.Currency(),balance_op_total,balance_op_summ,account.Currency());
//--- 結果を返す
return(ret);
/*
結果:
OnTester: Maximum balance drawdown in money: 5188.50 USD. Total withdrawals: 2. Amount of withdrawals: 36.00 USD.
final balance 4867.50 USD
OnTester result 5188.5
*/
}
|