私は最近Central Banks and Big Playersというスレッドに興味をそそられました。基本的な戦略は、ロンドンとニューヨークのオープンで最初の2ピップの動きの方向に売買することです。スレッドの作成者は非常に小さいSL(2ピップ)、TP 140を使用しています。この戦略のために開発されたEA(下記のコードを参照)は、1つのことを除けばうまく機能します。それは価格があなたの有利に動くのでそれがであるようにそれはあなたのストップをBEに近づくまでピップずつ移動させます。私が抱えている問題は、EAがBEに立ち止まったとしても、私はまだ私がその取引で支払わなければならない手数料を失うことです。 EAに手数料を支払うためにBE (x)のピップ数までストップさせるために必要な変更を下のコード(太字)で調整できますか。ありがとうございました// --------------------------------------------- ---------------------
//| CBBP_EA_v2-0.mq4 |
//|ゼンロー
//| |
// ----------------------------------------------- -------------------
#property copyright Zen Leow
#property link
#include lt; stdlib.mqhgt;
#include lt; stderror.mqhgt;
#define PRIMARY_ORDER 1
#define SECONDARY_ORDER 2
extern int EA_MAGIC_NUM = 3947947。
extern bool IsECN = true;
extern bool CheckHourOfAttach = true;
外部文字列Session1 =ロンドンセッション。
extern int SessionOpenHour1 = 8。
外部文字列Session2 =ニューヨークセッション。
extern int SessionOpenHour2 = 13。
extern int GraceMinutes = 5。
extern bool Show_Comments = true;
extern double UserDefinedSpread = 0;
extern int Slippage = 3。
外部ダブルPip_Distance = 2。
extern int TakeProfit = 140;
extern int StopLoss = 5。
extern bool UseJumpingStop = false;
extern int TrailingStop = 0;
extern bool Add_Spread_To_StopLoss = false;
extern bool Use_Spread_Filtering = true;
extern double Spread_Filter = 1.0;
extern bool MoneyManagement = true;
extern double RiskPercent = 0.25。
extern bool UseEquity = false;
extern bool Use_BE = true;
extern double BreakEven_Pips = 5.0。
extern double FixedLots = 0.1;
extern double MaxLots = 15.0。
外部ダブル最小ロット= 0.01。
extern int LotsDecimalAllowed = 2;
string msg =;
double CustomSpread = 0。
int PipFactor = 1;
bool StartupStatusOK = true;
int the_StopLoss = 0;
bool TradeTheSession = true;
bool ProblemLogged = false;
int todayOfYear = 0;
bool SessionIsOn = false;
theSessionOpenPriceを2倍にします。
ストリングlatestSession。
int currentSessionHour;
bool SessionOpenPriceWritten = false;
// ----------------------------------------------- -------------------
//|エキスパート初期化機能|
// ----------------------------------------------- -------------------
int init()
{
//----
int currentHour = TimeHour(TimeCurrent());
if((currentHour == SessionOpenHour1 || currentHour == SessionOpenHour2)CheckHourOfAttach)
{
警告(IMPT: セッション1 営業時間中または セッション2 営業時間中は、このEAを添付しないでください)。
StartupStatusOK = false;
}
if(期間()gt; PERIOD_H1)
{
アラート(このEAは1時間未満の時間枠でチャートにのみ添付できます)。
StartupStatusOK = false;
}
if(MinLots lt; MarketInfo(Symbol()、MODE_MINLOT))
{
アラート(無効なMinLots: MinLots |試す: MarketInfo(Symbol()、MODE_MINLOT));
StartupStatusOK = false;
}
if(MaxLots gt; MarketInfo(Symbol()、MODE_MAXLOT))
{
アラート(無効なMaxLots: MaxLots |試す: MarketInfo(Symbol()、MODE_MAXLOT));
StartupStatusOK = false;
}
if(SessionOpenHour1 lt; 0 || SessionOpenHour1 gt; 23)
{
アラート( Session1 に対して無効な開始時間が与えられています: SessionOpenHour1);
StartupStatusOK = false;
}
if(SessionOpenHour2 lt; 0 || SessionOpenHour2 gt; 23)
{
アラート( Session2 に対して無効な開始時間が与えられました: SessionOpenHour2)。
StartupStatusOK = false;
}
if(MoneyManagement(RiskPercent lt; = 0 || RiskPercent gt; 100))
{
警告(無効なRiskPercent: RiskPercent %)。
StartupStatusOK = false;
}
if(StartupStatusOK)
{
警告(CBBP EAが Symbol() に有効な入力でロードされている)。
}
WriteToLogFile(CBBP EAの開始)。
GetSpread();
todayOfYear = DayOfYear();
//フラクショナルピップを満たす
if(数字== 3 ||数字== 5)
{
PipFactor = 10。
}
Slippage = Slippage * PipFactor;
if(Show_Comments)
{
WriteComment();
}
//----
(0)を返します。
}
// ----------------------------------------------- -------------------
//|エキスパート非初期化関数
// ----------------------------------------------- -------------------
int deinit()
{
//----
//----
(0)を返します。
}
// ----------------------------------------------- -------------------
//|取引管理ロジック関数
// ----------------------------------------------- -------------------
bool TradeNotPlacedYet()
{
int total = OrdersTotal();
if(合計> 0)
{
for(int cnt = 0; cntlt; total; cnt )
{
if(OrderSelect(cnt、SELECT_BY_POS))
{
if(OrderSymbol()== Symbol()OrderMagicNumber()== EA_MAGIC_NUM)
{
SessionIsOn = false。
戻り値(false)。
}
}
}
}
//このセッションですでに取引が開始および終了している場合
int histotal = OrdersHistoryTotal();
if(histotal gt; 0)
{
(cnt = 0; cntlt; histotal; cnt )の場合
{
if(OrderSelect(cnt、SELECT_BY_POS、MODE_HISTORY))
{
if(OrderSymbol()== Symbol()OrderMagicNumber()== EA_MAGIC_NUM)
{
datetime LatestSessionStart = iTime(NULL、PERIOD_H1,0);
datetime LatestSessionGrace = LatestSessionStart (GraceMinutes * 60);
datetime CurrentOrderOpenTime = OrderOpenTime();
if(CurrentOrderOpenTime gt; = LatestSessionStart CurrentOrderOpenTime lt; = LatestSessionGrace)
{
SessionIsOn = false。
戻り値(false)。
}
}
}
}
}
戻り値(true)。
}
bool Should_Buy(int SessionOpenIndex)
{
if(SessionOpenIndex == -1)
{
戻り値(false)。
}
theSessionOpenPrice = Open [SessionOpenIndex];
double theSessionCurrentPrice = Close [0];
if(theSessionCurrentPrice gt; theSessionOpenPrice)
{
double lookoutPrice = theSessionOpenPrice (Pip_Distance * Point * PipFactor);
if(theSessionCurrentPrice gt; = lookoutPrice)
{
if(Spread_Still_In_Range())
{
戻り値(true)。
}
それ以外の
{
if(!ProblemLogged)
{
string logmsg =価格がこのセッションオープン価格から DoubleToStr(Pip_Distance、1) 移動したときのスプレッドは DoubleToStr(CustomSpread、1) です。このセッションでは取引はありません。
WriteToLogFile(logmsg);
ProblemLogged = true;
TradeTheSession = false;
}
}
}
}
戻り値(false)。
}
bool Should_Sell(int SessionOpenIndex)
{
if(SessionOpenIndex == -1)
{
戻り値(false)。
}
theSessionOpenPrice = Open [SessionOpenIndex];
double theSessionCurrentPrice = Close [0];
if(theSessionCurrentPrice lt; theSessionOpenPrice)
{
double lookoutPrice = theSessionOpenPrice - (Pip_Distance * Point * PipFactor);
if(theSessionCurrentPrice lt; = lookoutPrice)
{
if(Spread_Still_In_Range())
{
戻り値(true)。
}
それ以外の
{
if(!ProblemLogged)
{
string logmsg =価格がこのセッションオープン価格から DoubleToStr(Pip_Distance、1) 移動したときのスプレッドは DoubleToStr(CustomSpread、1) です。このセッションでは取引はありません。
WriteToLogFile(logmsg);
ProblemLogged = true;
TradeTheSession = false;
}
}
}
}
戻り値(false)。
}
double PositionSizeToOpen(int StopLossPips)
{
Double PositionSize。
ダブルリスクドル。
if(MoneyManagement StopLossPips gt; 0)
{
if(UseEquity)
{
riskDollars =(AccountEquity()/100)* RiskPercent;
}
それ以外の
{
riskDollars =(AccountBalance()/100)* RiskPercent;
}
PositionSize =(riskDollarsStopLossPips)/(MarketInfo(Symbol()、MODE_TICKVALUE)* PipFactor);
}
if(MoneyManagement StopLossPips lt; = 0)
{
if(UseEquity)
{
PositionSize =((AccountEquity()/100)* RiskPercent)/(MarketInfo(Symbol()、MODE_LOTSIZE)/AccountLeverage());
}
それ以外の
{
PositionSize =((AccountBalance()/100)* RiskPercent)/(MarketInfo(Symbol()、MODE_LOTSIZE)/AccountLeverage());
}
}
if(!MoneyManagement)
{
PositionSize = FixedLots;
}
if(PositionSize lt; MinLots)
{
PositionSize = MinLots;
}
if(PositionSize gt; MaxLots)
{
PositionSize = MaxLots;
}
PositionSize = NormalizeDouble(PositionSize、LotsDecimalAllowed);
return(PositionSize);
}
ブールSendOrders(int BuyOrSell、ダブルLotSize、ダブルPriceToOpen、ダブルSlippage、ダブルSL_Price、ダブルTP_Price、文字列コメント、日時有効期限)
{
intチケット、errorType。
if(BuyOrSell == OP_BUY)
{
if(IsECN)
{
印刷(入札: 入札 依頼: 依頼 |開始注文: Symbol() 、 BuyOrSell 、 LotToize 、 スリップ 、 SL_Price 、 TP_Price 、 comments 、 EA_MAGIC_NUM 、 ExpirationTime 、グリーン) ;
ticket = OrderSend(Symbol()、BuyOrSell、LotSize、PriceToOpen、Slippage、0、0、コメント、EA_MAGIC_NUM、ExpirationTime、Green);
if(ticketgt; 0)
{
if(OrderSelect(ticket、SELECT_BY_TICKET、MODE_TRADES))
{
印刷(注文をオープンに購入:、OrderOpenPrice());
msg = ticket :購入ポジションは DoubleToStr(OrderOpenPrice()、Digits)でオープンしました。
WriteToLogFile(msg);
if(OrderModify(ticket、OrderOpenPrice()、SL_Price、TP_Price、0))
{
印刷(ストップロスとテイクプロフィットを追加)
msg = ticket :ストップロス: DoubleToStr(SL_Price、Digits) そしてTake Profit DoubleToStr(TP_Price、Digits) が追加されました。
WriteToLogFile(msg);
}
それ以外の
{
errorType = GetLastError();
印刷(ストップロスとテイクプロフィットの追加エラー - 、ErrorDescription(errorType));
msg = ticket :ストップロスの追加エラー: DoubleToStr(SL_Price、Digits) and Take Profit DoubleToStr(TP_Price、Digits) - ErrorDescription(errorType);
WriteToLogFile(msg);
}
戻り値(true)。
}
}
それ以外の
{
errorType = GetLastError();
印刷(注文を開くときのエラー:ErrorDescription(errorType));
msg =買いポジションを開くことはできません。 ErrorDescription(errorType);
WriteToLogFile(msg);
戻り値(false)。
}
}
それ以外の
{
印刷(入札: 入札 依頼: 依頼 |開始注文: Symbol() 、 BuyOrSell 、 LotToize 、 スリップ 、 SL_Price 、 TP_Price 、 comments 、 EA_MAGIC_NUM 、 ExpirationTime 、グリーン) ;
ticket = OrderSend(Symbol()、BuyOrSell、LotSize、PriceToOpen、Slippage、SL_Price、TP_Price、コメント、EA_MAGIC_NUM、ExpirationTime、Green);
if(ticketgt; 0)
{
if(OrderSelect(ticket、SELECT_BY_TICKET、MODE_TRADES))
{
印刷(注文をオープンに購入:、OrderOpenPrice());
msg = ticket :買いポジションがオープンしました。 ErrorDescription(errorType);
WriteToLogFile(msg);
戻り値(true)。
}
}
それ以外の
{
errorType = GetLastError();
印刷(注文を開くときのエラー:ErrorDescription(errorType));
msg =買いポジションを開くことはできません。 ErrorDescription(errorType);
WriteToLogFile(msg);
戻り値(false)。
}
}
}
if(BuyOrSell == OP_SELL)
{
if(IsECN)
{
印刷(買い: 買い 売り: 売り :売り注文を開く: Symbol() 、 BuyOrSell 、 LotSize 、 スリップ 、 SL_Price 、 TP_Price 、 コメント 、 EA_MAGIC_NUM 、 ExpirationTime 、赤) ;
ticket = OrderSend(Symbol()、BuyOrSell、LotSize、PriceToOpen、Slippage、0、0、コメント、EA_MAGIC_NUM、ExpirationTime、Red);
if(ticketgt; 0)
{
if(OrderSelect(ticket、SELECT_BY_TICKET、MODE_TRADES))
{
印刷(注文をオープンに販売:、OrderOpenPrice());
msg = ticket :売りポジションは DoubleToStr(OrderOpenPrice()、Digits)でオープンしました。
WriteToLogFile(msg);
if(OrderModify(ticket、OrderOpenPrice()、SL_Price、TP_Price、0))
{
印刷(ストップロスとテイクプロフィットを追加)
msg = ticket :ストップロス: DoubleToStr(SL_Price、Digits) そしてTake Profit DoubleToStr(TP_Price、Digits) が追加されました。
WriteToLogFile(msg);
}
それ以外の
{
errorType = GetLastError();
印刷(ストップロスとテイクプロフィットの追加エラー - 、ErrorDescription(errorType));
msg = ticket :ストップロスの追加エラー: DoubleToStr(SL_Price、Digits) and Take Profit DoubleToStr(TP_Price、Digits) - ErrorDescription(errorType);
WriteToLogFile(msg);
}
戻り値(true)。
}
}
それ以外の
{
errorType = GetLastError();
印刷(販売注文のオープンエラー:、ErrorDescription(errorType));
msg =売りポジションを開くことはできません。 ErrorDescription(errorType);
WriteToLogFile(msg);
戻り値(false)。
}
}
それ以外の
{
印刷(買い: 買い 売り: 売り :売り注文を開く: Symbol() 、 BuyOrSell 、 LotSize 、 スリップ 、 SL_Price 、 TP_Price 、 コメント 、 EA_MAGIC_NUM 、 ExpirationTime 、赤) ;
ticket = OrderSend(Symbol()、BuyOrSell、LotSize、PriceToOpen、Slippage、SL_Price、TP_Price、コメント、EA_MAGIC_NUM、ExpirationTime、Red);
if(ticketgt; 0)
{
if(OrderSelect(ticket、SELECT_BY_TICKET、MODE_TRADES))
{
印刷(売り注文がオープンしました:、OrderOpenPrice());
msg = ticket :売りポジションがオープンしました。 ErrorDescription(errorType);
WriteToLogFile(msg);
戻り値(true)。
}
}
それ以外の
{
errorType = GetLastError();
印刷(販売注文のオープンエラー:、ErrorDescription(errorType));
msg =売りポジションを開くことはできません。 ErrorDescription(errorType);
WriteToLogFile(msg);
戻り値(false)。
}
}
}
}
bool Spread_Still_In_Range()
{
if(Use_Spread_Filtering CustomSpread gt; Spread_Filter)
{
戻り値(false)。
}
戻り値(true)。
}
void OpenBuyOrder()
{
RefreshRates();
double PriceToOpen、TakeProfitPrice、StopLossPrice、PositionSize。
PriceToOpen =お願いします。
PriceToOpen = NormalizeDouble(PriceToOpen、Digits);
PositionSize = PositionSizeToOpen(the_StopLoss);
if(TakeProfit == 0)
{
TakeProfitPrice = 0;
}
それ以外の
{
TakeProfitPrice = PriceToOpen (テイクプロフィット*ポイント*ピップファクター);
TakeProfitPrice = NormalizeDouble(TakeProfitPrice、Digits);
}
if(the_StopLoss == 0)
{
StopLossPrice = 0;
}
それ以外の
{
StopLossPrice = PriceToOpen - (the_StopLoss * Point * PipFactor);
StopLossPrice = NormalizeDouble(StopLossPrice、Digits);
}
if(PositionSize lt; MinLots)
{
PositionSize = MinLots;
}
if(PositionSize gt; MaxLots)
{
PositionSize = MaxLots;
}
PositionSize = NormalizeDouble(PositionSize、LotsDecimalAllowed);
文字列DateNow = TimeDay(TimeCurrent()) - TimeMonth(TimeCurrent()) - TimeYear(TimeCurrent());
SendOrders(OP_BUY、PositionSize、PriceToOpen、Slippage、StopLossPrice、TakeProfitPrice、CBBP_ DateNow、0)。
}
void OpenSellOrder()
{
RefreshRates();
double PriceToOpen、TakeProfitPrice、StopLossPrice、PositionSize。
PriceToOpen =入札単価
PriceToOpen = NormalizeDouble(PriceToOpen、Digits);
PositionSize = PositionSizeToOpen(the_StopLoss);
if(TakeProfit == 0)
{
TakeProfitPrice = 0;
}
それ以外の
{
TakeProfitPrice = PriceToOpen - (TakeProfit * Point * PipFactor);
TakeProfitPrice = NormalizeDouble(TakeProfitPrice、Digits);
}
if(the_StopLoss == 0)
{
StopLossPrice = 0;
}
それ以外の
{
StopLossPrice = PriceToOpen (the_StopLoss * Point * PipFactor);
StopLossPrice = NormalizeDouble(StopLossPrice、Digits);
}
if(PositionSize lt; MinLots)
{
PositionSize = MinLots;
}
if(PositionSize gt; MaxLots)
{
PositionSize = MaxLots;
}
PositionSize = NormalizeDouble(PositionSize、LotsDecimalAllowed);
文字列DateNow = TimeDay(TimeCurrent()) - TimeMonth(TimeCurrent()) - TimeYear(TimeCurrent());
SendOrders(OP_SELL、PositionSize、PriceToOpen、Slippage、StopLossPrice、TakeProfitPrice、DateNow、0)。
}
void ManageTrades()
{
double TrailLevel = PipFactor * Point * TrailingStop;
TrailLevel = NormalizeDouble(TrailLevel、Digits);
double BE_Level = PipFactor * Point * BreakEven_Pips;
BE_Level = NormalizeDouble(BE_Level、Digits);
ダブルTakeProfitPrice、StopLossPrice。
文字列logmsg =;
intチケット、errorType。
double closeNOWprice = 0;
int total = OrdersTotal();
if(合計> 0)
{
//後退しなければならない
(int cnt = total-1; cntgt; = 0; cnt--)の場合
{
if(OrderSelect(cnt、SELECT_BY_POS))
{
if(OrderSymbol()== Symbol()OrderMagicNumber()== EA_MAGIC_NUM)
{
if(OrderType()== OP_BUY)
{
//ストップロスとテイクプロフィットが追加されなかった場合ここでそれについて何かしましょう...
if(OrderStopLoss()== 0 the_StopLoss!= 0)
{
ticket = OrderTicket();
//すでに価格がストップロスレベルを超えてしまった場合は、すぐに取引を閉じる
if(OrderOpenPrice() - 入札gt; = PipFactor * Point * the_StopLoss)
{
closeNOWprice = NormalizeDouble(Bid、Digits);
if(OrderClose(ticket、OrderLots()、closeNOWprice、Slippage、Red))
{
印刷(注文の緊急(ストップロス)閉鎖:、チケット、成功)。
logmsg = ticket :緊急(ストップロス)閉鎖に成功しました。
WriteToLogFile(logmsg);
持続する;
}
それ以外の
{
errorType = GetLastError();
(ERROR!Emergency(Stop Loss)注文のクローズ:、ticket、failed! - 、ErrorDescription(errorType))を印刷します。
logmsg = ticket :注文の緊急(ストップロス)完了: ticket failed! - ErrorDescription(エラータイプ)。
WriteToLogFile(logmsg);
}
}
それ以外の
{
//今すぐストップロスとテイクプロフィットレベルを設定しよう
StopLossPrice = OrderOpenPrice() - (the_StopLoss * Point * PipFactor);
StopLossPrice = NormalizeDouble(StopLossPrice、Digits);
TakeProfitPrice = OrderOpenPrice() (TakeProfit * Point * PipFactor);
TakeProfitPrice = NormalizeDouble(TakeProfitPrice、Digits);
if(OrderModify(ticket、OrderOpenPrice()、StopLossPrice、TakeProfitPrice、0))
{
印刷(注文の注文の損失を停止して利益を得る、チケット、正常に追加された)。
logmsg = ticket :ストップロスとテイクプロフィットが正常に追加されました。
WriteToLogFile(logmsg);
}
それ以外の
{
errorType = GetLastError();
印刷(エラー!注文のストップロスとテイクプロフィットの追加:チケット、失敗! - 、ErrorDescription(errorType));
logmsg = ticket :注文のストップロスとテイクプロフィットの追加: ticket failed! - ErrorDescription(エラータイプ)。
WriteToLogFile(logmsg);
}
}
}
//これはテイクプロフィットレベルのブリーチシナリオに取り組むことを除いて上記と同様
if(OrderTakeProfit()== 0 TakeProfit!= 0)
{
//すでに価格がストップロスレベルを超えてしまった場合は、すぐに取引を閉じる
if(Bid-OrderOpenPrice()gt; = PipFactor * Point * TakeProfit)
{
ticket = OrderTicket();
closeNOWprice = NormalizeDouble(Bid、Digits);
if(OrderClose(ticket、OrderLots()、closeNOWprice、Slippage、Blue))
{
印刷(注文の緊急(テイクプロフィット)閉鎖:、チケット、成功)。
logmsg = ticket :緊急(利益獲得)閉鎖に成功しました。
WriteToLogFile(logmsg);
持続する;
}
それ以外の
{
errorType = GetLastError();
(ERROR!Emergency(Take Profit)注文のクローズ:、ticket、failed! - 、ErrorDescription(errorType))の印刷
logmsg = ticket :緊急(テイクプロフィット)注文のクローズ: ticket failed! - ErrorDescription(エラータイプ)。
WriteToLogFile(logmsg);
}
}
}
//Breakevenを確認してください。
if(Use_BE)
{
if(Bid-OrderStopLoss()gt; PipFactor * Point * BreakEven_Pips)
{
if(OrderStopLoss()lt; OrderOpenPrice())
{
StopLossPrice = Bid - BE_Level;
StopLossPrice = NormalizeDouble(StopLossPrice、Digits);
if(StopLossPrice gt; OrderOpenPrice())
{
if(OrderModify(OrderTicket()、OrderOpenPrice()、OrderOpenPrice()、OrderTakeProfit()、0、Green)))
{
logmsg =買い注文 OrderTicket() SLが損益分岐点に達した。
WriteToLogFile(logmsg);
}
}
それ以外の
{
if(OrderModify(OrderTicket()、OrderOpenPrice()、StopLossPrice、OrderTakeProfit()、0、Green))
{
logmsg =買い注文 OrderTicket() SLが損益分岐点に向かっている。
WriteToLogFile(logmsg);
}
}
}
}
}
//フローティングストップを確認
if(!UseJumpingStop(TrailingStop!= 0))
{
if((Bid-OrderOpenPrice())gt;トレイルレベル((Bid-OrderOpenPrice())gt; = BE_Level || OrderStopLoss()gt; = OrderOpenPrice()))
{
if(OrderStopLoss()lt; Bid-TrailLevel)
{
StopLossPrice = NormalizeDouble(Bid-TrailLevel、Digits);
OrderModify(OrderTicket()、OrderOpenPrice()、StopLossPrice、OrderTakeProfit()、0、緑);
}
}
}
//ジャンプ停止を確認
if(UseJumpingStop(TrailingStop!= 0))
{
if((Bid-OrderStopLoss()gt;(TrailLevel * 2))((Bid-OrderOpenPrice())gt; = BE_Level || OrderStopLoss()gt; = OrderOpenPrice()))
{
if((OrderStopLoss() TrailLevel)lt; OrderTakeProfit()|| OrderTakeProfit()== 0)//ストップロスレベルをテイクレベルより高くすることはできません
{
StopLossPrice = NormalizeDouble(OrderStopLoss() TrailLevel、Digits);
OrderModify(OrderTicket()、OrderOpenPrice()、StopLossPrice、OrderTakeProfit()、0、緑);
}
}
}
}
if(OrderType()== OP_SELL)
{
//ストップロスとテイクプロフィットが追加されなかった場合ここでそれについて何かしましょう...
if(OrderStopLoss()== 0 the_StopLoss!= 0)
{
ticket = OrderTicket();
//すでに価格がストップロスレベルを超えてしまった場合は、すぐに取引を閉じる
if(Ask - OrderOpenPrice()gt; = PipFactor * Point * the_StopLoss)
{
closeNOWprice = NormalizeDouble(Ask、Digits);
if(OrderClose(ticket、OrderLots()、closeNOWprice、Slippage、Red))
{
印刷(注文の緊急(ストップロス)閉鎖:、チケット、成功)。
logmsg = ticket :緊急(ストップロス)閉鎖に成功しました。
WriteToLogFile(logmsg);
持続する;
}
それ以外の
{
errorType = GetLastError();
(ERROR!Emergency(Stop Loss)注文のクローズ:、ticket、failed! - 、ErrorDescription(errorType))を印刷します。
logmsg = ticket :注文の緊急(ストップロス)完了: ticket failed! - ErrorDescription(エラータイプ)。
WriteToLogFile(logmsg);
}
}
それ以外の
{
//今すぐストップロスとテイクプロフィットレベルを設定しよう
StopLossPrice = OrderOpenPrice() (the_StopLoss * Point * PipFactor);
StopLossPrice = NormalizeDouble(StopLossPrice、Digits);
TakeProfitPrice = OrderOpenPrice() - (TakeProfit * Point * PipFactor);
TakeProfitPrice = NormalizeDouble(TakeProfitPrice、Digits);
if(OrderModify(ticket、OrderOpenPrice()、StopLossPrice、TakeProfitPrice、0))
{
印刷(注文の注文の損失を停止して利益を得る、チケット、正常に追加された)。
logmsg = ticket :ストップロスとテイクプロフィットが正常に追加されました。
WriteToLogFile(logmsg);
}
それ以外の
{
errorType = GetLastError();
印刷(エラー!注文のストップロスとテイクプロフィットの追加:チケット、失敗! - 、ErrorDescription(errorType));
logmsg = ticket :注文のストップロスとテイクプロフィットの追加: ticket failed! - ErrorDescription(エラータイプ)。
WriteToLogFile(logmsg);
}
}
}
//これはテイクプロフィットレベルのブリーチシナリオに取り組むことを除いて上記と同様
if(OrderTakeProfit()== 0 TakeProfit!= 0)
{
//すでに価格がストップロスレベルを超えてしまった場合は、すぐに取引を閉じる
if(OrderOpenPrice() - gt; = PipFactor * Point * TakeProfitを要求)
{
ticket = OrderTicket();
closeNOWprice = NormalizeDouble(Ask、Digits);
if(OrderClose(ticket、OrderLots()、closeNOWprice、Slippage、Blue))
{
印刷(注文の緊急(テイクプロフィット)閉鎖:、チケット、成功)。
logmsg = ticket :緊急(利益獲得)閉鎖に成功しました。
WriteToLogFile(logmsg);
持続する;
}
それ以外の
{
errorType = GetLastError();
(ERROR!Emergency(Take Profit)注文のクローズ:、ticket、failed! - 、ErrorDescription(errorType))の印刷
logmsg = ticket :緊急(テイクプロフィット)注文のクローズ: ticket failed! - ErrorDescription(エラータイプ)。
WriteToLogFile(logmsg);
}
}
}
//Breakevenをチェックする
if(Use_BE)
{
if(OrderStopLoss() - Ask gt; PipFactor * Point * BreakEven_Pips)
{
if(OrderStopLoss()gt; OrderOpenPrice())
{
StopLossPrice = Ask BE_Level;
StopLossPrice = NormalizeDouble(StopLossPrice、Digits);
if(StopLossPrice lt; OrderOpenPrice())
{
if(OrderModify(OrderTicket()、OrderOpenPrice()、OrderOpenPrice()、OrderTakeProfit()、0、Green)))
{
logmsg =売り注文 OrderTicket() SLが損益分岐点に達した。
WriteToLogFile(logmsg);
}
}
それ以外の
{
if(OrderModify(OrderTicket()、OrderOpenPrice()、StopLossPrice、OrderTakeProfit()、0、Green))
{
logmsg =売り注文 OrderTicket() SL損益分岐点レベルへの移行。
WriteToLogFile(logmsg);
}
}
}
}
}
//フローティングストップを確認
if(!UseJumpingStop(TrailingStop!= 0))
{
if((OrderOpenPrice() - Ask)gt; TrailLevel((OrderOpenPrice() - Ask)gt; = BE_Level || OrderStopLoss()lt; = OrderOpenPrice()))
{
if(OrderStopLoss()gt; Ask TrailLevel)
{
StopLossPrice = NormalizeDouble(Ask TrailLevel、Digits);
OrderModify(OrderTicket()、OrderOpenPrice()、StopLossPrice、OrderTakeProfit()、0、赤);
}
}
}
//ジャンプ停止を確認
if(UseJumpingStop(TrailingStop!= 0))
{
//ポジションがストップロス金額で利益を得ている場合にのみアクティブになります。
if(((OrderStopLoss() - Ask)gt;(TrailLevel * 2))((OrderOpenPrice() - Ask)gt; = BE_Level || OrderStopLoss()lt; = OrderOpenPrice()))
{
if((OrderStopLoss() - TrailLevel)gt; OrderTakeProfit()|| OrderTakeProfit()== 0)//ストップロスレベルをテイク利益レベルより小さくすることはできません
{
StopLossPrice = NormalizeDouble(OrderStopLoss() - TrailLevel、Digits);
OrderModify(OrderTicket()、OrderOpenPrice()、StopLossPrice、OrderTakeProfit()、0、赤);
}
}
}
}
}
}
}
}
}
// ----------------------------------------------- -------------------
//|その他の機能
// ----------------------------------------------- -------------------
void GetSpread()
{
if(UserDefinedSpread lt; = 0)
{
CustomSpread =(MarketInfo(Symbol()、MODE_SPREAD))/PipFactor;
}
それ以外の
{
カスタムスプレッド=ユーザー定義スプレッド。
}
//ストップロスにスプレッドを追加する必要がありますか?
ストップロス=ストップロス。
if(ストップロスgt; 0)
{
if(Add_Spread_To_StopLoss)
{
ストップロス=ストップロス カスタムスプレッド。
}
それ以外の
{
ストップロス=ストップロス。
}
}
}
void WriteComment()
{
msg =;
int currentHour = TimeHour(Time [0]);
if(SessionOpenHour1 lt; SessionOpenHour2)
{
if(currentHour lt; SessionOpenHour2)
{
msg = EAは Session2 Openを待っています。
}
if(currentHour lt; SessionOpenHour1 || currentHour gt; SessionOpenHour2)
{
msg = EAは Session1 Openを待っています。
}
}
それ以外の
{
if(currentHour lt; SessionOpenHour1)
{
msg = EAは Session1 Openを待っています。
}
if(currentHour lt; SessionOpenHour2 || currentHour gt; SessionOpenHour1)
{
msg = EAは Session2 Openを待っています。
}
}
msg = msg \現在の時刻: 現在の時刻 : 時刻の分(TimeCurrent());
if(PipFactor == 10)
{
msg = msg \ nフラクショナルピップを使った作業。
}
それ以外の
{
msg = msg \ n非フラクショナルピップを使った作業。
}
//double stoplevel = MarketInfo(Symbol()、MODE_STOPLEVEL)/PipFactor;
if(MoneyManagement the_StopLoss gt; 0)
{
msg = msg \ストップロスによる資金管理機能の使用。
}
msg = msg \現在のスプレッド: DoubleToStr(カスタムスプレッド、1) pips;
if(Add_Spread_To_StopLoss the_StopLoss!= 0)
{
msg = msg \ nスプレッドがストップロスに追加されます。
}
それ以外の
{
msg = msg \ nスプレッドはストップロスに追加されません。
}
if(SessionIsOn)
{
msg = msg \ on \取引時間 latestSession 。エントリーを探しています...
msg = msg \ nセッションオープン価格: DoubleToStr(theSessionOpenPrice、Digits);
msg = msg \ n現在の価格: DoubleToStr(閉じる[0]、桁数);
}
それ以外の
{
msg = msg \ n latestSession 取引ウィンドウが閉じられました。
}
コメント(msg);
}
void WriteToLogFile(文字列入力)
{
文字列filename = CBBP- Symbol() - Day() - Month() - Year() 。log;
int handle = FileOpen(ファイル名、FILE_READ | FILE_WRITE);
if(handlegt; 1)
{
文字列プレフィックス=日() 月() 年() - 時間() : 分() : 秒() = gt; ;
FileSeek(ハンドル、0、SEEK_END)。/ファイルの終わりに行く
FileWrite(handle、prefix input);
FileClose(handle);
}
}
//このメソッドは、新しいファイルを新しい日に生成するために作成されます。最初に書かれる行が書かれないことがあります。
//ここで使用されているダミーメッセージはバッファとして機能するので、有用な情報が見逃されることはありません。
void CreateNewLogFileIfNewDay()
{
if(todayOfYear!= DayOfYear())
{
todayOfYear = DayOfYear();
WriteToLogFile(ダミーメッセージ)。
}
}
bool TimeToTrade()
{
int currentHour = TimeHour(Time [0]);
int currentMinute = TimeMinute(TimeCurrent());
if((currentHour == SessionOpenHour1 || currentHour == SessionOpenHour2)currentMinute lt; GraceMinutes)
{
戻り値(true)。
}
戻り値(false)。
}
int GetSessionOpenCandle()
{
int SessionOpenCandleIndex = -1;
for(int i = 0; ilt; = GraceMinutes 1; i )
{
if(TimeHour(Time [i])== currentSessionHour TimeMinute(Time [i])== 0)
{
SessionOpenCandleIndex = i;
ブレーク;
}
}
return(SessionOpenCandleIndex);
}
// ----------------------------------------------- -------------------
//|エキスパートスタート機能|
// ----------------------------------------------- -------------------
int start()
{
//----
if(!StartupStatusOK)
{
コメント(EAの入力にエラーがありました。もう一度確認してください。);
(0)を返します。
}
CreateNewLogFileIfNewDay();
ManageTrades();
GetSpread();
if(TimeToTrade())
{
currentSessionHour = TimeHour(Time [0]);
if(currentSessionHour == SessionOpenHour1)
{
latestSession = Session1;
}
if(currentSessionHour == SessionOpenHour2)
{
latestSession = Session2;
}
if(TradeNotPlacedYet()TradeTheSession)
{
int SessionOpenIndex = GetSessionOpenCandle();
SessionIsOn = true;
if(!SessionOpenPriceWritten)
{
文字列logmsg = latestSession open DoubleToStr(Open [SessionOpenIndex]、Digits);
印刷(logmsg);
WriteToLogFile(logmsg);
SessionOpenPriceWritten = true;
}
if(Should_Buy(SessionOpenIndex)TradeTheSession)
{
OpenBuyOrder();
TradeTheSession = false;
}
if(Should_Sell(SessionOpenIndex)TradeTheSession)
{
OpenSellOrder();
TradeTheSession = false;
}
}
それ以外の
{
SessionIsOn = false。
}
}
それ以外の
{
SessionIsOn = false。
TradeTheSession = true;
ProblemLogged = false。
SessionOpenPriceWritten = false;
}
if(Show_Comments)
{
WriteComment();
}
//----
(0)を返します。