if (prevshortcurrentlong && currentshort>currentlong>Trendsetter && Ask > resistance - upperproximity*Point){ //---conditions to open long positions change as desired
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES )) Print("BUY order opened : ",OrderOpenPrice());
}
else Print("Error opening BUY order : ",GetLastError());
return(0);
}
//+---------enter short positions----------+
if (prevshort>prevlong && currentshort<currentlong && currentshort<currentlong<Trendsetter && Ask < support + lowerproximity*Point){ //---conditions to open short positions change as desired
申し訳ありませんが、コード全体を検証する時間がないので、このスニペットを検証してみましょう。
if(OrderSymbol()==Symbol() && OrderType()==OP_SELL && Bid+Profit>OrderOpenPrice())
例えば、OrderOpenPrice = 1.2100とします。
で、利益は0.0010です。
つまり、ビッドが1.2100-0.0010 = 1.2090以下であれば、ショートトレードを決済したいわけです。
ここで、ギャップダウンがあり、価格が1.2090を飛び越えて、現在の買値が1.2088になったと仮定します。あなたの計算式によると
ビッド + プロフィット = 1.2088 + 0.0010 = 1.2098.これは、システムが利益のしきい値を超えているにもかかわらず、OrderOpenPriceより大きいものではありません。したがって、注文は決済されません。私の意見では、クローズ条件のロジックを再検討し、再スクリプト化する必要があります。また、アスク価格でしか決済できないので、ショートトレードをするときはアスク価格を扱うべきです。
もう一つアドバイスです。
for (int cnt = 0 ; cnt = 0; cnt--)のカウントループとか。
頑張ってください。これは勉強になります!Majiさんありがとうございます。これはとにかく私が欲しかった決済ロジックではありません...私が欲しかったロジックは、トレーリングストップロスが 発動していない場合に決済させるというものでした。このロジックは、トレーリング・ストップ・ロスが発生しなかった場合に閉じるようにしたかったのですが、発生したかどうかを検出する方法を正確に理解していないので、私が意図した通りにプログラムする方法を学ぶまでは、この時間からクローズするストラテジーを使うことはできません。
新しい終値基準...コードを正しく配置するのを助けてください...
ロングEMAがminortrendsetterを下回ること(ロングの場合)、ショートの場合はその逆である、この中に別の終値の基準を置くのに助けが必要です。私はこの新しい基準でそれらを閉じるために、ロングとショートのオープンポジションを分離する方法を正確に理解していない。
//+--------- settings may vary use at your own risk-----------------+
//+--------------user inputs--------------------+
extern double Trendsetter = 250;
extern double Minortrendsetter = 150;
extern double LongEMA = 20;
extern double ShortEMA = 5;
extern double TrailingStop = 15;
extern double TrailingStopTrigger = 1;
extern double StopLoss = 186;
extern double TakeProfit = 250;
extern double Lots = 0.1;
extern double EquityStop = 9;
//---- Custom "Channel-1" Indicator and Filter Parameters
extern int Hours=36;
extern color col=SkyBlue;
extern double TF = 60; //--which bar period for the custom indicator to use
extern double upperproximity = 30; //---disallows long orders within this proximity to resistance line
extern double lowerproximity = 30; //---disallows short orders within this proximity to the support line
//+-----------close based on not triggering trailing stop in allotted time----------------+
extern int MonitorInMinutes = 60; // minutes after open to check state of trade
extern int ThresholdMove = 11; // if after that time we don't have +'x' pips we will exit
extern int MinsMultiplier = 600; // multiplies the MonitorInMinutes to make minutes (if 'x'=60) into hours
//+----------------------end of allotted time user inputs-----------------------------+
//+-----------------------------end of user inputs----------------------------------+
//+------------------------------------------------------------------+
//| expert start function
//+------------------------------------------------------------------+
int start(){
CloseOrder();
int cnt, ticket;
if(Bars<100){
Print("bars less than 100");
return(0);
}
//+----------------------Get Moving Average(s) Data----------------------------------------+
double currentlong=iMA(NULL,0,LongEMA,0,MODE_EMA,PRICE_CLOSE,0);//--current period longEMA
double currentshort=iMA(NULL,0,ShortEMA,0,MODE_EMA,PRICE_CLOSE,0);//--current period shortEMA
double trendsetter=iMA(NULL,0,Trendsetter,0,MODE_EMA,PRICE_CLOSE,0);//--current period TrendsetterEMA
double minorts=iMA(NULL,0,Minortrendsetter,0,MODE_EMA,PRICE_CLOSE,0);//--current period MinortrendsetterEMA
double prevlong=iMA(NULL,0,LongEMA,0,MODE_EMA,PRICE_CLOSE,1);//--previous period longEMA
double prevshort=iMA(NULL,0,ShortEMA,0,MODE_EMA,PRICE_CLOSE,1);//--previous period shortEMA
double prevtrendsetter=iMA(NULL,0,Trendsetter,0,MODE_EMA,PRICE_CLOSE,1);//--previous period TrendsetterEMA
double prevminorts=iMA(NULL,0,Minortrendsetter,0,MODE_EMA,PRICE_CLOSE,1);//--previous period MinortrendsetterEMA
//+----------------------------end of Get Moving Average(s) Data-----------------------------+
//+--------------------channel filter---------------------------+
double resistance = iCustom(NULL,TF,"Channel-1",Hours,col,0,0);
double support = iCustom(NULL,TF,"Channel-1",Hours,col,2,0);
//+------------------- end channel filter------------------------+
//+---------Obnoxious money management code needs revision----------------+
int total=OrdersTotal();
if(total<1){
if(AccountFreeMargin()<(1000*Lots)){
Print("We have no money. Free Margin = ", AccountFreeMargin());
return(0);
}
//+---------end of Obnoxious money management code-----------------+
//+---------------------------------------Order Entry--------------------------------------------+
//+---------enter long positions----------+
if (prevshortcurrentlong && currentshort>currentlong>Trendsetter && Ask > resistance - upperproximity*Point){ //---conditions to open long positions change as desired
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point, NULL,16384,0,Green);
if(ticket>0){
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES )) Print("BUY order opened : ",OrderOpenPrice());
}
else Print("Error opening BUY order : ",GetLastError());
return(0);
}
//+---------enter short positions----------+
if (prevshort>prevlong && currentshort<currentlong && currentshort<currentlong<Trendsetter && Ask < support + lowerproximity*Point){ //---conditions to open short positions change as desired
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+StopLoss*Point,Bid-TakeProfit*Point, NULL,16384,0,Red);
if(ticket>0) {
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES )) Print("SELL order opened : ",OrderOpenPrice());
}
else Print("Error opening SELL order : ",GetLastError());
return(0);
}
}
//+---------end of order entry-------------------------+
//+-------------------------Trailing Stop Code------------------------------------+
for(cnt=0;cnt<total;cnt++) {
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL && OrderSymbol()==Symbol()) {
if(OrderType()==OP_BUY){
if(TrailingStop>0) {if(Bid-OrderOpenPrice()>Point*TrailingStopTrigger) {
if(OrderStopLoss()<Bid-Point*TrailingStop) {
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);
return(0);
}
}
}
}else{
if(TrailingStop>0) {if((OrderOpenPrice()-Ask)>(Point*TrailingStopTrigger)) {
if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0)) {
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);
return(0);
}
}
}
}
//+-------------------------End of Trailing Stop Code----------------------------+
//+---------------------Equity Stop Code---------------------------+
if((AccountEquity()+ EquityStop)<AccountBalance()) {
{
int ttotal = OrdersTotal();
for(int i=ttotal-1;i>=0;i--)
{
OrderSelect(i, SELECT_BY_POS);
int type = OrderType();
bool result = false;
switch(type)
{
//Close opened long positions
case OP_BUY : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
break;
//Close opened short positions
case OP_SELL : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );
}
if(result == false)
{
Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
Sleep(3000);
}
}
return(0);
}
}
}
}
}
//+---------------------End of Equity Stop Code---------------------------+
//|
//+---------------------Close Based on Time-------------------------------+
//+--------------needs revision, not working as desired---------------------+
//+------------I want it to close IF and ONLY IF trailing stop is NOT triggered-------------+
void CloseOrder()
{
double Profit=ThresholdMove*Point;
int total = OrdersTotal();
for (int cnt = 0 ; cnt < total ; cnt++)
{
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
if ((CurTime()-OrderOpenTime())>MonitorInMinutes*60*MinsMultiplier)
{
if(OrderSymbol()==Symbol() && OrderType()==OP_BUY && Bid-Profit<OrderOpenPrice() )
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet);
}
if(OrderSymbol()==Symbol() && OrderType()==OP_SELL && Bid+Profit>OrderOpenPrice())
{
OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet);
}
}
}
}
//+---------------------------end of close on time code---------------+OK コードのこの部分を理解する必要があります...。
最初の行はカウントする
二行目はカウントされた注文を選択する
3行目...これは私を騙した...選択された注文 が売りであるかどうかを知りたいのだと思う...しかし、"0 "はどうしたのだろう?私はここに5つの 'if'行があることを意味します....
もしそのうちの一つでも真であれば、注文を修正するのだろうが、そうでなければ...else'に行って...これはどうにかしてロングとショート両方のポジションを扱う必要があるが、まだ理解できていない。
ロングEMAとminortrendsetterEMAのクロスに基づいてロングまたはショートを閉じるための基準を追加したいので...私はすべてのこの中でそれを行うにはどこにあるのかわかりません。
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL && OrderSymbol()==Symbol()) {
if(OrderType()==OP_BUY){
if(TrailingStop>0) {if(Bid-OrderOpenPrice()>Point*TrailingStopTrigger) {
if(OrderStopLoss()<Bid-Point*TrailingStop) {
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);
return(0);
}
}
}
}else{
if(TrailingStop>0) {if((OrderOpenPrice()-Ask)>(Point*TrailingStopTrigger)) {
if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0)) {
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);
return(0);
}
}
}
}OK私はすべてのifとelseは、ssの行につながることを理解します。
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);
オーダーモディファイ(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);
このOrder Modifyのラインがどのように構築されているのか、もっと詳しく知りたいのですが、これは注文を変更するだけでなく、クローズしているのでしょうか? それとも、注文を変更するだけで、他の何かが注文をクローズしているのでしょうか?
MetaEditorのヘルプを見る
OP_BUY 0 Buying position.
OP_SELL 1 Selling position.
OP_BUYLIMIT 2 Buy limit pending position.
OP_SELLLIMIT 3 Sell limit pending position.
OP_BUYSTOP 4 Buy stop pending position.
OP_SELLSTOP 5 Sell stop pending position.
[/PHP]
so, <= OP_SELL is OP_BUY or OP_SELL. Just instead of using
[PHP]if((OrderType()==OP_SELL) || (OrderType()==OP_BUY) ...タイピングを減らすだけ
さあ、始めましょう。
void CloseOrders(int op)
{
int tik[30], t = 0;
for(int i =0;i<OrdersTotal();i++){
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
if(OrderSymbol()==Symbol() && MagicNum==OrderMagicNumber() && OrderType() == op){
tik[t] = OrderTicket(); t++;
}
}
}
for (i = 0; i<t; i++)
{
if(OrderSelect(tik,SELECT_BY_TICKET)){
double prc = Bid;
if (op == OP_SELL) prc = Ask;
CloseOrder(tik, OrderLots(), prc);
}
}
}Call it : CloseOrder(OP_BUY); // すべての買い注文をクローズする。
または
CloseOrder(OP_SELL); // 全ての売り注文をクローズする。
注文を 配列にまとめて削除しているのは、1番のポジションの注文を決済すると、次の注文がまた1になってしまい、問題が発生するからです。
これは忘れました
{
int CloseCnt, err;
// try to close 3 Times
CloseCnt = 0;
color clr = Violet;
if (OrderType() == OP_SELL)
clr = Orange;
while (CloseCnt < 3)
{
if (OrderClose(ticket,numLots,close_price,Slippage,clr))
{
CloseCnt = 3;
}
else
{
err=GetLastError();
Print(CloseCnt," Error closing order : (", err , ") " + ErrorDescription(err));
if (err > 0) CloseCnt++;
}
}
}
[/PHP]
and dont forget to add this line after #property link
[PHP]#property link "http://www.elihayun.com"
#include
まだ混乱しています...
私がこのコードを元々作ったのではないことを理解してください。誰かが作ったものを理解し、それを修正する作業をしているのです。私は、オリジナルとはもうほとんど似ていないほど修正した今だけ、プロパティラインに私の名前を入れました。今は手付かずの部分よりも、手を加えた部分の方が多い。まだ理解できない部分があり、そのため修正できていません。
私が探しているのは注文をクローズするコードなのですが、これはすべてトレーリングストップを更新するための修正に過ぎないようです。
これは、クローズする方法は1つではありません。
今、このEAにはストップロスでクローズする機能があります。
トレーリングストップでクローズする。
テイクプロフィットターゲットで クローズする。
または、オープン後、定義された時間の経過に基づきクローズする。
しかし、このEAにないもの、それは、ロングEMAがminortrendsetterEMAに戻ったらクローズする機能 です。どうすればいいのでしょうか?つまり、これら他のすべての終値オプションの上に新しいコードを置くにはどうしたらいいのでしょうか?
私の要件定義では、ある側面を使いたくないときは、単にそのパラメーターの基準を極端にして、その基準を基本的にオフにするような形で発動させないようにしています。そうすることで、他の基準がどのような結果を返すかを再生することができるのです。
これは忘れました
{
int CloseCnt, err;
// try to close 3 Times
CloseCnt = 0;
color clr = Violet;
if (OrderType() == OP_SELL)
clr = Orange;
while (CloseCnt < 3)
{
if (OrderClose(ticket,numLots,close_price,Slippage,clr))
{
CloseCnt = 3;
}
else
{
err=GetLastError();
Print(CloseCnt," Error closing order : (", err , ") " + ErrorDescription(err));
if (err > 0) CloseCnt++;
}
}
}
[/PHP]
and dont forget to add this line after #property link
[PHP]#property link "http://www.elihayun.com"
#include
OK 素晴らしい ありがとうございます
これは基本的にクローズコードだけなのですか?
これはロングとショートの両方のポジションを決済するのでしょうか?
もし、ロンジマがミノルトレンドセッテレマにクロスして戻ってきたら、どのようにクローズすればよいのでしょうか? ショートを開いていたらクロスアップ、ロングを開いていたらクロスダウン?
p.s. 私は今日の午後遅くまでコンピュータに戻るつもりはありません。その時にまたチェック します。
もう1つ質問
スワップとはどういう意味ですか?
LONGポジションを開くことを決定するたびに、CloseOrders(OP_SELL)を呼び出します。
その逆もしかりです。
注文をオープンしているコードを見てください。
クローズする注文があるかどうかを確認 する必要はありません。ルーチンがそれをやってくれるのです。
ところで、私は2006.07.02のようなセルをエクセルで日付に変換するコードを投稿しています。そこにあなたのポストを見てください