-- Appling this indicator to to EUR/USD chart will exit EUR/USD long pos if EUR/USD avg[period] crosses below threshold
-- and will exit EUR/USD short pos if EUR/USD avg[period] crosses above threshold.
-- I wish to modify it to work also on an additional another instument say USD/JPY.
-- I mean (I could) but do not want to have another instance applied it to a separate USD/JPY chart.
-- Wish to have ONLY ONE instance to exit both EUR/USD and USD/JPY.
- Code: Select all
function Init()
indicator:name("avg");
indicator:description("Khairy N");
-- indicator:requiredSource(core.Tick);
indicator:requiredSource(core.Bar);
indicator:type(core.Oscillator);
-- indicator:type(core.Indicator);
-- indicator:setTag("group", "Classic Oscillators");
indicator:setTag("group", "Moving Averages");
indicator.parameters:addGroup("Calculation");
indicator.parameters:addInteger("nn", "nn", "nn", 22, 1, 1000000);
indicator.parameters:addInteger("pos", "pos", "pos", 1, -1, 1);
indicator.parameters:addDouble("thrsh", "thrsh", "thrsh", 1.23, 0.1, 1000);
indicator.parameters:addGroup("Style");
indicator.parameters:addColor("clravg", "clravg", "clravg", core.rgb(255, 0, 255));
indicator.parameters:addInteger("widthavg", "widthavg", "widthavg", 1, 1, 5);
indicator.parameters:addInteger("styleavg", "styleavg", "styleavg", core.LINE_SOLID);
indicator.parameters:setFlag("styleavg", core.FLAG_LINE_STYLE);
indicator.parameters:addString("Account", "Account2Trade", "", "");
indicator.parameters:setFlag("Account", core.FLAG_ACCOUNT);
end
local nn, Account, Offer, thrsh, pos
local first;
local source = nil;
local avg = nil;
local ColorMode, UPclr, DNclr;
local psz;
local fst=false
function Prepare()
pos = 0 -- position =1 long =-1 short =0 flat
fst=true
nn = instance.parameters.nn;
pos = instance.parameters.pos;
thrsh = instance.parameters.thrsh -- thrshold to exit long above or short below
source = instance.source;
first = source:first()
UPclr = core.rgb(255, 0, 0);
DNclr = core.rgb(255, 255, 255);
psz = source:pipSize();
Account = instance.parameters.Account;
Offer = core.host:findTable("offers"):find("Instrument", instance.source:instrument()).OfferID;
local name = profile:id() .. "(" .. source:name() .. ", " .. nn .. ", " .. thrsh .. ")";
instance:name(name);
avg = instance:addStream("avg", core.Line, name, "avg", instance.parameters.clravg, first)
avg:setWidth(instance.parameters.widthavg);
avg:setStyle(instance.parameters.styleavg);
end
function Update(period)
if period>first+nn then
avg[period] = mathex.avg( source.close, period-nn, period)
if avg[period]<thrsh and pos==1 then exit("B") end
if avg[period]>thrsh and pos==-1 then exit("S") end
end
end
function AsyncOperationFinished(cookie, success, message)
end
function exit(BuySell)
local valuemap, success, msg;
if haveTrades(BuySell) then
valuemap = core.valuemap();
if BuySell == "B" then
BuySell = "S";
else
BuySell = "B";
end
valuemap.OrderType = "CM";
valuemap.OfferID = Offer;
valuemap.AcctID = Account;
valuemap.NetQtyFlag = "Y";
valuemap.BuySell = BuySell;
success, msg = terminal:execute(101, valuemap);
if not(success) then
terminal:alertMessage(instance.bid:instrument(), instance.bid[instance.bid:size() - 1], "alert_OpenOrderFailed" .. msg, instance.bid:date(instance.bid:size() - 1));
end
end
end
function haveTrades(BuySell)
local enum, row;
local found = false;
enum = core.host:findTable("trades"):enumerator();
row = enum:next();
while (not found) and (row ~= nil) do
if row.AccountID == Account and
row.OfferID == Offer and
(row.BS == BuySell or BuySell == nil) then
found = true;
end
row = enum:next();
end
return found
end