Gamestudio Links
Zorro Links
Newest Posts
MT5 bridge not working on MT5 v. 5 build 4160
by EternallyCurious. 04/25/24 20:49
Data from CSV not parsed correctly
by EternallyCurious. 04/25/24 10:20
Trading Journey
by howardR. 04/24/24 20:04
M1 Oversampling
by Petra. 04/24/24 10:34
Zorro FIX plugin - Experimental
by flink. 04/21/24 07:12
Scripts not found
by juergen_wue. 04/20/24 18:51
zorro 64bit command line support
by 7th_zorro. 04/20/24 10:06
StartWeek not working as it should
by jcl. 04/20/24 08:38
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
4 registered members (EternallyCurious, AndrewAMD, TipmyPip, Quad), 902 guests, and 8 spiders.
Key: Admin, Global Mod, Mod
Newest Members
Mega_Rod, EternallyCurious, howardR, 11honza11, ccorrea
19048 Registered Users
Previous Thread
Next Thread
Print Thread
Rating: 5
Page 1 of 2 1 2
FGProBot #418202
02/22/13 06:39
02/22/13 06:39
Joined: Nov 2012
Posts: 209
S
SFF Offline OP
Member
SFF  Offline OP
Member
S

Joined: Nov 2012
Posts: 209
Hi,

I have a source code of this EA(1st version) and want to convert it and how it performs as it is profitable.
We can learn something from this EA so I post here.

I just made a incomplete Zorro code and if someone writes complete version based the original, it would be great.

The EA is found here(no source code now).
http://worldwide-invest.org/threads/16707-share-FGProBot-EA-o)-by-pipdigger


The imcomplete code.
Code:
int    SlowVolatilityOffset     = 3;
int    FastVolatilityBase       = 5;
int    SlowVolatilityBase       = 58;
var VolatilityFactor         = 2.2;
int    ProfitLossVolatilityBase = 46;
var ProfitTarget             = 0.43;
var StopLoss                 = 0.16;
int    FridayStopHour           = 16;

var VF(int Fa, int Sa)
{
   var sumF = 0.0, sumS = 0.0;
   int i;
   
   for (i = 1; i <= Fa; i++)
      sumF += priceClose(i) - priceOpen(i);
   for (i = 1; i <= Sa; i++)
      sumS += abs(priceClose(i + SlowVolatilityOffset) - priceOpen(i + SlowVolatilityOffset));
   
   return((sumF / Fa) / (sumS / Sa));
   }
   
   bool IsTradeTime()
{
   if (dow() >= 5 && FridayStopHour >= 0 && hour() >= FridayStopHour)
      return(false);
   else
      return(true);
      }

function run(){
	
set(TICKS);
	


var VolFac = VF(FastVolatilityBase, SlowVolatilityBase);
var Range = HH(ProfitLossVolatilityBase) - LL(ProfitLossVolatilityBase);


if(abs(VolFac) > VolatilityFactor){

      if(IsTradeTime() && VolFac > 0.0){
      	TakeProfit = Range*0.43;
      	Stop = Range*0.16;
enterLong();
}
else if(IsTradeTime() && VolFac < 0.0){
	TakeProfit = Range*0.43;
	Stop = Range*0.16;
	enterShort();
}
}
}



The original code.
Code:
//+------------------------------------------------------------------+
//|                            FGProBot 0.9 by pip.digger 2013.01.30 |
//|                                                                  |
//|                                                                  |
//| inspired by     Pro_Bot by HotFx 2012.08.06, http://hotfx.0pk.ru |
//|                                       http://hot-fx.blogspot.com |
//| and                ForexGrowthBot, http://www.ForexGrowthBot.com |
//+------------------------------------------------------------------+
#property copyright "FGProBot 0.9 by pip.digger 2013.01.30"
#property link "wwi.org"

// externals
extern double LotSize                  = 0.1;
extern double Risk                     = 1.0;
extern int    FridayStopHour           = 16;
bool   HardTPSL                 = TRUE;
extern int    FastVolatilityBase       = 5;
extern int    SlowVolatilityBase       = 58;
extern int    SlowVolatilityOffset     = 3;
extern double VolatilityFactor         = 2.2;
extern double ProfitTarget             = 0.43;
extern double StopLoss                 = 0.16;
extern int    ProfitLossVolatilityBase = 46;
extern string BotComment               = "FGProBot 0.9 by pip.digger";
extern int    Magic                    = 20130130;
extern int    Slippage                 = 3;

// globals
double MinLot, MaxLot, LotDigits, Range;
datetime bt = 0;
string symb, txt = "\n FGProBot 0.9 by pip.digger \n Lot: ";

int init()
{
   symb = Symbol();

	if(Digits%2==1) Slippage *= 10;
   
   LotDigits = MathLog(MarketInfo(symb, MODE_LOTSTEP)) / MathLog(0.1);
   MinLot = MarketInfo(symb, MODE_MINLOT);
   MaxLot = MarketInfo(symb, MODE_MAXLOT);
}

int start()
{
   double VolFac, rg;
   int i;
   static int ticket;
   
   // only on new bar openening...
   if(bt != Time[0]) {
      bt = Time[0];
      
      VolFac = VF(FastVolatilityBase, SlowVolatilityBase);
      Range = High[iHighest(symb, 0, MODE_HIGH, ProfitLossVolatilityBase, 1)] - Low[iLowest(symb, 0, MODE_LOW, ProfitLossVolatilityBase, 1)];

      // check for any trades to close by new bar opening (example here, trades w/o TP or SL assigned)
      // can also be used to implement other (dynamic) closing strategies
      for(i = OrdersTotal() - 1; i >= 0; i--){
         if(OrderSelect(i, SELECT_BY_POS))
            if(OrderSymbol() == symb && OrderMagicNumber() == Magic)
               if(OrderTakeProfit() == 0.0) // check if TP has been reached
                  switch(OrderType()) {
                     case OP_BUY:
                        if(Bid >= OrderOpenPrice() + RangeFromComment() * ProfitTarget)
                           OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, Blue);
                        break;
                     case OP_SELL:
                        if(Ask <= OrderOpenPrice() - RangeFromComment() * ProfitTarget)
                           OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, Red);
                        break;
                  }
               if (OrderStopLoss() == 0.0) // check if SL has been reached
                  switch(OrderType()) {
                     case OP_BUY:
                        if(Bid <= OrderOpenPrice() - RangeFromComment() * StopLoss)
                           OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, Blue);
                        break;
                     case OP_SELL:
                        if(Ask >= OrderOpenPrice() + RangeFromComment() * StopLoss)
                           OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, Red);
                        break;
                  }
      }
            
      Comment(txt, Lot());
   }
   
   // place new trade if prerequisites are met
   if(IsTradeTime() && MathAbs(VolFac) > VolatilityFactor)
      if(VolFac > 0.0) {
         ticket = OrderSend(symb, OP_BUY, Lot(), ND(Ask), Slippage, 0.0, 0.0, StringConcatenate(DoubleToStr(Range/Point, 0), " - ",  BotComment), Magic, 0, Blue);
         if(ticket != -1)
            VolFac = 0.0;
      } else if(VolFac < 0.0) {
         ticket = OrderSend(symb, OP_SELL, Lot(), ND(Bid), Slippage, 0.0, 0.0, StringConcatenate(DoubleToStr(Range/Point, 0), " - ", BotComment), Magic, 0, Red);
         if(ticket != -1)
            VolFac = 0.0;
      }
   
   // set hard TP & SL for last opened trade
   if(HardTPSL && OrderSelect(ticket, SELECT_BY_TICKET))
      if(OrderSymbol() == symb && OrderMagicNumber() == Magic)
         switch(OrderType()) {
            case OP_BUY:
               if(OrderModify(ticket, OrderOpenPrice(), ND(Ask - Range * StopLoss), ND(Ask + Range * ProfitTarget), 0, Blue))
                  ticket = -1;
               break;
            case OP_SELL:
               if(OrderModify(ticket, OrderOpenPrice(), ND(Bid + Range * StopLoss), ND(Bid - Range * ProfitTarget), 0, Red))
                  ticket = -1;
               break;
            default:
               ticket = -1;
         }
      
   return(0);
}

// return lotsize for next trade (currently here, per 1000 free margin)
double Lot()
{
   double Lots;
   
   if(Risk > 0.0)
      Lots = AccountFreeMargin() * (Risk/100.0) / ((Range * StopLoss / Point) * MarketInfo(symb, MODE_TICKVALUE));
   else
      Lots = LotSize;
   
   return(NormalizeDouble(MathMin(MathMax(MinLot, Lots), MaxLot), LotDigits));
}

// normalize double to accuracy of Digits of current symbol price
double ND(double val)
{
   return (NormalizeDouble(val, Digits));
}

// calculate volatility (acceleration) factor
double VF(int Fa, int Sa)
{
   double sumF = 0.0, sumS = 0.0;
   
   for (int i = 1; i <= Fa; i++)
      sumF += Close[i] - Open[i];
   for (i = 1; i <= Sa; i++)
      sumS += MathAbs(Close[i + SlowVolatilityOffset] - Open[i + SlowVolatilityOffset]);
   
   return((sumF / Fa) / (sumS / Sa));
}

// trading time filter
bool IsTradeTime()
{
   if (DayOfWeek() >= 5 && FridayStopHour >= 0 && Hour() >= FridayStopHour)
      return(false);
   else
      return(true);
}

// extract range from trade comment
double RangeFromComment()
{
   return(StrToDouble(StringSubstr(OrderComment(), 0, StringFind(OrderComment(), " - "))) * Point);
}


Last edited by SFF; 02/24/13 01:23.
Re: FGProBot [Re: SFF] #418208
02/22/13 08:24
02/22/13 08:24
Joined: Jul 2000
Posts: 27,982
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,982
Frankfurt
Your incomplete code looks good at a first glance - you're definitely making progress. What's the problem with completing the code?

Be aware though that the parameter setup of this EA looks like heavy curve overfitting. So take any claims about the backtest performance with a grain of salt.

Re: FGProBot [Re: jcl] #418217
02/22/13 09:43
02/22/13 09:43
Joined: Nov 2012
Posts: 209
S
SFF Offline OP
Member
SFF  Offline OP
Member
S

Joined: Nov 2012
Posts: 209
Thanks, I am learning everyday.

I don't know MQL much and For Zorro I am still trying how to code "Lot" correctly.
and "IsTradeTime".

Could you please a simple code for them?

In MQL, I don't know what they mean - "RangeFromComment()" and "ND()"

About this code, Do I need to use a imcomplete bar in this script?
if(bt != Time[0]) {
bt = Time[0];
}

These type of volatility EAs is so popular now.
What do you think of them? Are they really profitable for long term?

Last edited by SFF; 02/22/13 09:46.
Re: FGProBot [Re: SFF] #418319
02/23/13 17:10
02/23/13 17:10
Joined: Sep 2003
Posts: 929
Spirit Offline

Moderator
Spirit  Offline

Moderator

Joined: Sep 2003
Posts: 929
Lot needs not be coded, use Margin instead. IsTradeTime is the same in Zorro, check out the description of dow() and hour(). I think ND is only a fix for some MT4 bug and not needed with Zorro, and RangeFromComment is for storing a variable in the trade struct, Zorro uses "TradeVar" for this.

Re: FGProBot [Re: Spirit] #418355
02/24/13 01:25
02/24/13 01:25
Joined: Nov 2012
Posts: 209
S
SFF Offline OP
Member
SFF  Offline OP
Member
S

Joined: Nov 2012
Posts: 209
Thank you Spirit.

I added IsTradeTime() and TP/SL and It works fine.
Lot management still needed.

Less than a 60 min bar on EUR/USD, It showed a good result in back test.

Re: FGProBot [Re: Spirit] #418356
02/24/13 03:57
02/24/13 03:57
Joined: Sep 2012
Posts: 99
T
TankWolf Offline
Junior Member
TankWolf  Offline
Junior Member
T

Joined: Sep 2012
Posts: 99
I played around with it, no matter what I tried the strategy didnt seem profitable to me but here is the base code if you wish to play around with it. My only query is should "VolFac" be a var or a series. Seems more logical to me this should be a series of different vol factors unless the VF function causes the "VolFac" variable to change all the time otherwise wouldnt it only compare against the same vol factor value every comparision?

Quote:

int FridayStopHour = 16;
int SlowVolatilityOffset = 3;

var VF(int Fa, int Sa)
{
var sumF = 0.0, sumS = 0.0;
int i;

for (i = 1; i <= Fa; i++)
sumF += priceClose(i) - priceOpen(i);
for (i = 1; i <= Sa; i++)
sumS += abs(priceClose(i + SlowVolatilityOffset) - priceOpen(i + SlowVolatilityOffset));

return((sumF / Fa) / (sumS / Sa));
}

bool IsTradeTime()
{
if (dow(0) >= 5 && FridayStopHour >= 0 && hour(0) >= FridayStopHour)
return(false);
else
return(true);
}

function run() {
set(TICKS+TESTNOW);
BarPeriod = 15;
LookBack = 100;

int FastVolatilityBase = 5;
int SlowVolatilityBase = 58;
var VolatilityFactor = 2.2;
int ProfitLossVolatilityBase = 46;
var VolFac = VF(FastVolatilityBase, SlowVolatilityBase);

Stop = 0.16*ATR(ProfitLossVolatilityBase);
TakeProfit = 0.43*ATR(ProfitLossVolatilityBase);

if(IsTradeTime()&& abs(VolFac) > VolatilityFactor) {

if(VolFac > 0.0)
enterLong();
else if(VolFac < 0.0)
enterShort();
}
}

Re: FGProBot [Re: TankWolf] #418357
02/24/13 04:06
02/24/13 04:06
Joined: Sep 2012
Posts: 99
T
TankWolf Offline
Junior Member
TankWolf  Offline
Junior Member
T

Joined: Sep 2012
Posts: 99
Hmmm played around a bit more my interperation of the range value may of been wrong to calculate TP & SL. Here is updated code without Walk Forward that shows a decent profit.

Quote:

int FridayStopHour = 16;
int SlowVolatilityOffset = 3;

var VF(int Fa, int Sa)
{
var sumF = 0.0, sumS = 0.0;
int i;

for (i = 1; i <= Fa; i++)
sumF += priceClose(i) - priceOpen(i);
for (i = 1; i <= Sa; i++)
sumS += abs(priceClose(i + SlowVolatilityOffset) - priceOpen(i + SlowVolatilityOffset));

return((sumF / Fa) / (sumS / Sa));
}

bool IsTradeTime()
{
if (dow(0) >= 5 && FridayStopHour >= 0 && hour(0) >= FridayStopHour)
return(false);
else
return(true);
}

function run() {
set(TICKS+TESTNOW);
BarPeriod = 15;
LookBack = 100;

int FastVolatilityBase = 5;
int SlowVolatilityBase = 58;
var VolatilityFactor = 2.2;
int ProfitLossVolatilityBase = 46;
var VolFac = VF(FastVolatilityBase, SlowVolatilityBase);
var Range = HH(ProfitLossVolatilityBase) - LL(ProfitLossVolatilityBase);

Stop = 0.16*Range;
TakeProfit = 0.43*Range;

if(IsTradeTime()&& abs(VolFac) > VolatilityFactor) {

if(VolFac > 0.0)
enterLong();
else if(VolFac < 0.0)
enterShort();
}
}


Question still stands in regards to the VolFac & the Range variable now though.

Re: FGProBot [Re: TankWolf] #418358
02/24/13 04:17
02/24/13 04:17
Joined: Nov 2012
Posts: 209
S
SFF Offline OP
Member
SFF  Offline OP
Member
S

Joined: Nov 2012
Posts: 209
Thank you for your code.

Your updated code shows same result as my version.
Could you implement a lot management?

By the way,
ProBot also shows a decent profit.
You can code it easily as it is a simple and short code.
http://hot-fx.blogspot.jp/2012/08/probot.html

Last edited by SFF; 02/24/13 04:24.
Re: FGProBot [Re: SFF] #418360
02/24/13 04:29
02/24/13 04:29
Joined: Sep 2012
Posts: 99
T
TankWolf Offline
Junior Member
TankWolf  Offline
Junior Member
T

Joined: Sep 2012
Posts: 99
Yeah Im looking into optimizing & setting up optimal margin factors will post when I get the best result I can find.

That probot site is in Russian I cant read that haha. tongue

Last edited by TankWolf; 02/24/13 04:33.
Re: FGProBot [Re: TankWolf] #418375
02/24/13 11:19
02/24/13 11:19
Joined: Nov 2012
Posts: 209
S
SFF Offline OP
Member
SFF  Offline OP
Member
S

Joined: Nov 2012
Posts: 209
Here is a source code of the latest version.
Someone could convert it.

Code:
//+------------------------------------------------------------------+
//|                           FGProBot 0.9.5 &#65385; pip.digger 2013.02.22 |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "FGProBot 0.9.5 &#65385; pip.digger 2013.02.22"
#property link "WwI.org"

// externals
extern string EA_Configuration         = "==== EA Configuration ====";
extern int    Magic                    = 20130130;
extern string BotComment               = "0.9.5";
extern string TradeSettings            = "==== Trade settings ====";
extern int    Slippage                 = 3;
extern string TimeSettings             = "==== Time settings ====";
extern int    MondayStartHour          = 11;
extern int    FridayStopHour           = 18;
extern string OpenSettings             = "==== Open trade settings ====";
extern int    FastVolatilityBase       = 10;
extern int    SlowVolatilityBase       = 50;
extern int    SlowVolatilityOffset     = 4;
extern double VolatilityFactor         = 1.5;
extern bool   OpenWithTrendOnly        = false;
extern string CloseSettings            = "==== Close trade settings ====";
extern double StopLoss                 = 0.23;
extern double RiskRewardRatio          = 2.1;
extern int    RangeMode                = 2;
extern int    RangeMaxBase             = 16;
extern int    RangeMinBase             = 10;
extern bool   NormalizeRange           = true;
extern bool   MoveTPSL                 = true;
extern bool   UseProfitLock            = true;
extern double ProfitLockTrigger        = 0.85;
extern double ProfitLockPercent        = 0.8;
extern bool   UseVolaClose             = true;
extern double VolaCloseTrigger         = 0.7;
extern double VolaCloseMinProfit       = 0.3;
 bool   UseTrailingStop          = false;
 double TrailingStopTrigger      = 0.5;
 int    ATRBase                  = 7;
 double ATRFactor                = 6;
extern string Money_Management         = "==== Money Management ====";
extern double LotSize                  = 0.1;
extern double Risk                     = 1.0;
extern bool   RiskBooster              = true;
extern double BoosterPower             = 0.8;
extern double BoosterCap               = 5.0;

// globals
double MinLot, MaxLot, LotDigits, StpLvl, Range;
datetime bt = 0;
int lm = -1, ld = -1;
string symb, txt = "\n FGProBot 0.9.5 &#65385; pip.digger \n Lot: ";

int init()
{
   symb = Symbol();

	if(Digits%2==1) Slippage *= 10;
   
   LotDigits = MathLog(MarketInfo(symb, MODE_LOTSTEP)) / MathLog(0.1);
   MinLot = MarketInfo(symb, MODE_MINLOT);
   MaxLot = MarketInfo(symb, MODE_MAXLOT);
   StpLvl = MarketInfo(symb, MODE_STOPLEVEL);
}

int start()
{
   double VolFac, x = 1.0, Dist;
   int i;
   static int ticket;
   string OrderCmt;
   
   // run every new bar openening
   if(bt != Time[0]) {
      bt = Time[0];
      
      VolFac = VF(FastVolatilityBase, SlowVolatilityBase);
      
      switch(RangeMode) {
         case 1:
            Range = High[iHighest(NULL, 0, MODE_HIGH, RangeMaxBase, 1)] - Low[iLowest(NULL, 0, MODE_LOW, RangeMaxBase, 1)];
            break;
         case 2:
            double avg = 0.0, avgt;
            for(i = 1; i <= RangeMinBase; i++)
               avg += High[i] - Low[i];
            avg /= RangeMinBase;
            for(i = RangeMinBase + 1; i <= RangeMaxBase; i++) {
               avgt = (avg * (i - 1) + (High[i] - Low[i])) / i;
               if(avgt > avg)
                  break;
               else
                  avg = avgt;
            }
            i--;
            Range = High[iHighest(NULL, 0, MODE_HIGH, i, 1)] - Low[iLowest(NULL, 0, MODE_LOW, i, 1)];
            break;
         case 3:
            if(ld != Day()) { // calc once per new day
               ld = Day();
               double a[100];
               int k = MathCeil(RangeMaxBase * 0.4);
               Range = 0.0;
               for(i = 0; i < RangeMaxBase; i++)
                  a[i] = iHigh(NULL, PERIOD_D1, i + 1) - iLow(NULL, PERIOD_D1, i + 1);
               ArraySort(a, RangeMaxBase, 0, MODE_DESCEND);
               for(i = 0; i < k; i++)
                  Range += a[i];
               Range /= k;
            }
            break;
      } // end switch RangeMode
      
      if(NormalizeRange) {
         for(i = OrdersTotal() - 1; i >= 0; i--) {
            if(OrderSelect(i, SELECT_BY_POS))
               if(OrderSymbol() == symb && OrderMagicNumber() == Magic) {
                  if(VolFac * VolFacFromComment() < 0.0) // if VF now and VF from date have different sign -> skip
                     continue;
                  Range = MathMin(Range / MathAbs(VolFac), RangeFromComment() / MathAbs(VolFacFromComment())) * MathAbs(VolFac);
               }
         }
      } // end if NormalizeRange
      
      // do something with opened trades on every new bar opening
      if(MoveTPSL) {
         for(i = OrdersTotal() - 1; i >= 0; i--)
            if(OrderSelect(i, SELECT_BY_POS))
               if(OrderSymbol() == symb && OrderMagicNumber() == Magic) {
                  double vfr = VolFac / VolFacFromComment();
                  if(vfr < 0.0) // if VF now and VF from date have different sign -> skip
                     continue;
                  switch(OrderType()) {
                     case OP_BUY:
                        if(vfr > 1.0) {
                           Dist = RangeFromComment() * StopLoss;
                           if(Ask + Dist * RiskRewardRatio > OrderTakeProfit())
                              OrderModify(OrderTicket(), OrderOpenPrice(), ND(Bid - Dist), ND(Ask + Dist * RiskRewardRatio), 0, Blue);
                        }
                        break;
                     case OP_SELL:
                        if(vfr > 1.0) {
                           Dist = RangeFromComment() * StopLoss;
                           if(Bid - Dist * RiskRewardRatio < OrderTakeProfit())
                              OrderModify(OrderTicket(), OrderOpenPrice(), ND(Ask + Dist), ND(Bid - Dist * RiskRewardRatio), 0, Red);
                        }
                        break;
                  } // end switch
               } // end symbol and magic check
      } // end MoveTPSL
      
      if(UseVolaClose) // Close open trades if Vola falls below VolaCloseTrigger * VolatilityFactor
         if(VolFac / VolatilityFactor < VolaCloseTrigger)
            for(i = OrdersTotal() - 1; i >= 0; i--)
               if(OrderSelect(i, SELECT_BY_POS))
                  if(OrderSymbol() == symb && OrderMagicNumber() == Magic)
                     switch(OrderType()) {
                        case OP_BUY:
                           if(Bid > OrderOpenPrice() + (OrderTakeProfit() - OrderOpenPrice()) * VolaCloseMinProfit)
                              OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, Blue);
                           break;
                        case OP_SELL:
                           if(Ask < OrderOpenPrice() - (OrderOpenPrice() - OrderTakeProfit()) * VolaCloseMinProfit)
                              OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, Red);
                           break;
                     } // end switch
      
      Comment(txt, Lot());
   } // end run every new bar openening
   
   // place new trade if prerequisites are met
   if(IsTradeTime() && MathAbs(VolFac) > VolatilityFactor && !(OpenWithTrendOnly && (CheckOpenWithTrend() * VolFac > 0.0))) {
      OrderCmt = StringConcatenate("vf[", DoubleToStr(VolFac, 2), "] rg[", DoubleToStr(Range/Point, 0), "] ", BotComment);
      if(RiskBooster) {
         x = MathPow(MathAbs(VolFac) / VolatilityFactor, BoosterPower);
         if(BoosterCap > 1.0)
            x = MathMin(x, BoosterCap);
      }
      if(VolFac > VolatilityFactor) {
         ticket = OrderSend(symb, OP_BUY, Lot(x), Ask, Slippage, 0.0, 0.0, OrderCmt, Magic, 0, Blue);
         if(ticket != -1)
            VolFac = 0.0;
      } else if(VolFac < (-VolatilityFactor)) {
         ticket = OrderSend(symb, OP_SELL, Lot(x), Bid, Slippage, 0.0, 0.0, OrderCmt, Magic, 0, Red);
         if(ticket != -1)
            VolFac = 0.0;
      }
   }
   
   // set hard TP & SL for last opened trade
   if(OrderSelect(ticket, SELECT_BY_TICKET))
      if(OrderSymbol() == symb && OrderMagicNumber() == Magic)
         switch(OrderType()) {
            case OP_BUY:
               if(OrderModify(ticket, OrderOpenPrice(), ND(Bid - MathMax(Range * StopLoss, StpLvl * Point)), ND(Ask + Range * StopLoss * RiskRewardRatio), 0, Blue))
                  ticket = -1;
               break;
            case OP_SELL:
               if(OrderModify(ticket, OrderOpenPrice(), ND(Ask + MathMax(Range * StopLoss, StpLvl * Point)), ND(Bid - Range * StopLoss * RiskRewardRatio), 0, Red))
                  ticket = -1;
               break;
            default:
               ticket = -1;
         }
   
   // run every minute
   if(lm != Minute()) {
      lm = Minute();
      
      if(UseProfitLock) { // set SL to ProfitLockPercent * PriceDelta, when ProfitLockTrigger / TP has been reached
         for(i = OrdersTotal() - 1; i >= 0; i--)
            if(OrderSelect(i, SELECT_BY_POS))
               if(OrderSymbol() == symb && OrderMagicNumber() == Magic)
                  switch(OrderType()) {
                     case OP_BUY:
                        Dist = OrderTakeProfit() - OrderOpenPrice();
                        x = (OrderStopLoss() - OrderOpenPrice()) / Dist;
                        Dist *= ProfitLockTrigger;
                        if(x < ProfitLockTrigger * ProfitLockPercent && Bid > OrderOpenPrice() + Dist)
                           OrderModify(OrderTicket(), OrderOpenPrice(), ND(OrderOpenPrice() + Dist * ProfitLockPercent), OrderTakeProfit(), 0, Blue);
                        break;
                     case OP_SELL:
                        Dist = OrderOpenPrice() - OrderTakeProfit();
                        x = (OrderOpenPrice() - OrderStopLoss()) / Dist;
                        Dist *= ProfitLockTrigger;
                        if(x < ProfitLockTrigger * ProfitLockPercent && Ask < OrderOpenPrice() - Dist)
                           OrderModify(OrderTicket(), OrderOpenPrice(), ND(OrderOpenPrice() - Dist * ProfitLockPercent), OrderTakeProfit(), 0, Red);
                        break;
                  } // end switch
      } // end UseProfitLock
      
      if(UseTrailingStop) { // Trail with ATR based SL, when TrailingStopTrigger / TP has been reached
         Dist = ATRFactor * iATR(NULL, 0, ATRBase, 0);
         
         for(i = OrdersTotal() - 1; i >= 0; i--)
            if(OrderSelect(i, SELECT_BY_POS))
               if(OrderSymbol() == symb && OrderMagicNumber() == Magic)
                  switch(OrderType()) {
                     case OP_BUY:
                        if((Bid - OrderOpenPrice()) / (OrderTakeProfit() - OrderOpenPrice()) > TrailingStopTrigger)
                           if(Bid - OrderStopLoss() > Dist)
                              OrderModify(OrderTicket(), OrderOpenPrice(), ND(Bid - Dist), OrderTakeProfit(), 0, Blue);
                        break;
                     case OP_SELL:
                        if((OrderOpenPrice() - Ask) / (OrderOpenPrice() - OrderTakeProfit()) > TrailingStopTrigger)
                           if(OrderStopLoss() - Ask > Dist)
                              OrderModify(OrderTicket(), OrderOpenPrice(), ND(Ask + Dist), OrderTakeProfit(), 0, Red);
                        break;
                  } // end switch
      } // end UseTrailingStop
   } // end run every minute
   
   return(0);
}

// return lotsize for next trade
double Lot(double x = 1.0)
{
   if(Risk > 0.0)
      x *= AccountFreeMargin() * (Risk/100.0) / ((Range * StopLoss / Point) * MarketInfo(symb, MODE_TICKVALUE));
   else
      x = LotSize;
   
   return(NormalizeDouble(MathMin(MathMax(MinLot, x), MaxLot), LotDigits));
}

// normalize double to accuracy of Digits of current symbol price
double ND(double val)
{
   return (NormalizeDouble(val, Digits));
}

// calculate volatility (acceleration) factor
double VF(int Fa, int Sa)
{
   int i;
   double sumF = 0.0, sumS = 0.0, k;
   
   for (i = 1; i <= Sa; i++)
      sumS += MathAbs(Close[i + SlowVolatilityOffset] - Open[i + SlowVolatilityOffset]);
   sumS /= Sa;
   
   k = 2.0 / (Fa + 1.0);
   for (i = 1; i <= Fa; i++)
      sumF += MathPow((1.0 - k), (i - 1)) * (Close[i] - Open[i]);
   sumF *= k;

   return(sumF/sumS);
}

// trading time filter
bool IsTradeTime()
{
   if(MondayStartHour >= 0)
      if(DayOfWeek() == 0 || DayOfWeek() == 1 && Hour() < MondayStartHour)
         return(false);
   
   if(FridayStopHour >= 0)
      if(DayOfWeek() == 6 || DayOfWeek() == 5 && Hour() >= FridayStopHour)
         return(false);

   return(true);
}

// extract range from trade comment
double RangeFromComment()
{
   int start, length;
   
   start = StringFind(OrderComment(), "rg[") + 3;
   length = StringFind(OrderComment(), "]", start) - start;
   
   return(StrToDouble(StringSubstr(OrderComment(), start, length)) * Point);
}

// extract volfac from trade comment
double VolFacFromComment()
{
   int start, length;
   
   start = StringFind(OrderComment(), "vf[") + 3;
   length = StringFind(OrderComment(), "]", start) - start;
   
   return(StrToDouble(StringSubstr(OrderComment(), start, length)));
}

// check price relative to open orders, return values:
// -1 : do not place new sell order!
//  0 : new buy or sell oder OK
//  1 : do not place new buy order!
int CheckOpenWithTrend(double PrcDelta = 0.0)
{
   bool IsBelowLowest = true, IsAboveHighest = true, ExistSellOrder = false, ExistBuyOrder = false;
   
   for(int i = OrdersTotal() - 1; i >= 0; i--)
      if(OrderSelect(i, SELECT_BY_POS))
         if(OrderSymbol() == symb && OrderMagicNumber() == Magic)
            switch(OrderType()) {
               case OP_BUY:
                  ExistBuyOrder = true;
                  if(Ask <= OrderOpenPrice() + PrcDelta)
                     IsAboveHighest = false;
                  break;
               case OP_SELL:
                  ExistSellOrder = true;
                  if(Bid >= OrderOpenPrice() + PrcDelta)
                     IsBelowLowest = false;
                  break;
            }
   if(ExistSellOrder && !IsBelowLowest)
      return(-1);
   else if (ExistBuyOrder && !IsAboveHighest)
      return(1);
   else
      return(0);
}


Page 1 of 2 1 2

Moderated by  Petra 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1