ashish.sah.np

Info
Username: | ashish.sah.np |
Name: | ashish.sah.np |
Member since: | 19 Dec 2022 |
About
Signature
Last Forum Posts
@Not able to increase the volume by the amount by some factor: 06 Jan 2023, 17:55
I'm trying to multiply the Volume for next trade by dividing the factor obtained by ratio of last net loss and a user defined value. But it doesn't seem to increase the volume it just uses the _mult value as 1 always. Can someone provide me the alternative code or any solution.
[Parameter("Average Trade Price (amt)", Group = "Volume", DefaultValue = 2, MinValue = 0.01, Step = 0.01)]
public double Invest {get;set;}
[Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 0.01, MinValue = 0.01, Step = 0.01)]
public double Quantity {get;set;}
private double _mult=1;
protected override void OnStart()
{
Positions.Closed += OnPositionsClosed;
}
private void OnPositionsClosed(PositionClosedEventArgs args)
{
var position = args.Position;
if (position.NetProfit < 0)
{
_mult = Math.Abs((position.NetProfit)/Invest);
}
else
{
_mult = 1;
}
}
protected override void OnTick()
{
var volumeInUnits = (Symbol.QuantityToVolumeInUnits(Math.Round((Quantity*_mult), 2,
MidpointRounding.AwayFromZero)));
ExecuteMarketOrder(TradeType.Buy, SymbolName, volumeInUnits, "Long",SL,TP);
}
@Invalid Logon message: Tag specified out of required order, field=57\x0110=118\x01': 25 Dec 2022, 08:53
Getting this response from the server b'8=FIX.4.4\x019=153\x0135=5\x0134=1\x0149=CSERVER\x0150=TRADE\x0152=20221225-06:28:44.551\x0156=demo.icmarkets.8579286\x0158=Invalid Logon message: Tag specified out of required order, field=57\x0110=118\x01'
Python code that I use:
import socket
import base64
import time, datetime
import hmac
import hashlib
PASSPHRASE = "xxxx"
USERNAME = "xxxxx"
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("h50.p.ctrader.com", 5201))
seq_num = "1" # Correction: using the same MsgSeqNum for signed text and for the field 34
# Correction: t is the same in both signed RawData and in SendingTime (52)
timestamp = str(time.time())
t = str(datetime.datetime.utcnow()).replace("-","").replace(" ", "-")[:-3]
# Correction: '|' is not a valid separator for FIX, it must be '\u0001'
message = "\u0001".join([t, "A", seq_num, "CSERVER", PASSPHRASE]).encode("utf-8")
msgType = "A"
body = "34={}|52={}|49=demo.icmarkets.xxx|56=CSERVER|98=0|57=TRADE|50=TRADE|141=Y|108=30|553={}|554={}|".format(seq_num, t,USERNAME, PASSPHRASE) # using the same time (t) and seq_num as in signed text
# Correction: bodyLength is the number of characters, not bytes, also it must include everything after "8=FIX.4.2|9={}|" i.e. the "35=A|" part of the header
bodyLength = len("35={}|".format(msgType)) + len(body)
header = "8=FIX.4.4|9={}|35={}|".format(bodyLength, msgType)
msg = header + body
msg = msg.replace('|', '\u0001') # Correction: '|' is not a valid separator for FIX, it must be '\u0001'
# generate the checksum:
def check_sum(s):
sum = 0
for char in msg:
sum += ord(char)
sum = str(sum % 256)
while len(sum) < 3:
sum = '0' + sum
return sum
c_sum = check_sum(msg)
logon = msg + "10={}\u0001".format(c_sum)
logon = logon.encode('ascii')
print(logon)
s.sendall(logon)
print(s.recv(4096))
Output :
b'8=FIX.4.4\x019=136\x0135=A\x0134=1\x0152=20221225-06:28:44.504\x0149=demo.icmarkets.xxxx\x0156=CSERVER\x0198=0\x0157=TRADE\x0150=TRADE\x01141=Y\x01108=30\x01553=xxxx\x01554=xxxxx\x0110=073\x01' b'8=FIX.4.4\x019=153\x0135=5\x0134=1\x0149=CSERVER\x0150=TRADE\x0152=20221225-06:28:44.551\x0156=demo.icmarkets.xxxxxx\x0158=Invalid Logon message: Tag specified out of required order, field=57\x0110=118\x01'
@FIX API python code guide: 24 Dec 2022, 17:20
Hello, I’m totally new to the FIX protocol, and as a beginner in Python, the information on the official github page is just overwhelming for me, so please can anyone provide me the code from which I can place a new order and close all the previous orders?
It would be a great help for me ????. I just need to perform this to task for now and later on I will do the rest on myself.
@cbot only working for a short history.: 21 Dec 2022, 17:32
PanagiotisChar said:
Hi there,
You need to provide some more information
- Which symbol and dates do you backtest?
- What exception do you get in the log?
Hello there, Thank you for responding and assisting. The attached link is the indicator that I am using.
It ran the following tests for less data successfully:
When I tried to increase the backtesting time, I got the following error:
Also, while running the backtest, the ctrader app began to display "Not Responding" as if it was consuming major app resources, but backtesting other cbots on my PC went smoothly. Is there anything in my code that causes this issue, and is there any solution to it?
https://ctrader.com/algos/indicators/show/3197
@Why isn't this scalping working in TradingView but not Ctrader?: 21 Dec 2022, 16:53
PanagiotisChar said:
Hi again,
Make sure your SL is set outside the spread. Else it won't be placed.
Is there anyway to check whether SL and TP are inside the spread and to excuse such trades?
@Why isn't this scalping working in TradingView but not Ctrader?: 20 Dec 2022, 15:37
PanagiotisChar said:
Hi there,
Here is your problem
You are passing the volume as quantity but the input should be in units. See below
ExecuteMarketOrder(TradeType.Buy, SymbolName, Symbol.NormalizeVolumeInUnits(Quantity), "Buy", StopLossBuy, TakeProfitBuy);
Hello Thanks for the support. Really appreciate.
Could you please assist me in setting the previous high as a stop loss by converting the price difference in pips correctly? I have tried with the below code, but in the positions tab, T/P and S/L are showing blank for some trades while the majority of trades are closed, so I'm wondering if my below method is correct or not.
And the way I am taking the EMA value, is it correct? This will be a big help for me. Thanks in advance. May God bless your trading portfolio.
using cAlgo.API;
using System;
using cAlgo.API.Indicators;
namespace cAlgo
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class SampleEmacBot : Robot
{
[Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
public double Quantity { get; set; }
[Parameter("Source", Group = "EMA")]
public DataSeries Source { get; set; }
[Parameter("Periods", Group = "EMA", DefaultValue = 5)]
public int Periods { get; set; }
private ExponentialMovingAverage ema;
protected override void OnStart()
{
ema = Indicators.ExponentialMovingAverage(Source, Periods);
}
protected override void OnBar()
{
var currentCandleClose = Bars.ClosePrices.Last(1);
var currentCandleOpen = Bars.OpenPrices.Last(1);
var previousCandleHigh = Bars.HighPrices.Last(2);
var previousCandleLow = Bars.LowPrices.Last(2);
var StopLossBuy = (Math.Abs(previousCandleLow - Bars.OpenPrices.Last(0)))/Symbol.PipSize;
var TakeProfitBuy = ((Math.Abs(previousCandleLow - Bars.OpenPrices.Last(0)))/Symbol.PipSize)*4;
var StopLossSell = (Math.Abs(previousCandleHigh - Bars.OpenPrices.Last(0)))/Symbol.PipSize;
var TakeProfitSell =((Math.Abs(previousCandleHigh- Bars.OpenPrices.Last(0)))/Symbol.PipSize)*4;
var volumeInUnits = Symbol.QuantityToVolumeInUnits(Quantity);
if (((previousCandleHigh < ema.Result.Last(2) )&& (currentCandleClose < ema.Result.Last(1))) && (Bars.OpenPrices.Last(0) <= currentCandleClose) )
{
ExecuteMarketOrder(TradeType.Buy, SymbolName, volumeInUnits, "Buy", StopLossBuy, TakeProfitBuy);
}
if (((previousCandleLow > ema.Result.Last(2)) && (currentCandleClose > ema.Result.Last(1))) && (Bars.OpenPrices.Last(0) >= currentCandleClose))
{
ExecuteMarketOrder(TradeType.Sell, SymbolName, volumeInUnits, "Sell", StopLossSell, TakeProfitSell);
}
}
}
}
@cbot only working for a short history.: 20 Dec 2022, 15:15
When I try to run my CBot for backtesting larger past data sets, it crashes. Is there a problem with what I'm doing below? I am trying to close time after 2 min.
using cAlgo.API;
using System;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
namespace cAlgo
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class NewcBot : Robot
{
[Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
public double Quantity { get; set; }
[Parameter(DefaultValue = 14, MinValue = 2)]
public int Period { get; set; }
[Parameter(DefaultValue = 5)]
public int PeriodK { get; set; }
[Parameter(DefaultValue = 3)]
public int PeriodD { get; set; }
[Parameter(DefaultValue = 1)]
public int Timeru { get; set; }
private STOCHcciIndicator Stochcci;
protected override void OnStart()
{
Stochcci = Indicators.GetIndicator<STOCHcciIndicator>(Period,PeriodK,PeriodD);
}
protected override void OnBar()
{
var volumeInUnits = Symbol.QuantityToVolumeInUnits(Quantity);
if (((Stochcci.StochCCIv.Last(1) < 20 )&& (Stochcci.Trigger.Last(1) > 30)) && Positions.Count ==0)
{
ExecuteMarketOrder(TradeType.Buy, SymbolName, volumeInUnits, "Buy");
}
else if (((Stochcci.StochCCIv.Last(1) > 80 )&& (Stochcci.Trigger.Last(1) < 70)) && Positions.Count ==0)
{
ExecuteMarketOrder(TradeType.Sell, SymbolName, volumeInUnits, "Sell");
}
if(Bars.OpenTimes.Last(0) - LastResult.Position.EntryTime >= TimeSpan.FromMinutes( Timeru ))
{
foreach ( var position in Positions)
{
ClosePosition(position);
}
}
}
}
}
@Why isn't this scalping working in TradingView but not Ctrader?: 19 Dec 2022, 10:04
I coded the following automated strategy in cTrader, but it doesn't seem to open any positions while backtesting. But the same logic is working fine in TradingView. Am I doing something wrong here, or am I missing something? Any community help will be really helpful for me as i have just started using the cTrdader and C# language.
using cAlgo.API;
using cAlgo.API.Indicators;
namespace cAlgo
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class SampleEmacBot : Robot
{
[Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
public double Quantity { get; set; }
[Parameter("Source", Group = "EMA")]
public DataSeries Source { get; set; }
[Parameter("Periods", Group = "EMA", DefaultValue = 5)]
public int Periods { get; set; }
private ExponentialMovingAverage ema;
protected override void OnStart()
{
ema = Indicators.ExponentialMovingAverage(Source, Periods);
}
protected override void OnTick()
{
var currentCandleClose = Bars.ClosePrices.Last(0);
var currentCandleOpen = Bars.OpenPrices.Last(0);
var previousCandleHigh = Bars.HighPrices.Last(1);
var previousCandleLow = Bars.LowPrices.Last(1);
var emaValue = ema.Result.Last(0);
var StopLossBuy = previousCandleLow;
var TakeProfitBuy = previousCandleLow*3;
var StopLossSell = previousCandleHigh;
var TakeProfitSell = previousCandleHigh*3;
if (((previousCandleHigh < emaValue )&& (currentCandleClose < emaValue)) && (currentCandleOpen <= currentCandleClose) )
{
ExecuteMarketOrder(TradeType.Buy, SymbolName, Quantity, "Buy", StopLossBuy, TakeProfitBuy);
}
if (((previousCandleLow > emaValue) && (currentCandleClose > emaValue)) && (currentCandleOpen >= currentCandleClose))
{
ExecuteMarketOrder(TradeType.Sell, SymbolName, Quantity, "Sell", StopLossSell, TakeProfitSell);
}
}
}
}