0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

mt4をTradingView風にする

Posted at

はじめに

MT4のチャート画面をTradingView風に
チャートを上下にスクロールできるようにします。

↓↓↓↓↓こんな感じです。↓↓↓↓↓

ezgif-64fde82625a737.gif

mql4コード

下記がmql4のコードです。EAのコードになります。

tradingviewmode.mq4
#property copyright ""
#property link      ""
#property version   "1.00"
#property strict

int OnInit()
{
  // TradingViewモードボタン
  ButtonCreate(0, "tradingviewmode_button", 0, 5, 30, 260, 50, CORNER_LEFT_UPPER, "TradingViewモード","MS Pゴシック", 13, clrWhite, color(3552822), clrWhite, false, false, false, false, 0);
  return(INIT_SUCCEEDED);
}

void OnDeinit(const int reason)
{
  ObjectDelete("tradingviewmode_button");
  ChartSetInteger(0, CHART_SCALEFIX, 0, false);// Tradingmode 解除
}

void OnTick()
{
}

void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
{
  color status_on_clr = color(11753728);
  color status_off_clr = color(3552822);
  if(id == CHARTEVENT_OBJECT_CLICK){
    if(sparam == "tradingviewmode_button"){
      if((bool)ObjectGetInteger(0, "tradingviewmode_button", OBJPROP_STATE, 0)){
        ObjectSetInteger(0, "tradingviewmode_button", OBJPROP_BGCOLOR, status_on_clr);
        ChartSetInteger(0, CHART_MOUSE_SCROLL, 0, true);
        ChartSetInteger(0, CHART_SCALEFIX, 0, true); // この設定で上下のチャートをスクロールできる
      }else{
        ObjectSetInteger(0, "tradingviewmode_button", OBJPROP_BGCOLOR, status_off_clr);
        ChartSetInteger(0, CHART_SCALEFIX, 0, false);
      }
    }
  }
}

bool ButtonCreate(const long              chart_ID=0,               // chart's ID
                  const string            name="Button",            // button name
                  const int               sub_window=0,             // subwindow index
                  const int               x=0,                      // X coordinate
                  const int               y=0,                      // Y coordinate
                  const int               width=50,                 // button width
                  const int               height=18,                // button height
                  const ENUM_BASE_CORNER  corner=CORNER_LEFT_UPPER, // chart corner for anchoring
                  const string            text="Button",            // text
                  const string            font="Arial",             // font
                  const int               font_size=10,             // font size
                  const color             clr=clrBlack,             // text color
                  const color             back_clr=C'236,233,216',  // background color
                  const color             border_clr=clrNONE,       // border color
                  const bool              state=false,              // pressed/released
                  const bool              back=false,               // in the background
                  const bool              selection=false,          // highlight to move
                  const bool              hidden=true,              // hidden in the object list
                  const long              z_order=0)                // priority for mouse click
{
  ResetLastError();
  if(ObjectFind(name) == 0){ return true; }
  if(!ObjectCreate(chart_ID, name, OBJ_BUTTON, sub_window, 0, 0)){
    Print(__FUNCTION__, ": failed to create the button! Error code = ", GetLastError());
    return(false);
  }
  ObjectSetInteger(chart_ID, name, OBJPROP_XDISTANCE, x);
  ObjectSetInteger(chart_ID, name, OBJPROP_YDISTANCE, y);
  ObjectSetInteger(chart_ID, name, OBJPROP_XSIZE, width);
  ObjectSetInteger(chart_ID, name, OBJPROP_YSIZE, height);
  ObjectSetInteger(chart_ID, name, OBJPROP_CORNER, corner);
  ObjectSetString(chart_ID, name, OBJPROP_TEXT, text);
  ObjectSetString(chart_ID, name, OBJPROP_FONT, font);
  ObjectSetInteger(chart_ID, name, OBJPROP_FONTSIZE, font_size);
  ObjectSetInteger(chart_ID, name, OBJPROP_COLOR, clr);
  ObjectSetInteger(chart_ID, name, OBJPROP_BGCOLOR, back_clr);
  ObjectSetInteger(chart_ID, name, OBJPROP_BORDER_COLOR, border_clr);
  ObjectSetInteger(chart_ID, name, OBJPROP_BACK, back);
  ObjectSetInteger(chart_ID, name, OBJPROP_STATE, state);
  ObjectSetInteger(chart_ID, name, OBJPROP_SELECTABLE, selection);
  ObjectSetInteger(chart_ID, name, OBJPROP_SELECTED, selection);
  ObjectSetInteger(chart_ID, name, OBJPROP_HIDDEN, hidden);
  return(true);
}

仕組み

MQLに詳しいかたならわかるかもですが
別にコードを書く必要はありません。
ボタンも必要ないです。。

下記の設定を入れているだけです。
この設定でチャートのスケールを固定しています。

ChartSetInteger(0, CHART_SCALEFIX, 0, true); // この設定で上下のチャートをスクロールできる

詳しくは下記のリンクをご参照ください。
https://docs.mql4.com/constants/chartconstants/enum_chart_property
https://docs.mql4.com/constants/chartconstants/charts_samples#chart_scalefix

この設定はMT4の画面からも設定できます。
「スケールの固定」にチェックを入れると
TradingView風になります。

image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?