V 2h is in post 1, with two more fixes. Leon sent me this pm last night:
Wavegarrick wrote:Subject: Desky. TDesk's trading drone.
Hi Steve,
Sorry for the false alarm. The first hedge went of fine with EurUsd and I thought all is ok. When I saw the second trade in the opposite direction with EurJpy there was no hedge trade opened. I closed the Ea an re attached it to see if it would not solve the problem but still no hedge trade. There are also other trades in the opposite direction with no hedge trades. I decided to close the hedge trade with EurUsd and immediately a hedge trade opened with EurJpy. From what I can see is that the Ea is only allowing one hedge trade at a time to be opened with all the currency pairs that I am trading.
I am attaching a picture with the trades as mentioned above
I hope I am understanding the hedging feature correctly.
Cheers
Leon
DIYers, do a search for, "//We need to know the the position is hedged to prevent order closure further down" and look down three lines. The first conditional is, "if (!BetterOrderSelect(cc, SELECT_BY_POS, MODE_TRADES))". This checks that the trade has not been closed. There are no further checks, so all open trades are examined and once a hedge is discovered then a variable is set to tell Desky that there is a hedge in place, so he cannot add hedges to other pairs. Add these two conditionals:
Code: Select all
if (OrderSymbol() != symbol)
continue;
if (OrderMagicNumber() != MagicNumber)
continue;
The second fix is adding the code that allows Desky to send stop and limit orders rather than an immediate market trade. DIYers, do a search for, "bool LookForTradingOpportunities(string symbol, int pairIndex, int type)" and replace the entire function with:
Code: Select all
bool LookForTradingOpportunities(string symbol, int pairIndex, int type)
{
//return;//TEMPORARY. REMOVE LATER
GetBasics(symbol);
double take = 0, stop = 0, price = 0;
bool SendTrade = false, result = false;
double SendLots = Lot;
//Check filters
if (!IsTradingAllowed(symbol, pairIndex) ) return(false);
/////////////////////////////////////////////////////////////////////////////////////
//Trading decision.
bool SendLong = false, SendShort = false;
//Long trade
//Specific system filters
if (BuySignal)
SendLong = true;
//Usual filters
if (SendLong)
{
if (UseZeljko && !BalancedPair(symbol, OP_BUY) ) return(false);
}//if (SendLong)
/////////////////////////////////////////////////////////////////////////////////////
if (!SendLong)
{
//Short trade
//Specific system filters
if (SellSignal)
SendShort = true;
if (SendShort)
{
//Usual filters
//Other filters
if (UseZeljko && !BalancedPair(symbol, OP_SELL) ) return(false);
}//if (SendShort)
}//if (!SendLong)
////////////////////////////////////////////////////////////////////////////////////////
//Long
if (SendLong)
{
price = NormalizeDouble(MarketInfo(symbol, MODE_ASK), digits);
//Immediate market trade need no further adjustment
//Buy stop
if (type == OP_BUYSTOP)
price = NormalizeDouble(price + (DistanceFromMarket / factor), digits);
//Buy limit
if (type == OP_BUYLIMIT)
price = NormalizeDouble(price - (DistanceFromMarket / factor), digits);
stop = CalculateStopLoss(symbol, OP_BUY, price);
take = CalculateTakeProfit(symbol, OP_BUY, price);
//Lot size calculated by risk
if (!CloseEnough(RiskPercent, 0)) SendLots = CalculateLotSize(symbol, price, stop );
SendTrade = true;
}//if (SendLong)
//Short
if (SendShort)
{
price = NormalizeDouble(MarketInfo(symbol, MODE_BID), digits);
//Immediate market trade need no further adjustment
//Sell stop
if (type == OP_SELLSTOP)
price = NormalizeDouble(price - (DistanceFromMarket / factor), digits);
//Sell limit
if (type == OP_SELLLIMIT)
price = NormalizeDouble(price + (DistanceFromMarket / factor), digits);
stop = CalculateStopLoss(symbol, OP_SELL, price);
take = CalculateTakeProfit(symbol, OP_SELL, price);
//Lot size calculated by risk
if (!CloseEnough(RiskPercent, 0)) SendLots = CalculateLotSize(symbol, price, stop);
SendTrade = true;
}//if (SendShort)
if (SendTrade)
{
result = true;//Allow sending the grid if not sending an immediate market trade
//if (SendImmediateMarketTrade)
result = SendSingleTrade(symbol, type, TradeComment, SendLots, price, stop, take);
if (result)
{
//The latest garbage from the morons at Crapperquotes appears to occasionally break Matt's OR code, so tell the
//ea not to trade for a while, to give time for the trade receipt to return from the server.
TimeToStartTrading[pairIndex] = TimeCurrent() + PostTradeAttemptWaitSeconds;
//if (BetterOrderSelect(TicketNo, SELECT_BY_TICKET, MODE_TRADES) )
// CheckTpSlAreCorrect(type);
}//if (result)
}//if (SendTrade)
return(result);
}//End bool LookForTradingOpportunities(string symbol, int PairIndex)
The more advanced of you will easily spot the few lines of code I have added. This sends the trades. You still need to add the code that calls the function. Search for, "if (!StopTrading)". This heads up quite a large code block, so easiest is to copy this over the top of the existing block:
Code: Select all
if (!StopTrading)
{
if (TimeCurrent() >= TimeToStartTrading[pairIndex])
{
if (OpenTrades == 0)
{
if (BuySignal || SellSignal)
{
if (BuySignal)
{
//Immediate market trade
if (SendImmediateMarketTrade)
result = LookForTradingOpportunities(symbol, pairIndex, OP_BUY);
//Pending orders
if (SendPendingTrades)
{
if (TypeOfPendingTrade == Buy_stop)
result = LookForTradingOpportunities(symbol, pairIndex, OP_BUYSTOP);
if (TypeOfPendingTrade == Buy_limit)
result = LookForTradingOpportunities(symbol, pairIndex, OP_BUYLIMIT);
if (TypeOfPendingTrade == Buy_stop_and_buy_limit)
{
result = LookForTradingOpportunities(symbol, pairIndex, OP_BUYSTOP);
Sleep(5000);//Give the platform time to catch up
result = LookForTradingOpportunities(symbol, pairIndex, OP_BUYLIMIT);
}//if (TypeOfPendingTrade == Buy_stop_and_buy_limit)
}//if (SendPendingTrades)
if (UseGridTrading)
if (result)
{
if (TypeOfGrid == Stop || TypeOfGrid == Both)
{
SendBuyGrid(symbol, OP_BUYSTOP, ask + (DistanceFromMarket / factor), Lot );
}//if (TypeOfGrid == OP_BUYSTOP || TypeOfGrid == Both)
if (TypeOfGrid == Limit || TypeOfGrid == Both)
{
SendBuyGrid(symbol, OP_BUYLIMIT, ask - (DistanceFromMarket / factor), Lot );
}//if (TypeOfGrid == OP_BUYSTOP || TypeOfGrid == Both)
}//if (result)
}//if (BuySignal)
if (SellSignal)
{
//Immediate market trade
if (SendImmediateMarketTrade)
result = LookForTradingOpportunities(symbol, pairIndex, OP_SELL);
//Pending orders
if (SendPendingTrades)
{
if (TypeOfPendingTrade == Sell_Stop)
result = LookForTradingOpportunities(symbol, pairIndex, OP_SELLSTOP);
if (TypeOfPendingTrade == Sell_limit)
result = LookForTradingOpportunities(symbol, pairIndex, OP_SELLLIMIT);
if (TypeOfPendingTrade == Sell_stop_and_sell_limit)
{
result = LookForTradingOpportunities(symbol, pairIndex, OP_SELLSTOP);
Sleep(5000);//Give the platform time to catch up
result = LookForTradingOpportunities(symbol, pairIndex, OP_SELLLIMIT);
}//if (TypeOfPendingTrade == Sell_stop_and_sell_limit)
}//if (SendPendingTrades)
if (UseGridTrading)
if (result)
{
if (TypeOfGrid == Stop || TypeOfGrid == Both)
{
SendSellGrid(symbol, OP_SELLSTOP, bid - (DistanceFromMarket / factor), Lot );
}//if (TypeOfGrid == OP_SELLSTOP || TypeOfGrid == Both)
if (TypeOfGrid == Limit || TypeOfGrid == Both)
{
SendSellGrid(symbol, OP_SELLLIMIT, bid + (DistanceFromMarket / factor), Lot );
}//if (TypeOfGrid == OP_BUYSTOP || TypeOfGrid == Both)
}//if (result)
}//if (SellSignal)
//This takes care of grid trading only.
if (!SendImmediateMarketTrade)
if (UseGridTrading)
{
if (BuySignal)
{
if (TypeOfGrid == Stop || TypeOfGrid == Both)
{
SendBuyGrid(symbol, OP_BUYSTOP, ask + (DistanceFromMarket / factor), Lot );
}//if (TypeOfGrid == Stop || TypeOfGrid == Both)
if (TypeOfGrid == Limit || TypeOfGrid == Both)
{
SendBuyGrid(symbol, OP_BUYLIMIT, ask - (DistanceFromMarket / factor), Lot );
}//if (TypeOfGrid == Limit || TypeOfGrid == Both)
}//if (BuySignal)
if (SellSignal)
{
if (TypeOfGrid == Stop || TypeOfGrid == Both)
{
SendSellGrid(symbol, OP_SELLSTOP, bid - (DistanceFromMarket / factor), Lot );
}//if (TypeOfGrid == OP_SELLSTOP || TypeOfGrid == Both)
if (TypeOfGrid == Limit || TypeOfGrid == Both)
{
SendSellGrid(symbol, OP_SELLLIMIT, bid + (DistanceFromMarket / factor), Lot );
}//if (TypeOfGrid == OP_BUYSTOP || TypeOfGrid == Both)
}//if (SellSignal)
}//if (UseGridTrading)
}//if (BuySignal || SellSignal)
}//if (OpenTrades == 0)
}//if (TimeCurrent() >= TimeToStartTrading[PairIndex])
}//if (!StopTrading)
The sharp-eyed amongst you will see that the above code will need further adaptation if members start to want to send
both a pending order rather than an immediate market trade,
and a grid of stop/limit orders. Let's cross that bridge if someone builds it.
Not a DIYer yet? Try it - you cannot do any harm. Park a copy of your working version somewhere safe so you can return to it if you muck up. If all else fails, then redownload from post 1. Make the changes and hit the F7 key to recompile the code into machine code; no errors appearing mean you have got it right. It is quite satisfying.
