V 1t is in post 1.
I started on Rinse 'n Repeat again this morning. I transferred the offsetting code to him and was able to watch it in action. There were corrections to make and I have transferred these to HGBnG.
The easiest way to make these corrections yourselves is to copy the whole of this version of bool CanStopOnlyTradesBeOffset() over the top of the existing code.
Code: Select all
bool CanStopOnlyTradesBeOffset()
{
//Simple offset when the latest trade falls by 1 grid size and we are no thedged
double TradePipsProfit = 0;//
bool result = false;
if (!Hedged)
{
//Buys
if (MarketBuysCount >= NoOfSoOpenTradesToStartOffset)//Must be at least 4 trades open
if (BuyPipsUpl > 0)//The position must be winning
{
if (OrderSelect(HighestBuyTicketNo, SELECT_BY_TICKET, MODE_TRADES))
{
//Is it a losing trade?
TradePipsProfit = CalculateTradeProfitInPips(OP_BUY);
if (TradePipsProfit < 0)
{
//Is it losing by at least DistanceBetweenTrades?
TradePipsProfit*= -1;//Convert to a positive number for the comparison
if (TradePipsProfit >= DistanceBetweenTrades)
{
result = OrderCloseBy(HighestBuyTicketNo, LowestBuyTicketNo, clrBlue);
if (result)
{
//Replace the buy stop
SendSingleTrade(Symbol(), OP_BUYSTOP, TradeCommentCOT, Lot, HighestBuyPrice, 0, 0);
return(true);
}//if (result)
}//if (TradePipsProfit >= DistanceBetweenTrades)
}//if (TradePipsProfit < 0)
}//if (OrderSelect(HighestBuyTicketNo, SELECT_BY_TICKET, MODE_TRADES))
}//if (BuyPipsUpl > 0)
//Sells
if (MarketSellsCount >= NoOfSoOpenTradesToStartOffset)//Must be at least 4 trades open
if (SellPipsUpl > 0)//The position must be winning
{
if (OrderSelect(LowestSellTicketNo, SELECT_BY_TICKET, MODE_TRADES))
{
//Is it a losing trade?
TradePipsProfit = CalculateTradeProfitInPips(OP_SELL);
if (TradePipsProfit < 0)
{
//Is it losing by at least DistanceBetweenTrades?
TradePipsProfit*= -1;//Convert to a positive number for the comparison
if (TradePipsProfit >= DistanceBetweenTrades)
{
result = OrderCloseBy(LowestSellTicketNo, HighestSellTicketNo, clrBlue);
if (result)
{
//Replace the sell stop
SendSingleTrade(Symbol(), OP_SELLSTOP, TradeCommentCOT, Lot, LowestSellPrice, 0, 0);
return(true);
}//if (result)
}//if (TradePipsProfit >= DistanceBetweenTrades)
}//if (TradePipsProfit < 0)
}//if (OrderSelect(LowestSellTicketNo, SELECT_BY_TICKET, MODE_TRADES))
}//if (SellPipsUpl > 0)
}//if (!Hedged)
/////////////////////////////////////////////////////////////////////////////
//We have reached this point, so there were no simple offsets. Look for
//the opportunity to offset some of the profitable side of the hedge
//against the biggest loser on the other side
double CashLoss = 0;
double CashProfit = 0;
int NoOfTrades = 0;
bool ClosePossible = false;
int cc = 0;
if (Hedged)
{
//Can we offset some buy trades against the lowest losing sell trade
if (BuyCashUpl > 0)//The buy side of the hedge must be profitable overall
if (MarketBuysCount >= NoOfSoOpenTradesToStartOffset)//Must be sufficient trades open to start offsetting
if (OrderSelect(LowestSellTicketNo, SELECT_BY_TICKET, MODE_TRADES))//Select the lowest sell
{
CashLoss = (OrderSwap() + OrderCommission() + OrderProfit());//Calculate its cash position
if (CashLoss < 0)//Is it losing?
{
CashLoss*= -1;//Convert to a positive for comparison with the profit on the other side
//Calculate the profit on the other side of the hedge
for (cc = MarketBuysCount; cc > 0; cc--)
{
if (OrderSelect(FifoBuyTicket[cc - 1][TradeTicket], SELECT_BY_TICKET, MODE_TRADES) )
{
CashProfit+= (OrderSwap() + OrderCommission() + OrderProfit());
NoOfTrades++;
}//if (OrderSelect(FifoBuyTicket[cc - 1][ticket], SELECT_BY_TICKET, MODE_TRADES) )
//Is the profit big enough to close the trade on the other side of the hedge?
if (CashProfit >= CashLoss)
{
//Yippee
ClosePossible = true;
break;
}//if (CashProfit >= CashLoss)
}//for (int cc = MarketBuysCount; cc >= 0; cc--)
//Are there closures to make?
if (ClosePossible)
{
ForceTradeClosure = true;
while (ForceTradeClosure)
{
ForceTradeClosure = false;
if (OrderSelect(LowestSellTicketNo, SELECT_BY_TICKET, MODE_TRADES) )
{
result = CloseOrder(LowestSellTicketNo);
if (!result)
ForceTradeClosure = true;
}//if (OrderSelect(LowestSellTicketNo, SELECT_BY_TICKET, MODE_TRADES) )
for (cc = MarketBuysCount; cc >= (MarketBuysCount - (NoOfTrades - 1)); cc--)
{
if (OrderSelect(FifoBuyTicket[cc - 1][TradeTicket], SELECT_BY_TICKET, MODE_TRADES))
{
result = CloseOrder(FifoBuyTicket[cc - 1][TradeTicket]);
if (!result)
{
ForceTradeClosure = true;
cc++;
}//if (!result)
}//if (OrderSelect(FifoBuyTicket[cc - 1][TradeTicket], SELECT_BY_TICKET, MODE_TRADES))
}//for (cc = MarketBuysCount; cc >= NoOfTrades; cc--)
}//while (ForceTradeClosure)
return(true);
}//if (ClosePossible)
}//if (CashLoss < 0)
}//if (OrderSelect(LowestSellTicketNo, SELECT_BY_TICKET, MODE_TRADES))
CashLoss = 0;
CashProfit = 0;
NoOfTrades = 0;
ClosePossible = false;
//Can we offset some sell trades against the highest losing buy trade
if (SellCashUpl > 0)//The sell side of the hedge must be profitable overall
if (MarketSellsCount >= NoOfSoOpenTradesToStartOffset)//Must be sufficient trades open to start offsetting
if (OrderSelect(HighestBuyTicketNo, SELECT_BY_TICKET, MODE_TRADES))//Select the highest buy
{
CashLoss = (OrderSwap() + OrderCommission() + OrderProfit());//Calculate its cash position
if (CashLoss < 0)//Is it losing?
{
CashLoss*= -1;//Convert to a positive for comparison with the profit on the other side
//Calculate the profit on the other side of the hedge
for (cc = 0; cc < MarketSellsCount; cc++)
{
if (OrderSelect(FifoSellTicket[cc][TradeTicket], SELECT_BY_TICKET, MODE_TRADES) )
{
CashProfit+= (OrderSwap() + OrderCommission() + OrderProfit());
NoOfTrades++;
}//if (OrderSelect(FifoSellTicket[cc1][TradeTicket], SELECT_BY_TICKET, MODE_TRADES) )
//Is the profit big enough to close the trade on the other side of the hedge?
if (CashProfit >= CashLoss)
{
//Yippee
ClosePossible = true;
break;
}//if (CashProfit >= CashLoss)
}//for (cc = 0; cc < MarketSellsCount - 1; cc++)
//Are there closures to make?
if (ClosePossible)
{
ForceTradeClosure = true;
while (ForceTradeClosure)
{
ForceTradeClosure = false;
if (OrderSelect(HighestBuyTicketNo, SELECT_BY_TICKET, MODE_TRADES) )
{
result = CloseOrder(HighestBuyTicketNo);
if (!result)
ForceTradeClosure = true;
}//if (OrderSelect(HighestBuyTicketNo, SELECT_BY_TICKET, MODE_TRADES) )
for (cc = 0; cc <= NoOfTrades; cc++)
{
if (OrderSelect(FifoSellTicket[cc][TradeTicket], SELECT_BY_TICKET, MODE_TRADES) )
{
result = CloseOrder(FifoSellTicket[cc][TradeTicket]);
if (!result)
{
ForceTradeClosure = true;
cc--;
}//if (!result)
}//if (OrderSelect(FifoBuyTicket[cc - 1][TradeTicket], SELECT_BY_TICKET, MODE_TRADES) )
}//for (cc = 0; cc <= NoOfTrades; cc++)
}//while (ForceTradeClosure)
return(true);
}//if (ClosePossible)
}//if (CashLoss < 0)
}//if (OrderSelect(HighestBuyTicketNo, SELECT_BY_TICKET, MODE_TRADES))
}//if (Hedged)
////////////////////////////////////////////////////////////////////////////
//Got this far, so no trades closed
return(false);
}//bool CanStopOnlyTradesBeOffset()
I shall be reopening RnR as soon as I am sure he is working correctly.

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.