Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //+------------------------------------------------------------------+
- //| mql4_ぶりぶりピラミ.mq4 |
- //| Copyright 2018, MetaQuotes Software Corp. |
- //| https://www.mql5.com |
- //+------------------------------------------------------------------+
- #property copyright "Copyright 2018, MetaQuotes Software Corp."
- #property link "https://www.mql5.com"
- #property version "1.03"
- #property strict
- #define MAGIC 123456 // エキスパートアドバイザのMagicNumber
- enum Lot_Type_enum{
- TYPE_NOMAL, // 初期値で固定
- TYPE_ADD, // 加算(1, 2, 3, 4, ..)
- TYPE_MUL, // 乗算(1, 2, 4, 8, ..)
- };
- //XMの仕様に関する定数
- const int LOTS_DIGITS = 2; //ロット数の小数点以下の桁数
- const double MAX_LOT_SIZE = 50.0; //XMでの1ポジションあたりの最大ロット数
- //インプット変数(大文字で定義する)
- input Lot_Type_enum ENTRY_LOT_TYPE = TYPE_ADD; //ロット計算方法
- input double ENTRY_LOT = 0.1; //ロット初期値、1=10万通貨
- input double ENTRY_LOT_LIMIT = 1.0; //ロット上限。XMは上限50。
- input double ENTRY_LOT_INTERVAL = 0.01; //ロット加算幅。加算時のみ有効
- input double ENTRY_PIPS = 10; //エントリーする値幅。単位=pips
- input double LOSS_CUT = 20; //損切り設定。単位=pips
- sinput int SLIP_PAGE = 10; //スリッページ、単位=ポイント?
- input double TRAIL_PRICE = 10000; //トレールが発動する金額。
- input double TRAIL_RATE = 0.8; //トレールで追いかける幅。倍率1=100%
- //sinput int STOP_TIME1 = 6; //取引しない時間:開始(00分)~。-1は設定なし
- //sinput int STOP_TIME2 = 8; //取引しない時間:~終了(00分)まで。
- enum hour_enum{
- hxx=-1, //無効
- h00=0, //0時
- h01=1, //1時
- h02=2, //2時
- h03=3, //3時
- h04=4, //4時
- h05=5, //5時
- h06=6, //6時
- h07=7, //7時
- h08=8, //8時
- h09=9, //9時
- h10=10, //10時
- h11=11, //11時
- h12=12, //12時
- h13=13, //13時
- h14=14, //14時
- h15=15, //15時
- h16=16, //16時
- h17=17, //17時
- h18=18, //18時
- h19=19, //19時
- h20=20, //20時
- h21=21, //21時
- h22=22, //22時
- h23=23, //23時
- };
- sinput hour_enum STOP_TIME1 = h06; //取引しない時間:開始(00分)~。
- sinput hour_enum STOP_TIME2 = h08; //取引しない時間:~終了(00分)まで。
- //グローバル変数
- double myEntry_Lot = 0.0; //エントリー枚数。1=10万通貨
- double myEntry_Lot_Limit = 0.0; //ロット限界数
- double myEntry_Lot_Interval = 0.0; //増減幅
- double myEntry_Pips = 0.0; //エントリーする値幅。pips
- double mySlip_Page = 0.0; //スリップページ。ポイント
- double myLoss_Cut = 0.0; //損切り設定。pips
- //double myLast_Entry_Ask = 0.0; //最後に買いエントリーした値
- //double myLast_Entry_Bid = 0.0; //最後に売りエントリーした値
- datetime myBarTime; //新しいバーか判定用
- double myTrailing_Stop = 0.0; //トレール設定用
- double myMargin = 0; //過去有効証拠金
- bool gTrailFlg_L = false; //利益確定モードフラグ。L方向
- bool gTrailFlg_S = false; //利益確定モードフラグ。S方向
- double gBase_Price = 0; //トレールベースになる価格
- //実 行の場合、XMTrading MT4\MQL4\Filesの中(たぶん。未確認)
- //テストの場合、XMTrading MT4\tester\filesの中
- //string gFileName = "復帰test.txt"; //復帰用ログのファイル名。""で復帰なし
- sinput string gFileName = ""; //復帰用ファイル名。""で復帰なし
- //double gEntry_Lot_Now_L = 0; //ロット増減時の買い現在ロット
- //double gEntry_Lot_Now_S = 0; //ロット増減時の売り現在ロット
- int myStopTime1 = 0; //取引しない時間:開始
- int myStopTime2 = 0; //取引しない時間:終了
- //+------------------------------------------------------------------+
- //| Expert initialization function |
- //+------------------------------------------------------------------+
- int OnInit()
- {
- //---
- //グローバル変数の初期化
- myInit();
- //ロット増減設定ありの場合
- // if(myEntry_Lot_Limit > 0){
- // gEntry_Lot_Now_L = myEntry_Lot;
- // gEntry_Lot_Now_S = myEntry_Lot;
- // }
- //復帰テスト用コード
- // int t1 = OrderSend(Symbol(),OP_BUY,0.1,Ask,10,0,0,NULL,MAGIC,0,clrBlue);
- // int t2 = OrderSend(Symbol(),OP_SELL,0.2,Bid,10,0,0,NULL,MAGIC,0,clrRed);
- //復帰判定
- zzz();
- //---
- return(INIT_SUCCEEDED);
- }
- //+------------------------------------------------------------------+
- //| Expert deinitialization function |
- //+------------------------------------------------------------------+
- void OnDeinit(const int reason)
- {
- //---
- }
- //+------------------------------------------------------------------+
- //| Expert tick function |
- //+------------------------------------------------------------------+
- void OnTick()
- {
- //---
- //自動売買が許可されているか?
- if(IsTradeAllowed()==false){
- string ss = "自動売買が許可されていません";
- Alert(ss);
- return;
- }
- //取引可能な時間かチェック
- if(myTradeTime(true) == false){
- // Sleep(60000); //1分スリープ。テストでは機能しないみたい?
- return;
- }
- //現在の通貨が取引可能かチェック
- if(MarketInfo(Symbol(),MODE_TRADEALLOWED) == 0){
- // Print(Symbol() + "は現在取引できません。");
- return;
- }
- //一応リフレッシュレート
- RefreshRates();
- //---
- double lot = myEntry_Lot;
- int flg = 0;
- //手動で決済された場合を想定して
- if(myLongTotal() <= 0){
- //枚数と前回エントリー値のリセット
- // myLast_Entry_Ask = 0;
- // gEntry_Lot_Now_L = myEntry_Lot;
- gTrailFlg_L = false; //トレールフラグリセット
- //有効証拠金が増えていたら(損切りした場合は減るから)
- double dd = myGetMargin();
- if(dd > myMargin){
- myMargin = dd; //有効証拠金の更新
- myFileWrite(gFileName, (string)myMargin); //復帰用のログに出力
- }
- }
- if(myShortTotal() <= 0){
- //枚数と前回エントリー値のリセット
- // myLast_Entry_Bid = 0;
- // gEntry_Lot_Now_S = myEntry_Lot;
- gTrailFlg_S = false; //トレールフラグリセット
- //有効証拠金が増えていたら(損切りした場合は減るから)
- double dd = myGetMargin();
- if(dd > myMargin){
- myMargin = dd; //有効証拠金の更新
- myFileWrite(gFileName, (string)myMargin); //復帰用のログに出力
- }
- }
- //決済判定
- flg = eee(true);
- if(flg == OP_BUY){
- //L決済判定
- // myCloseLong(); //買いを全て決済
- //買いを全て決済
- if(myCloseLong()){
- myMargin = myGetMargin(); //有効証拠金の更新
- myFileWrite(gFileName, (string)myMargin); //復帰用のログに出力
- // //ロット増減設定ありの場合
- // if(myEntry_Lot_Limit > 0){
- // gEntry_Lot_Now_L = myEntry_Lot;
- // }
- }else{
- //決済失敗した場合
- }
- }else if(flg == OP_SELL){
- //S決済判定
- // myCloseShort(); //売りを全て決済
- //売りを全て決済
- if(myCloseShort()){
- myMargin = myGetMargin(); //有効証拠金の更新
- myFileWrite(gFileName, (string)myMargin); //復帰用のログに出力
- // //ロット増減設定ありの場合
- // if(myEntry_Lot_Limit > 0){
- // gEntry_Lot_Now_S = myEntry_Lot;
- // }
- }else{
- //決済失敗した場合
- }
- }else{
- //(0)の場合何もしない。
- }
- //損切り判定
- flg = ccc(true);
- if(flg == OP_BUY){
- //L損切り判定
- // myCloseLong(); //買いを全て決済
- //買いを全て決済
- if(myCloseLong()){
- // //ロット増減設定ありの場合
- // if(myEntry_Lot_Limit > 0){
- // gEntry_Lot_Now_L = myEntry_Lot;
- // }
- }else{
- //決済失敗した場合
- }
- }else if(flg == OP_SELL){
- //S損切り判定
- // myCloseShort(); //売りを全て決済
- //売りを全て決済
- if(myCloseShort()){
- // //ロット増減設定ありの場合
- // if(myEntry_Lot_Limit > 0){
- // gEntry_Lot_Now_S = myEntry_Lot;
- // }
- }else{
- //決済失敗した場合
- }
- }else{
- //(0)の場合何もしない。
- }
- //エントリー判定
- flg = aaa();
- if(flg == OP_BUY){
- //指定pips上げ判定
- //買い注文
- lot = LotCalc(OP_BUY);
- if(myGetLong(lot)){
- //注文成功
- }else{
- //注文失敗
- }
- /*
- if(myEntry_Lot_Limit > 0){
- //ロット増減あり
- // myGetLong(gEntry_Lot_Now_L); //エントリー
- lot = LotCalc(OP_BUY);
- if(myGetLong(lot)){
- //注文成功
- //次回エントリー枚数計算
- if(myEntry_Lot > myEntry_Lot_Limit){
- //減算の場合
- //特定の小数点(0.07など)でおかしな値になるので差をとって判定する
- double d = NormalizeDouble(gEntry_Lot_Now_L - myEntry_Lot_Limit, 2);
- if(d >= myEntry_Lot_Interval){
- gEntry_Lot_Now_L = NormalizeDouble(gEntry_Lot_Now_L - myEntry_Lot_Interval, 2);
- }else{
- gEntry_Lot_Now_L = myEntry_Lot_Limit;
- }
- }else if(myEntry_Lot < myEntry_Lot_Limit){
- //増算の場合
- //特定の小数点(0.07など)でおかしな値になるので差をとって判定する
- double d = NormalizeDouble(myEntry_Lot_Limit - gEntry_Lot_Now_L, 2);
- if(d >= myEntry_Lot_Interval){
- gEntry_Lot_Now_L = NormalizeDouble(gEntry_Lot_Now_L + myEntry_Lot_Interval, 2);
- }else{
- gEntry_Lot_Now_L = myEntry_Lot_Limit;
- }
- }
- }else{
- //注文失敗
- }
- }else{
- //ロット増減なし
- if(myGetLong(lot)){
- //注文成功
- }else{
- //注文失敗
- }
- }
- */
- }else if(flg == OP_SELL){
- //指定pips下げ判定
- //売り注文
- lot = LotCalc(OP_SELL);
- if(myGetShort(lot)){
- //注文成功
- }else{
- //注文失敗
- }
- /*
- if(myEntry_Lot_Limit > 0){
- //ロット増減あり
- // myGetShort(gEntry_Lot_Now_S);
- if(myGetShort(gEntry_Lot_Now_S)){
- //注文成功
- //エントリー枚数計算
- if(myEntry_Lot > myEntry_Lot_Limit){
- //減算の場合
- //特定の小数点(0.07など)でおかしな値になるので差をとって判定する
- double d = NormalizeDouble(gEntry_Lot_Now_S - myEntry_Lot_Limit, 2);
- if(d >= myEntry_Lot_Interval){
- gEntry_Lot_Now_S = NormalizeDouble(gEntry_Lot_Now_S - myEntry_Lot_Interval, 2);
- }else{
- gEntry_Lot_Now_S = myEntry_Lot_Limit;
- }
- }else if(myEntry_Lot < myEntry_Lot_Limit){
- //増算の場合
- //特定の小数点(0.07など)でおかしな値になるので差をとって判定する
- double d = NormalizeDouble(myEntry_Lot_Limit - gEntry_Lot_Now_S, 2);
- if(d >= myEntry_Lot_Interval){
- gEntry_Lot_Now_S = NormalizeDouble(gEntry_Lot_Now_S + myEntry_Lot_Interval, 2);
- }else{
- gEntry_Lot_Now_S = myEntry_Lot_Limit;
- }
- }
- }else{
- //注文失敗
- }
- }else{
- //ロット増減なし
- if(myGetShort(lot)){
- //注文成功
- }else{
- //注文失敗
- }
- }
- */
- }else{
- //(-1)の場合何もしない。
- }
- }
- //+------------------------------------------------------------------+
- //+------------------------------------------------------------------+
- // エントリー判定用関数
- //+------------------------------------------------------------------+
- int aaa(bool rog = false)
- {
- int flg = -1;
- /*
- //?(現在未設定)
- //?判定にスプをかけるかどうか?
- double Symbol_Spread = MarketInfo(Symbol(),MODE_SPREAD) * myAdjustPoint(Symbol()); //スプレットを取得
- //前回の「買い」エントリー値より、○pipsあがったら
- if(myLast_Entry_Ask == 0 || Ask >= (myLast_Entry_Ask + myEntry_Pips)){
- //上げ判定あり
- flg = OP_BUY;
- //ログ出力
- if(rog == true) Print("--上げ判定あり:", Ask);
- //前回の「売り」エントリー値より、○pips下がったら
- }else if(myLast_Entry_Bid == 0 || Bid <= (myLast_Entry_Bid - myEntry_Pips)){
- //下げ判定あり
- flg = OP_SELL;
- //ログ出力
- if(rog == true) Print("--下げ判定あり:", Bid);
- }
- */
- double Last_Entry_Ask = GetLastPrice(OP_BUY);
- double Last_Entry_Bid = GetLastPrice(OP_SELL);
- //前回の「買い」エントリー値より、○pipsあがったら
- if(Last_Entry_Ask == 0 || Ask >= (Last_Entry_Ask + myEntry_Pips)){
- //上げ判定あり
- flg = OP_BUY;
- //ログ出力
- if(rog == true) Print("--上げ判定あり:", Ask);
- //前回の「売り」エントリー値より、○pips下がったら
- }else if(Last_Entry_Bid == 0 || Bid <= (Last_Entry_Bid - myEntry_Pips)){
- //下げ判定あり
- flg = OP_SELL;
- //ログ出力
- if(rog == true) Print("--下げ判定あり:", Bid);
- }
- return(flg);
- }
- //+------------------------------------------------------------------+
- // 損切り判定
- // -1:なし、0:OP_BUY、1:OP_SELL
- //+------------------------------------------------------------------+
- int ccc(bool rog = false)
- {
- int flg = -1;
- //損切り判定(pips)
- if( (myLongTotal() > 0) && (myAveragePrice(OP_BUY) - Bid) > myLoss_Cut){
- //L平均値が損切り分下がったら
- flg = OP_BUY;
- if(rog == true) PrintFormat("--L損切り判定。[損切]Bid:"+ (string)Bid +" < L平均:"+ (string)myAveragePrice(OP_BUY) );
- }else if( (myShortTotal() > 0) && (Ask - myAveragePrice(OP_SELL)) > myLoss_Cut){
- //S平均値が損切り分上がったら
- flg = OP_SELL;
- if(rog == true) PrintFormat("--S損切り判定。[損切]Ask:"+ (string)Ask +" > S平均:"+ (string)myAveragePrice(OP_SELL) );
- }
- return flg;
- }
- //+------------------------------------------------------------------+
- //|利益確定チェック
- //|戻り値:-1(デフォルト)全部ポジ、0(OP_BUY)、1(OP_SELL)
- //+------------------------------------------------------------------+
- int eee(bool rog = false)
- {
- double ProfitLong = myOrderProfit(OP_BUY);
- double ProfitShort = myOrderProfit(OP_SELL);
- //現純利益が、(過去純利益+指定価格)を超えたら
- if(myGetMargin() > (myMargin + TRAIL_PRICE)){
- //どっち方向か判定(利益が出ている方で)
- if(ProfitLong > ProfitShort){
- if(gTrailFlg_L == false){
- if(rog == true) Print("--L方向にトレール判定あり:",AccountEquity());
- gTrailFlg_L = true;
- //利益分を計算
- gBase_Price = myGetMargin() - myMargin;
- // //反対ポジションの前回エントリー位置だけを操作する
- // myLast_Entry_Bid = Bid; //最後にエントリーした値を記憶
- }
- }else if(ProfitLong < ProfitShort){
- if(gTrailFlg_S == false){
- if(rog == true) Print("--S方向にトレール判定あり:",AccountEquity());
- gTrailFlg_S = true;
- //利益分を計算
- gBase_Price = myGetMargin() - myMargin;
- // //反対ポジションの前回エントリー位置だけを操作する
- // myLast_Entry_Ask = Ask; //最後にエントリーした値を記憶
- }
- }else{
- //利益方向がわからんから警告する
- Print("[警告]SとLの利益が同じでトレール方向がわかりません。");
- }
- }else{
- return -1;
- }
- //-------------------------
- //トレール対象になる利益
- //gBase_Price
- //
- //現利益の計算
- //(gGetMargin() - gMargin)
- //
- //利益確定する価格
- //(gBase_Price * Trail)
- //-------------------------
- if(gTrailFlg_L){
- //トレール分利益が下がったら
- if( (myGetMargin() - myMargin) <= (gBase_Price * TRAIL_RATE) )
- {
- if(rog == true) Print("利益確定します。:", (myGetMargin() - myMargin));
- //買いポジ決済のフラグを返す
- gTrailFlg_L = false;
- return OP_BUY;
- }else if( (myGetMargin() - myMargin) > gBase_Price){
- //トレールベース値を超えたら更新
- gBase_Price = (myGetMargin() - myMargin);
- }
- }else if(gTrailFlg_S){
- //トレール分利益が下がったら
- if( (myGetMargin() - myMargin) <= (gBase_Price * TRAIL_RATE) )
- {
- if(rog == true) Print("利益確定します。:", (myGetMargin() - myMargin));
- //売りポジ決済のフラグを返す
- gTrailFlg_S = false;
- return OP_SELL;
- }else if( (myGetMargin() - myMargin) > gBase_Price){
- //トレールベース値を超えたら更新
- gBase_Price = (myGetMargin() - myMargin);
- }
- }
- return -1;
- }
- //+------------------------------------------------------------------+
- //|復帰用の処理関数
- //|戻り値:
- //+------------------------------------------------------------------+
- void zzz()
- {
- //復帰なし
- if(gFileName=="") return;
- //復帰あり
- double price = myGetMargin();
- if(FileIsExist(gFileName) == false){
- //ファイルが存在しない場合、現在の有効証拠金をスタートとして記録
- myFileWrite(gFileName, (string)myGetMargin()); //復帰用のログに出力
- Print("復帰用ログファイルを作成");
- }else{
- //前回有効証拠金の取得(なければ現在値)
- string str = myFileReadStringLast(gFileName);
- if(str != ""){
- price = (double)str;
- }
- }
- //有効証拠金の更新
- myMargin = price;
- Print("--[復帰用] 有効証拠金:", myMargin);
- /*
- int x = OrdersTotal();
- if(x < 1) return;
- //前回買いの約定価格を取得
- price = 0;
- for(int i = x-1; i >= 0; i--){
- if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) == true){
- if(OrderType() == OP_BUY){
- price = OrderOpenPrice(); //最後にエントリーした値
- //エントリー枚数計算
- if(myEntry_Lot_Limit > 0){
- if(myEntry_Lot > myEntry_Lot_Limit){
- //減算の場合
- gEntry_Lot_Now_L = OrderLots() - myEntry_Lot_Interval;
- }else if(myEntry_Lot < myEntry_Lot_Limit){
- //増算の場合
- gEntry_Lot_Now_L = OrderLots() + myEntry_Lot_Interval;
- }
- }
- break;
- }
- }
- }
- myLast_Entry_Ask = price; //前回値としてセット
- Print("--[復帰用] 買い:", myLast_Entry_Ask);
- //前回売りの約定価格を取得
- price = 0;
- for(int i = x-1; i >= 0; i--){
- if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) == true){
- if(OrderType() == OP_SELL){
- price = OrderOpenPrice(); //最後にエントリーした値を記憶
- //エントリー枚数計算
- if(myEntry_Lot_Limit > 0){
- if(myEntry_Lot > myEntry_Lot_Limit){
- //減算の場合
- gEntry_Lot_Now_S = OrderLots() - myEntry_Lot_Interval;
- }else if(myEntry_Lot < myEntry_Lot_Limit){
- //増算の場合
- gEntry_Lot_Now_S = OrderLots() + myEntry_Lot_Interval;
- }
- }
- break;
- }
- }
- }
- myLast_Entry_Bid = price; //前回値としてセット
- Print("--[復帰用] 売り:", myLast_Entry_Bid);
- */
- //前回買いの約定価格を取得
- price = GetLastPrice(OP_BUY);
- Print("--[復帰用] 買い:", price);
- //前回売りの約定価格を取得
- price = GetLastPrice(OP_SELL);
- Print("--[復帰用] 売り:", price);
- }
- //+------------------------------------------------------------------+
- //前回のエントリー価格の取得
- //+------------------------------------------------------------------+
- double GetLastPrice(int type)
- {
- double price = 0;
- int x = OrdersTotal();
- if(x < 1) return price;
- //前回買いの約定価格を取得
- price = 0;
- for(int i = x-1; i >= 0; i--){
- if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) == true){
- if(OrderType() == type){
- price = OrderOpenPrice(); //最後にエントリーした値
- break;
- }
- }
- }
- return price;
- }
- //+------------------------------------------------------------------+
- //エントリーロット計算関数
- //+------------------------------------------------------------------+
- double LotCalc(int type = -1)
- {
- double lot = myEntry_Lot;
- int num = 0;
- if(type == OP_BUY){
- num = myLongTotal();
- }else if(type == OP_SELL){
- num = myShortTotal();
- }else{
- //ここにはこない
- }
- //ポジがなければ初期値
- if(num < 1) return lot;
- //ロット計算
- switch(ENTRY_LOT_TYPE){
- case TYPE_ADD:
- //加算
- lot = myEntry_Lot + NormalizeDouble(num * myEntry_Lot_Interval, LOTS_DIGITS);
- break;
- case TYPE_MUL:
- //乗算
- lot = myEntry_Lot * NormalizeDouble(MathPow(2, num), LOTS_DIGITS);
- break;
- default:
- //それ以外は初期値とする
- break;
- }
- //設定できる上限枚数
- if(lot > myEntry_Lot_Limit){
- lot = myEntry_Lot_Limit;
- }
- return lot;
- }
- //+------------------------------------------------------------------+
- //以下、ライブラリーより抜粋
- //+------------------------------------------------------------------+
- //+------------------------------------------------------------------+
- // グローバル変数の初期化
- //+------------------------------------------------------------------+
- void myInit()
- {
- //初期値設定
- //ロット
- if(ENTRY_LOT > 0.01 && ENTRY_LOT_LIMIT < MAX_LOT_SIZE){
- // myEntry_Lot = ENTRY_LOT;
- myEntry_Lot = NormalizeDouble(ENTRY_LOT, LOTS_DIGITS);
- }else{
- myEntry_Lot = 0.01;
- }
- //ロット限界値の設定
- // if(ENTRY_LOT_LIMIT >= 0.01 && ENTRY_LOT_LIMIT != ENTRY_LOT){
- // myEntry_Lot_Limit = ENTRY_LOT_LIMIT;
- if(ENTRY_LOT_LIMIT >= 0.01 && ENTRY_LOT_LIMIT < MAX_LOT_SIZE){
- myEntry_Lot_Limit = NormalizeDouble(ENTRY_LOT_LIMIT, LOTS_DIGITS);
- }else{
- myEntry_Lot_Limit = MAX_LOT_SIZE;
- }
- //ロット増減幅
- // if(ENTRY_LOT_INTERVAL >= 0.01){
- // myEntry_Lot_Interval = ENTRY_LOT_INTERVAL;
- if(ENTRY_LOT_INTERVAL >= 0.01 && ENTRY_LOT_LIMIT < MAX_LOT_SIZE){
- myEntry_Lot_Interval = NormalizeDouble(ENTRY_LOT_INTERVAL, LOTS_DIGITS);
- }else{
- myEntry_Lot_Interval = 0.01;
- }
- //pipsは計算が必要。
- //エントリーpipsの計算
- myEntry_Pips = ENTRY_PIPS * myAdjustPoint(Symbol());
- //損切りpipsの計算
- myLoss_Cut = LOSS_CUT * myAdjustPoint(Symbol());
- //新しいバーか判定用の初期設定
- datetime t[1];
- CopyTime(_Symbol, _Period, 0, 1, t);
- myBarTime = t[0];
- //有効証拠金の更新
- myMargin = myGetMargin();
- //取引しない時間の設定
- if(0 <= STOP_TIME1 && STOP_TIME1 <= 24){
- myStopTime1 = STOP_TIME1;
- }else{
- myStopTime1 = -1; //設定なし
- }
- if(0 <= STOP_TIME2 && STOP_TIME2 <= 24){
- myStopTime2 = STOP_TIME2;
- }else{
- myStopTime2 = -1; //設定なし
- }
- }
- //+------------------------------------------------------------------+
- // 取引可能な時間か判断する関数
- // 引数:ログ出力するか
- // 戻値:true:取引可能、false:取引不可
- //+------------------------------------------------------------------+
- bool myTradeTime(bool rog = false)
- {
- bool ret = true;
- MqlDateTime stm;
- datetime tm = TimeCurrent(stm);
- if(myStopTime1 < 0 || myStopTime2 < 0){
- //-1の場合何もしない
- }else if(myStopTime1 > myStopTime2){
- //日をまたぐ(例:23~3など)
- if(myStopTime1 <= stm.hour || stm.hour < myStopTime2){
- ret = false;
- }
- }else if(myStopTime1 < myStopTime2){
- //日をまたがない(例:6~8など)
- if(myStopTime1 <= stm.hour && stm.hour < myStopTime2){
- ret = false;
- }
- }else{
- //同じ時間(例:6~6など)
- if(myStopTime1 == stm.hour && stm.hour == myStopTime2){
- ret = false;
- }
- }
- //ログ出力
- // if(ret==false && rog==true) Print("現在時刻: " + (string)stm.hour + ":" + (string)stm.min + " は取引停止です");
- if(ret==false && rog==true && stm.min==0 && stm.sec==0) Print("現在時刻: " + TimeToString(tm) + " は取引停止です");
- return ret;
- }
- //+------------------------------------------------------------------+
- //| 買いポジションの発注 |
- //+------------------------------------------------------------------+
- bool myGetLong(double lot, bool rog = false)
- {
- int slippage = SLIP_PAGE;
- int Ticket = OrderSend(Symbol(),OP_BUY,lot,Ask,slippage,0,0,NULL,MAGIC,0,clrBlue);
- if(Ticket==-1){
- //注文失敗
- //エラー処理
- int err = GetLastError();
- switch(err){
- case ERR_MARKET_CLOSED:
- Print("--注文エラー。ERR_MARKET_CLOSED");
- break;
- }
- return false;
- }
- // myLast_Entry_Ask = Ask; //最後にエントリーした値を記憶
- //ログ
- if(rog == true) PrintFormat("--[注文] 買い: %f:%f ロット", Ask, lot);
- return true;
- }
- //+------------------------------------------------------------------+
- //| 売りポジションの発注 |
- //+------------------------------------------------------------------+
- bool myGetShort(double lot, bool rog = false)
- {
- int slippage = SLIP_PAGE;
- int Ticket = OrderSend(Symbol(),OP_SELL,lot,Bid,slippage,0,0,NULL,MAGIC,0,clrRed);
- if(Ticket==-1){
- //注文失敗
- //エラー処理
- int err = GetLastError();
- switch(err){
- case ERR_MARKET_CLOSED:
- Print("--注文エラー。ERR_MARKET_CLOSED");
- break;
- }
- return false;
- }
- // myLast_Entry_Bid = Bid; //最後にエントリーした値を記憶
- //ログ
- if(rog == true) PrintFormat("--[注文] 売り: %f:%f ロット", Bid, lot);
- return true;
- }
- //+------------------------------------------------------------------+
- //| 買いポジションだけを全て決済 |
- //+------------------------------------------------------------------+
- bool myCloseLong()
- {
- int slippage = SLIP_PAGE;
- bool Clsed = false;
- // PrintFormat("--買いポジションを全て決済します--");
- for(int i = OrdersTotal() - 1; i >= 0; i--){
- if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) == true){
- if(OrderType() == OP_BUY){
- Clsed = OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),slippage,clrBlue);
- }
- if(Clsed==false){
- //エラー処理
- int err = GetLastError();
- switch(err){
- case ERR_MARKET_CLOSED:
- Print("--決済エラー。ERR_MARKET_CLOSED");
- break;
- }
- }
- }
- }
- // myLast_Entry_Ask = 0; //最後にエントリーした値を記憶
- // myMargin = myGetMargin(); //有効証拠金の更新
- // PrintFormat("----------------------------------");
- return true;
- }
- //+------------------------------------------------------------------+
- //| 売りポジションだけを全て決済 |
- //+------------------------------------------------------------------+
- bool myCloseShort()
- {
- int slippage = SLIP_PAGE;
- bool Clsed = false;
- // PrintFormat("--売りポジションを全て決済します--");
- for(int i = OrdersTotal() - 1; i >= 0; i--){
- if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) == true){
- if(OrderType() == OP_SELL){
- Clsed = OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),slippage,clrRed);
- }
- if(Clsed==false){
- //エラー処理
- int err = GetLastError();
- switch(err){
- case ERR_MARKET_CLOSED:
- Print("--決済エラー。ERR_MARKET_CLOSED");
- break;
- }
- }
- }
- }
- // myLast_Entry_Bid = 0; //最後にエントリーした値を記憶
- // myMargin = myGetMargin(); //有効証拠金の更新
- // PrintFormat("----------------------------------");
- return true;
- }
- //+------------------------------------------------------------------+
- //| 全てのポジションを決済 |
- //+------------------------------------------------------------------+
- bool myOderClose()
- {
- int slippage = SLIP_PAGE;
- bool Clsed = false;
- PrintFormat("--ポジションを全て決済します----");
- for(int i = OrdersTotal() - 1; i >= 0; i--){
- if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) == true){
- if(OrderType() == OP_BUY){
- Clsed = OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),slippage,clrBlue);
- }else if(OrderType() == OP_SELL){
- Clsed = OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),slippage,clrRed);
- }
- if(Clsed==false){
- //注文エラー処理
- int err = GetLastError();
- switch(err){
- case ERR_MARKET_CLOSED:
- Print("--決済エラー。ERR_MARKET_CLOSED");
- break;
- }
- }
- }
- }
- // myLast_Entry_Ask = 0; //最後にエントリーした値を記憶
- // myLast_Entry_Bid = 0; //最後にエントリーした値を記憶
- // myMargin = myGetMargin(); //有効証拠金の更新
- // PrintFormat("----------------------------------");
- return true;
- }
- //+------------------------------------------------------------------+
- //| 買いポジションの数を返す |
- //+------------------------------------------------------------------+
- int myLongTotal()
- {
- int cnt = 0;
- for(int i = OrdersTotal() - 1; i >= 0; i--){
- if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) == true){
- if(OrderType() == OP_BUY){
- cnt++;
- }
- }
- }
- return cnt;
- }
- //+------------------------------------------------------------------+
- //| 売りポジションの数を返す |
- //+------------------------------------------------------------------+
- int myShortTotal()
- {
- int cnt = 0;
- for(int i = OrdersTotal() - 1; i >= 0; i--){
- if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) == true){
- if(OrderType() == OP_SELL){
- cnt++;
- }
- }
- }
- return cnt;
- }
- //+------------------------------------------------------------------+
- //|通貨別ポイントの倍率を返す
- //|引数:Symbol()などで指定
- //+------------------------------------------------------------------+
- double myAdjustPoint(string currency)
- {
- double point = 0.0;
- int symbol_digit = (int)MarketInfo(currency,MODE_DIGITS);
- if(symbol_digit == 2 || symbol_digit == 3){
- point = 0.01;
- }else if(symbol_digit == 4 || symbol_digit == 5){
- point = 0.0001;
- }
- return point;
- }
- //+------------------------------------------------------------------+
- //|現在の有効証拠金を取得
- //+------------------------------------------------------------------+
- double myGetMargin(bool rog = false){
- double d = 0;
- //有効証拠金 = 残高+保有ポジションの評価損益+保有ポジションのスワップ損益
- //純資産で良い
- d = AccountEquity(); //純資産
- //ログ出力
- if(rog == true) Print("純資産 = ",AccountEquity() );
- return d;
- }
- //+------------------------------------------------------------------+
- //|平均約定価格の取得
- //|flg= -1(デフォルト)全部ポジ、0(OP_BUY)、1(OP_SELL)
- //+------------------------------------------------------------------+
- double myAveragePrice(int flg = -1)
- {
- double sumPrice_x_Lot = 0.0;
- double sumLot = 0.0;
- int x = OrdersTotal();
- if(x < 1) return 0.0;
- for(int i = x-1; i >= 0; i--){
- if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) == true){
- if(OrderType() == OP_BUY && (flg==OP_BUY || flg==-1)){
- //買いポジションの場合
- // sumPrice_x_Lot += NormalizeDouble(OrderOpenPrice(), Digits) * OrderLots();
- sumPrice_x_Lot += OrderOpenPrice() * OrderLots();
- sumLot += OrderLots();
- }
- if(OrderType() == OP_SELL && (flg==OP_SELL || flg==-1)){
- //売りポジションの場合
- // sumPrice_x_Lot += NormalizeDouble(OrderOpenPrice(), Digits) * OrderLots();
- sumPrice_x_Lot += OrderOpenPrice() * OrderLots();
- sumLot += OrderLots();
- }
- }
- }
- // ゼロ割り防止
- if(sumLot == 0.0){
- return(0.0);
- }
- return (NormalizeDouble(sumPrice_x_Lot / sumLot, Digits));
- }
- //+------------------------------------------------------------------+
- //|ポジションの損益の取得
- //|flg= -1(デフォルト)全部ポジ、0(OP_BUY)、1(OP_SELL)
- //+------------------------------------------------------------------+
- double myOrderProfit(int flg = -1)
- {
- double profit = 0.0;
- for(int i = OrdersTotal() - 1; i >= 0; i--){
- if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) == true){
- if(OrderType() == OP_BUY && (flg==OP_BUY || flg==-1)){
- //買いポジションの場合
- profit += OrderProfit();
- }
- if(OrderType() == OP_SELL && (flg==OP_SELL || flg==-1)){
- //売りポジションの場合
- profit += OrderProfit();
- }
- }
- }
- return profit;
- }
- //+------------------------------------------------------------------+
- // ファイルに書き出し
- // 引数:ファイル名、書き出す文字1、2
- // 戻値:true:成功、false:失敗
- //+------------------------------------------------------------------+
- bool myFileWrite(string filename, string str1, string str2 = "")
- {
- bool ret = false;
- if(filename == ""){
- //ファイル名なしならファイル出力無し
- return true;
- }
- MqlDateTime stm;
- datetime tm = TimeCurrent(stm);
- if(FileIsExist(filename) == false){
- //
- Print("ファイルがありません!");
- }
- int handle = FileOpen(filename,FILE_READ|FILE_WRITE|FILE_CSV,",");
- if(handle > 0){
- FileSeek(handle, 0, SEEK_END);
- if(str1 != "" && str2 != ""){
- FileWrite(handle,TimeCurrent(), str1, str2);
- }else if(str1 != ""){
- FileWrite(handle,TimeCurrent(), str1);
- }else{
- FileWrite(handle,TimeCurrent(), -1);
- }
- FileClose(handle);
- ret = true;
- }
- return ret;
- }
- //+------------------------------------------------------------------+
- // ファイルから最後の値を読み込み
- // 引数:ファイル名
- // 戻値:
- //+------------------------------------------------------------------+
- string myFileReadStringLast(string filename)
- {
- string ret = "";
- if(filename == ""){
- //復帰なしなら""で返す
- return ret;
- }
- if(FileIsExist(filename) == false){
- //ファイルがない
- return ret;
- }
- int handle = FileOpen(filename,FILE_READ|FILE_CSV,",");
- if(handle > 0){
- FileSeek(handle, 0, SEEK_SET);
- //最後に必要情報があるからそれを返す
- string str;
- while(true){
- str = FileReadString(handle);
- if(FileIsEnding(handle)) break;
- }
- FileClose(handle);
- ret = str;
- }
- return ret;
- }
- //+------------------------------------------------------------------+
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement