//+------------------------------------------------------------------+
//| 如果執行達到此點, |
//| 說明沒有掛單和開倉。www.emoneybtc.com |
//+------------------------------------------------------------------+
//---- 放置BuyStop 和 SellStop:
double _OpenPriceLevel, _StopLossLevel, _TakeProfitLevel;
_OpenPriceLevel = NormalizeDouble( Ask + Luft*Point, Digits );
if ( StopLoss > 0 )
{ _StopLossLevel = NormalizeDouble( _OpenPriceLevel -
StopLoss*Point, Digits ); }
else
{ _StopLossLevel = 0.0; }
if ( TakeProfit > 0 )
{ _TakeProfitLevel = NormalizeDouble( _OpenPriceLevel +
TakeProfit*Point, Digits ); }
else
{ _TakeProfitLevel = 0.0; }
if ( OrderSend ( Symbol(), OP_BUYSTOP, Lot, _OpenPriceLevel,
5, _StopLossLevel, _TakeProfitLevel, "",
_MagicNumber ) < 0 )
{
Alert( "OrderSend Error #", GetLastError() );
return(-1);
}
_OpenPriceLevel = NormalizeDouble(Bid - Luft*Point, Digits);
if ( StopLoss > 0 )
{ _StopLossLevel = NormalizeDouble( _OpenPriceLevel +
StopLoss*Point, Digits ); }
else
{ _StopLossLevel = 0.0; }
if ( TakeProfit > 0 )
{ _TakeProfitLevel = NormalizeDouble( _OpenPriceLevel -
TakeProfit*Point, Digits ); }
else
{ _TakeProfitLevel = 0.0; }
if ( OrderSend ( Symbol(), OP_SELLSTOP, Lot, _OpenPriceLevel,
5, _StopLossLevel,
_TakeProfitLevel, "", _MagicNumber ) < 0 )
{
Alert( "OrderSend Error #", GetLastError() );
return(-1);
}
return(0);
}
現在讓我們寫出可以簡化控制建倉代碼的函數,它必須用每個類型的定單進行搜索,然後將這些信息存儲在全局變量裡,程序如下:
// 在定單特性中的整體變量會被儲存:
int _BuyTicket = 0, _SellTicket = 0, _BuyStopTicket = 0;
int _SellStopTicket = 0, _BuyLimitTicket = 0, _SellLimitTicket = 0;
double _BuyLots = 0.0, _SellLots = 0.0, _BuyStopLots = 0.0;
double _SellStopLots = 0.0, _BuyLimitLots = 0.0,
_SellLimitLots = 0.0;
double _BuyOpenPrice = 0.0, _SellOpenPrice = 0.0,
_BuyStopOpenPrice = 0.0;
double _SellStopOpenPrice = 0.0, _BuyLimitOpenPrice = 0.0,
_SellLimitOpenPrice = 0.0;
double _BuyStopLoss = 0.0, _SellStopLoss = 0.0, _BuyStopStopLoss = 0.0;
double _SellStopStopLoss = 0.0, _BuyLimitStopLoss = 0.0, _SellLimitStopLoss = 0.0;
double _BuyTakeProfit = 0.0, _SellTakeProfit = 0.0,
_BuyStopTakeProfit = 0.0;
double _SellStopTakeProfit = 0.0, _BuyLimitTakeProfit = 0.0,
_SellLimitTakeProfit = 0.0;
datetime _BuyOpenTime = -1, _SellOpenTime = -1,
_BuyStopOpenTime = -1;
datetime _SellStopOpenTime = -1, _BuyLimitOpenTime = -1,
_SellLimitOpenTime = -1;
double _BuyProfit = 0.0, _SellProfit = 0.0, _BuySwap = 0.0,
_SellSwap = 0.0;
double _BuyCommission = 0.0, _SellCommission = 0.0;
string _BuyComment = "", _SellComment = "", _BuyStopComment = "";
string _SellStopComment = "", _BuyLimitComment = "",
_SellLimitComment = "";
datetime _BuyStopExpiration = -1, _SellStopExpiration = -1;
datetime _BuyLimitExpiration = -1, _SellLimitExpiration = -1;
void OneTypeOrdersInit( int magic )
{
// 變量歸零:
_BuyTicket = 0; _SellTicket = 0; _BuyStopTicket = 0;
_SellStopTicket = 0; _BuyLimitTicket = 0; _SellLimitTicket = 0;
_BuyLots = 0.0; _SellLots = 0.0; _BuyStopLots = 0.0;
_SellStopLots = 0.0; _BuyLimitLots = 0.0; _SellLimitLots = 0.0;
_BuyOpenPrice = 0.0; _SellOpenPrice = 0.0; _BuyStopOpenPrice = 0.0;
_SellStopOpenPrice = 0.0; _BuyLimitOpenPrice = 0.0;
_SellLimitOpenPrice = 0.0;
_BuyStopLoss = 0.0; _SellStopLoss = 0.0; _BuyStopStopLoss = 0.0;
_SellStopStopLoss = 0.0; _BuyLimitStopLoss = 0.0;
_SellLimitStopLoss = 0.0;
_BuyTakeProfit = 0.0; _SellTakeProfit = 0.0;
_BuyStopTakeProfit = 0.0;
_SellStopTakeProfit = 0.0; _BuyLimitTakeProfit = 0.0;
_SellLimitTakeProfit = 0.0;
_BuyOpenTime = -1; _SellOpenTime = -1; _BuyStopOpenTime = -1;
_SellStopOpenTime = -1; _BuyLimitOpenTime = -1;
_SellLimitOpenTime = -1;
_BuyProfit = 0.0; _SellProfit = 0.0; _BuySwap = 0.0;
_SellSwap = 0.0;
_BuyCommission = 0.0; _SellCommission = 0.0;
_BuyComment = ""; _SellComment = ""; _BuyStopComment = "";
_SellStopComment = ""; _BuyLimitComment = "";
_SellLimitComment = "";
_BuyStopExpiration = -1; _SellStopExpiration = -1;
_BuyLimitExpiration = -1; _SellLimitExpiration = -1;
int _GetLastError = 0, _OrdersTotal = OrdersTotal();
for ( int z = _OrdersTotal - 1; z >= 0; z -- )
{
if ( !OrderSelect( z, SELECT_BY_POS ) )
{
_GetLastError = GetLastError();
Print("OrderSelect(", z, ",SELECT_BY_POS) - Error #",
_GetLastError );
continue;
}
if ( OrderMagicNumber() == magic && OrderSymbol() ==
Symbol() )
{
switch ( OrderType() )
{
case OP_BUY:
_BuyTicket = OrderTicket();
_BuyLots = NormalizeDouble( OrderLots(), 1 );
_BuyOpenPrice = NormalizeDouble( OrderOpenPrice(),
Digits );
_BuyStopLoss = NormalizeDouble( OrderStopLoss(),
Digits );
_BuyTakeProfit = NormalizeDouble( OrderTakeProfit(),
Digits );
_BuyOpenTime = OrderOpenTime();
_BuyProfit = NormalizeDouble( OrderProfit(), 2 );
_BuySwap = NormalizeDouble( OrderSwap(), 2 );
_BuyCommission = NormalizeDouble( OrderCommission(),
2 );
_BuyComment = OrderComment();
break;
case OP_SELL:
_SellTicket = OrderTicket();
_SellLots = NormalizeDouble( OrderLots(), 1 );
_SellOpenPrice = NormalizeDouble( OrderOpenPrice(),
Digits );
_SellStopLoss = NormalizeDouble( OrderStopLoss(),
Digits );
_SellTakeProfit = NormalizeDouble( OrderTakeProfit(),
Digits );
_SellOpenTime = OrderOpenTime();
_SellProfit = NormalizeDouble( OrderProfit(), 2 );
_SellSwap = NormalizeDouble( OrderSwap(), 2 );
_SellCommission = NormalizeDouble( OrderCommission(),
2 );
_SellComment = OrderComment();
break;
case OP_BUYSTOP:
_BuyStopTicket = OrderTicket();
_BuyStopLots = NormalizeDouble( OrderLots(), 1 );
_BuyStopOpenPrice = NormalizeDouble( OrderOpenPrice(),
Digits );
_BuyStopStopLoss = NormalizeDouble( OrderStopLoss(),
Digits );
_BuyStopTakeProfit = NormalizeDouble( OrderTakeProfit(),
Digits );
_BuyStopOpenTime = OrderOpenTime();
_BuyStopComment = OrderComment();
_BuyStopExpiration = OrderExpiration();
break;
case OP_SELLSTOP:
_SellStopTicket = OrderTicket();
_SellStopLots = NormalizeDouble( OrderLots(), 1 );
_SellStopOpenPrice = NormalizeDouble( OrderOpenPrice(),
Digits );
_SellStopStopLoss = NormalizeDouble( OrderStopLoss(),
Digits );
_SellStopTakeProfit = NormalizeDouble( OrderTakeProfit(),
Digits );
_SellStopOpenTime = OrderOpenTime();
_SellStopComment = OrderComment();
_SellStopExpiration = OrderExpiration();
break;
case OP_BUYLIMIT:
_BuyLimitTicket = OrderTicket();
_BuyLimitLots = NormalizeDouble( OrderLots(), 1 );
_BuyLimitOpenPrice = NormalizeDouble( OrderOpenPrice(),
Digits );
_BuyLimitStopLoss = NormalizeDouble( OrderStopLoss(),
Digits );
_BuyLimitTakeProfit = NormalizeDouble( OrderTakeProfit(),
Digits );
_BuyLimitOpenTime = OrderOpenTime();
_BuyLimitComment = OrderComment();
_BuyLimitExpiration = OrderExpiration();
break;
case OP_SELLLIMIT:
_SellLimitTicket = OrderTicket();
_SellLimitLots = NormalizeDouble( OrderLots(), 1 );
_SellLimitOpenPrice = NormalizeDouble( OrderOpenPrice(),
Digits );
_SellLimitStopLoss = NormalizeDouble( OrderStopLoss(),
Digits );
_SellLimitTakeProfit = NormalizeDouble( OrderTakeProfit(),
Digits );
_SellLimitOpenTime = OrderOpenTime();
_SellLimitComment = OrderComment();
_SellLimitExpiration = OrderExpiration();
break;
}
}
}
}
現在我們將函數用到智能交易程序中:
extern int _MagicNumber = 1123;
extern double Lot = 0.1;
extern int StopLoss = 60;
// 止損點的間距(0 - d無)
extern int TakeProfit = 100;
// 贏利點的間距 (0 - 無)
extern int TrailingStop = 50;
//追蹤止損點 (0 - 無)
extern int Luft = 20;
// 掛單交易放置水平的間距
#include <OneTypeOrdersControl.mq4>
int start()
{
int _GetLastError = 0;
//---- 記住開倉的參量(如果可用)
OneTypeOrdersInit( _MagicNumber );
//---- 如果我們兩個都是掛單交易,退出
//---- 等待他們開啟
if ( _BuyStopTicket > 0 && _SellStopTicket > 0 ) return(0);
//---- 如果 BUY 倉位開倉
if ( _BuyTicket > 0 )
{
//---- 如果SellStop 還沒有刪除,刪除它:
if ( _SellStopTicket > 0 )
{
if ( !OrderDelete( _SellStopTicket ) )
{
Alert( "OrderDelete 錯誤#", GetLastError() );
return(-1);
}
}
//---- 檢測止損被移動:
//---- 如果追蹤止損不是很小,
if ( TrailingStop > MarketInfo( Symbol(),
MODE_STOPLEVEL ) )
{
//---- 如果贏利倉位超過追蹤止損點,
if ( NormalizeDouble( Bid - _BuyOpenPrice, Digits ) >
NormalizeDouble( TrailingStop*Point, Digits ) )
{
//---- 如果新止損水平超過當前倉位
//---- (或者當前倉位沒有止損),
if(NormalizeDouble( Bid - TrailingStop*Point,
Digits ) > _BuyStopLoss
|| _BuyStopLoss <= 0.0 )
{
//---- 修改定單
if ( !OrderModify( _BuyTicket, _BuyOpenPrice,
NormalizeDouble( Bid - TrailingStop*Point,
Digits ),
_BuyTakeProfit, 0 ) )
{
Alert( "OrderModify 錯誤#",
GetLastError() );
return(-1);
}
}
}
}
//---- 如果沒有開倉倉位,退出,無事可做
return(0);
}
//---- 這個單元格與BUY倉位的單元格相似
//---- 這就是我們不能做標注的原因...
if ( _SellTicket > 0 )
{
if ( _BuyStopTicket > 0 )
{
if ( !OrderDelete( _BuyStopTicket ) )
{
Alert( "OrderDelete錯誤 #", GetLastError() );
return(-1);
}
}
if(TrailingStop > MarketInfo( Symbol(), MODE_STOPLEVEL))
{
if(NormalizeDouble( _SellOpenPrice - Ask, Digits ) >
NormalizeDouble( TrailingStop*Point, Digits ) )
{
if(NormalizeDouble( Ask + TrailingStop*Point,
Digits ) < _SellStopLoss
|| _SellStopLoss <= 0.0 )
{
if(!OrderModify( _SellTicket, _SellOpenPrice,
NormalizeDouble( Ask + TrailingStop*Point,
Digits ),
_SellTakeProfit, 0 ) )
{
Alert( "OrderModify Error #",
GetLastError() );
return(-1);
}
}
}
}
return(0);
}
最後,我想來比較一下使用函數與否在搜索定單時的速度。為此,我們用相同的版本來連續測試 “Every tick” 模式10次(用_MagicNumber最優化),用 MetaTrader 軟件來計算時間 — 時間時自己計算的。
挖礦簡介挖礦這個詞源於對加密貨幣與黃金的類比。黃金或貴金屬很稀有,電子代幣也是,增加總量的唯一方法就是挖礦。以太坊也是這樣,發行的唯一辦法
據Coindesk 7月9日 報道,沃爾瑪(Walmart)已成功獲得包裹追蹤新專利,利用區塊鏈技術打造快遞儲物系統,提供快遞預約服務。當
智能合約是區塊鏈平台的基礎。借助智能合約,可以在處理交易時安全地應用規則。可以使用它們自動執行驗證步驟,對過去包含在已簽署的物理合約中的
被譽為繼蒸汽機、電力、互聯網之後具有顛覆性創新的技術,區塊鏈如何避免重蹈互聯網技術興起之初產生巨大泡沫的覆轍,盡管有人稱2018年是區塊鏈
寶馬集團正在使用創新的數字技術來優化其流程,區塊鏈技術就是典型案例。該技術可實現數據共享,並在整個汽車產業鏈中進行應用。寶馬集團正在使用這
歡迎訪問 外 匯 邦 WWW.WaiHuiBang.com 外匯基本面分析就是研究能影響一個國家整體經濟的本質因素,在分析了諸如經濟指
WTI紐約原油CFD(CL)期貨行情,新聞,報價 美原油 (CFD差價合約並非期貨) 交易單位. 1000美式桶.
在支付寶財富上有一款博時黃金理財產品,很多小伙伴都有注意到博時黃金在一定情況下進行交易是要收取手續費的,比如持有時間少於7天基金公
最佳答案: 一、2020年A股全年休市安排如下:(一)元旦:1月1日(星期三)休市,1月2日(星期四
最佳答案: 變頻空調省電還是定頻空調省電,要看開啟時間。根據經驗,如果空調開啟時間在5個小時以上,那