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
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:
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.
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.
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.
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.
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 :
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 :
//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.
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.
- 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.
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.