codypd

Simple Strategy Code Stub

This is a very basic strategy implementation

Use as a code stub for your strategy code. I wrote it because I could not find one.

This particular strategy goes long on Tuesdays at 10 am and goes short at 3 pm on Thursdays.
Because US markets open at 9:30 you have to have your chart in 30 minute or less resolution for trades to fire.

You can gut that code and replace it with your own to start testing your own indicators and strategies.
If you build a strategy that doesn't use "hour" like I have this resolution requirement won't apply.

For giggles, compare its performance to other strategies. Weird, huh? About 53% effective on most equities and indexes.

This strategy does the minimum needed to get a strategy working
and uses default position sizes and buys at market with no stops.
Again, it is the minimal code stub needed to test an indicator/rule based strategy.
A great code reference for building more sophisticated strategies can be
found here => The code is written by @greatwolf and is very well structured for anyone looking to fully utilize
strategy features for position sizing, limit orders, stops and cancellations.

Script de código abierto

Siguiendo el verdadero espíritu de TradingView, el autor de este script lo ha publicado en código abierto, para que los traders puedan entenderlo y verificarlo. ¡Un hurra por el autor! Puede utilizarlo de forma gratuita, aunque si vuelve a utilizar este código en una publicación, debe cumplir con lo establecido en las Normas internas. Puede añadir este script a sus favoritos y usarlo en un gráfico.

Exención de responsabilidad

La información y las publicaciones que ofrecemos, no implican ni constituyen un asesoramiento financiero, ni de inversión, trading o cualquier otro tipo de consejo o recomendación emitida o respaldada por TradingView. Puede obtener información adicional en las Condiciones de uso.

¿Quiere utilizar este script en un gráfico?
//@version=2
strategy(title = "Simple Strategy Code Stub", default_qty_type = strategy.percent_of_equity, default_qty_value = 10, initial_capital = 200000, overlay = false)

//Basic strategy implementation
//Use as a code stub for your strategy code.
//I wrote it because I could not find one.

//This strategy goes long on Tuesdays at 10 am and goes short at 3 pm on Thursdays
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//!!!! Because US markets open at 9:30 you have to have your chart in 30 minute or less resolution for trades to fire !!!!  
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

//If you build a strategy that doesn't use "hour" like I have this resolution requirement won't apply.
//For giggles, compare its performance to other strategies.  Weird, huh? About 53% effective on most equities and indexes.

//This strategy does the minimum needed to get a strategy working
//and uses default position sizes and buys at market with no stops.
//Again, it is the minimal code stub needed to test an indicator/rule based strategy.
//A great code reference for building more sophisticated strategies can be
//found here =>  https://www.tradingview.com/chart/BTCUSD/57NvsPus-Ichimoku-Lag-Line-strategy/
//The code is written by @greatwolf and is very well structured for anyone looking to fully utilize
//strategy features for position sizing, limit orders, stops and cancellations.


//First, let's set our buySignal and sellSignal variables

buySignal = ( dayofweek == tuesday ? (hour == 10 ? true : false) : false)
sellSignal = ( dayofweek == thursday ? (hour == 15 ? true : false) : false)

//Second, plot the signals so you can see them firing
//You don't have to plot the signals, nor do it before trades. I placed
//this step here to make the process of building a signal methodical.
plot(buySignal, color = green)
plot(sellSignal, color = red)

//Third, enter the trades.
//Yes - you'd think you could just use "buySignal" instead of "buySignal == true" but you need
//a series of boolean values passed in as opposed to the boolean variable (even though that variable
//is set in series).  If none of that last bit makes sense, just take my word, force the system to evaluate
//the "== true" expression or it won't work.//
strategy.entry("simpleBuy", strategy.long, when = (buySignal == true))
strategy.entry("simpleSell", strategy.short, when = (sellSignal == true))

//Fourth, exit the trades
strategy.exit("simpleBuy", "simpleBuy", when = (sellSignal == true))
strategy.exit("simpleSell", "simpleSell", when = (buySignal == true))

//That's it.
//If your strategy isn't working and your are ready to SWEAR that you are doing everything here, just copy this code
//and paste in your changes line by line.  Why do you think I wrote it?  That's right, I was in the same spot.