Can this pinescript be turned in to a cTrader algo?

Created at 24 Jun 2022, 15:02
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
KI

kimveie

Joined 24.06.2022

Can this pinescript be turned in to a cTrader algo?
24 Jun 2022, 15:02


Hey.

 

Can this script be turned into a cTrader algo?

Also is it possible to make an alternave version with pip instead of % on the TP and stop loss?

 

 

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Kveie

//@version=5
strategy("1 min EMA Scalp by kveie", overlay=true, initial_capital = 100.0, default_qty_value = 100, default_qty_type = strategy.percent_of_equity)

//EMA Settings
var g_emaSettings = "EMA Settings"
i_fastEMA = input.int(8, title = "fastEMA", group=g_emaSettings)
i_slowEMA = input.int(144, title = "slowEMA", group=g_emaSettings)
i_trendEMA = input.int(957, title = "trendEMA", group=g_emaSettings)


//TimeZones
var g_timezones = "TimeZones"
i_ny = input.session("1215-1500", "NY Session", group=g_timezones)
i_lo = input.session("0915-1500", "London Session", group=g_timezones)
i_as = input.session("2145-0030", "Asia Session", group=g_timezones)
timeny = time(timeframe.period, i_ny)
timelo = time(timeframe.period, i_lo)
timeas = time(timeframe.period, i_as)
bgcolor(time == timeny ? color.new(color.yellow, 70): na, title = "NY Session")
bgcolor(time == timelo ? color.new(color.white, 80): na, title = "LO Session")
bgcolor(time == timeas ? color.new(color.blue, 70): na, title = "Asia Session")


//Backtester Settings
var g_tester            = 'Back tester table'
i_drawTester            = input.bool(title='Show back tester', defval=true, group=g_tester, tooltip='Turn on/off inbuilt backtester display', inline = "backtester1")
i_testerPosition        = input.string(title='', defval="Top right", options = ["Top right", "Top left", "Bottom right", "Bottom left"], group=g_tester, tooltip='Position of the backtester table', inline = "backtester1")
i_rr                    = input.float(title='R:R', defval=1.5, group=g_tester, tooltip='Risk:Reward profile')
startBalance            = input.float(title='Starting Balance', defval=100.0, group=g_tester, tooltip='Your starting balance for the custom inbuilt tester system')
riskPerTrade            = input.float(title='Risk Per Trade', defval=7, group=g_tester, tooltip='Your desired % risk per trade (as a whole number)')


//Truncate Settings
truncate(_number, _decimalPlaces) =>
    _factor = math.pow(10, _decimalPlaces)
    int(_number * _factor) / _factor


// Trading Settings Basics
fast = ta.ema(close, i_fastEMA)
slow = ta.ema(close, i_slowEMA)
trend = ta.ema(close, i_trendEMA)
p1 = plot(fast, title='fast EMA', color=color.aqua, linewidth=3)
p2 = plot(slow, title='slow EMA', color=#e91e63, linewidth=4)
p3 = plot(trend, title="trend EMA", color=color.yellow, linewidth=5)

//Conditions
goLongCondition1 = ta.crossover(fast, slow)
goLongCondition2 = fast > trend

goShortCondition1 = ta.crossunder(fast, slow)
goShortCondition2 = fast < trend

fill_color = fast > trend ? color.new(#056656, 85) : color.new(#673ab7, 85)
fill(p1, p3, color=fill_color, title="TrendFill")


strategy.entry("LONG", strategy.long, when=goLongCondition1 and goLongCondition2)
strategy.entry("SHORT", strategy.short, when=goShortCondition1 and goShortCondition2)

//
// The Fixed Percent Stop Loss Code
// User Options to Change Inputs (%)
stopPer = input.float(0.4, title='Stop Loss %') / 100
takePer = input.float(0.6, title='Take Profit %') / 100

// Determine where you've entered and in what direction
entry = strategy.position_avg_price
longStop = strategy.position_avg_price * (1 - stopPer)
shortStop = strategy.position_avg_price * (1 + stopPer)
shortTake = strategy.position_avg_price * (1 - takePer)
longTake = strategy.position_avg_price * (1 + takePer)

if strategy.position_size > 0
    strategy.exit(id="Close Long", stop=longStop, limit=longTake)
if strategy.position_size < 0
    strategy.exit(id="Close Short", stop=shortStop, limit=shortTake)

//PLOT FIXED SLTP LINE
entryLine = plot(strategy.opentrades>0 ? entry : na, style=plot.style_linebr, color=color.white, linewidth=1, title="Entry")
longSL = plot(strategy.position_size > 0 ? longStop : na, style=plot.style_linebr, color=#fe0a0a, linewidth=1, title="Long Fixed SL")
shortSL= plot(strategy.position_size < 0 ? shortStop : na, style=plot.style_linebr, color=#fe0a0a, linewidth=1, title="Short Fixed SL")
longTP = plot(strategy.position_size > 0 ? longTake : na, style=plot.style_linebr, color=#1fff00, linewidth=1, title="Long Take Profit")
shortTP = plot(strategy.position_size < 0 ? shortTake : na, style=plot.style_linebr, color=#1fff00, linewidth=1, title="Short Take Profit")

fill(entryLine, longSL, color.new(#fe0a0a,75), title="Long SL Fill")
fill(entryLine, shortSL, color.new(#fe0a0a,75), title="Short SL Fill")
fill(entryLine, longTP, color.new(#1fff00,75), title="Long TP Fill")
fill(entryLine, shortTP, color.new(#1fff00,75), title="Short TP Fill")

//Declare performance tracking variables
var balance = startBalance
var drawdown = 0.0
var maxDrawdown = 0.0
var maxBalance = 0.0
var totalWins = 0
var totalLoss= 0
   
//Detect winning trades
if strategy.wintrades != strategy.wintrades[1]
    balance := balance + ((riskPerTrade / 100) * balance) * i_rr
    totalWins := totalWins + 1
    if balance > maxBalance
        maxBalance := balance
       
//Detect losing trades
if strategy.losstrades != strategy.losstrades[1]
    balance := balance - ((riskPerTrade / 100) * balance) * i_rr
    totalLoss := totalLoss + 1
    //Update drawdown
    drawdown := (balance / maxBalance) - 1
    if drawdown < maxDrawdown
        maxDrawdown := drawdown
       


// Prepare stats table
var table testTable = table.new(i_testerPosition == "Top right" ? position.top_right : i_testerPosition == "Top left" ? position.top_left : i_testerPosition == "Bottom right" ? position.bottom_right :position.bottom_left, 5, 2, border_width=2)
f_fillCell(_table, _column, _row, _title, _value, _bgcolor, _txtcolor) =>
    _cellText = _title + '\n' + _value
    table.cell(_table, _column, _row, _cellText, bgcolor=_bgcolor, text_color=_txtcolor)


// Draw stats table
var bgcolor = color.new(color.black, 0)
if i_drawTester
    if barstate.islastconfirmedhistory
        // Update table
        dollarReturn = balance - startBalance
        f_fillCell(testTable, 0, 0, 'Total Trades:', str.tostring(strategy.closedtrades), bgcolor, color.white)
        f_fillCell(testTable, 0, 1, 'Win Rate:', str.tostring(truncate(strategy.wintrades / strategy.closedtrades * 100, 2)) + '%', (strategy.wintrades / strategy.closedtrades * 100) >= 50 ? color.green : color.red, color.white)
        f_fillCell(testTable, 1, 0, 'Starting:', "$" + str.tostring(startBalance), bgcolor, color.white)
        f_fillCell(testTable, 1, 1, 'Return:', + str.tostring(truncate(dollarReturn,2), "$"), dollarReturn > 0 ? color.green : color.red, color.white)

// --- END TESTER CODE --- //


@kimveie
Replies

PanagiotisCharalampous
27 Jun 2022, 12:12

Dear kimveie,

Yes it can be. If you are looking for someone to do this for you, you can contact a Consultant or post a Job.

Best Regards,

Panagiotis 

Join us on Telegram and Facebook


@PanagiotisCharalampous