トレーリングストップを複数ポジションで管理したいです

2022/06/20 22:40
673

EAつくーるのコードの書き換えについて質問です。
つくーるのトレーリングストップは、単体ポジションの損益を管理するので、ナンピン+トレーリングストップを設定すると、1ポジション毎にトレールが発生してしまうため、単体損益>合計損益になってしまい、疑似的にグリッド、トラリピトレードのような状態になってしまいます。

例えば、3ポジションを持った状態で、合計20pips含み益を抱えたら、総ポジション損益が±0の位置へ損切を設定する。

といったような設定をしたい場合、どのようにコードを書き換えたら良いでしょうか?

コメント

GogoJungle
2022/06/21 20:44

下記がナンピンを処理している箇所でございます。

具体的なコードのご案内ができず、誠に恐れ入りますが、// 下記に処理を追加します
次の行に総ポジション損益が±0の位置へ損切を設定する処理を追加することで可能でございます。

//--------------------------------------------------------------------------------------------------------+
//ナンピン関数                                                                                            
//   処理:損益(pips単位)に応じてポジションの追加を行う                                                  
//   引数:ナンピンを行うポジションのマジックナンバー                                                      
//   戻り値:なし                                                                                          
//--------------------------------------------------------------------------------------------------------+
void NanpinLogic(
   int max,
   double interval,
   double mult,
   double tp,
   double sl,
   double add,
   int &magic[])
{
   double price , total, lots;
   int count, type;

   for(int i = 0; i < ArrayRange(magic, 0); i++) {
      getNanpinInfo(magic[i], type, price, count, total, lots);

      if(price > 0) {

         // 下記に処理を追加します

         if((total > tp && tp != 0) || (total * -1 > sl && sl != 0)) {
            if(closePosition(magic[i], type) == true) return;
         }
      }

ご参考いただけますと幸いです。何卒よろしくお願い申し上げます。

まめゾウFX
2024/02/05 22:35

私も、EAに同じ機能を持たせたく検索し、ここにたどり着きました。
具体的にはどういったコードを記載すればいいでしょうか?
よろしくお願いいたします。

GogoJungle
2024/02/06 11:21

ご利用ありがとうございます。

誠に申し訳ないのですがコードについてはサポートが難しく
複雑でございますのでご案内ができず誠に恐れ入ります。

ご不便をおかけしますが何卒よろしくお願い申し上げます。

とらじろう
2024/09/22 19:58

下記にしましたが、すべてのポジションでは、トレーリングストップになりません。複数のポジションでは、トレーリングストップがかかるのですが・・・・
void NanpinLogic(
int max,
int is_custom,
string clots,
double interval,
double mult,
double tp,
double sl,
double add,
double limit,
int &magic[])
{
double price, total, lots;
int count, type;
if(limit == 0) limit = 10000;

string custom_lots_str[];
double custom_lots[];
int clots_cnt = StringSplit(clots, ‘,’, custom_lots_str);

if(clots_cnt > 0) {
clots_cnt = MathMin(clots_cnt, max);
ArrayResize(custom_lots, max);
for(int i = 0; i < clots_cnt; i++) {
custom_lots[i] = StringToDouble(custom_lots_str[i]);
}
if(clots_cnt < max) {
for(int i = clots_cnt; i < max; i++) {
custom_lots[i] = custom_lots[clots_cnt - 1];
}
}
}

for(int i = 0; i < ArrayRange(magic, 0); i++) {
getNanpinInfo(magic[i], type, price, count, total, lots);

  if(price &gt; 0) {
     // 総ポジション損益が±0の位置へ損切を設定
     if(total == 0) {
        if(type == OP_BUY) {
           setTPSL(0, sl, OrderTicket());
          
        } else if(type == OP_SELL) {
        setTPSL(0, sl, OrderTicket());
           
           
        }
     }

     if((total &gt; tp && tp != 0) || (total * -1 &gt; sl && sl != 0)) {
        if(closePosition(magic[i], type) == true) return;
     }
  }

  if(count &gt;= max) return;

  if(type == OP_BUY && Ask &lt;= (price - PipsToPrice(interval))) {
     if(is_custom == 0) {
        if(add == 0){
           openPosition(1, MathMin(lots * MathPow(mult, count + 1), limit), 0, 0, magic[i]);
        }
        if(add &gt; 0){
           if(mult == 0 || mult == 1) {
              openPosition(1, MathMin(lots + (add * (count + 1)), limit), 0, 0, magic[i]);
           }
           else {
              openPosition(1, MathMin(lots * MathPow(mult, count + 1) + (add * (count + 1)), limit), 0, 0, magic[i]);
           }
        }
     }
     else {
        openPosition(1, MathMin(custom_lots[count], limit), 0, 0, magic[i]);
     }
  }

  if(type == OP_SELL && Bid &gt;= (price + PipsToPrice(interval))) {
     if(is_custom == 0) {
        if(add == 0){
           openPosition(-1, MathMin(lots * MathPow(mult, count + 1), limit), 0, 0, magic[i]);
        }
        if(add &gt; 0){
           if(mult == 0 || mult == 1) {
              openPosition(-1, MathMin(lots + (add * (count + 1)), limit), 0, 0, magic[i]);
           }
           else {
              openPosition(-1, MathMin(lots * MathPow(mult, count + 1) + (add * (count + 1)), limit), 0, 0, magic[i]);
           }
        }
     }
     else {
        openPosition(-1, MathMin(custom_lots[count], limit), 0, 0, magic[i]);
     }
  }

}
}

とらじろう
2024/09/22 20:40

// 総ポジション損益が±0の位置へ損切を設定
double breakevenPrice = price; // ブレイクイーブン価格を設定
if(type == OP_BUY && total > 0) {
setTPSL(0, breakevenPrice, magic[i]); // 買いポジションの場合
} else if(type == OP_SELL && total < 0) {
setTPSL(breakevenPrice, 0, magic[i]); // 売りポジションの場合
}
うーん 機能しませんね

とらじろう
2024/09/22 20:55

if(price > 0) {
// 総ポジション損益を取得
double profitPips = getOrderProfitPips(IntegerToString(magic[i]), 0, false);
double breakevenPrice = price + PipsToPrice(profitPips); // ブレイクイーブン価格を設定

     if(type == OP_BUY && total &gt; 0) {
        setTPSL(0, breakevenPrice, magic[i]); // 買いポジションの場合
     } else if(type == OP_SELL && total &lt; 0) {
        setTPSL(breakevenPrice, 0, magic[i]); // 売りポジションの場合
     }

     if((total &gt; tp && tp != 0) || (total * -1 &gt; sl && sl != 0)) {
        if(closePosition(magic[i], type) == true) return;
     }
  }

うーん、 あきらめるしかないか

関連トピックス

検索結果がありません。

ノーコードで誰でも簡単EA開発!MQL言語学習にも使える! | GogoJungle

注目トピックス

検索結果がありません。