コードに関するヘルプが必要です - ページ 4

 
diamstar:
EAを添付した後、右上にスマイリーではなくxがついたd e aの名前を見ました。 時刻はgmtで入力しました。


diamstar さん、こんにちは。

まず最初に、ここで入手したコードは学習用であることを理解する必要があります。

つまり、テストや修正、コードの使用はご自身の責任で行ってください。

あなたの問題に戻って、あなたがスマイリーフェイスを取得しない場合は、エキスパートアドバイザーズボタンを確認して ください。

赤くなっていたら、それを押してください。

また、MT4の設定を確認する必要があるかもしれません。

ツールからオプションを選択します(一番下のやつ)。

もし、このように表示されない場合は:

のようにします。

これでうまくいくはずです。EAを停止したい場合は、Expert Adviserボタンを押すだけで良いことを覚えておいてください。

バックテストはもうしましたか?

時刻はサーバータイムであるべきです。

 
ありがとうございます。スマイリーが表示されるようになりました。今週テストした後、更新します。改めてありがとうございました。
 
diamstar:
ありがとうございます。今、スマイリーが表示されています。今週テストした後、更新します。改めてありがとうございました。


ストラテジーテスターで バックテストを実行する方法、ビジュアルかどうか、最適化機能はご存知ですか?

それとも、デモでテストするのが好きですか?ストラテジーテスターを使えば、より早く、より簡単に最適な設定を見つけることができるかもしれません。

幸運を祈ります。

 
スマイリーが表示されていますが、注文はまだ有効になっていません。ストラテジーテスターの ジャーナルを確認したところ、ordersend error 130が表示されています。
 
diamstar :
スマイリーが表示されていますが、注文はまだ有効になっていません。ストラテジーテスターのジャーナルを確認しましたが、ordersendエラー130が表示されています


使用している設定とチャートの時間枠を投稿していただけますか?

設定の説明が必要な場合は、問題ありません。

これも少し改善されたバージョンで、これもいつものように学習するためのものです。

 //+------------------------------------------------------------------+
//|                                               News_Trader_v1.mq4 |
//|                                            Copyright © 2013 _3DE |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2013 _3DE"
#property link        "http://www.metaquotes.net"
extern string   Note1       = "Set parameters in Pips not points !" ;
extern string   Note5       = "Set PeriodSignal in minutes!" ;
extern string   Note6       = "1=M1;5=M5;15=M15;30=M30;60=H1;240=H4;1440=D1!" ;
extern int      PeriodForSignal= 15 ;
extern int      TakeProfit  = 25 ; // Take profit pips
extern int      StopLoss    = 0 ; // Stop loss pips (manual trading)
extern string   Note4       = "Leave SetDistance to zero if trading news !" ;
extern int      SetDistance= 10 ; // Distance for BuyStop and SellStop from price at news time
extern string   Note2       = "Set day of the month for the news !" ;
extern string   Note3       = "Set to zero to trade every day at the same time !" ;
extern int      DayOfNews   = 0 ; // Day of the month of news
extern int      NewsHour    = 0 ; // Hour of news
extern int      NewsMin     = 1 ; // Minute of news
extern int      Expiration= 600 ; // Expiration of pending orderes
extern int      BEPips      = 0 ; // Move to break even after BEPips
extern int      TrailingStop= 0 ; // What distance to keep trailing
extern int      Slip        = 5 ; // Slippage
extern int      MagicNumber= 2210 ; // Must be unique for every chart
extern double   Lots= 0.1 ;
extern bool     WriteLog= false ; // Write a log file 
extern string   TradeLog    = "MI_Log" ;
input    string   EaComment   = "NewsTrader_EA" ;

double high_M1,low_M1,openPriceBuyStop,openPriceSellStop,slOpenBuyStop,slSellStop,tpOpenBuyStop,tpSellStop,spread,price;

string filename;
int pointMultiply= 10 ;
double minDist= 0 ;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   if (SetDistance== 0 )pointMultiply= 10 ;
   else {pointMultiply=SetDistance;}
   if ( Digits == 3 || Digits == 5 )
     {
      pointMultiply  *= 10 ;
      TakeProfit     *= 10 ;
      StopLoss       *= 10 ;
      BEPips         *= 10 ;
      TrailingStop   *= 10 ;
      SetDistance    *= 10 ;
     }
   minDist= MarketInfo ( NULL , MODE_STOPLEVEL )* Point ;
//----
   return ( 0 );
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----

//----
   return ( 0 );
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
   int i;
   int OrdersCondition,minofday,minofnews;

   filename= Symbol ()+TradeLog+ "-" + Month ()+ "-" + Day ()+ ".txt" ;

   if (BEPips> 0 ) DoBE(BEPips);

   if (TrailingStop> 0 ) DoTrail();

   OrdersCondition=CheckOrdersCondition();
   if ( Day ()==DayOfNews || DayOfNews== 0 )
     {
      minofday= Hour ()* 60 + Minute ();
      minofnews=NewsHour* 60 +NewsMin;

       if ((minofday==minofnews- 2 ) || (minofday==minofnews- 1 ))
        {
         high_M1= iHigh ( NULL ,PeriodForSignal, 0 );
         low_M1= iLow ( NULL ,PeriodForSignal, 0 );
         //--- Get the highest high and lowest low for the last 3 bars on 1 minute

         for (i= 1 ;i<= 3 ;i++) if ( iHigh ( NULL ,PeriodForSignal,i)>high_M1) high_M1= iHigh ( NULL ,PeriodForSignal,i);
         for (i= 1 ;i<= 3 ;i++) if ( iLow ( NULL ,PeriodForSignal,i)<low_M1) low_M1= iLow ( NULL ,PeriodForSignal,i);

         spread= Ask - Bid ;
         openPriceBuyStop= NormalizeDouble ((high_M1+spread+(pointMultiply* Point )), Digits );
         slOpenBuyStop= NormalizeDouble (high_M1, Digits );
         tpOpenBuyStop= NormalizeDouble ((openPriceBuyStop+(TakeProfit* Point )+spread), Digits );
         if ((openPriceBuyStop-slOpenBuyStop)<minDist)
           {
            slOpenBuyStop= NormalizeDouble (openPriceBuyStop-minDist-spread, Digits );
           }
         //---
         openPriceSellStop= NormalizeDouble (low_M1-(pointMultiply* Point ), Digits );
         slSellStop= NormalizeDouble (low_M1, Digits );
         tpSellStop= NormalizeDouble ((openPriceSellStop-(TakeProfit* Point )-spread), Digits );
         if ((slSellStop-openPriceSellStop)<minDist)
           {
            slSellStop= NormalizeDouble (openPriceSellStop+minDist+spread, Digits );
           }
         //---
         if (StopLoss> 0 &&SetDistance> 0 )
           {
            price=( Ask + Bid )/ 2 ;
            high_M1=price+(SetDistance* Point );
            low_M1=price-(SetDistance* Point );
            openPriceBuyStop= NormalizeDouble (high_M1+spread, Digits );
            slOpenBuyStop= NormalizeDouble (openPriceBuyStop-(StopLoss* Point ), Digits );
            tpOpenBuyStop= NormalizeDouble (openPriceBuyStop+(TakeProfit* Point ), Digits );

             if ((openPriceBuyStop-slOpenBuyStop)<minDist)
              {
               slOpenBuyStop= NormalizeDouble (openPriceBuyStop-minDist-(StopLoss* Point ), Digits );
               Alert ( "Stop too close ! Check your StopLoss settitngs !!!" );
              }
            openPriceSellStop= NormalizeDouble (low_M1-spread, Digits );
            slSellStop= NormalizeDouble (openPriceSellStop+(StopLoss* Point ), Digits );

             if ((slSellStop-openPriceSellStop)<minDist)
              {
               slSellStop= NormalizeDouble (openPriceSellStop+minDist+(StopLoss* Point ), Digits );
               Alert ( "Stop too close ! Check your StopLoss settitngs !!!" );
              }
            tpSellStop= NormalizeDouble (openPriceSellStop-(TakeProfit* Point ), Digits );
           }
         if (OrdersCondition== 0 )
           {
             if (WriteLog)Write( "Opening BuyStop & SellStop, OrdersCondition=" +OrdersCondition+ " MinOfDay=" +minofday);
            OpenBuyStop();
            OpenSellStop();
           }

         if (OrdersCondition== 10 )
           {
             if (WriteLog)Write( "Opening SellStop, OrdersCondition=" +OrdersCondition+ " MinOfDay=" +minofday);
            OpenSellStop();
           }

         if (OrdersCondition== 1 )
           {
             if (WriteLog)Write( "Opening BuyStop , OrdersCondition=" +OrdersCondition+ " MinOfDay=" +minofday);
            OpenBuyStop();
           }
        }
     }
   if ((minofday>=minofnews) && (minofday<=minofnews+Expiration- 1 ))
     {

       if (OrdersCondition== 1001 )
        {
         if (WriteLog)Write( "Deleting SellStop Because of BuyStop Hit, OrdersCondition=" +OrdersCondition+ " MinOfDay=" +minofday);
         DeleteSellStop();
        }

       if (OrdersCondition== 110 )
        {
         if (WriteLog)Write( "Deleting BuyStop Because of SellStop Hit, OrdersCondition=" +OrdersCondition+ " MinOfDay=" +minofday);
         DeleteBuyStop();
        }
     }

   if (minofday>=minofnews+Expiration)
     {
       if (OrdersCondition== 11 )
        {
         if (WriteLog)Write( "Deleting BuyStop and SellStop Because 5 min expired, OrdersCondition=" +OrdersCondition+ " MinOfDay=" +minofday);
         DeleteBuyStop();
         DeleteSellStop();
        }

       if ((OrdersCondition== 10 ) || (OrdersCondition== 110 ))
        {
         if (WriteLog)Write( "Deleting BuyStop Because 5 min expired, OrdersCondition=" +OrdersCondition+ " MinOfDay=" +minofday);
         DeleteBuyStop();
        }

       if ((OrdersCondition== 1 ) || (OrdersCondition== 1001 ))
        {
         if (WriteLog)Write( "Deleting SellStop Because 5 min expired, OrdersCondition=" +OrdersCondition+ " MinOfDay=" +minofday);
         DeleteSellStop();
        }
     }

//----
   return ( 0 );
  }
//+------------------------------------------------------------------+
//| int CheckOrdersCondition()                                       |
//+------------------------------------------------------------------+

int CheckOrdersCondition()
  {
   int result= 0 ;
   for ( int i= 0 ;i< OrdersTotal ();i++)
     {
       OrderSelect (i, SELECT_BY_POS , MODE_TRADES );
       if (( OrderType ()== OP_BUY ) && ( OrderSymbol ()== Symbol ()) && ( OrderMagicNumber ()==MagicNumber))
        {
         result+= 1000 ;
        }
       if (( OrderType ()== OP_SELL ) && ( OrderSymbol ()== Symbol ()) && ( OrderMagicNumber ()==MagicNumber))
        {
         result+= 100 ;
        }
       if (( OrderType ()== OP_BUYSTOP ) && ( OrderSymbol ()== Symbol ()) && ( OrderMagicNumber ()==MagicNumber))
        {
         result+= 10 ;
        }
       if (( OrderType ()== OP_SELLSTOP ) && ( OrderSymbol ()== Symbol ()) && ( OrderMagicNumber ()==MagicNumber))
        {
         result+= 1 ;
        }

     }
   return (result); // 0 means we have no trades
  }
// OrdersCondition Result Pattern
//    1    1    1    1
//    b    s    bs   ss
//  
//+------------------------------------------------------------------+
//|void OpenBuyStop()                                                |
//+------------------------------------------------------------------+

void OpenBuyStop()
  {
   int ticket,tries;
   tries= 0 ;
   if (! GlobalVariableCheck ( "InTrade" ))
     {
       while (tries< 3 )
        {
         GlobalVariableSet ( "InTrade" , TimeCurrent ());   // set lock indicator
         ticket= OrderSend ( Symbol (), OP_BUYSTOP ,Lots,openPriceBuyStop,Slip,slOpenBuyStop,tpOpenBuyStop,EaComment,MagicNumber, 0 ,Red);
         if (WriteLog)Write( "in function OpenBuyStop OrderSend Executed , ticket =" +ticket);
         GlobalVariableDel ( "InTrade" );   // clear lock indicator
         if (ticket<= 0 )
           {
            tries++;
           }
         else tries= 3 ;
        }
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OpenSellStop()
  {
   int ticket,tries;
   tries= 0 ;
   if (! GlobalVariableCheck ( "InTrade" ))
     {
       while (tries< 3 )
        {
         GlobalVariableSet ( "InTrade" , TimeCurrent ());   // set lock indicator
         ticket= OrderSend ( Symbol (), OP_SELLSTOP ,Lots,openPriceSellStop,Slip,slSellStop,tpSellStop,EaComment,MagicNumber, 0 ,Red);
         if (WriteLog)Write( "in function OpenSellStop OrderSend Executed , ticket =" +ticket);
         GlobalVariableDel ( "InTrade" );   // clear lock indicator
         if (ticket<= 0 )
           {
            tries++;
           }
         else tries= 3 ;
        }
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void DoBE( int byPips)
  {
   for ( int i= 0 ; i< OrdersTotal (); i++)
     {
       OrderSelect (i, SELECT_BY_POS , MODE_TRADES );
       if ( OrderSymbol ()== Symbol () && ( OrderMagicNumber ()==MagicNumber)) // only look if mygrid and symbol...
        {
         if ( OrderType ()== OP_BUY ) if ( Bid - OrderOpenPrice ()>byPips* Point ) if ( OrderStopLoss ()< OrderOpenPrice ())
           {
             if (WriteLog)Write( "Movine StopLoss of Buy Order to BE+1" );
             OrderModify ( OrderTicket (), OrderOpenPrice (), OrderOpenPrice ()+ Point , OrderTakeProfit (),Red);
           }
         if ( OrderType ()== OP_SELL ) if ( OrderOpenPrice ()- Ask >byPips* Point ) if ( OrderStopLoss ()> OrderOpenPrice ())
           {
             if (WriteLog)Write( "Movine StopLoss of Buy Order to BE+1" );
             OrderModify ( OrderTicket (), OrderOpenPrice (), OrderOpenPrice ()- Point , OrderTakeProfit (),Red);
           }
        }
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void DoTrail()
  {
   for ( int i= 0 ; i< OrdersTotal (); i++)
     {
       OrderSelect (i, SELECT_BY_POS , MODE_TRADES );
       if ( OrderSymbol ()== Symbol () && ( OrderMagicNumber ()==MagicNumber)) // only look if mygrid and symbol...
        {

         if ( OrderType ()== OP_BUY )
           {
             if ( Bid - OrderOpenPrice ()> Point *TrailingStop)
              {
               if ( OrderStopLoss ()< Bid - Point *TrailingStop)
                 {
                   OrderModify ( OrderTicket (), OrderOpenPrice (), Bid - Point *TrailingStop, OrderTakeProfit (), 0 ,Green);
                   return ;
                 }
              }
           }

         if ( OrderType ()== OP_SELL )
           {
             if (( OrderOpenPrice ()- Ask )>( Point *TrailingStop))
              {
               if (( OrderStopLoss ()>( Ask + Point *TrailingStop)) || ( OrderStopLoss ()== 0 ))
                 {
                   OrderModify ( OrderTicket (), OrderOpenPrice (), Ask + Point *TrailingStop, OrderTakeProfit (), 0 ,Red);
                   return ;
                 }
              }
           }
        }
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void DeleteBuyStop()
  {
   for ( int i= 0 ; i< OrdersTotal (); i++)
     {
       OrderSelect (i, SELECT_BY_POS , MODE_TRADES );
       if ( OrderSymbol ()== Symbol () && ( OrderMagicNumber ()==MagicNumber) && ( OrderType ()== OP_BUYSTOP ))
        {
         OrderDelete ( OrderTicket ());
         if (WriteLog)Write( "in function DeleteBuyStopOrderDelete Executed" );
        }

     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void DeleteSellStop()
  {
   for ( int i= 0 ; i< OrdersTotal (); i++)
     {
       OrderSelect (i, SELECT_BY_POS , MODE_TRADES );
       if ( OrderSymbol ()== Symbol () && ( OrderMagicNumber ()==MagicNumber) && ( OrderType ()== OP_SELLSTOP ))
        {
         OrderDelete ( OrderTicket ());
         if (WriteLog)Write( "in function DeleteSellStopOrderDelete Executed" );
        }

     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Write( string str)
  {
   int handle;

   handle= FileOpen (filename, FILE_READ | FILE_WRITE | FILE_CSV , "/t" );
   FileSeek (handle, 0 , SEEK_END );
   FileWrite(handle,str+ " Time " + TimeToStr ( CurTime (), TIME_DATE | TIME_SECONDS ));
   FileClose (handle);
  }
//+------------------------------------------------------------------+

以前は1分の時間枠の信号しか取得していなかったので、PeriodForSgnalパラメーターを追加しました。これで、ニュースを取引する場合は、5、15、60分に設定できます。

StopLoss = 0の場合、ストップロスが計算されます。 SetDistance = 0の場合、 保留中の注文の距離が計算されます。

 
うまくいきました!!!今日はバックテストをすることができました、入力の一つを間違えてしまいました。まだ、デモ 口座でテストしてみようと思います。100万回ありがとうございました。
 
diamstar:
うまくいきました!!!今日はバックテストを実行することができました、入力の一つを間違えてしまいました。まだデモ口座でいくつかテストしてみます。100万回ありがとうございました。


どういたしまして。

あなたが学んでいることをうれしく思います。

また、何か質問があれば聞いてください。

 
こんにちは、E Aはバックテストでは 正常に動作していますが、デモでは動作していません。ネットで見た記事では、order sendとorder modifyは別々に送らなければならないようなことが書かれていました。私は本当にこれを理解していない。ありがとうございます。
 
diamstar:
こんにちは、EAはバックテストでは正常に動作していますが、デモでは動作していません。ネットで見た記事では、order sendとorder modifyは別々に送らなければならないようなことが書かれています。私は本当にこれを理解していない。ありがとうございます。


diamstar さん、こんにちは。

あなたが学んでいることを聞いてうれしいです。さて、本当にライブ口座ではなく、デモ口座の話をしているのでしょうか?

ブローカーによっては、EAがライブ口座でストップロスやテイクプロフィットを 使った注文を出すことを許可していない場合があることは承知しています。

EAを修正して、損切りと利益確定をゼロにして注文を出し、別の機能で損切りと利益確定を修正することです。

しかし、これはデモではなくライブ口座での話です。

もしデモでそのようなことが起こった場合は、Terminal ボタンを押して Experts タブと Journal タブをチェックし、そこに何かエラーが表示されるかどうかを確認する必要があります。

もし、エラーメッセージが表示されたなら、その内容を教えてください。

何をしようとしているのか、何が間違っているのか、このような些細なことから推測するのは難しいのです。どのように使っているのか、ストップは何なのか、テイクプロフィットは何なのか、などなど教えてください。

このEAは、適切なパラメータと注文の適切なタイミングで良い結果が得られる可能性を示しているので、私はこのEAの改良に取り組んでいるところです。

あなたが興味を持っている限り、私はあなたに更新を維持します。

 
ありがとうございます。入力値を変えたらデモで完璧に動いたくらいです。そしてもちろん、私はもっと学びたいと思っています。