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 open-source

Nello spirito di condivisione promosso da TradingView, l'autore (al quale vanno i nostri ringraziamenti) ha deciso di pubblicare questo script in modalità open-source, così che chiunque possa comprenderlo e testarlo. Puoi utilizzarlo gratuitamente, ma il riutilizzo del codice è subordinato al rispetto del Regolamento. Per aggiungerlo al grafico, mettilo tra i preferiti.

Declinazione di responsabilità

Le informazioni ed i contenuti pubblicati non costituiscono in alcun modo una sollecitazione ad investire o ad operare nei mercati finanziari. Non sono inoltre fornite o supportate da TradingView. Maggiori dettagli nelle Condizioni d'uso.

Vuoi usare questo script sui tuoi grafici?
//@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.