NR Super Trend Strategy

Strategies are published here.

Moderator: admin

Re: NR Super Trend Strategy

Postby Apprentice » Mon Jul 06, 2015 4:34 am

Unfortunately you can not copy / past code.
NR Super Trend Strategy uses a simple enter funkciju.
enter("B")
You use
enter("B", Stop1, Limit1);
enter("B", Stop2, Limit2);
enter("B", Stop3, Limit3);

You can use enter function from my MCP Strategy Template.
enter(BuySell,Instrument,Amount, Limit , SetStop ,Stop , TrailingStop)
viewtopic.php?f=28&t=2712
Some modifications will be required.
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 36341
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Re: NR Super Trend Strategy

Postby MC. Trend Trader » Mon Jul 06, 2015 4:48 pm

Hello Apprentice

I want this Position/Ordermanager in NR_Supertrend Strategy.

Here is a example Ordermanagement strategy that works, but I can not get to rewrite it for this strategy, it just will not work properly,

Can you Help?

Code: Select all
function Init() --The strategy profile initialization
    strategy:name("Order Management");
    strategy:description("");
    strategy:setTag("NonOptimizableParameters", "ShowAlert,PlaySound,SoundFile,RecurrentSound,SendMail,Email");


    strategy.parameters:addGroup("Price");
    strategy.parameters:addString("Type", "Price Type", "", "Bid");
    strategy.parameters:addStringAlternative("Type", "Bid", "", "Bid");
    strategy.parameters:addStringAlternative("Type", "Ask", "", "Ask");
   
   if not BAR then
    strategy.parameters:addString("Price", "Price Source", "", "close");
    strategy.parameters:addStringAlternative("Price", "OPEN", "", "open");
    strategy.parameters:addStringAlternative("Price", "HIGH", "", "high");
    strategy.parameters:addStringAlternative("Price", "LOW", "", "low");
    strategy.parameters:addStringAlternative("Price","CLOSE", "", "close");
    strategy.parameters:addStringAlternative("Price", "MEDIAN", "", "median");
    strategy.parameters:addStringAlternative("Price", "TYPICAL", "", "typical");
    strategy.parameters:addStringAlternative("Price", "WEIGHTED", "", "weighted");   
   end
 
    strategy.parameters:addString("TF", "Time frame", "", "m5");
    strategy.parameters:setFlag("TF", core.FLAG_PERIODS);
   
    strategy.parameters:addGroup("Strategy Parameters");

    strategy.parameters:addInteger("amount", "Amount in lots", "", 1, 1, 100);

    strategy.parameters:addInteger("Limit1", "1st limit order (in pips)", "", 10, 1, 10000);
    strategy.parameters:addInteger("Limit2", "2nd limit order (in pips)", "", 20, 1, 10000);
    strategy.parameters:addInteger("Limit3", "3d limit order (in pips)", "", 30, 1, 10000);

    strategy.parameters:addInteger("Stop1", "1st stop order (in pips)", "", 10, 1, 10000);
    strategy.parameters:addInteger("Stop2", "2nd stop order (in pips)", "", 20, 1, 10000);
    strategy.parameters:addInteger("Stop3", "3d stop order (in pips)", "", 30, 1, 10000);

--    strategy.parameters:addBoolean("TrailingStop", "Trailing stop order", "", false);

    CreateTradingParameters();

end

function CreateTradingParameters()
    strategy.parameters:addGroup("Trading Parameters");

    strategy.parameters:addBoolean("AllowTrade", "Allow strategy to trade", "", false);
    strategy.parameters:setFlag("AllowTrade", core.FLAG_ALLOW_TRADE);
    strategy.parameters:addString("ALLOWEDSIDE", "Allowed side", "Allowed side for trading or signaling, can be Sell, Buy or Both", "Both");
    strategy.parameters:addStringAlternative("ALLOWEDSIDE", "Both", "", "Both");
    strategy.parameters:addStringAlternative("ALLOWEDSIDE", "Buy", "", "Buy");
    strategy.parameters:addStringAlternative("ALLOWEDSIDE", "Sell", "", "Sell");

--    strategy.parameters:addBoolean("AllowMultiple", "Allow Multiple", "", true);
    strategy.parameters:addString("Account", "Account to trade on", "", "");
    strategy.parameters:setFlag("Account", core.FLAG_ACCOUNT);

    strategy.parameters:addGroup("Alerts");
    strategy.parameters:addBoolean("ShowAlert", "ShowAlert", "", true);
    strategy.parameters:addBoolean("PlaySound", "Play Sound", "", false);
    strategy.parameters:addFile("SoundFile", "Sound File", "", "");
    strategy.parameters:setFlag("SoundFile", core.FLAG_SOUND);
    strategy.parameters:addBoolean("RecurrentSound", "Recurrent Sound", "", true);
    strategy.parameters:addBoolean("SendEmail", "Send Email", "", false);
    strategy.parameters:addString("Email", "Email", "", "");
    strategy.parameters:setFlag("Email", core.FLAG_EMAIL);
end


local Source;

local SoundFile = nil;
local RecurrentSound = false;
local ALLOWEDSIDE;
local AllowMultiple;
local AllowTrade;
local Offer;
local CanClose;
local Account;
local Amount;
local Limit;
local Stop;
local TrailingStop;
local ShowAlert;
local Email;
local SendEmail;
local BaseSize;


local Indicator={};
local Short={};
local Source;

local first;
local Price;

local FastMA;
local SlowMA;

local Limit1, Limit2, Limit3;
local Stop1, Stop2, Stop3;

--
function Prepare(nameOnly)

   if not BAR then
    Price = instance.parameters.Price;
   end
   
    assert(instance.parameters.TF ~= "t1", "The time frame must not be tick");
 

    local name;
    name = profile:id() .. "( " .. instance.bid:name() .. ")";
    local i;

    instance:name(name);


    PrepareTrading();

    if nameOnly then
        return ;
    end

    Source = ExtSubscribe(1, nil, instance.parameters.TF, instance.parameters.Type == "Bid", "close");
   
    FastMA = core.indicators:create("MVA", Source, 5);
    SlowMA = core.indicators:create("MVA", Source, 20);



end

function PrepareTrading()
    ALLOWEDSIDE = instance.parameters.ALLOWEDSIDE;

    local PlaySound = instance.parameters.PlaySound;
    if PlaySound then
        SoundFile = instance.parameters.SoundFile;
    else
        SoundFile = nil;
    end
    assert(not(PlaySound) or (PlaySound and SoundFile ~= ""), "Sound file must be chosen");

    ShowAlert = instance.parameters.ShowAlert;
    RecurrentSound = instance.parameters.RecurrentSound;

    SendEmail = instance.parameters.SendEmail;

    if SendEmail then
        Email = instance.parameters.Email;
    else
        Email = nil;
    end
    assert(not(SendEmail) or (SendEmail and Email ~= ""), "E-mail address must be specified");

    ExtSetupSignal(profile:id() .. ":", ShowAlert);
    ExtSetupSignalMail(name);

    AllowTrade = instance.parameters.AllowTrade;
    if AllowTrade then
        Account = instance.parameters.Account;
        Amount = instance.parameters.amount;
        BaseSize = core.host:execute("getTradingProperty", "baseUnitSize", instance.bid:instrument(), Account);
        Offer = core.host:findTable("offers"):find("Instrument", instance.bid:instrument()).OfferID;
        CanClose = core.host:execute("getTradingProperty", "canCreateMarketClose", instance.bid:instrument(), Account);
        Limit1 = instance.parameters.Limit1;
        Limit2 = instance.parameters.Limit2;
        Limit3 = instance.parameters.Limit3;
        Stop1 = instance.parameters.Stop1;
        Stop2 = instance.parameters.Stop2;
        Stop3 = instance.parameters.Stop3;
--        TrailingStop = instance.parameters.TrailingStop;
    end
end


function ExtUpdate(id, source, period)  -- The method called every time when a new bid or ask price appears.

    -- update moving average
    FastMA:update(core.UpdateLast);
    SlowMA:update(core.UpdateLast);

    if AllowTrade then
        if not(checkReady("trades")) or not(checkReady("orders")) then
            return ;
        end
    end

    if period < 1 or not(SlowMA.DATA:hasData(period - 1)) then
        return ;
    end


   -- specify your conditions for entering the market (long):
   if core.crossesOver(FastMA.DATA, SlowMA.DATA, period) then
       if haveTrades("B") or haveTrades("S") then
           return;   
       else
          enter("B", Stop1, Limit1);
          enter("B", Stop2, Limit2);
          enter("B", Stop3, Limit3);
              if ShowAlert then
                  ExtSignal(source, period, "BUY", SoundFile, Email, RecurrentSound);
              end

       end

   -- specify your conditions for entering the market (short):
   elseif core.crossesOver(SlowMA.DATA, FastMA.DATA, period) then
       if haveTrades("B") or haveTrades("S") then
           return;   
       else
          enter("S", Stop1, Limit1);
          enter("S", Stop2, Limit2);
          enter("S", Stop3, Limit3);
              if ShowAlert then
                  ExtSignal(source, period, "SELL", SoundFile, Email, RecurrentSound);
              end
       end
   end

end



function ExtAsyncOperationFinished(cookie, success, message)
    if cookie == 100 then
        -- timer
    elseif cookie == 200 and not success then
        terminal:alertMessage(instance.bid:instrument(), instance.bid[instance.bid:size() - 1], "Open order failed" .. message, instance.bid:date(instance.bid:size() - 1));
    elseif cookie == 201 and not success then
        terminal:alertMessage(instance.bid:instrument(), instance.bid[instance.bid:size() - 1], "Close order failed" .. message, instance.bid:date(instance.bid:size() - 1));
    end
end



--===========================================================================--
--                    TRADING UTILITY FUNCTIONS                              --
--============================================================================--

function checkReady(table)
    return core.host:execute("isTableFilled", table);
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

-- enter into the specified direction
function enter(BuySell, Stop, Limit)
    if not(AllowTrade) then
        return true;
    end

    local valuemap, success, msg;
    valuemap = core.valuemap();

    valuemap.OrderType = "OM";
    valuemap.OfferID = Offer;
    valuemap.AcctID = Account;
    valuemap.Quantity = Amount * BaseSize;
    valuemap.BuySell = BuySell;

   local StopTrade = Stop;
   local LimitTrade = Limit;

    -- add stop/limit

    valuemap.PegTypeStop = "O";
        if BuySell == "B" then
            valuemap.PegPriceOffsetPipsStop = -StopTrade;
        else
            valuemap.PegPriceOffsetPipsStop = StopTrade;
        end

 --   if TrailingStop then
 --       valuemap.TrailStepStop = 1;
 --    end

    valuemap.PegTypeLimit = "O";
        if BuySell == "B" then
            valuemap.PegPriceOffsetPipsLimit = LimitTrade;
        else
            valuemap.PegPriceOffsetPipsLimit = -LimitTrade;
        end

    if (not CanClose) then
        valuemap.EntryLimitStop = "Y";
    end


    success, msg = terminal:execute(200, valuemap);

    if not(success) then
        terminal:alertMessage(instance.bid:instrument(), instance.bid[instance.bid:size() - 1], "Open order failed" .. msg, instance.bid:date(instance.bid:size() - 1));
        return false;
    end

    return true;
end

dofile(core.app_path() .. "\\strategies\\standard\\include\\helper.lua");


Regards

MC. Trend Trader
MC. Trend Trader
FXCodeBase: Initiate
 
Posts: 107
Joined: Sun Jul 05, 2015 12:36 am

Re: NR Super Trend Strategy

Postby Apprentice » Mon Jul 13, 2015 4:49 am

Your request is added to the development list.
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 36341
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Re: NR Super Trend Strategy

Postby Stance » Tue Jul 14, 2015 1:48 am

Would it be possible to add Relative Currency Strength ( viewtopic.php?f=17&t=59518&hilit=currency+strength ) to this strategy so that it would only take a trade if it matches the currency strength?

ie go long on AUDUSD only if AUD is stronger than USD.
Stance
FXCodeBase: Initiate
 
Posts: 141
Joined: Mon Jun 01, 2015 4:34 pm

Re: NR Super Trend Strategy

Postby Apprentice » Fri Jan 26, 2018 1:52 pm

The strategy was revised and updated.
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 36341
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Previous

Return to Custom Strategies

Who is online

Users browsing this forum: Google [Bot] and 17 guests