Apprentice wrote:Can you provide a complete strategy code?
My code is like this.
Can you help me how to modify,thank you!
function Init()
strategy:name("MA Crossover Strytegy");
strategy.parameters:addString("TF","Time frame","","H1");
strategy.parameters:setFlag("TF",core.FLAG_PERIODS);
strategy.parameters:addString("Account","Account to trade on","","");
strategy.parameters:setFlag("Account",core.FLAG_ACCOUNT);
strategy.parameters:addInteger("FastMA_Period","FastMA_Period","",5);
strategy.parameters:addInteger("SlowMA_Period","SlowMA_Period","",8);
strategy.parameters:addInteger("Amount","Trade Amount in Lots","",1);
strategy.parameters:addInteger("Limit","Limit Order in pips","",30);
strategy.parameters:addInteger("Stop","Stop Order in pips","",30);
end
local Account;
local BaseSize;
local Offer;
local FastMA_Period;
local SlowMA_Period;
local Amount;
local Limit;
local Stop;
local Source;
local first;
local FastMA;
local SlowMA;
function Prepare()
Account=instance.parameters.Account;
BaseSize=core.host:execute("getTradingProperty","baseUnitSize",instance.bid:instrument(),Account);
Offer=core.host:findTable("offers"):find("Instrument",instance.bid:instrument()).OfferID;
FastMA_Period=instance.parameters.FastMA_Period;
SlowMA_Period=instance.parameters.SlowMA_Period;
Amount=instance.parameters.Amount;
Limit=instance.parameters.Limit;
Stop=instance.parameters.Stop;
Source=ExtSubscribe(1,nil,instance.parameters.TF,true,"bar");
first=Source:first();
local name=profile:id().."("..instance.bid:instrument()..")";
instance:name(name);
FastMA=core.indicators:create("MVA",Source.close,FastMA_Period);
SlowMA=core.indicators:create("MVA",Source.close,SlowMA_Period);
end
function ExtUpdate(id,source,period)
if period<first+math.max(FastMA_Period,SlowMA_Period) or id~=1 then return end
FastMA:update(core.UpdateLast);
SlowMA:update(core.UpdateLast);
--My trend map sometimes does not automatically update.
--The strategy that does not get the latest trend map at run time.
--How to automatically update the trend map?
local bb,dd=HT1("B");
--My trades list sometimes does not automatically update.
--The strategy that does not get the latest trades information at run time.
if FastMA.DATA[period]>SlowMA.DATA[period] then
if bb<3 then enter("B") end
if dd>0 then exit("S") end
elseif FastMA.DATA[period]<SlowMA.DATA[period] then
if dd<3 then enter("S") end
if bb>0 then exit("B") end
end
end
function HT1(bsk)--Can I use the "refresh" to automatically update My trades list?
local enum=core.host:findTable("trades"):enumerator();
local row=enum:next();
local bb1=0;
local dd1=0;
while row~=nil do
if row.AccountID==Account and row.OfferID==Offer then
if row.BS==bsk then bb1=bb1+1 else dd1=dd1+1 end
end
row=enum:next();
end
return bb1,dd1 end
function enter(BuySell)
local valuemap=core.valuemap();
valuemap.Command="CreateOrder";
valuemap.OrderType="OM";
valuemap.OfferID=Offer;
valuemap.AcctID=Account;
valuemap.Quantity=Amount*BaseSize;
valuemap.BuySell=BuySell;
valuemap.PegTypeStop="O";
valuemap.PegTypeLimit="O";
if BuySell=="B" then valuemap.PegPriceOffsetPipsStop=-Stop;
valuemap.PegPriceOffsetPipsLimit=Limit;
else valuemap.PegPriceOffsetPipsStop=Stop;
valuemap.PegPriceOffsetPipsLimit=-Limit;
end
executing=true;
--need to write "executing"?
local success,msg=terminal:execute(2000,valuemap);
if not(success) then
executing=false;
--need to write "executing"?
terminal:alertMessage(instance.bid:instrument(),instance.bid[NOW],"Open order failed"..msg,instance.bid:date(NOW));
else terminal:alertMessage(instance.bid:instrument(),instance.bid[NOW],"Open order ok",instance.bid:date(NOW)) end
end
function exit(BuySell)
local valuemap,success,msg;
valuemap=core.valuemap();
if BuySell=="B" then BuySell="S" else BuySell="B" end
valuemap.Command="CreateOrder";
valuemap.OrderType="CM";
valuemap.OfferID=Offer;
valuemap.AcctID=Account;
valuemap.NetQtyFlag="Y";
valuemap.BuySell=BuySell;
executing=true;
--need to write "executing"?
success,msg=terminal:execute(101,valuemap);
if not(success) then
executing=false;
--need to write "executing"?
terminal:alertMessage(instance.bid:instrument(), instance.bid[NOW],"Close order failed"..msg,instance.bid:date(NOW));
end
end
function ExtAsyncOperationFinished(cookie,success,message)
if cookie==2000 and not(success) and Account~="TESTACCT" then
executing=false;
--need to write "executing"?
terminal:alertMessage(instance.bid:instrument(),instance.bid[NOW],"Open order failed"..message,instance.bid:date(NOW));
elseif cookie==2000 and success and Account~="TESTACCT" then
executing=false;
--need to write "executing"?
terminal:alertMessage(instance.bid:instrument(),instance.bid[NOW],"Open order ok"..message,instance.bid:date(NOW));
--There is no hint after the deal.
--How to improve?
end
end
dofile(core.app_path().."\\strategies\\standard\\include\\helper.lua");