Hello folks.
I'm trying to play with MT4 programming a bit.
In this case I just tried to modify BenTrailStop so that I have an expert, which just opens a position as soon as it's started and then closes the trade whenever the trailing rules of BenTrailStop make it close.
Somewhat it compiles just fine, opens an order, but never adjusts S/L or trailing stop. So the position can go to +1000, and drop down all the way until the Account is wiped out.
I'm afraid, it's just a minor thing, but I really need some advise here, because I'm just stuck since days.
Greetings.
MikeFT
I'm trying to play with MT4 programming a bit.
In this case I just tried to modify BenTrailStop so that I have an expert, which just opens a position as soon as it's started and then closes the trade whenever the trailing rules of BenTrailStop make it close.
Somewhat it compiles just fine, opens an order, but never adjusts S/L or trailing stop. So the position can go to +1000, and drop down all the way until the Account is wiped out.
I'm afraid, it's just a minor thing, but I really need some advise here, because I'm just stuck since days.
Greetings.
MikeFT
Inserted Code
#property copyright "NAME here" #property link "WEB here" #include <stdlib.mqh> #include <WinUser32.mqh> string ProgName = "MY_1st try"; string ProgVersion = "vXXX"; extern double Lots = 1.0; // change this number to change the lotsize double LastError; int TotalOrders; //Ende meiner Variablen //Start der Variablen für BenTrailStop extern int MagicNumber = 0; extern bool Own_Symbol_Only = true; extern double Trail_From = 15.0; extern double Trail_Max = 15.0; extern double Trail_Percent = 66; double Pip; int init() { } int deinit() { } int start() { if (OrdersTotal() < 1){ OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,"Text",0,0,CLR_NONE); } //-------------------------------------------------------------------------------------------- //Here starts the Ben Trail Stop //-------------------------------------------------------------------------------------------- { //------------------------------------------------------------------- bool mod; int err; // for better error reporting double My_Profit, My_Trail, My_SL, lTrail_Max, lTrail_From, Stop_Level; //---- for (int ii = 0; ii < OrdersTotal(); ii++){ if (OrderSelect(ii,SELECT_BY_POS,MODE_TRADES)){ if(Own_Symbol_Only && OrderSymbol() != Symbol())continue; if(OrderMagicNumber() == MagicNumber || MagicNumber == 0){ RefreshRates(); Pip = MarketInfo(OrderSymbol(),MODE_POINT); if (MarketInfo(OrderSymbol(),MODE_DIGITS) == 3|| MarketInfo(OrderSymbol(),MODE_DIGITS) == 5)Pip*=10; lTrail_Max = Trail_Max * Pip; lTrail_From = Trail_From * Pip; Stop_Level = MarketInfo(OrderSymbol(),MODE_STOPLEVEL)*Pip; switch(OrderType()){ case OP_BUY : My_Profit = MarketInfo(OrderSymbol(), MODE_BID) - OrderOpenPrice(); My_Trail = MathMin(My_Profit * Trail_Percent/100,lTrail_Max); My_SL = NormalizeDouble(MarketInfo(OrderSymbol(),MODE_BID)-My_Trail,Digits); if(My_Profit > lTrail_From){ if(MarketInfo(OrderSymbol(),MODE_BID) - My_SL > Stop_Level){ //+------------------------------------------------------------------+ // This will cause a double OrderModify if OrderStopLoss() returns 0 // Combining both tests fixes the bug that returns error 1. // // if(OrderStopLoss() == 0)mod = OrderModify(OrderTicket(),OrderOpenPrice(),My_SL,OrderTakeProfit(),0, CLR_NONE); //+------------------------------------------------------------------+ if(OrderStopLoss() < My_SL || OrderStopLoss() == 0) mod = OrderModify(OrderTicket(),OrderOpenPrice(),My_SL,OrderTakeProfit(),0, CLR_NONE); } } break; case OP_SELL : My_Profit = OrderOpenPrice() - MarketInfo(OrderSymbol(), MODE_ASK); My_Trail = MathMin(My_Profit * Trail_Percent/100,lTrail_Max); My_SL = NormalizeDouble(MarketInfo(OrderSymbol(),MODE_ASK)+My_Trail,Digits); if(My_Profit > lTrail_From){ if(My_SL - MarketInfo(OrderSymbol(),MODE_ASK) > Stop_Level){ // Combined these as well for cleaner code // if(OrderStopLoss() == 0)mod = OrderModify(OrderTicket(),OrderOpenPrice(),My_SL,OrderTakeProfit(),0,CLR_NONE); if(My_SL < OrderStopLoss() || OrderStopLoss() == 0) mod = OrderModify(OrderTicket(),OrderOpenPrice(),My_SL,OrderTakeProfit(),0,CLR_NONE); } } break; } // Modified to output a text description of the error // if(!mod && GetLastError() > 1)Print("Error entering Trailing Stop - Error " + GetLastError()); if(!mod) { err = GetLastError(); if (err > 1) Print("Error entering Trailing Stop - Error (" + err + "} " + ErrorDescription(err) ); } } } else Print("Error selecting order"); } //---- return(0); } }