過去にもEAでの方向判定、売買のポジションオープンとクローズなどの基本設計を書きました。
そして今回は使えるトータル的な設計を公開します。
シンプルで、ロジック的な設計になっており、バックグラウンドのテストでも利益が出せたものです。
ベースの方針は方向に従ってポジションを追加し、トータル利益が閾値以上だったら全決済しますが、方向が変わったら、利益の閾値以上の反対方向のポジションを決済します。
ですので、これからは上昇なのだ、落下なのだ、関係なく、予想もしないシステムになっています。
実際の実現は次ですが、黒字の部分は調整、置き換えできる部分です。
各自実際の運用金額によって安全に売買できるサイズが決まっているので、ご調整ください。
- Use the Daily/H1 line to check the directions
- If direction continued
- {
- If the total profit is greater than 10%,
- {
- close all positions ※ optional operate
- }
- Else
- {
- If there is no open position in the current position
- {
- If direction position size – opposite position size < threshold size 0.5Lot
- {
- add new position, 0.1Lot
- }
- }
- }
- }
- Else
- {
- If any opposite position has a profit above threshold 50pips
- {
- close the position
- }
- }
実際のコーディングは以下です。
この部分は専門知識が必要で、上記の設計に基づいたものです。
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
double ask = Ask;
double bid = Bid;
if (GeneralManager_IsTickMovedEnough(ask, bid))
{
int currDirection, prevDirection;
// Get privious direction and current direction.
prevDirection = GeneralManager_GetDirection();
GeneralManager_UpdateDirection(ask, bid);
currDirection = GeneralManager_GetDirection();
// same direction
if (currDirection == prevDirection)
{
if (GeneralManager_GetIsTotalProfitCloseOK())
{
Print(__FUNCTION__, ", Total profit ok and close all positions. Line:", __LINE__);
GeneralManager_CloseAllPosition();
}
else
{
if (DIRECTION_UP == currDirection)
{
if (false == GeneralManager_GetIsSamePositionExist(OP_BUY))
{
double buyPositionSize = GeneralManager_OrderTotalSize(OP_BUY);
double sellPositionSize = GeneralManager_OrderTotalSize(OP_SELL);
if (buyPositionSize - sellPositionSize < GeneralManager_GetMaxDiffSize())
{
Print(__FUNCTION__, ", Buy size:", DoubleToStr(buyPositionSize, 2), " Sell size:", DoubleToStr(sellPositionSize, 2), " Open new buy position. Line:", __LINE__);
GeneralManager_OpenNewPosition(OP_BUY);
}
}
}
else
{
if (false == GeneralManager_GetIsSamePositionExist(OP_SELL))
{
double buyPositionSize = GeneralManager_OrderTotalSize(OP_BUY);
double sellPositionSize = GeneralManager_OrderTotalSize(OP_SELL);
if (sellPositionSize - buyPositionSize < GeneralManager_GetMaxDiffSize())
{
Print(__FUNCTION__, ", Buy size:", DoubleToStr(buyPositionSize, 2), " Sell size:", DoubleToStr(sellPositionSize, 2), " Open new sell position. Line:", __LINE__);
GeneralManager_OpenNewPosition(OP_SELL);
}
}
}
}
}
// direction changed
else
{
if (DIRECTION_UP == currDirection)
{
Print(__FUNCTION__, ", Close profit enough sell position. Line:", __LINE__);
GeneralManager_CloseProfitEnoughPosition(OP_SELL);
}
else
{
Print(__FUNCTION__, ", Close profit enough buy position. Line:", __LINE__);
GeneralManager_CloseProfitEnoughPosition(OP_BUY);
}
}
GeneralManager_SetLastTick(ask, bid);
}
}
各関数の詳細を興味があれば以前の記事をご参考ください。
全てのコードを提供する予定はありませんが、肝心な所、設計思想を公開します。
技術力に自信ある方はチャレンジしてみてください。
上記の設計は一番シンプルなもので、それをベースに細かく個別なシグナルで売買を行なってもいいでしょう。
その場合でも全体的な状況を上記のようにトータルの買いと売りの差分を把握し、資金許し範囲でのポジション追加を制御する必要があります。
FX
コメント