Last active
July 2, 2020 17:59
-
-
Save binaryfoundry/2755511f31c0f29df61b68864c0b84d7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//The MIT License | |
//Copyright (c) 2019-2020, blockulator. | |
//Permission is hereby granted, free of charge, to any person obtaining a copy | |
//of this software and associated documentation files (the "Software"), to deal | |
//in the Software without restriction, including without limitation the rights | |
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
//copies of the Software, and to permit persons to whom the Software is | |
//furnished to do so, subject to the following conditions: | |
//The above copyright notice and this permission notice shall be included in | |
//all copies or substantial portions of the Software. | |
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
//THE SOFTWARE. | |
//USE THIS SCRIPT AT YOUR OWN RISK. | |
#property strict | |
#property show_inputs | |
#define RISK_PERCENT 2 | |
#define EXPIRATION_MINUTES 120 | |
extern ENUM_ORDER_TYPE order_type_ = OP_BUYLIMIT; | |
extern double limit_order_price_ = 0; | |
extern double take_price_ = 0; | |
extern double stop_price_ = 0; | |
extern int risk_percent_ = RISK_PERCENT; | |
extern bool limit_expiration_enabled_ = false; | |
extern int limit_expiration_minutes_ = EXPIRATION_MINUTES; | |
void OnStart() | |
{ | |
double price = 0; | |
if (order_type_ == OP_SELL || | |
order_type_ == OP_BUY) | |
{ | |
price = order_type_ == OP_BUY ? Ask : Bid; | |
} | |
else if ( | |
order_type_ == OP_SELLLIMIT || | |
order_type_ == OP_BUYLIMIT) | |
{ | |
price = limit_order_price_; | |
} | |
else | |
{ | |
Alert("ERROR: Unsupported Order Type."); | |
return; | |
} | |
double lots = LotsCalculate( | |
risk_percent_, | |
price, | |
stop_price_, | |
order_type_); | |
datetime expiration = 0; | |
if (limit_expiration_enabled_) | |
{ | |
expiration = TimeCurrent() + (limit_expiration_minutes_ * 60); | |
} | |
Print("Lots: " + DoubleToString(lots)); | |
double min_lots = MarketInfo( | |
Symbol(), | |
MODE_MINLOT); | |
if (lots < min_lots) | |
{ | |
Alert("WARNING: Calculated Lots too small for broker."); | |
return; | |
} | |
int new_ticket = OrderSend( | |
Symbol(), | |
order_type_, | |
lots, | |
price, | |
0, | |
stop_price_, | |
take_price_, | |
NULL, | |
0, | |
expiration); | |
Print("Ticket: " + IntegerToString(new_ticket)); | |
if (new_ticket <= 0) | |
{ | |
Alert("ERROR: Check journal."); | |
} | |
} | |
double LotsNormalize(double lots) | |
{ | |
double lot_step = MarketInfo( | |
Symbol(), | |
MODE_LOTSTEP); | |
return(MathRound(lots / lot_step) * lot_step); | |
} | |
double LotsCalculate( | |
double percent, | |
double entry_price, | |
double stop_price, | |
int order_type) | |
{ | |
double tick_size = SymbolInfoDouble( | |
Symbol(), | |
SYMBOL_TRADE_TICK_SIZE); | |
double stop_range = MathAbs(entry_price - stop_price); | |
double lots = (AccountFreeMargin() * (percent / 100.0)) / | |
(stop_range / tick_size); | |
return LotsNormalize(lots); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment