I have detected buy and sell signals, but these will in turn be cancelled after I add higher time frame CCA and Stochastic, and either of these filters do not allow a trade. I often have to code systems where one signal will close an opposite direction trade. This snippet is all about OCO stuff:
Code: Select all
//Close trades on an opposite direction signal
BuyCloseSignal = false;
SellCloseSignal = false;
if (BuySignal)
SellCloseSignal = true;
if (SellSignal)
BuyCloseSignal = true;
That is usually ReadIndicatorValues() finished but LC does not want a trade opening during the same candle that saw a trade close. Copy this snippet to finish off ReadIndicatorValues()
Code: Select all
/////////////////////////////////////////////////////////////////////////////////////
//Anything else?
//We do not want a trade sending if one has already closed during this candle.
if (BuySignal)
if (HasTradeAlreadyClosed(Symbol(), OP_BUY) )
{
BuySignal = false;
return;//Nothing more to do
}//if (HasTradeAlreadyClosed(OP_BUY) )
if (SellSignal)
if (HasTradeAlreadyClosed(Symbol(), OP_SELL) )
{
SellSignal = false;
}//if (HasTradeAlreadyClosed(OP_SELL) )
Code: Select all
bool HasTradeAlreadyClosed(string symbol, int type)
{
//Look at the trades in the History tab. Return 'true' if a trade closed
//during the current candle, else return 'false'
if (OrdersHistoryTotal() == 0)
return(false);
for (int cc = OrdersHistoryTotal() - 1; cc >= 0; cc--)
{
if (!OrderSelect(cc, SELECT_BY_POS, MODE_TRADES) ) continue;
if (OrderSymbol() != symbol ) continue;
if (OrderMagicNumber() != MagicNumber) continue;
if (OrderType() != type) continue;
//We find a trade that closed during the current candle
//by comparing its close time with the open time of the candle.
if (OrderCloseTime() >= iTime(symbol, TradingTimeFrame, 0) )
return(true);
}//for (int cc = OrdersHistoryTotal() - 1; cc >= 0; cc--)
//Got this far, so no relevant trade found
return(false);
}//bool HasTradeAlreadyClosed(int type)
Code: Select all
void ReadIndicatorValues()
{
int cc = 0;
//Declare a shift for use with indicators.
int shift = 0;
if (!EveryTickMode)
{
shift = 1;
}//if (!EveryTickMode)
//Declare a datetime variable to force cca reading only at the open of a new candle.
static datetime OldCcaReadTime = 0;
//Accommodate every tick mode
if (EveryTickMode)
OldCcaReadTime = 0;
//Allow easy experimentation.
//shift = 2;
//Read the indi
if (OldCcaReadTime != iTime(Symbol(), TradingTimeFrame, 0) )
{
OldCcaReadTime = iTime(Symbol(), TradingTimeFrame, 0);
//Is the line green at shift?
StepMaColour[1] = green;
//Or is it red? The indi returns EMPTY_VALUE when the line is green.
double val = GetStupidStep(Symbol(), TradingTimeFrame, StepSensitivity, StepSize,
StepShift, StepPrice, 1, shift);
//It returns a price if brown
if (!CloseEnough(val, EMPTY_VALUE) )
StepMaColour[1] = red;
//Is the line green at shift 1?
StepMaColour[2] = green;
//Or is it red? The indi returns EMPTY_VALUE when the line is green.
val = GetStupidStep(Symbol(), TradingTimeFrame, StepSensitivity, StepSize,
StepShift, StepPrice, 1, shift + 1);
//It returns a price if brown
if (!CloseEnough(val, EMPTY_VALUE) )
StepMaColour[2] = red;
//What is the colour change status of the line?
//Is it all green?
if (StepMaColour[2] == green && StepMaColour[1] == green)
StepMaChangeStatus = allgreen;
//Is it all red?
if (StepMaColour[2] == red && StepMaColour[1] == red)
StepMaChangeStatus = allred;
//Has it changed red to green?
if (StepMaColour[2] == red && StepMaColour[1] == green)
StepMaChangeStatus = justturnedgreen;
//Has it changed green to red?
if (StepMaColour[2] == green && StepMaColour[1] == red)
StepMaChangeStatus = justturnedred;
}//if (OldCcaReadTime != iTime(Symbol(), TradingTimeFrame, 0) )
/////////////////////////////////////////////////////////////////////////////////////
//IN HERE GOES CODE TO CALL OTHER CCA'S. I SHALL ADD THIS LATER
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
//Now to calculate whether we have a trade signal or not. I do this
//by combining all the indi's used by the trading system.
//Turn off any previous signal
BuySignal = false;
SellSignal = false;
//Look for a Buy signal
if (StepMaChangeStatus == justturnedgreen)
BuySignal = true;
//Look for a sell signal
if (!BuySignal)
if (StepMaChangeStatus == justturnedred)
SellSignal = true;
//Close trades on an opposite direction signal
BuyCloseSignal = false;
SellCloseSignal = false;
if (BuySignal)
SellCloseSignal = true;
if (SellSignal)
BuyCloseSignal = true;
/////////////////////////////////////////////////////////////////////////////////////
//Anything else?
//We do not want a trade sending if one has already closed during this candle.
if (BuySignal)
if (HasTradeAlreadyClosed(Symbol(), OP_BUY) )
{
BuySignal = false;
return;//Nothing more to do
}//if (HasTradeAlreadyClosed(OP_BUY) )
if (SellSignal)
if (HasTradeAlreadyClosed(Symbol(), OP_SELL) )
{
SellSignal = false;
}//if (HasTradeAlreadyClosed(OP_SELL) )
/////////////////////////////////////////////////////////////////////////////////////
}//End void ReadIndicatorValues()
Next up: editing LookForTradeClosure()