解释
来自 MQL4 MT4 MetaTrader 4 初学者的问题
fxsaber, 2017.09.08 13:52
使用 ChartApplyTemplate 时,需要强制同步,我在圣经中是这样做的
static bool TemplateApply( const long Chart_ID, const string &Str, const bool Sync = true ) { string TmpStr = Str; const bool SyncFlag = (Sync && Chart_ID && (Chart_ID != ::ChartID()) && !::IsStopped()); if (SyncFlag) { const color ColorStopLevel = (color)::ChartGetInteger(Chart_ID, CHART_COLOR_STOP_LEVEL); if ((bool)(ColorStopLevel >> 24)) ::ChartSetInteger(Chart_ID, CHART_COLOR_STOP_LEVEL, ColorStopLevel & 0xFFFFFF); const int NewColorStopLevel = (int)EXPERT::StringBetween(TmpStr, EXPERT_STOPLEVEL, STRING_END) | (0x01 << 24); TmpStr = Str; EXPERT::StringReplace(TmpStr, EXPERT_STOPLEVEL, STRING_END, EXPERT_STOPLEVEL + (string)NewColorStopLevel + STRING_END); } short Data[]; const bool Res = ::StringToShortArray(TmpStr, Data, 0, ::StringLen(TmpStr)) && ::FileSave(FILENAME, Data) && ::ChartApplyTemplate((ulong)Chart_ID, FILENAME); if (Res && SyncFlag) { long Value; while ((!::IsStopped() && ::ChartGetInteger(Chart_ID, CHART_COLOR_STOP_LEVEL, 0, Value) && (!(bool)((int)Value >> 24)))) ::Sleep(0); ::ChartSetInteger(Chart_ID, CHART_COLOR_STOP_LEVEL, (int)Value & 0xFFFFFF); } return(Res); }
ChartApply 不会立即触发。只有在触发后才能执行任何进一步操作。
要了解模板是否已应用,需要更改模板本身(在圣经中更改了图表一个特性的颜色--第 4 个字节,负责透明度),并通过睡眠(ChartGetInterger)等待该值成为图表的一个属性。之后,通过 ChartSetInteger 设置其正常值。
如果我们需要在抛出脚本的同一图表上运行智能交易系统,则需要打开一个新图表,并通过模板在该图表上运行相同的(脚本),然后在关闭辅助图表后,在我们需要的图表上运行智能交易系统。这是通过ExpertLoader_Example.mq5 来 完成的。
может быть полезна при написании различных управляющих панелей чартами/советниками и т.п.
该库无需 DLL 即可运行,完全符合市场要求。
一个小技巧 - 在 OBJ_CHART 对象上运行 EA/脚本。
这样启动的智能交易系统就会挂掉 - 它们不会以任何方式被执行。但脚本却运行得非常好。因此,这提供了一些可能性。
例如,您可以在图表上使用指标中的订单函数,因为图表上已经有一个正在运行的智能交易系统。而且不需要打开任何新的辅助图表。
编译 脚本 Scripts\OrderSend.mq5
#include <MT4Orders.mqh> //https://www.mql5.com/zh/code/16006 #include <GlobalVariables.mqh> //https://www.mql5.com/ru/forum/189649#comment_4854618 struct ORDERSEND { int Type; double Volume; double Price; int SlipPage; double SL; double TP; long Magic; datetime Expiration; color Arrow_Color; }; void OrderSend() { const ORDERSEND Order = _GlobalVariableGet<ORDERSEND>("ORDERSEND"); const string Symb = _GlobalVariableGet<string>("Symbol"); const string comment = _GlobalVariableGet<string>("Comment"); _GlobalVariableSet(__FUNCTION__, OrderSend(Symb, Order.Type, Order.Volume, Order.Price, Order.SlipPage, Order.SL, Order.TP, comment, Order.Magic, Order.Expiration, Order.Arrow_Color)); _GlobalVariableDel("ORDERSEND"); _GlobalVariableDel("Symbol"); _GlobalVariableDel("Comment"); } void OnStart() { OrderSend(); }
并运行 "可以交易 "的指标。
#include <fxsaber\Expert.mqh> //https://www.mql5.com/zh/code/19003 #include <GlobalVariables.mqh> //https://www.mql5.com/ru/forum/189649#comment_4854618 struct ORDERSEND { int Type; double Volume; double Price; int SlipPage; double SL; double TP; long Magic; datetime Expiration; color Arrow_Color; }; template <typename T> long _GlobalVariableGet2( const string Name, const ulong MaxTime = 1 e6 ) { const ulong StartTime = GetMicrosecondCount(); while (!IsStopped() && !GlobalVariableCheck(Name) && (GetMicrosecondCount() - StartTime < MaxTime)) Sleep(0); return(_GlobalVariableGet<T>(Name)); } // OrderSend,即使是在带有智能交易系统的图表上,也能通过指标运行 long OrderSend( string Symb, const int Type, const double dVolume, const double Price, const int SlipPage, const double SL, const double TP, string comment = NULL, const long magic = 0, const datetime dExpiration = 0, color arrow_color = clrNONE ) { MqlParam Params[1]; Params[0].string_value = "Scripts\\OrderSend.ex5"; ORDERSEND Order; Order.Type = Type; Order.Volume = dVolume; Order.Price = Price; Order.SlipPage = SlipPage; Order.SL = SL; Order.TP = TP; Order.Magic = magic; Order.Expiration = dExpiration; Order.Arrow_Color = arrow_color; const long Res = ObjectCreate(0, __FILE__, OBJ_CHART, 0, 0, 0) && _GlobalVariableSet("ORDERSEND", Order) && _GlobalVariableSet("Symbol", Symb) && _GlobalVariableSet("Comment", comment) && EXPERT::Run(ObjectGetInteger(0, __FILE__, OBJPROP_CHART_ID), Params) && ObjectDelete(0, __FILE__) ? _GlobalVariableGet2<long>(__FUNCTION__) : -1; _GlobalVariableDel(__FUNCTION__); return(Res); } void OnInit() { Print(OrderSend(_Symbol, ORDER_TYPE_BUY, 1, SymbolInfoDouble(_Symbol, SYMBOL_ASK), 100, 0, 0, "From Indicator", 9)); } int OnCalculate( const int, const int, const int, const double &[] ) { return(0); }
Totosha16, 2018.02.07 18:57
目前,我正在尝试使用您的库解决一个简单的问题,归结起来就是:关闭除当前图表(运行 ExpertRemove 脚本的图表)之外的所有图表上的所有智能交易系统。您能告诉我怎么做吗?
#include <fxsaber\Expert.mqh> //https://www.mql5.com/zh/code/19003 void OnStart() { const long CurrentChart = ChartID(); long Chart = ChartFirst(); while (Chart != -1) { if (Chart != CurrentChart) EXPERT::Remove(Chart); Chart = ChartNext(Chart); } }
Vladislav Andruschenko, 2018.02.09 10:14 AM
如何在智能交易系统中获取外部变量列表,这样我就不必在数组中重复列出这些变量?也就是说,在图表上进行设置时,智能交易系统会读取自身并查看外部设置。
fxsaber, 2018.02.09 12:44 pm.
#include <fxsaber\Expert.mqh> //https://www.mql5.com/zh/code/19003 input string Input1 = "Hello World!"; input int Input2 = 123; string GetExpertData( const ulong Chart = 0 ) { string Str = NULL; MqlParam Parameters[]; string Names[]; if (EXPERT::Parameters(Chart, Parameters, Names)) { Str += "\n" + ChartSymbol(Chart) + " " + EnumToString(ChartPeriod(Chart)) + " " + Parameters[0].string_value + "\n"; const int Amount = ArraySize(Names); for (int i = 0; i < Amount; i++) Str += (string)i + ": "+ Names[i] + " = " + Parameters[i + 1].string_value + "\n"; } return(Str); } void OnInit() { Print(GetExpertData()); }
结果
0: Input1 = Hello World! 1: Input2 = 123
或者像这样
#include <fxsaber\Expert.mqh> //https://www.mql5.com/zh/code/19003 input string Input1 = "Hello World!"; input int Input2 = 123; void OnInit() { MqlParam Parameters[]; string Names[]; if (EXPERT::Parameters(0, Parameters, Names)) ArrayPrint(Parameters); }
结果
[type] [integer_value] [double_value] [string_value] [0] ... 0 0.00000 "Experts\Test2.ex5" [1] ... 0 0.00000 "Hello World!" [2] ... 123 123.00000 "123"
fxsaber, 2018.02.22 23:53
从指标播放任意时长的声音文件。
Scripts\PlaySound.mq5 脚本。
#include <GlobalVariables.mqh> //https://www.mql5.com/ru/forum/189649#comment_4854618 void OnStart() { const string SoundName = "SOUND"; if (GlobalVariableCheck(SoundName)) { PlaySound(_GlobalVariableGet<string>(SoundName)); _GlobalVariableDel(SoundName); } }
指标
#property indicator_chart_window #property indicator_buffers 0 #property indicator_plots indicator_buffers #include <fxsaber\Expert.mqh> //https://www.mql5.com/zh/code/19003 #include <GlobalVariables.mqh> //https://www.mql5.com/ru/forum/189649#comment_4854618 class PLAYER { public: const string Name; const long Chart; const long chartID; PLAYER( const long iChart = 0 ) : Name(__FILE__), Chart(iChart ? iChart : ::ChartID()), chartID(::ObjectCreate(this.Chart, this.Name, OBJ_CHART, 0, 0, 0) && ::ObjectSetInteger(this.Chart, this.Name, OBJPROP_XSIZE, 0) && ::ObjectSetInteger(this.Chart, this.Name, OBJPROP_YSIZE, 0) ? ::ObjectGetInteger(this.Chart, this.Name, OBJPROP_CHART_ID) : this.Chart) { } ~PLAYER() { if (this.chartID != this.Chart) ::ObjectDelete(this.Chart, this.Name); } void PlaySound( string FileName, const string ScriptName = "Scripts\\PlaySound.ex5" ) const { static const string SoundName = "SOUND"; if (_GlobalVariableSet(SoundName, FileName)) { MqlParam Params[1]; Params[0].string_value = ScriptName; if (!EXPERT::Run(this.chartID, Params)) _GlobalVariableDel(SoundName); } } }; int OnCalculate( const int rates_total , const int prev_calculated, const int, const double& [] ) { if (!prev_calculated) { const PLAYER Player; Player.PlaySound("email.wav"); } return(rates_total); }
事实证明,MT4 缺少的远不止 FileSave 和 FileLoad(各写了 3 行):
- 没有 CHART_EXPERT_NAME(除了修正所有其他细微差别后的名称标签外,没有任何东西可以替代它)。
- 据我所知,Frontal FileLoad 并不合适,因为保存的模板采用 ANSI 编码。
,我不得不编写一个类似于 TemplateToString 的程序,以文本模式读取文件。 - STRING_END 必须为空,四重模板中没有"\r\n"。
- MT4 中的 <expert> 标签也用于指标,因此即使进行了所有这些编辑,您也只能依靠最后指定 EA 的事实(是否总是如此?)好吧,你需要找到它。
Andrey Khatimlianskii:
MT4 中的 <expert> 标签也用于指示器,因此即使进行了所有这些编辑,您也只能依靠最后指定的智能交易系统(总是这样吗?)好吧,你需要找到它。
该源代码可能有助于理解该问题。
fxsaber, 2017.05.12 13:45
#property strict #property indicator_chart_window #property indicator_buffers 2 #define PATH "MQL4\\indicators\\" #include <TypeToBytes.mqh> //https://www.mql5.com/zh/code/16280 string GetIndicatorName( void ) { const string StrName = ::MQLInfoString(MQL_PROGRAM_PATH); const int Pos = ::StringFind(StrName, PATH) + ::StringLen(PATH); return(::StringSubstr(StrName, Pos, ::StringLen(StrName) - Pos - 4)); } void SeekToString( const int handle, const string Str ) { while (!::FileIsEnding(handle)) if (::FileReadString(handle) == Str) break; return; } struct BUFFER_STRUCT { int Shift; int Type; color Color; ENUM_LINE_STYLE Style; int Width; }; const BUFFER_STRUCT GetBufferProperties( const uint Num = 0, const bool FlagSave = true ) { BUFFER_STRUCT Res = {0}; const string FileName = ::WindowExpertName() + ".tpl"; if (FlagSave ? ::ChartSaveTemplate(0, "..\\MQL4\\Files\\" + FileName) : true) { const int handle = ::FileOpen(FileName, ::FILE_READ|::FILE_CSV); if (handle > 0) { ::SeekToString(handle, "name=" + ::GetIndicatorName()); if (Num == 0) ::SeekToString(handle, "</expert>"); else { const string TmpStr = "weight_" + (string)(Num - 1); while (!::FileIsEnding(handle)) if (::StringFind(::FileReadString(handle), TmpStr) == 0) break; } if (!::FileIsEnding(handle)) { static const string Property[] = {"shift", "draw", "color", "style", "weight"}; const string StrNum = "_" + (string)Num + "="; for (int i = 0; i < ::ArraySize(Property); i++) _W(Res)[i * sizeof(int)] = (int)::StringToInteger(::StringSubstr(::FileReadString(handle), ::StringLen(Property[i] + StrNum))); } ::FileClose(handle); } } return(Res); } void OnInit() { string Str = "Colors:"; for (int i = 0; i < indicator_buffers; i++) Str += " " + (string)i + "-" + (string)::GetBufferProperties(i).Color; Alert(Str); } void start() { }
这一信息来源可能有助于理解这一问题。
谢谢!和往常一样,比我想的要复杂一些;)
您的代码有一个不可否认的优点--可以被提取和使用。但要修改其中的任何内容都相当困难,这是一个缺点。
Expert:
作者: fxsaber