|
楼主 |
发表于 2024-3-19 13:14:52
|
显示全部楼层
完整优化后的代码
- #include <stdio.h>
- #include <MetaTrader5/MetaTrader5.h>
- // 定义交易批次结构体
- struct TradeBatch {
- double lot_size;
- double price_interval;
- double take_profit;
- };
- extern int BuyOrSell = 0; // 0=多, 1=空
- extern int MaxTrades = 7;
- extern int sliptime = 30;
- extern int slippage = 3;
- extern int MagicN = 10533;
- extern string Commentt = "QQ:2913811";
- // 交易批次列表
- TradeBatch trade_batches[7];
- double current_bid_price;
- double current_ask_price;
- int long_index = 0;
- int short_index = 0;
- bool OpenLong = false;
- bool OpenShort = false;
- bool TrackOpenLong = false;
- bool TrackOpenShort = false;
- datetime TimeLong = 0;
- datetime TimeShort = 0;
- void OnInit() {
- SetIndexStyle(0, DRAW_LINE, EMPTY);
- SetIndexBuffer(0, Gda_296);
-
- trade_batches[0] = {0.01, 15, 15};
- trade_batches[1] = {0.02, 15, 15};
- trade_batches[2] = {0.04, 15, 15};
- trade_batches[3] = {0.07, 15, 15};
- trade_batches[4] = {0.09, 15, 15};
- trade_batches[5] = {0.12, 15, 15};
- trade_batches[6] = {0.15, 15, 15};
- for (int i = 0; i < 7; ++i) {
- if (Digits == 5 || Digits == 3) {
- trade_batches[i].price_interval *= 10;
- trade_batches[i].take_profit *= 10;
- }
- else if (StringFind(Symbol(), "XAU", 0) >= 0 || StringFind(Symbol(), "GOLD", 0) >= 0 && (Digits == 2 || Digits == 3)) {
- trade_batches[i].price_interval *= (Digits == 2 ? 10 : 100);
- trade_batches[i].take_profit *= (Digits == 2 ? 10 : 100);
- }
- }
- }
- void OnTick() {
- CountOrders();
- TrackOpenLong = false;
- TrackOpenShort = false;
- current_bid_price = Bid;
- current_ask_price = Ask;
- if ((long_index + short_index == 0 && BuyOrSell == 0) || (long_index > 0 && long_index < MaxTrades && CheckOpenPosition(OP_BUY))) {
- TrackOpenLong = true;
- TryOpenPosition(OP_BUY, long_index, OpenLong, TimeLong);
- }
- if ((long_index + short_index == 0 && BuyOrSell == 1) || (short_index > 0 && short_index < MaxTrades && CheckOpenPosition(OP_SELL))) {
- TrackOpenShort = true;
- TryOpenPosition(OP_SELL, short_index, OpenShort, TimeShort);
- }
- if (!TrackOpenLong) ResetTrack(OpenLong, TimeLong);
- if (!TrackOpenShort) ResetTrack(OpenShort, TimeShort);
- O_Modify(OP_BUY);
- O_Modify(OP_SELL);
- }
- bool CheckOpenPosition(int positionSide) {
- double reference_price = positionSide == OP_BUY ? current_ask_price : current_bid_price;
- double last_open_price = positionSide == OP_BUY ? G_order_open_price_352 : G_order_open_price_360;
- return (positionSide == OP_BUY ? last_open_price - reference_price : reference_price - last_open_price) >= trade_batches[positionSide == OP_BUY ? long_index - 1 : short_index - 1].price_interval * Point;
- }
- void TryOpenPosition(int positionSide, int &index, bool &isOpen, datetime &trackTime) {
- if (index == 0 || isOpen && TimeCurrent() - trackTime >= sliptime * TimeSpanSeconds(M1)) {
- isOpen = true;
- double order_price = positionSide == OP_BUY ? Ask : Bid;
- int ticket = OrderSend(Symbol(), positionSide, trade_batches[positionSide == OP_BUY ? long_index : short_index].lot_size, order_price, slippage * Point, 0, 0, Commentt, MagicN, 0, positionSide == OP_BUY ? Blue : Red);
- if (ticket > 0) {
- isOpen = false;
- BuyOrSell = positionSide == OP_BUY ? TRUE : FALSE;
- index++;
- }
- }
- }
- void ResetTrack(bool &isOpen, datetime &trackTime) {
- isOpen = false;
- trackTime = 0;
- }
- // ... 保留原有的O_Modify函数和其他辅助函数 ...
复制代码
|
|