Nanningbob 10.2 auto-trading EA

Share your 10.x automated traders here.
Locked
User avatar
SteveHopwood
Owner
Posts: 9904
Joined: Tue Nov 15, 2011 8:43 am
Location: Misterton - an insignificant village in England. Very pleasant to live in.

Re: TradeWeeklyPivotCross

Post by SteveHopwood »

phil_trade wrote:Hi Steve

You Described in post 1 the TradeWeeklyPivotCross

- Trading conditions are:
- either both Slopes must be ranging or any part of the D1 candle must be trading within the moving averages.

and the code is

Code: Select all

void LookForWeeklyCrossRangeTrade()
{
   //Called from LookForTradingOpportunities() if magic = 0, indicating no trades have been triggered by other systems.
   //Looks for D1/H4 Slopes both ranging, or some part of the D1 candle tradubg within the moving averages
   
   if (!TradeRange) return;//Usaer option to trade this way.
      
   //Only allow one open trade when the market is ranging.
   if (OpenTrades > 0) return;
   
   bool NowRanging = false;
   
   if (D1SlopeTrend == ranging && LtfSlopeTrend == ranging) NowRanging = true;
   if (!NowRanging)
   {
      double Hma = GetHighestMovingAverage(PERIOD_D1);
      double Lma = GetLowestMovingAverage(PERIOD_D1);
   }//if (!NowRanging)
   
   //Is any part of the D1 candle trading within the moving averages?
   if (iLow(NULL, PERIOD_D1, 0) > Hma || iHigh(NULL, PERIOD_D1, 0) < Lma) return;
I suspect a problem if NowRanging =true because D1Slope and LtfSlopeTrend are ranging
This code line are not executed
if (!NowRanging)
{
double Hma = GetHighestMovingAverage(PERIOD_D1);
double Lma = GetLowestMovingAverage(PERIOD_D1);
}//if (!NowRanging)

-> Hma and Lma not declared and suppose = 0 ??
-> iLow(NULL, PERIOD_D1, 0) > Hma always true
so if D1Slope and LtfSlope ranging, it's impossible to have WPC range Trade

I suggest
-> Always declare Variable at top of Function/Procedure
and/or move code like this

Code: Select all

   if (!NowRanging)
   {
      double Hma = GetHighestMovingAverage(PERIOD_D1);
      double Lma = GetLowestMovingAverage(PERIOD_D1);
      
   //Is any part of the D1 candle trading within the moving averages?
   if (iLow(NULL, PERIOD_D1, 0) > Hma || iHigh(NULL, PERIOD_D1, 0) < Lma) return;      
      
   }//if (!NowRanging)
otherwise we can't have WPC trade on only D1Slope/LtfSlope Ranging

Am I right ?

Philippe
You are turning out to be a real star here, Philippe. Thanks again.

I have slightly re-ordered the code, and I think the extra comments explain this ok:

Code: Select all

   bool NowRanging = false;
   
   //Detect a range when both Slopes are ranging.
   if (D1SlopeTrend == ranging && LtfSlopeTrend == ranging) NowRanging = true;
   //detect a range if any part of the D1 candle trading within the moving averages?
   if (!NowRanging)
   {
      //Get highest and lowest moving averages
      double Hma = GetHighestMovingAverage(PERIOD_D1);
      double Lma = GetLowestMovingAverage(PERIOD_D1);
      //Is any part of the D1 candle trading within the moving averages?
      if (iLow(NULL, PERIOD_D1, 0) > Hma || iHigh(NULL, PERIOD_D1, 0) < Lma) return;
   }//if (!NowRanging)
Look OK?

Version 1p is in post 1 with this change.

:D
Read the effing manual, ok?

Afterprime is the official SHF broker. Read about them at https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?p=175790#p175790.

I still suffer from OCCD. Good thing, really.

Anyone here feeling generous? My paypal account is always in the market for a tiny donation. pianodoodler@hotmail.com is the account.

To see The Weekly Roundup of stuff you guys might have missed Click here

My special thanks to Thomas (tomele) for all the incredible work he does here.
phil_trade

Re: 10.2; the auto-trading EA is born.

Post by phil_trade »

a star...hmmm i'm just reading your code :oops:

I have try manually moving SL/TP line without any result and can't see why in code AdjustStopLoss() ?

Is it OK with your Empty4 ?
phil_trade

Re: 10.2; the auto-trading EA is born.

Post by phil_trade »

SteveHopwood wrote:
phil_trade wrote:Steve

I had from time to time error 130 on modifyOrder for SL and TP like for BUY EURCHF 22/05/2012 22H13 GMT WPCrange trade on SlopeRanging because SL and TP too near from Bid.
Bid 1.20117 and R1 1.2011 R2 1.2013 S1 1.2003 S2 1.2001

So the trade was passed without SL and TP !

Some criminal and some pairs have very wide STOPLEVEL (40 pips minimum for EURCHF)

so I suggest to modify code like below. This solve for EURCHF with a trade with SL at 1.20055 and TP 1.20157 but may be you will prefer not to trade when Bid too near calculated SL and TP.

Philippe

Code: Select all

double CalculateStopLoss(int type)
{
   //Returns the stop loss for use in LookForTradingOpps and InsertMissingStopLoss
   double stop, price;


   RefreshRates();
   
   double Stoplevel = MarketInfo(Symbol(),MODE_STOPLEVEL);

/*
#define  BbRangeTrade "BB Range trade"
*/
   if (type == OP_BUY)
   {
      price = Ask;
      if (StopLoss > 0) 
      {
         stop = NormalizeDouble(price - (StopLoss * Point), Digits);
         HiddenStopLoss = stop;
      }//if (StopLoss > 0) 

      if (StopLoss == 0)
      {
         //BB Range trade. Use Bb extent
         if (TradeOrigin == BbRangeTrade)
         {
            double extent = BbUpper - BbLower;
            stop = NormalizeDouble(BbLower - extent, Digits);
         }//if (TradeOrigin == BbRangeTrade)

         //Wpc range trade. Uses S1/S2 as sl
         if (TradeOrigin == WpcRangeTrade)
         {
            stop = WS2;
            if (VolatilityGroup < 4) stop = WS1;
         }//if (TradeOrigin == WpcRangeTrade)
      
         //D1 trend trade
         if (TradeOrigin == D1TrendTrade)
         {
            stop = MS2;
            if (VolatilityGroup < 4) stop = MS1;
         }//if (TradeOrigin == D1TrendTrade)
            
         //H4 trend trade
         if (TradeOrigin == H4TrendTrade)
         {
            stop = WS2;
            if (VolatilityGroup < 4) stop = WS1;
         }//if (TradeOrigin == H4TrendTrade)
         
       
      }//if (StopLoss == 0)
            
      if (HiddenPips > 0 && stop > 0) stop = NormalizeDouble(stop - (HiddenPips * Point), Digits);

      // Control stop level minima
      if (Bid-stop <= Stoplevel*Point) 
      {
      stop = Bid - (Stoplevel*Point);
      }

   }//if (type == OP_BUY)
   


   if (type == OP_SELL)
   {
      price = Bid;
      if (StopLoss > 0) 
      {
         stop = NormalizeDouble(price + (StopLoss * Point), Digits);
         HiddenStopLoss = stop;         
      }//if (StopLoss > 0) 

      if (StopLoss == 0)
      {
         //BB Range trade. Use Bb extent
         if (TradeOrigin == BbRangeTrade)
         {
            extent = BbUpper - BbLower;
            stop = NormalizeDouble(BbUpper + extent, Digits);
         }//if (TradeOrigin == BbRangeTrade)

         //Wpc range trade. Uses R1/R2 as sl
         if (TradeOrigin == WpcRangeTrade)
         {
            stop = WR2;
            if (VolatilityGroup < 4) stop = WR1;
         }//if (TradeOrigin == WpcRangeTrade)

         //D1 trend trade
         if (TradeOrigin == D1TrendTrade)
         {
            stop = MR2;
            if (VolatilityGroup < 4) stop = MR1;
         }//if (TradeOrigin == D1TrendTrade)

         //H4 trend trade
         if (TradeOrigin == H4TrendTrade)
         {
            stop = WR2;
            if (VolatilityGroup < 4) stop = WR1;
         }//if (TradeOrigin == H4TrendTrade)
      
    
      }//if (StopLoss == 0)
      
      if (HiddenPips > 0 && stop > 0) stop = NormalizeDouble(stop + (HiddenPips * Point), Digits);

      // Control stop level minima
      if (stop-Ask <= Stoplevel*Point) 
      {
      stop = Ask + (Stoplevel*Point);
      }

   }//if (type == OP_SELL)
   
   return(stop);
   
}//End double CalculateStopLoss(int type)

double CalculateTakeProfit(int type, string origin)
{
   //Returns the stop loss for use in LookForTradingOpps and InsertMissingStopLoss
   double take, price;

   RefreshRates();
   
   double Stoplevel = MarketInfo(Symbol(),MODE_STOPLEVEL);   
   
   if (type == OP_BUY)
   {
      price = Ask;
      if (TakeProfit > 0) 
      {
         take = NormalizeDouble(price + (TakeProfit * Point), Digits);
         HiddenTakeProfit = take;
      }//if (TakeProfit > 0) 

      //D1 trend trade
      if (TradeOrigin == D1TrendTrade)
      {
         //Override any take profit choice if D1SlopeTrend is buy and hold
         if (D1SlopeTrend == buyhold)
         {
            take = 0;
            AdaptedBreakEven = true;
            AdaptedJumpingStop = true;
         }//if (D1SlopeTrend == buyhold)

         //If D1 slope is not buy and hold, then set a tp based on the next full resistance level
         if (D1SlopeTrend != buyhold)
         {
            //Work down through the Monthly Resistance levels until Bid is lower than the resistance. 
            //I have assumed that the market will be above the support levels, and can amend this easily if not.
            if (price < MR3)
            {
               if (MR3 - price >= (MinD1TrendTradeTpPips * Point) ) take = MR3;
            }//if (price < MR3)
            
            if (price < MR2)
            {
               if (MR2 - price >= (MinD1TrendTradeTpPips * Point) ) take = MR2;
            }//if (price < MR3)

            if (price < MR1)
            {
               if (MR1 - price >= (MinD1TrendTradeTpPips * Point) ) take = MR1;
            }//if (price < MR3)
            
            if (price < MonthlyPivot)
            {
               if (MonthlyPivot - price >= (MinD1TrendTradeTpPips * Point) ) take = MonthlyPivot;
            }//if (price < MonthlyPivot)
         }//if (D1SlopeTrend == buyhold)
         
      }//if (TradeOrigin = D1Trend)
      
      //H4 trend trade
      if (TradeOrigin == H4TrendTrade)
      {
         //Override any take profit choice if D1SlopeTrend is buy and hold
         if (D1SlopeTrend == buyhold)
         {
            take = 0;
            AdaptedBreakEven = true;
            AdaptedJumpingStop = true;
         }//if (D1SlopeTrend == buyhold)
         
         //D1 trend is not buy and hold, so set a tp based on the next resistance. 
         //This is weekly R1 for pairs in groups 1 to 3, R2 for the others.
         if (D1SlopeTrend == buy)
         {
            take = WR2;
            if (VolatilityGroup < 4) take = WR1;
         }//if (D1SlopeTrend == buy)         
      }//if (TradeOrigin == H4TrendTrade)
      
      //BB Range trade. Use Weekly pivot tp. Range trade should only be sent at 
      //MinimumRangeTakeProfit from the weekly pivot
      if (TradeOrigin == BbRangeTrade)
      {
         take = WeeklyPivot;         
      }//if (TradeOrigin == RangeTrade)
      
      //Wpc range trade. Uses R1/R2 as tp
      if (TradeOrigin == WpcRangeTrade)
      {
         take = WR2;
         if (VolatilityGroup < 4) take = WR1;
      }//if (TradeOrigin == WpcRangeTrade)
      
      if (HiddenPips > 0 && take > 0) take = NormalizeDouble(take + (HiddenPips * Point), Digits);

       // Control stop level minima
       if (take-Ask <= Stoplevel*Point) 
       {
       take = Ask + (Stoplevel*Point);
       }


   }//if (type == OP_BUY)
   
   if (type == OP_SELL)
   {
      price = Bid;
      if (TakeProfit > 0) 
      {
         take = NormalizeDouble(price - (TakeProfit * Point), Digits);
         HiddenTakeProfit = take;         
      }//if (TakeProfit > 0) 

      if (TradeOrigin == D1TrendTrade)
      {
         //Override any take profit choice if D1SlopeTrend is sell and hold
         if (D1SlopeTrend == sellhold)
         {
            take = 0;
            AdaptedBreakEven = true;
            AdaptedJumpingStop = true;
         }//if (D1SlopeTrend == buyhold)

         //If D1 slope is not sell and hold, then set a tp based on the next full support level
         if (D1SlopeTrend != buyhold)
         {
            //Work up through the Monthly support levels until Bid is higher than support. I have assumed that the
            //market will be below the resistance levels, and can amend this easily if not.
            if (price > MS3)
            {
               if (price - MS3 >= (MinD1TrendTradeTpPips * Point) ) take = MS3;
            }//if (price > MS3)
            
            if (price > MS2)
            {
               if (price - MS2 >= (MinD1TrendTradeTpPips * Point) ) take = MS2;
            }//if (price > MS2)
            
            if (price > MS1)
            {
               if (price - MS1 >= (MinD1TrendTradeTpPips * Point) ) take = MS1;
            }//if (price > MS1)
            
            if (price > MonthlyPivot)
            {
               if (price - MonthlyPivot >= (MinD1TrendTradeTpPips * Point) ) take = MonthlyPivot;
            }//if (price > MonthlyPivot)
         }//if (D1SlopeTrend == buyhold)
      }//if (TradeOrigin == D1Trend)
      
      //H4 trend trade
      if (TradeOrigin == H4TrendTrade)
      {
         //Override any take profit choice if D1SlopeTrend is buy and hold
         if (D1SlopeTrend == sellhold)
         {
            take = 0;
            AdaptedBreakEven = true;
            AdaptedJumpingStop = true;
         }//if (D1SlopeTrend == buyhold)
         
         //D1 trend is not buy and hold, so set a tp based on the next resistance. 
         //This is weekly R1 for pairs in groups 1 to 3, R2 for the others.
         if (D1SlopeTrend == sell)
         {
            take = WS2;
            if (VolatilityGroup < 4) take = WS1;
         }//if (D1SlopeTrend == sell)
      }//if (TradeOrigin == H4TrendTrade)

      //BB Range trade. Use Weekly pivot tp. Range trade should only be sent at 
      //MinimumRangeTakeProfit from the weekly pivot
      if (TradeOrigin == BbRangeTrade)
      {
         take = WeeklyPivot;         
      }//if (TradeOrigin == RangeTrade)

      //Wpc range trade. Uses S1/S2 as tp
      if (TradeOrigin == WpcRangeTrade)
      {
         take = WS2;
         if (VolatilityGroup < 4) take = WS1;
      }//if (TradeOrigin == WpcRangeTrade)

      if (HiddenPips > 0 && take > 0) take = NormalizeDouble(take - (HiddenPips * Point), Digits);

       // Control stop level minima
       if (Bid-take <= Stoplevel*Point) 
       {
       take = Bid - (Stoplevel*Point);
       }


   }//if (type == OP_SELL)
   
   return(take);
   
}//End double CalculateTakeProfit(int type, string origin)
Or people can use HiddenPips to have their stop loss pretty much where they want.

Still, better to have correct than incorrect code and I am not one to turn down free code, so V 1o in post 1 has this mod. Thanks again.

:D
Steve,

I just see that you force takeprofit =0 in CalculateTakeProfit() for D1SlopeTrade.
so we need to adjust code to avoid systematic TP at StopLevel * point :

Code: Select all

      // Control stop level minima. Provided by Philippe. Thanks, P.
       if (take > 0 && take-Ask <= Stoplevel*Point)
       {
       take = Ask + (Stoplevel*Point);
       }  
ooppss :oops:
Last edited by phil_trade on Wed May 23, 2012 7:41 am, edited 2 times in total.
User avatar
SteveHopwood
Owner
Posts: 9904
Joined: Tue Nov 15, 2011 8:43 am
Location: Misterton - an insignificant village in England. Very pleasant to live in.

Re: 10.2; the auto-trading EA is born.

Post by SteveHopwood »

phil_trade wrote:Yes but HiddenPips is a Hidden Parameter :lol:
Why don't put it extern ?
Oops. I only meant to remove it temporarily. :lol:

V 1q in post 1 with Stealth replaced. If you do not use Stealth,you do not need the update.

:D
Read the effing manual, ok?

Afterprime is the official SHF broker. Read about them at https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?p=175790#p175790.

I still suffer from OCCD. Good thing, really.

Anyone here feeling generous? My paypal account is always in the market for a tiny donation. pianodoodler@hotmail.com is the account.

To see The Weekly Roundup of stuff you guys might have missed Click here

My special thanks to Thomas (tomele) for all the incredible work he does here.
User avatar
SteveHopwood
Owner
Posts: 9904
Joined: Tue Nov 15, 2011 8:43 am
Location: Misterton - an insignificant village in England. Very pleasant to live in.

Re: 10.2; the auto-trading EA is born.

Post by SteveHopwood »

phil_trade wrote:a star...hmmm i'm just reading your code :oops:

I have try manually moving SL/TP line without any result and can't see why in code AdjustStopLoss() ?

Is it OK with your Empty4 ?
Don't know. Haven't tried it yet.

:D
Read the effing manual, ok?

Afterprime is the official SHF broker. Read about them at https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?p=175790#p175790.

I still suffer from OCCD. Good thing, really.

Anyone here feeling generous? My paypal account is always in the market for a tiny donation. pianodoodler@hotmail.com is the account.

To see The Weekly Roundup of stuff you guys might have missed Click here

My special thanks to Thomas (tomele) for all the incredible work he does here.
User avatar
SteveHopwood
Owner
Posts: 9904
Joined: Tue Nov 15, 2011 8:43 am
Location: Misterton - an insignificant village in England. Very pleasant to live in.

Re: 10.2; the auto-trading EA is born.

Post by SteveHopwood »

I had a look at the code and saw logic bloops in both Adjust functions.

V1r in post 1.

:D
Read the effing manual, ok?

Afterprime is the official SHF broker. Read about them at https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?p=175790#p175790.

I still suffer from OCCD. Good thing, really.

Anyone here feeling generous? My paypal account is always in the market for a tiny donation. pianodoodler@hotmail.com is the account.

To see The Weekly Roundup of stuff you guys might have missed Click here

My special thanks to Thomas (tomele) for all the incredible work he does here.
User avatar
SteveHopwood
Owner
Posts: 9904
Joined: Tue Nov 15, 2011 8:43 am
Location: Misterton - an insignificant village in England. Very pleasant to live in.

Re: 10.2; the auto-trading EA is born.

Post by SteveHopwood »

Reading this and the following post http://www.forexfactory.com/showthread. ... ost5690933, I see my wpc logic is not quite correct. I will make the requisite changes tomorrow.

:D
Read the effing manual, ok?

Afterprime is the official SHF broker. Read about them at https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?p=175790#p175790.

I still suffer from OCCD. Good thing, really.

Anyone here feeling generous? My paypal account is always in the market for a tiny donation. pianodoodler@hotmail.com is the account.

To see The Weekly Roundup of stuff you guys might have missed Click here

My special thanks to Thomas (tomele) for all the incredible work he does here.
phil_trade

Re: 10.2; the auto-trading EA is born.

Post by phil_trade »

Steve,

I just see that you force takeprofit =0 in CalculateTakeProfit() for D1SlopeTrade.
so we need to adjust code to avoid systematic TP at StopLevel * point :

Code: Select all

      // Control stop level minima. Provided by Philippe. Thanks, P.
       if (take > 0 && take-Ask <= Stoplevel*Point)
       {
       take = Ask + (Stoplevel*Point);
       }  
ooppss :oops:
Last edited by phil_trade on Thu May 24, 2012 10:12 am, edited 1 time in total.
phil_trade

Re: 10.2; the auto-trading EA is born.

Post by phil_trade »

SteveHopwood wrote:I had a look at the code and saw logic bloops in both Adjust functions.

V1r in post 1.

:D

Yes... it's ok now ! Very good tools !

re ooops. It's OK when you manually move the line but after a jumping SL, SL is moved back to the line !

I suggest to move line within the JumpingSL like this :

Code: Select all

   //Move 'hard' stop loss whether hidden or not. Don't want to risk losing a breakeven through disconnect.
   if (modify)
   {
      while (IsTradeContextBusy() ) Sleep(100);
      result = OrderModify(OrderTicket(), OrderOpenPrice(), NewStop, OrderTakeProfit(), OrderExpiration(), CLR_NONE);      
      if (!result) 
      {
      ReportError();  
      }
      else
      {
      Print(Symbol()+" Jumping SL");
      ObjectMove(LineName, 0, TimeCurrent(), NewStop);
      }           

      if (PartCloseEnabled && OrderLots() > Preserve_Lots)// Only try to do this if the jump stop worked
      {
         bool PartCloseSuccess = PartCloseTradeFunction();
         //if (!PartCloseSuccess) SetAGlobalTicketVariable();
      }//if (PartCloseEnabled && OrderLots() > Preserve_Lots)
   }//if (modify)
but we can't move anymore manually line when Jumping SL is engaged. It will be a tool to adjust SL at the beginning of the trade and not after profit begin.

any other idea ?
User avatar
SteveHopwood
Owner
Posts: 9904
Joined: Tue Nov 15, 2011 8:43 am
Location: Misterton - an insignificant village in England. Very pleasant to live in.

Re: 10.2; the auto-trading EA is born.

Post by SteveHopwood »

phil_trade wrote:
but we can't move anymore manually line when Jumping SL is engaged. It will be a tool to adjust SL at the beginning of the trade and not after profit begin.

any other idea ?
Thanks again Philippe. The jumping stop thingy was a bit of LUC kicking in. The function has evolved over time and I had some incorrect logic.

Version 1s is in post 1. As well as the fixes to the two points Phil raised, I have adjusted the WPC Range trading code in light of Bob's post at http://www.forexfactory.com/showthread. ... ost5690923

- Trading conditions are:
  • - D1 Slope must have a clear buy or sell bias. H4 must be ranging.
    - trades are taken at the cross of WP, in the direction of the D1 Slope.
    -- tp and sl are the relevant resistance and support levels i.e. weekly R1/S1 for trading groups 1, 2, 3, and R2/S2 for the rest.

:D
Read the effing manual, ok?

Afterprime is the official SHF broker. Read about them at https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?p=175790#p175790.

I still suffer from OCCD. Good thing, really.

Anyone here feeling generous? My paypal account is always in the market for a tiny donation. pianodoodler@hotmail.com is the account.

To see The Weekly Roundup of stuff you guys might have missed Click here

My special thanks to Thomas (tomele) for all the incredible work he does here.
Locked

Return to “10.x EA forum”