Content-Length: 321531 | pFad | http://note.mu/hosono_p/n/n4658db9fc51a

キャッシュバックEAの作り方|ホソノP
見出し画像

キャッシュバックEAの作り方

キャッシュバックEAはスプレッドコストよりもボラティリティによる変動が大きいときにのみ稼げる手法で基本両建てになります。

なのである程度ボラティリティがあるかつボラティリティ相対スプレッドコストが小さい通貨ペア両建て、場合によってはトレーリングストップを使用して最適化すればいけます。

肝は

  • 両建て

  • ストップリミットの最適化

です。

参考にドル円キャッシュバックEAを作成したので他の通貨ペアで探してみてください。



EAのダウンロード

MT4ソースコード全文

//+------------------------------------------------------------------+
//|                                                      DualPositionEA |
//|                                      Copyright 2024, Your Name Here |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, Your Name Here"
#property link      "http://www.companyname.net"
#property version   "1.00"
#property strict

// EA Parameters
extern double Lots = 0.1;              // 取引ロット数
extern int StopLoss = 300;              // ストップロス(ポイント)
extern int TakeProfit = 850;            // テイクプロフィット(ポイント)
extern int TrailingTrigger = 850;       // トレーリングストップ発動ポイント
extern int TrailingStop = 300;          // トレーリングストップ幅

// Spread Filter Parameters
extern string Spread_Filter = "=== Spread Filter Settings ==="; // スプレッドフィルター設定
extern double MaxSpread = 20;             // 許容スプレッド(ポイント)
extern bool CheckSpreadOnTrailing = true; // トレーリング時もスプレッドチェック

// Magic Number
int MagicNumber = 99999999;

//+------------------------------------------------------------------+
//| Expert initialization function                                      |
//+------------------------------------------------------------------+
int OnInit() {
   return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Expert deinitialization function                                   |
//+------------------------------------------------------------------+
void OnDeinit(const int reason) {
}

//+------------------------------------------------------------------+
//| Expert tick function                                               |
//+------------------------------------------------------------------+
void OnTick() {
   // スプレッドチェック
   double spread = (Ask - Bid) / Point;
   if(spread > MaxSpread) return;
   
   // ポジションがない場合、両建てでエントリー
   if(position_count(OP_BUY) == 0 && position_count(OP_SELL) == 0) {
      position_entry(OP_BUY);
      position_entry(OP_SELL);
   }
   
   // トレーリングストップの管理
   if(!CheckSpreadOnTrailing || spread <= MaxSpread) {
      ManageTrailingStop();
   }
}

//+------------------------------------------------------------------+
//| ポジション数カウント関数                                           |
//+------------------------------------------------------------------+
int position_count(int side) {
   int count = 0;
   for(int i = OrdersTotal() - 1; i >= 0; i--) {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
         if(OrderType() == side) {
            if(OrderSymbol() == Symbol()) {
               if(OrderMagicNumber() == MagicNumber) {
                  count++;
               }
            }
         }
      }
   }
   return count;
}

void position_entry(int side) {
   double price, sl, tp;
   
   if(side == OP_BUY) {
      price = Ask;
      sl = price - StopLoss * Point;
      tp = price + TakeProfit * Point;
      if(OrderSend(Symbol(), OP_BUY, Lots, price, 3, sl, tp, "Dual Position EA", MagicNumber, 0, clrGreen) < 0) return;
   }
   else if(side == OP_SELL) {
      price = Bid;
      sl = price + StopLoss * Point;
      tp = price - TakeProfit * Point;
      if(OrderSend(Symbol(), OP_SELL, Lots, price, 3, sl, tp, "Dual Position EA", MagicNumber, 0, clrRed) < 0) return;
   }
}

void ManageTrailingStop() {
   for(int i = OrdersTotal() - 1; i >= 0; i--) {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
         if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) {
            double currentPrice = (OrderType() == OP_BUY) ? Bid : Ask;
            double openPrice = OrderOpenPrice();
            double stopLoss = OrderStopLoss();
            
            // BUYポジションのトレーリングストップ
            if(OrderType() == OP_BUY) {
               if(currentPrice - openPrice > TrailingTrigger * Point) {
                  double newSL = currentPrice - TrailingStop * Point;
                  if(newSL > stopLoss) {
                     if(!OrderModify(OrderTicket(), OrderOpenPrice(), newSL, OrderTakeProfit(), 0, clrBlue)) return;
                  }
               }
            }
            // SELLポジションのトレーリングストップ
            else if(OrderType() == OP_SELL) {
               if(openPrice - currentPrice > TrailingTrigger * Point) {
                  double newSL = currentPrice + TrailingStop * Point;
                  if(newSL < stopLoss || stopLoss == 0) {
                     if(!OrderModify(OrderTicket(), OrderOpenPrice(), newSL, OrderTakeProfit(), 0, clrBlue)) return;
                  }
               }
            }
         }
      }
   }
}

MT5 ソースコード全文


//+------------------------------------------------------------------+
//|                                                      DualPositionEA |
//|                                      Copyright 2024, Your Name Here |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, Your Name Here"
#property link      "http://www.companyname.net"
#property version   "1.00"

// EA Parameters
input double Lots = 0.1;              // 取引ロット数
input int StopLoss = 300;             // ストップロス(ポイント)
input int TakeProfit = 850;           // テイクプロフィット(ポイント)
input int TrailingTrigger = 850;      // トレーリングストップ発動ポイント
input int TrailingStop = 300;         // トレーリングストップ幅
input int Slippage = 3;               // スリッページ

// Spread Filter Parameters
input string Spread_Filter = "=== Spread Filter Settings ==="; // スプレッドフィルター設定
input int MaxSpread = 20;             // 許容スプレッド(ポイント)
input bool CheckSpreadOnTrailing = true; // トレーリング時もスプレッドチェック

// Magic Number
input int MagicNumber = 99999999;     // マジックナンバー

//+------------------------------------------------------------------+
//| Expert initialization function                                      |
//+------------------------------------------------------------------+
int OnInit() {
   return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Expert deinitialization function                                   |
//+------------------------------------------------------------------+
void OnDeinit(const int reason) {
}

//+------------------------------------------------------------------+
//| Expert tick function                                               |
//+------------------------------------------------------------------+
void OnTick() {
   // スプレッドチェック
   long spread = SymbolInfoInteger(Symbol(), SYMBOL_SPREAD);
   if(spread > MaxSpread) return;
   
   // ポジションがない場合、両建てでエントリー
   if(position_count(POSITION_TYPE_BUY) == 0 && position_count(POSITION_TYPE_SELL) == 0) {
      position_entry(ORDER_TYPE_BUY);
      position_entry(ORDER_TYPE_SELL);
   }
   
   // トレーリングストップの管理
   if(!CheckSpreadOnTrailing || spread <= MaxSpread) {
      ManageTrailingStop();
   }
}

//+------------------------------------------------------------------+
//| ポジション数カウント関数                                           |
//+------------------------------------------------------------------+
int position_count(ENUM_POSITION_TYPE side) {
   int count = 0;
   for(int i = PositionsTotal() - 1; i >= 0; i--) {
      if(PositionGetSymbol(i) != "") {
         if(PositionGetInteger(POSITION_TYPE) == side) {
            if(PositionGetString(POSITION_SYMBOL) == Symbol()) {
               if(PositionGetInteger(POSITION_MAGIC) == MagicNumber) {
                  count++;
               }
            }
         }
      }
   }
   return count;
}

//+------------------------------------------------------------------+
//| ポジションエントリー関数                                           |
//+------------------------------------------------------------------+
void position_entry(ENUM_ORDER_TYPE side) {
   MqlTradeRequest request = {};
   MqlTradeResult result = {};
   MqlTick last_tick;
   SymbolInfoTick(_Symbol, last_tick);
   
   request.action = TRADE_ACTION_DEAL;
   request.symbol = Symbol();
   request.volume = Lots;
   request.deviation = Slippage;
   request.magic = MagicNumber;
   request.comment = "Dual Position EA";
   request.type_filling = ORDER_FILLING_FOK;
   
   if(side == ORDER_TYPE_BUY) {
      request.price = last_tick.ask;
      request.type = ORDER_TYPE_BUY;
      request.sl = last_tick.ask - StopLoss * _Point;
      request.tp = last_tick.ask + TakeProfit * _Point;
   }
   else if(side == ORDER_TYPE_SELL) {
      request.price = last_tick.bid;
      request.type = ORDER_TYPE_SELL;
      request.sl = last_tick.bid + StopLoss * _Point;
      request.tp = last_tick.bid - TakeProfit * _Point;
   }
   
   if(!OrderSend(request, result)) return;
}

//+------------------------------------------------------------------+
//| トレーリングストップ管理関数                                       |
//+------------------------------------------------------------------+
void ManageTrailingStop() {
   MqlTick last_tick;
   SymbolInfoTick(_Symbol, last_tick);
   
   for(int i = PositionsTotal() - 1; i >= 0; i--) {
      if(PositionGetSymbol(i) != "") {
         if(PositionGetString(POSITION_SYMBOL) == Symbol() && 
            PositionGetInteger(POSITION_MAGIC) == MagicNumber) {
            
            double currentPrice = (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY) ? 
                                 last_tick.bid : last_tick.ask;
            double openPrice = PositionGetDouble(POSITION_PRICE_OPEN);
            double stopLoss = PositionGetDouble(POSITION_SL);
            ulong ticket = PositionGetInteger(POSITION_TICKET);
            
            // BUYポジションのトレーリングストップ
            if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY) {
               if(currentPrice - openPrice > TrailingTrigger * _Point) {
                  double newSL = currentPrice - TrailingStop * _Point;
                  if(newSL > stopLoss) {
                     ModifyPosition(ticket, newSL, PositionGetDouble(POSITION_TP));
                  }
               }
            }
            // SELLポジションのトレーリングストップ
            else if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL) {
               if(openPrice - currentPrice > TrailingTrigger * _Point) {
                  double newSL = currentPrice + TrailingStop * _Point;
                  if(newSL < stopLoss || stopLoss == 0) {
                     ModifyPosition(ticket, newSL, PositionGetDouble(POSITION_TP));
                  }
               }
            }
         }
      }
   }
}

//+------------------------------------------------------------------+
//| ポジション修正関数                                                 |
//+------------------------------------------------------------------+
bool ModifyPosition(ulong ticket, double sl, double tp) {
   MqlTradeRequest request = {};
   MqlTradeResult result = {};
   
   request.action = TRADE_ACTION_SLTP;
   request.position = ticket;
   request.symbol = Symbol();
   request.sl = sl;
   request.tp = tp;
   
   return OrderSend(request, result);
}

いいなと思ったら応援しよう!

ホソノP
よろしければサポートお願いします! いただいたサポートはクリエイターとしての活動費に使わせていただきます!








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://note.mu/hosono_p/n/n4658db9fc51a

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy