-- More information about this indicator can be found at: -- http://fxcodebase.com/code/viewtopic.php?f=28&t=2712 --+------------------------------------------------------------------+ --| Copyright © 2018, Gehtsoft USA LLC | --| http://fxcodebase.com | --+------------------------------------------------------------------+ --| Developed by : Mario Jemic | --| mario.jemic@gmail.com | --+------------------------------------------------------------------+ --| Support our efforts by donating | --| Patreon : https://goo.gl/GdXWeN | --| Paypal : https://goo.gl/9Rj74e | --| BitCoin : 15VCJTLaz12Amr7adHSBtL9v8XomURo9RF | --| BitCoin Cash : 1BEtS465S3Su438Kc58h2sqvVvHK9Mijtg | --| Ethereum : 0x8C110cD61538fb6d7A2B47858F0c0AaBd663068D | --| LiteCoin : LLU8PSY2vsq7B9kRELLZQcKf5nJQrdeqwD | --+------------------------------------------------------------------+ function Init() strategy:name("Simple strategy"); strategy:description("This is the simplest strategy"); strategy:setTag("group", "Samples"); strategy:type(core.Both); strategy.parameters:addInteger("stoch_k", "Stochastic %K Periods", "", 15, 1, 10000); strategy.parameters:addInteger("stoch_sd", "Stochastic %D Slowing Periods", "", 7, 1, 10000); strategy.parameters:addInteger("stoch_d", "Stochastic Periods ", "", 3, 1, 10000); strategy.parameters:addString("stoch_k_method", "Stochastic %K Method", "", "MVA"); strategy.parameters:addString("stoch_d_method", "Stochastic %D Method", "", "MVA"); strategy.parameters:addInteger("long_entry", "Long entry level", "", 20); strategy.parameters:addInteger("long_exit", "Long exit level", "", 60); strategy.parameters:addInteger("short_entry", "Short entry level", "", 80); strategy.parameters:addInteger("short_exit", "Short exit level", "", 40); strategy.parameters:addBoolean("is_bid", "Price Type", "", true); strategy.parameters:setFlag("is_bid", core.FLAG_BIDASK); strategy.parameters:addString("timeframe", "Time frame", "", "m5"); strategy.parameters:setFlag("timeframe", core.FLAG_PERIODS); strategy.parameters:addBoolean("allow_trade", "Allow strategy to trade", "", true); strategy.parameters:setFlag("allow_trade", core.FLAG_ALLOW_TRADE); strategy.parameters:addString("account", "Account to trade on", "", ""); strategy.parameters:setFlag("account", core.FLAG_ACCOUNT); strategy.parameters:addInteger("amount", "Trade Amount in Lots", "", 1); strategy.parameters:addBoolean("set_stop", "Set stop", "", true); strategy.parameters:addDouble("stop", "Stop in Pips", "", 30); strategy.parameters:addBoolean("set_limit", "Set limit", "", true); strategy.parameters:addDouble("limit", "Limit in Pips", "", 30); end local stoch; local source; local SOURCE_ID = 1; local allow_trade; local account; local amount; local stop; local limit; local long_entry; local long_exit; local short_entry; local short_exit; local offer_id; local set_stop; local set_limit; local base_size; local custom_id = "Simple Strategy"; function Prepare(onlyName) local name = string.format("%s (%s %s)", profile:id(), instance.bid:instrument(), instance.parameters.timeframe); instance:name(name); if onlyName then return; end allow_trade = instance.parameters.allow_trade; account = instance.parameters.account; amount = instance.parameters.amount; base_size = core.host:execute("getTradingProperty", "baseUnitSize", instance.bid:instrument(), account); stop = instance.parameters.stop; limit = instance.parameters.limit; long_entry = instance.parameters.long_entry; long_exit = instance.parameters.long_exit; short_entry = instance.parameters.short_entry; short_exit = instance.parameters.short_exit; set_stop = instance.parameters.set_stop; set_limit = instance.parameters.set_limit; offer_id = core.host:findTable("offers"):find("Instrument", instance.bid:instrument()).OfferID; source = ExtSubscribe1(SOURCE_ID, instance.bid:instrument(), instance.parameters.timeframe, 0, instance.parameters.is_bid, "bar"); local stoch_k = instance.parameters.stoch_k; local stoch_sd = instance.parameters.stoch_sd; local stoch_d = instance.parameters.stoch_d; local stoch_k_method = instance.parameters.stoch_k_method; local stoch_d_method = instance.parameters.stoch_d_method; stoch = core.indicators:create("STOCHASTIC", source, stoch_k, stoch_sd, stoch_d, stoch_k_method, stoch_d_method); end function ExtUpdate(id, source, period) stoch:update(core.UpdateAll); if not stoch.D:hasData(period - 1) then return; end if core.crossesUnder(stoch.D, long_entry, period) then if allow_trade then GoLong(); end end if core.crossesOver(stoch.D, short_entry, period) then if allow_trade then GoShort(); end end if core.crossesOver(stoch.D, long_exit, period) then if allow_trade then ExitLong(); end end if core.crossesUnder(stoch.D, short_exit, period) then if allow_trade then ExitShort(); end end end function GoLong() MarketOrder("B"); end function GoShort() MarketOrder("S"); end function MarketOrder(side) local valuemap = core.valuemap(); valuemap.Command = "CreateOrder"; valuemap.OrderType = "OM"; valuemap.OfferID = offer_id; valuemap.AcctID = account; valuemap.Quantity = amount * base_size; valuemap.BuySell = side; valuemap.CustomID = custom_id; if set_stop then valuemap.PegTypeStop = "O"; if side == "B" then valuemap.PegPriceOffsetPipsStop = -stop; else valuemap.PegPriceOffsetPipsStop = stop; end end if set_limit then valuemap.PegTypeLimit = "O"; if side == "B" then valuemap.PegPriceOffsetPipsLimit = limit; else valuemap.PegPriceOffsetPipsLimit = -limit; end end local success, msg = terminal:execute(200, valuemap); if not(success) then terminal:alertMessage(instance.bid:instrument(), instance.bid[NOW], "Open order failed" .. msg, instance.bid:date(NOW)); end end function ExitLong() Close("B"); end function ExitShort() Close("S"); end function Close(side) local enum = core.host:findTable("trades"):enumerator(); local row = enum:next(); while row ~= nil do if row.QTXT == custom_id and row.BS == side then local valuemap = core.valuemap(); valuemap.OrderType = "CM"; valuemap.OfferID = offer_id; valuemap.AcctID = account; valuemap.Quantity = row.Lot; valuemap.TradeID = row.TradeID; valuemap.CustomID = custom_id; if row.BS == "B" then valuemap.BuySell = "S"; else valuemap.BuySell = "B"; end local success, msg = terminal:execute(201, valuemap); if not(success) then terminal:alertMessage(instance.bid:instrument(), instance.bid[NOW], "Close order failed " .. msg, instance.bid:date(NOW)); end end row = enum:next(); end end dofile(core.app_path() .. "\\strategies\\standard\\include\\helper.lua");