-- Id: 10373
-- More information about this indicator can be found at:
-- http://fxcodebase.com/

--+------------------------------------------------------------------+
--|                               Copyright © 2019, Gehtsoft USA LLC |
--|                                            http://fxcodebase.com |
--+------------------------------------------------------------------+
--|                                 Support our efforts by donating  |
--|                                    Paypal: https://goo.gl/9Rj74e |
--+------------------------------------------------------------------+
--|                                Patreon :  https://goo.gl/GdXWeN  |
--|                    BitCoin : 15VCJTLaz12Amr7adHSBtL9v8XomURo9RF  |
--|                BitCoin Cash: 1BEtS465S3Su438Kc58h2sqvVvHK9Mijtg  |
--|           Ethereum : 0x8C110cD61538fb6d7A2B47858F0c0AaBd663068D  |
--|                   LiteCoin : LLU8PSY2vsq7B9kRELLZQcKf5nJQrdeqwD  |
--+------------------------------------------------------------------+
function Init()
    strategy:name("Profit Level Alert");
	strategy:setTag("NonOptimizableParameters", "Email,SendEmail,SoundFile,RecurrentSound,PlaySound,ShowAlert");
    strategy:description("GrossPL Alert");

    strategy.parameters:addGroup("Parameters");
    strategy.parameters:addDouble("ProfitLevel", "Profit level for signal", "", 0);
	
	strategy.parameters:addString("Method", "Method", "Method" , "GrossPL");
    strategy.parameters:addStringAlternative("Method", "GrossPL", "GrossPL" , "GrossPL");
    strategy.parameters:addStringAlternative("Method", "Balance", "Balance" , "Balance");
    strategy.parameters:addStringAlternative("Method", "Equity", "Equity" , "Equity");
    strategy.parameters:addStringAlternative("Method", "DayPL", "DayPL" , "DayPL");
	 
	 strategy.parameters:addBoolean("Stop", "Terminate After Alert", "", true);
	 

    strategy.parameters:addGroup("Notification");
    strategy.parameters:addBoolean("ShowAlert", "Show Alert", "", true);
    strategy.parameters:addBoolean("PlaySound", "Play Sound", "", true);
    strategy.parameters:addBoolean("RecurSound", "Recurrent Sound", "", false);
    strategy.parameters:addFile("SoundFile", "Sound File", "", "");
    strategy.parameters:setFlag("SoundFile", core.FLAG_SOUND);
    strategy.parameters:addBoolean("SendEmail", "Send Email", "", true);
    strategy.parameters:addString("Email", "Email", "", "");
    strategy.parameters:setFlag("Email", core.FLAG_EMAIL);
	
	strategy.parameters:addGroup("Account");
    strategy.parameters:addString("Account", "(FIFO) Choose Account", "", "");
   strategy.parameters:setFlag("Account", core.FLAG_ACCOUNT);
end

-- Parameters block
local gSource = nil; -- the source stream
local PlaySound;
local RecurrentSound;
local SoundFile;
local Email;
local SendEmail;
local Flag;
local ProfitLevel;
local Account;
local Method;
local Stop;
-- strategy instance initialization routine
-- Processes strategy parameters and subscribe to price streams
-- TODO: Calculate all constants, create instances all necessary indicators and load all required libraries
function Prepare(nameOnly)

    local name = profile:id() .. "(" .. instance.bid:instrument() .. ")";
    instance:name(name);

	Account= instance.parameters.Account;
	Method= instance.parameters.Method;
	Stop= instance.parameters.Stop;
    if nameOnly then
        return ;
    end

    AllowTicks = instance.parameters.AllowTicks;
    if (not(AllowTicks)) then
        assert(instance.parameters.TF ~= "t1", "The strategy cannot be applied on ticks.");
    end

    ShowAlert = instance.parameters.ShowAlert;
    if ShowAlert then
        PlaySound = instance.parameters.PlaySound;
        if PlaySound then
            RecurrentSound = instance.parameters.RecurSound;
            SoundFile = instance.parameters.SoundFile;
        else
            SoundFile = nil;
        end
        assert(not(PlaySound) or (PlaySound and SoundFile ~= ""), "Sound file must be specified");

        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");
    end

    ProfitLevel = instance.parameters.ProfitLevel;
    gSource = ExtSubscribe(1, nil, "t1", "Bid", "bar");

    ExtSetupSignal(profile:id() .. ":", ShowAlert);
    ExtSetupSignalMail(name);
    Flag=false;

end

function ExtUpdate(id, source, period)
local Value;
    
 	if Method == "GrossPL" then
	Value=core.host:findTable("accounts"):find("AccountID", Account).GrossPL;
    elseif Method == "Balance" then
	Value=core.host:findTable("accounts"):find("AccountID", Account).Balance;
    elseif Method == "Equity" then
	Value=core.host:findTable("accounts"):find("AccountID", Account).Equity;    
	elseif Method == "DayPL" then
	Value=core.host:findTable("accounts"):find("AccountID", Account).DayPL;
    end
	
	
     if Value >= ProfitLevel and not Flag  then
	 Flag= true;
      if ShowAlert then
        ExtSignal(source, period, "Profit level crosses (" .. ProfitLevel .. ")", SoundFile, Email, RecurrentSound);
      end
	  
	  if Stop then
      core.host:execute("stop");
	  end
     end
     
    
end


dofile(core.app_path() .. "\\strategies\\standard\\include\\helper.lua");
