//+------------------------------------------------------------------+ //| CTrade Test CFD JP225(OANDA).mq5 | //| Copyright 2021, MetaQuotes Software Corp. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2021, MetaQuotes Software Corp." #property link "https://www.mql5.com" #property version "1.00" #include //定数 //タイマー周期の指定(10秒) const int time = 10; //購入ロット単位の指定(1.00 = 株価指数×1) const double onelot = 1.00; //通貨の指定 const string tuuka = "JP225"; //スリッページ(pips) const ulong slip = 10; //購入幅(円) const double haba = 200; //変数 //現在のBid値(円) double bid = 0; //購入したmin値(円) double min = 0; //購入したmax値(円) double max = 0; //注文数(個) int ordersuu = 0; //取引関数に簡単にアクセスするためのクラス CTrade trade; //現在の価格を取得する構造体 MqlTick last_tick; int OnInit() { Print("プログラム開始"); //共通する注文パラメータの設定 trade.SetTypeFillingBySymbol(tuuka); trade.SetDeviationInPoints(slip); trade.LogLevel(LOG_LEVEL_ERRORS); ordersuu = PositionsTotal(); //ポジションがある場合→min値,max値の再設定 if(ordersuu > 0) { for(int i = ordersuu-1;i>=0;i--) { bool test = PositionSelectByTicket(PositionGetTicket(i)); double orderopenprice = PositionGetDouble(POSITION_PRICE_OPEN); //初回計算時はmax値,min値を共に再設定する if(i == ordersuu-1) { max = orderopenprice; min = max; } //2回目以降計算時は下記の判定で再設定する else { if(orderopenprice > max) { max = orderopenprice; } else if(orderopenprice < min) { min = orderopenprice; } } } } EventSetTimer(time); return(INIT_SUCCEEDED); } void OnDeinit(const int reason) { EventKillTimer(); Print("プログラム終了"); } void OnTick() { //現在Bid値の取得 SymbolInfoTick(tuuka,last_tick); bid = last_tick.bid; //購入ロジック //ポジションが無い場合→購入 if(ordersuu == 0) { //注文処理 bool test = trade.Sell(onelot, tuuka, bid, 0.0, 0.0, IntegerToString(ordersuu,3)); //Sleep(2000); if(test == true && (trade.ResultRetcode() == TRADE_RETCODE_PLACED || trade.ResultRetcode() == TRADE_RETCODE_DONE) ) { //初回購入時はmax値,min値を共に再設定する ordersuu++; //bool test2 = PositionSelectByTicket(PositionGetTicket(ordersuu-1)); //max = PositionGetDouble(POSITION_PRICE_OPEN); max = trade.ResultPrice(); min = max; Print("新規購入(売り)、ポジション数:",ordersuu); } else { Print("購入失敗", "、エラーコード:",GetLastError()); } } //(max値+幅)以上になった場合、新規購入(売り) + max値の更新 else if(bid >= (max + haba)) { //注文処理 bool test = trade.Sell(onelot, tuuka, bid, 0.0, 0.0, IntegerToString(ordersuu,3)); //Sleep(2000); if(test == true && (trade.ResultRetcode() == TRADE_RETCODE_PLACED || trade.ResultRetcode() == TRADE_RETCODE_DONE) ) { ordersuu++; //bool test2 = PositionSelectByTicket(PositionGetTicket(ordersuu-1)); //max = PositionGetDouble(POSITION_PRICE_OPEN); max = trade.ResultPrice(); Print("新規購入(売り)、ポジション数:",ordersuu); } else { Print("購入失敗、エラーコード:",GetLastError()); } } //売却ロジック //今回は省略 } void OnTimer() { Print("エラーコード:", GetLastError(), "、ポジション数:", ordersuu, "、MIN:", DoubleToString(min, 1), "円、MAX:", DoubleToString(max, 1), "円"); }