Drat, I pasted this from a Word doc and I see the font colors did not come thru. I had highlighted my changes:
from Word doc:
Your results show that there are still false signals. I think some of the criteria are incorrect, not by you, but from my poor description of what I was wanting. I’ve shown in red my changes. For instance it appears to me that a signal occurred when one of the criteria had NOT been met, i.e. all of the criteria have to be met. Can a BUY graphic signal be distinguishable from a SELL signal?
I’ve created a CCI value range to allow a better chance to create a signal. (I knew better!)
Oh, a “period” is a candle, right?
A BUY signal would occur when ALL of the following conditions are met at a particular time period:
1) CCI between –85 and -100 (adjustable?)
2) d(CCI)/dt is positive (the derivative is positive based on previous period) (adjustable?)
Remove: signal of MACD is > MACD (adjustable?)
3) d(MACD histogram)/dt is positive (the derivative is positive based on previous period) (adjustable?)
A SELL signal would occur when ALL of the following conditions are met at a particular time period:
1) CCI between 85 and 100 (adjustable?)
2) d(CCI/dt is negative (the derivative is negative based on previous period) (adjustable?)
Remove: signal of MACD is < MACD (adjustable?)
3) d(MACD histogram)/dt is negative (the derivative is negative based on previous period) (adjustable?)
- Code: Select all
function Init()
strategy:name("CCI/MACD signal");
strategy:description("CCI/MACD collaboration");
strategy.parameters:addGroup("Parameters");
strategy.parameters:addInteger("CCI_Period", "Period of CCI", "", 14);
strategy.parameters:addInteger("MACD_Short", "Period of short EMA for MACD", "", 12);
strategy.parameters:addInteger("MACD_Long", "Period of long EMA for MACD", "", 26);
strategy.parameters:addInteger("MACD_Signal", "Signal period of MACD", "", 9);
strategy.parameters:addDouble("CCI_Buy", "CCI parameter for BUY", -85, -100);
strategy.parameters:addDouble("CCI_Sell", "CCI parameter for SELL", 85, 100);
I don’t think I meant the following two lines. Is this when the MACD crosses zero?
If so, that is not what I had intended. Here as with the CCI I want to check for the slope of the MACD histogram. Check if Positive for a BUY and negative for a SELL.
strategy.parameters:addDouble("MACD_Buy", "MACD parameter for BUY", "", 0);
strategy.parameters:addDouble("MACD_Sell", "MACD parameter for SELL", "", 0);
strategy.parameters:addString("Period", "Timeframe", "", "m5");
strategy.parameters:setFlag("Period", core.FLAG_PERIODS);
strategy.parameters:addGroup("Signals");
strategy.parameters:addBoolean("ShowAlert", "Show Alert", "", true);
strategy.parameters:addBoolean("PlaySound", "Play Sound", "", false);
strategy.parameters:addFile("SoundFile", "Sound File", "", "");
end
local SoundFile;
local gSourceBid = nil;
local gSourceAsk = nil;
local first;
local BidFinished = false;
local AskFinished = false;
local LastBidCandle = nil;
local CCI;
local MACD;
function Prepare()
local FastN, SlowN;
ShowAlert = instance.parameters.ShowAlert;
if instance.parameters.PlaySound then
SoundFile = instance.parameters.SoundFile;
else
SoundFile = nil;
end
assert(not(PlaySound) or (PlaySound and SoundFile ~= ""), "Sound file must be specified");
assert(instance.parameters.Period ~= "t1", "Signal cannot be applied on ticks");
ExtSetupSignal("CCI/MACD signal:", ShowAlert);
gSourceBid = core.host:execute("getHistory", 1, instance.bid:instrument(), instance.parameters.Period, 0, 0, true);
gSourceAsk = core.host:execute("getHistory", 2, instance.bid:instrument(), instance.parameters.Period, 0, 0, false);
CCI = core.indicators:create("CCI", gSourceBid, instance.parameters.CCI_Period);
MACD = core.indicators:create("MACD", gSourceBid.close, instance.parameters.MACD_Short,instance.parameters.MACD_Long,instance.parameters.MACD_Signal);
first = math.max(CCI.DATA:first(), MACD.DATA:first()) + math.max(instance.parameters.CCI_Period,instance.parameters.MACD_Short,instance.parameters.MACD_Long)+ 2;
local name = profile:id() .. "(" .. instance.bid:instrument() .. "(" .. instance.parameters.Period .. ")" .. instance.parameters.CCI_Period .. "," .. instance.parameters.MACD_Short .. "," .. instance.parameters.MACD_Long .. "," .. instance.parameters.MACD_Signal .. "," .. instance.parameters.CCI_Buy .. "," .. instance.parameters.CCI_Sell .. "," .. instance.parameters.MACD_Buy .. "," .. instance.parameters.MACD_Sell .. ")";
instance:name(name);
end
local LastDirection=nil;
-- when tick source is updated
function Update()
if not(BidFinished) or not(AskFinished) then
return ;
end
local period;
period = gSourceBid:size() - 1;
-- update moving average
if period>first then
CCI:update(core.UpdateLast);
MACD:update(core.UpdateLast);
end
-- calculate enter logic
if LastBidCandle == nil or LastBidCandle ~= gSourceBid:serial(gSourceBid:size() - 1) then
LastBidCandle = gSourceBid:serial(gSourceBid:size() - 1);
period = gSourceBid:size() - 1;
if period > first then
if CCI.DATA[period]>instance.parameters.CCI_Buy and (CCI.DATA[period]-CCI.DATA[period-1])>0. and
MACD.SIGNAL[period]>instance.parameters.MACD_Buy and (MACD.MACD[period]-MACD.MACD[period-1]>0.) and LastDirection~=1 then
ExtSignal(gSourceAsk, period, "Buy", SoundFile);
LastDirection=1;
elseif CCI.DATA[period]<instance.parameters.CCI_Sell and (CCI.DATA[period]-CCI.DATA[period-1])<0. and
MACD.SIGNAL[period]<instance.parameters.MACD_Sell and (MACD.MACD[period]-MACD.MACD[period-1]<0.) and LastDirection~=-1 then
ExtSignal(gSourceBid, period, "Sell", SoundFile);
LastDirection=-1;
end
end
end
end
function AsyncOperationFinished(cookie)
if cookie == 1 then
BidFinished = true;
elseif cookie == 2 then
AskFinished = true;
end
end
local gSignalBase = ""; -- the base part of the signal message
local gShowAlert = false; -- the flag indicating whether the text alert must be shown
-- ---------------------------------------------------------
-- Sets the base message for the signal
-- @param base The base message of the signals
-- ---------------------------------------------------------
function ExtSetupSignal(base, showAlert)
gSignalBase = base;
gShowAlert = showAlert;
return ;
end
-- ---------------------------------------------------------
-- Signals the message
-- @param message The rest of the message to be added to the signal
-- @param period The number of the period
-- @param sound The sound or nil to silent signal
-- ---------------------------------------------------------
function ExtSignal(source, period, message, soundFile)
if source:isBar() then
source = source.close;
end
if gShowAlert then
terminal:alertMessage(source:instrument(), source[period], gSignalBase .. message, source:date(period));
end
if soundFile ~= nil then
terminal:alertSound(soundFile, false);