キーボードから発注するEAを作ってみた

キーボードから発注するEAを作ってみた

サムネ
こんにちは~ fx-onエンジニアの岩淵です。 今日はキーボードの入力を使ったプログラムの例をご紹介します。

仕様

キーボード入力でエントリー決済を行うEA bボタン:買いエントリー sボタン:売りエントリー cボタン:ポジション全決済 マジックナンバー、ロット数、TP/SLなどはパラメータから設定。 (ここもキーボードから入力したかったけど力尽きました。)

開発

キーボードが押された際の処理は、OnChartEvent関数内に書いていきます。 第一引数のidがCHARTEVENT_KEYDOWNであった場合はチャート上でキーが押されています。
void OnChartEvent(const int id,
                  const long& lparam,
                  const double& dparam,
                  const string& sparam){

   if(id == CHARTEVENT_KEYDOWN){
      //キーが押されてる
   }
}
どのボタンが押されたのかは第二引数のlparamの値で分かります。 どのボタンがどの数値かは覚えていないので、Printして確認します。
void OnChartEvent(const int id,
                  const long& lparam,
                  const double& dparam,
                  const string& sparam){

   if(id == CHARTEVENT_KEYDOWN){
      Print("Press:", lparam);
   }
}
どうやら私の環境だと、bボタンが66, sボタンが83、cボタンが64のようです。 私が使っているキーボードは英字配列なので、もしかすると数字の割当が普通のキーボードと違うかもしれません。 割当の確認は各自お願いします。 割当もパラメータ化とかユーザー入力でできたらカッコイイですね。 あとはlparamの値で分岐させて、発注処理と決済処理を書いてあげればOKです。 今回はキー入力の動作確認が目的なので、発注処理はアッサリと書いちゃっています。 最終的なソースは以下のようになりました。
#property version   "1.00"
#property strict

#include <stdlib.mqh>

extern int    MagicNumber = 123;
extern double Lots        = 0.1;
extern double SL          = 100;
extern double TP          = 100;
extern int    Slippage    = 10;

double sl, tp;
int    slippage;

int OnInit(){
   sl       = SL * Point;
   tp       = TP * Point;
   slippage = Slippage;
   
   if(Digits == 3 || Digits == 5){
      sl       *= 10;
      tp       *= 10;
      slippage *= 10;
   }
   
   return(INIT_SUCCEEDED);
}

void OnTick(){
   Comment(
      "MagicNumber:", MagicNumber, "\n",
      "Lots:", Lots, "\n",
      "SL:", SL, "pisp\n",
      "TP:", TP, "pips\n",
      "Slippage:", Slippage, "pips");
}

void OnChartEvent(const int id,
                  const long& lparam,
                  const double& dparam,
                  const string& sparam){

   if(id == CHARTEVENT_KEYDOWN){
      //キーが押されてる
      Print("Press:", lparam, ",", dparam, ",", sparam);
      switch((int)lparam){
         case 66:
            if(!OrderSend(Symbol(), OP_BUY, Lots, Ask, slippage, Ask - sl, Ask + tp, NULL, MagicNumber, 0, clrNONE)){
               Print(ErrorDescription(GetLastError()));
            }
            break;
         
         case 83:
            if(!OrderSend(Symbol(), OP_SELL, Lots, Bid, slippage, Bid + sl, Bid - tp, NULL, MagicNumber, 0, clrNONE)){
               Print(ErrorDescription(GetLastError()));
            }
            break;
         
         case 67:
            for(int i = OrdersTotal() - 1; i >= 0; i--){
               if(!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) break;
               if(OrderMagicNumber() != MagicNumber) continue;
               if(OrderSymbol() != Symbol()) continue;
               if(OrderType() == OP_BUY){
                  if(!OrderClose(OrderTicket(), OrderLots(), Bid, slippage, clrNONE)){
                     Print(ErrorDescription(GetLastError()));
                  }
               }
               else if(OrderType() == OP_SELL){
                  if(!OrderClose(OrderTicket(), OrderLots(), Ask, slippage, clrNONE)){
                     Print(ErrorDescription(GetLastError()));
                  }
               }
            }
         
            break;
            
         default:
            break;
      }
   }
}
あとはこのEAをチャートに入れてキーボードを押してみてください。 bで買い、sで売り、cで決済ができていることを確認して完成です。

感想

今回はキーボードから発注させてみました。 作っといてあれですけど、自分だったら絶対に使わないですね。 間違えて押しちゃって誤発注しそう... しかしキーボードが押されたイベントの取得自体は使いみちがありそうです。 EAだと使いづらいかもしれませんが、インジケーターなら用途は多そうです。 キー入力でオブジェクトを動かしてミニゲームなども作れそうです。
■ Important Matters and Risk Warnings Regarding Investment Products 【Financial Instruments Business Operator】
GogoJungle Inc.
Registration: Director-General of the Kanto Local Finance Bureau (Financial Instruments Business) No. 1960
【Member Association】
Japan Investment Advisers Association
【Fees and Expenses】
The use of software, e-books, investment advisory services, etc., provided on our platform involves purchase prices determined for each product.
Additionally, when conducting actual financial instrument transactions (such as FX or CFD trading), costs such as transaction fees and spreads (the difference between the sell and buy price) may be incurred through the connected brokerage firms.
【Margin and Risk of Loss】
Transactions such as FX (Foreign Exchange Margin Trading) and CFD (Contract for Difference) allow for trading in amounts exceeding the deposited margin.
Therefore, due to sudden fluctuations in target indices (currency, commodity prices, etc.), there is a risk of incurring losses that exceed the deposited margin (loss exceeding principal).
Furthermore, financial instrument transactions involve risks such as price fluctuations, which may result in losses falling below or exceeding the initial investment principal.
【Nature of Services and Trading Entity】
The Company operates a platform (marketplace) providing investment software and information; we do not engage in the buying, selling, mediation, brokerage, or agency of financial instruments on behalf of customers.
The services and information provided are intended as reference for investment decisions and do not guarantee future profits or specific results.
While some services include investment advice, they do not constitute a mandate or solicitation to trade specific financial instruments.
Final investment decisions and the execution of trades must be made at the customer’s own risk and discretion.